各位大侠,WinForm的窗体怎么才能根据自己的爱好自定义他的样式呀?
解决方案 »
- C#调用存储过程出错
- C#如何通过Collection中的元素得到拥有Collection的对象
- abstract 可以和 override 一起使用吗?
- 如何创建一个XML,在线等,谢谢!
- 如何设置DataGrid制定行字体或背景颜色?
- 请问:字符串s1,s2。怎样判断s1是否包含s2?谢谢!
- c#怎么设置解决方案的程序语言是中文还是英文
- C#Datagridview如何根据下拉表选择的内容 改变Datagridview显示的内容
- System.Threading.Timer问题!请帮忙解决
- 请问在c#中如何实现可拖动的参菜单和工具条,急用,谢谢!
- 请教如何在windows窗体中实时刷新一个时间
- 反射问题,大家帮忙一下
http://www.farproc.com/Article/ShowArticle.asp?ArticleID=191
5. 接下来就是实现窗体的移动功能。添加以下代码来创建一个Point对象,该对象(作为一个变量)决定在什么情况下移动窗体。private Point mouse_offset;
6. 创建窗体的MouseDown事件的事件处理函数。为该事件添加代码后,用户就可以在任何位置移动窗体了。代码如下:private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e){mouse_offset = new Point(-e.X, -e.Y);}
7. 创建窗体的MouseMove事件的事件处理函数。当鼠标左键被按下,同时鼠标被移动时,窗体的Location属性就被设置为新的位置了,这样窗体就被用户拖动了。private void Form1_MouseMove(object sender,System.Windows.Forms.MouseEventArgs e){if (e.Button == MouseButtons.Left){Point mousePos = Control.MousePosition;mousePos.Offset(mouse_offset.X, mouse_offset.Y);Location = mousePos;}}
private void CustomButton_Paint(object sender,System.Windows.Forms.PaintEventArgs e){//初始化一个GraphicsPath类的对象System.Drawing.Drawing2D.GraphicsPath myGraphicsPath = newSystem.Drawing.Drawing2D.GraphicsPath();//确定一个字符串,该字符串就是控件的形状string stringText = "Click Me!";//确定字符串的字体FontFamily family = new FontFamily("Arial");//确定字符串的风格int fontStyle = (int)FontStyle.Bold;//确定字符串的高度int emSize = 35;//确定字符串的起始位置,它是从控件开始计算而非窗体PointF origin = new PointF(0, 0);//一个StringFormat对象来确定字符串的字间距以及对齐方式StringFormat format = new StringFormat(StringFormat.GenericDefault);//用AddString方法创建字符串myGraphicsPath.AddString(stringText, family, fontStyle, emSize, origin, format);//将控件的Region属性设置为上面创建的GraphicsPath对象CustomButton.Region = new Region(myGraphicsPath);}
5. 创建按钮的Click事件的事件处理函数。添加该处理函数来改变控件的背景颜色,从而证实控件原来的那些功能没有被削减。 private void CustomButton_Click(object sender, System.EventArgs e){CustomButton.BackColor = Color.BlanchedAlmond;}
6.保存工程并运行。
进一步优化效果
以上我们运用了GraphicsPath类的实例对象来创建了自定义形状的一个按钮控件。不过我们用的是文本字符串形式的一个形状,是否可以用三角形或是圆形等形状呢?答案是肯定的。.Net Framework能为我们提供一些预先定义好了的形状以供我们在程序中使用。通过运用这些,你可以创造出几乎任意形状的控件,你还可以把它们结合起来使用以发挥更大的功能。 下面的实例就运用了四个椭圆,当它们被运用到控件上后,看起来就像人的眼睛,很有意思吧。 private void button1_Paint(object sender,System.Windows.Forms.PaintEventArgs e){System.Drawing.Drawing2D.GraphicsPath myGraphicsPath = newSystem.Drawing.Drawing2D.GraphicsPath();myGraphicsPath.AddEllipse(new Rectangle(0, 0, 125, 125));myGraphicsPath.AddEllipse(new Rectangle(75, 75, 20, 20));myGraphicsPath.AddEllipse(new Rectangle(120, 0, 125, 125));myGraphicsPath.AddEllipse(new Rectangle(145, 75, 20, 20));//改变按钮的背景颜色使之能被容易辨认button1.BackColor = Color.Chartreuse;button1.Size = new System.Drawing.Size(256, 256);button1.Region = new Region(myGraphicsPath);}
最后,你还得搞清楚窗体类是从System.Windows.Forms.Control类继承而来的。也就是说,由窗体设计器提供给你的窗体最终还是一个控件。
http://blog.csdn.net/tjvictor/archive/2006/12/06/1431784.aspx