编程笔记:Windows Forms in C#
来源:互联网 发布:js按钮隐藏和显示div 编辑:程序博客网 时间:2024/05/26 15:55
1、画线时遇到的奇怪问题
(以下摘取部分代码)
Graphics g = null;
g = CreateGraphics();
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
//下面三行代码会出问题,去掉就不会出现异常了
this.Refresh();
g = CreateGraphics();
g.Dispose(); // 可能是罪魁祸首
if (isMouseLeftDown)
{
myPen.EndCap = LineCap.RoundAnchor;
int startX = currentX;
int startY = currentY;
currentX = e.X;
currentY = e.Y;
g.DrawLine(myPen, new Point(startX, startY), new Point(e.X, e.Y));
}
}
有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。
************** 异常文本 **************
System.ArgumentException: 参数无效。
在 System.Drawing.Graphics.CheckErrorStatus(Int32 status)
在 System.Drawing.Graphics.DrawLine(Pen pen, Int32 x1, Int32 y1, Int32 x2, Int32 y2)
在 System.Drawing.Graphics.DrawLine(Pen pen, Point pt1, Point pt2)
在 MySecondApp.Form1.Form1_MouseMove(Object sender, MouseEventArgs e) 位置 D:/Code/C#/MySecondApp/MySecondApp/Form1.cs:行号 54
在 System.Windows.Forms.Control.OnMouseMove(MouseEventArgs e)
在 System.Windows.Forms.Control.WmMouseMove(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
在 System.Windows.Forms.Form.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
2、想设置Form的背景色为具有一定透明度的颜色,结果失败。
有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。
************** 异常文本 **************
System.ArgumentException: 控件不支持透明的背景色。
在 System.Windows.Forms.Control.set_BackColor(Color value)
在 System.Windows.Forms.Form.set_BackColor(Color value)
在 MySecondApp.Form1.pauseButton_Click(Object sender, EventArgs e) 位置 D:/Code/C#/MySecondApp/MySecondApp/Form1.cs:行号 81
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
3、这个错误经常犯,对象引用为空。
************** 异常文本 **************
System.NullReferenceException: 未将对象引用设置到对象的实例。
4、让Form对键盘事件进行响应。
将Form的KeyPreview设置为True。呵呵,Delphi也需要这样设置。
5、设置鼠标光标
this.Cursor = Cursors.Cross; //设置鼠标光标,为十字交叉
6、设置光标为自定义256色光标
using System.Runtime.InteropServices;
#region 设置256色鼠标指针
[DllImport("user32")]
private static extern IntPtr LoadCursorFromFile(string lpFileName);
#endregion
//调用自定义光标
IntPtr myCursor = LoadCursorFromFile(@"D:/Code/C#/MySecondApp2/MySecondApp/Resources/pen.cur");
this.Cursor = new Cursor(myCursor);
7、获取显示器屏幕的宽度和高度
int width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width; //显示器的宽度
int height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height; //显示器高度
8、模拟按键【Print Screen】
SendKeys.Send("{PRTSC}"); //其真实效果,和Alt+【Print Screen】一样,无法达到按下【Print Screen】效果
而且程序截获不了【Print Screen】,猜测是被系统程序先截获了。
9、按【Print Screen】拷贝屏幕,并保存为文件。
利用剪贴板(Clipboard)
if(!Clipboard.ContainsImage())
MessageBox.Show("请先按键盘上的[Print Screen]键");
else
{
Image snap = Clipboard.GetImage();
SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.Filter = "位图文件(*.bmp)|*.bmp";
if (sfDialog.ShowDialog() == DialogResult.OK)
{
string fileName = sfDialog.FileName;
snap.Save(fileName); //把截屏保存到bitmap图像文件
}
Clipboard.Clear();
}
10、打开设计器失败(未解决)
加载设计器时遇到一个或多个错误。这些错误在下面列出。一些错误可通过重新生成项目来修复,而另一些错误则需要更改代码。
参考文章
1.C# WinForm编程中的一点小收获(二)http://www.21tx.com/dev/2005/08/24/13867.html
2.c#打开、保存对话框的使用.http://hi.baidu.com/qusebar/blog/item/ec0d878bdfb3117d9e2fb410.html
3.SendKeys.Send("{PRTSC}");为什么只能拷当前FORM,而不是整个屏幕??http://topic.csdn.net/t/20020415/16/648565.html
Tag:C#,Form,GUI
- 编程笔记:Windows Forms in C#
- Windows Forms编程实战笔记(一至四章)
- Windows Forms in Action
- Pro .NET 2.0 Windows Forms and Custom Controls in C#
- Pro .NET 2.0 Windows Forms and Custom Controls in C#
- C# Windows Forms编程 实用关闭窗口方式
- 《C# windows Forms程序设计》学习笔记--未解决
- Windows Forms Programming with C#
- Using Windows Forms Controls in Internet Explorer
- Using Windows Forms Controls in Internet Explorer
- Using Windows Forms Controls in Internet Explorer
- Using Windows Forms Controls in Internet Explorer
- .NET Windows Forms in a Nutshell
- Hosting .NET Windows Forms Controls in IE
- Managing Processes and Threads in Windows Forms
- 进入windos forms世界--windows forms编程之旅
- c#多线程操作Windows Forms控件
- c#多线程操作Windows Forms控件
- 值类型和引用类型
- SQL Server各种日期计算方法
- 关于对字符长度的判断
- asp即时消息的实现
- 你的枕头套里有什么
- 编程笔记:Windows Forms in C#
- Cisco 2811 DHCP服务器 配置IP-MAC绑定
- VC中常见错误
- ewrw
- delphi-001
- 一些Web开发中常用的、做成cs文件的JS代码
- dsfds
- C++ 赋值操作符
- 开博啦