winform异型不规则界面设计

一,不规则WINFORM窗体

Author:unknown From:Internet
在以前版本的Visual Basic或Visual C++中,创建不规则窗体和控件是一件很复杂的事,不仅需要调用大量API函数而且工作量也不小。不过,现在在Visual C#下,情况就完全不同了。运用Windows Forms你就可以很轻易地创建出一个不规则的窗体以及窗体上的控件。一个具有不规则窗体和控件的应用程序肯定会更吸引广大的用户,微软的Windows Media Player 7就显示出这一点。作为程序员,您一定想在自己的程序中运用这点技术吧。

程序的窗体和控件都可以以非传统的方式被创建。本文就向大家展示如何在应用程序中创建不规则窗体,以及如何在窗体上创建各式各样的自定义形状的控件。

注:创建不规则窗体和控件这个过程包含了大量的图形编程工作,所以不同的计算机因内存和显卡的不同可能会导致最终的效果有所不同。因此,在发布你的应用程序前,务必在各种不同类型的计算机上做好测试工作。

实现方法

首先,创建一个位图文件作为程序的窗体。位图可以是任意形状的,但是位图文件区域一定要足够大,这样才能包含窗体上的所有控件。然后,你可以通过设置一些属性使该图成为程序的窗体。

把程序中的标题栏去掉,否则整个界面将显得很不协调。当然你去掉了标题栏也就去掉了它的最大化、最小化、关闭、移动窗体等功能。为了使程序仍然具有这些功能,我们需在程序中添加一些代码,这样用户就仍然可以像以前一样和程序进行交互。

因此,你需要完成如下工作:

1.创建一个作为窗体的位图文件。

2.创建一个Windows应用程序,用上述位图文件作为程序的窗体同时去掉其标题栏。

3.添加原标题栏具有的功能所需的代码。

具体步骤

下面我就具体向大家介绍如何创建不规则窗体。

创建一个具有不规则形状的位图文件

1.用任何画图程序就可以创建不规则形状的位图,你可以使用最容易也是最方便的画图程序。

2.用一种颜色画出一个不规则的区域作为程序的窗体,并用另一种颜色画出该位图的背景。(你要使该不规则区域足够大。)

3.保存位图文件。

下面就是一个例子:

在vs.net中创建一个新的工程

首先,设置窗体的背景从而建立窗体形状。

1.在窗体设计器中选中窗体使之获得焦点。

2.在属性对话框中进行如下设置:

●将FormBorderStyle属性设置为None。该属性去掉了程序的标题栏,同时也除去了标题栏的功能,不过我在后面还会向大家介绍如何添加代码以恢复这些功能的。

●将BackgroundImage属性设置为你创建的位图文件。你不必在工程中添加该文件,因为你一旦指定了该文件,它就会自动被添加到工程中。

●将TransparencyKey属性设置为位图文件的背景颜色值(在本例中是蓝色)。该属性使得位图的背景即上图中的蓝色部分不可见,从而窗体就呈现出一个不规则的椭圆形。

3.保存工程。按Ctrl+F5可以运行此程序。(注:因为没有标题栏,所以你可以通过Alt+F4来关闭程序)

将FormBorderStyle属性设置为None后,程序的标题栏就被去掉了。这样,为了获得原来标题栏的功能,我们必须手动添加代码。下面我就向大家介绍如何添加代码实现关闭功能以及移动窗体的功能。

实现窗体的关闭及移动

1.往窗体上拖放一个按钮控件。

2.在属性对话框中,将该控件的Text属性设置为“关闭”。

3.双击按钮添加一个Click事件处理函数。

4.在代码编辑器中添加如下代码:

  1. private void button1_Click(object sender, System.EventArgs e)
  2. {
  3. this.Close();
  4. }

二、不规则按钮Author:unknown From:Internet现在,我们已经创建了一个不规则的窗体,并实现了一些基本的移动窗体、关闭窗体的功能。然而,窗体上的按钮控件还是老一套,那么方方正正,使得整个界面不美观。接下来我就向大家介绍如何创建自定义形状的控件。 前面我们创建不规则窗体的时候用到了TransparencyKey属性,但是控件是没有该属性的,所以我们得找其他的方法来实现控件的不规则形状了。在窗体上画一个自定义形状的控件时,你需要精确的告知窗体在什么位置以及如何画该控件。在.Net Framework中有相应的类和方法来帮你实现这些,所以你不必担心具体实现。 .Net Framework中的类提供给控件一个指示说明,该指示说明能确定控件被画的形状。通过不同的指示说明,你就可以按你想要的方法来画控件了。该指示说明利用了GraphicsPath这个类,这个类代表了一系列用来画图的直线和曲线。首先,你得指定一个GraphicsPath类的对象并告知它你要画什么图形。然后,你将控件的Region属性设置为上述GraphicsPath类的对象。这样,你就可以创建任何自定义形状的控件了。

步骤如下:

● 创建一个GraphicsPath类的实例对象。

● 指定好该对象的各项细节(如大小、形状等等)。

● 将控件的Region属性设置为上面建立的GraphicsPath类的实例对象。 创建一个像文本的按钮控件:

1.拖放一个按钮控件到窗体上。

2.在属性对话框中进行如下设置:

● 将Name属性设置为CustomButton。

● 将BackColor属性设置为一个和窗体背景颜色不同的颜色值。

● 将其Text属性设置为空字符串。

3.添加窗体的Paint事件的事件处理函数。

4.添加以下代码,用GraphicsPath类的实例对象来画控件。

下面的代码以一串字符串的形式画该按钮控件,同时,程序还设置了字符串的字体、大小、风格等属性。字符串被赋给GraphicsPath类的实例对象。然后,该实例对象就被设置为按钮控件的Region属性。这样一个自定义形状的控件就完成了。

  1. private void CustomButton_Paint( object sender, System.Windows.Forms.PaintEventArgs e )
  2. {
  3. //初始化一个GraphicsPath类的对象
  4. System.Drawing.Drawing2D.GraphicsPath myGraphicsPath  = new System.Drawing.Drawing2D.GraphicsPath();
  5. //确定一个字符串,该字符串就是控件的形状
  6. string stringText = "Click Me!";
  7. //确定字符串的字体
  8. FontFamily family = new FontFamily("Arial");
  9. //确定字符串的风格
  10. int fontStyle = (int)FontStyle.Bold;
  11. //确定字符串的高度
  12. int emSize = 35;
  13. //确定字符串的起始位置,它是从控件开始计算而非窗体
  14. PointF origin = new PointF(0, 0);
  15. //一个StringFormat对象来确定字符串的字间距以及对齐方式
  16. StringFormat format = new StringFormat(StringFormat.GenericDefault);
  17. //用AddString方法创建字符串
  18. myGraphicsPath.AddString(stringText, family, fontStyle, emSize, origin, format);
  19. //将控件的Region属性设置为上面创建的GraphicsPath对象
  20. CustomButton.Region = new Region(myGraphicsPath);
  21. }
三、GDI+编程的10个基本技巧
  1. //创建绘图表面有两种常用的方法。下面设法得到PictureBox的绘图表面。
  2. private void Form1_Load(object sender, System.EventArgs e)
  3. {
  4. //得到pictureBox1的绘图表面
  5. Graphics g = this.pictureBox1.CreateGraphics();
  6. }
  7. private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  8. {
  9. //得到pictureBox1的绘图表面
  10. Graphics g = e.Graphics;
  11. }
  12. //可以利用Graphics对象绘制出各种图形图案。控件的Paint事件和OnPaint方法都可以绘图都是好时机。在OnPaint方法里绘制图案一定从参数e里面得到Graphics属性。下面是两个例子。
  13. protected override void OnPaint(PaintEventArgs e)
  14. {
  15. e.Graphics.Clear(Color.White);
  16. float x, y, w, h;
  17. x = this.Left+2;
  18. y = this.Top+2;
  19. w = this.Width-4;
  20. h = this.Height-4;
  21. Pen pen = new Pen(Color.Red, 2);
  22. e.Graphics.DrawRectangle(pen, x, y, w, h);
  23. base.OnPaint (e);
  24. }
  25. private void PictureBoxII_Resize(object sender, EventArgs e)
  26. {
  27. this.Invalidate();
  28. }
  29. private void button1_Click(object sender, System.EventArgs e)
  30. {
  31. this.pictureBoxII1.CreateGraphics().FillEllipse(
  32. Brushes.Blue, 10, 20, 50, 100);
  33. }
  34. //和文本有关的三个类:
  35. //FontFamily——定义有着相似的基本设计但在形式上有某些差异的一组字样。无法继承此类。
  36. //Font——定义特定的文本格式,包括字体、字号和字形属性。无法继承此类。
  37. //StringFormat——封装文本布局信息(如对齐方式和行距),显示操作(如省略号插入和国家标准 (National) 数字位替换)和 OpenType 功能。无法继承此类。
  38. //下面的程序显示了一段文字。
  39. private void button2_Click(object sender, System.EventArgs e)
  40. {
  41. Graphics g = this.pictureBoxII1.CreateGraphics();
  42. g.FillRectangle(Brushes.White, this.pictureBoxII1.ClientRectangle);
  43. string s = "aaaaaaaaaaaaaaaaaaaaaaaaaa";
  44. FontFamily fm = new FontFamily("ËÎÌå");
  45. Font f = new Font(fm, 20, FontStyle.Bold, GraphicsUnit.Point);
  46. RectangleF rectF = new RectangleF(30, 20, 180, 205);
  47. StringFormat sf = new StringFormat();
  48. SolidBrush sbrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
  49. sf.LineAlignment = StringAlignment.Center;
  50. sf.FormatFlags = StringFormatFlags.DirectionVertical;
  51. g.DrawString(s, f, sbrush, rectF, sf);
  52. }
  53. //GDI+的路径——GraphicsPath类
  54. //GraphicsPath类提供了一系列属性和方法,利用它可以获取路径上的关键点,可以添加直线段、圆等几何元素。可以获得包围矩形,进行拾取测试。这些功能都怎么用,要仔细看一下。
  55. private void button3_Click(object sender, System.EventArgs e)
  56. {
  57. //绘图表面
  58. Graphics g = this.pictureBoxII1.CreateGraphics();
  59. //填充成白色
  60. g.FillRectangle(Brushes.White, this.ClientRectangle);
  61. //弄一个绘图路径¶
  62. GraphicsPath gp = new GraphicsPath();
  63. //添加一些集合图形
  64. gp.AddEllipse(20, 20, 300, 200);
  65. gp.AddPie(50, 100, 300, 100, 45, 200);
  66. gp.AddRectangle(new Rectangle(100, 30, 100, 80));
  67. //在绘图表面上绘制绘图路径
  68. g.DrawPath(Pens.Blue, gp);
  69. //平移
  70. g.TranslateTransform(200, 20);
  71. //填充绘图路径¶
  72. g.FillPath(Brushes.GreenYellow, gp);
  73. gp.Dispose();
  74. }
  75. //区域——Region类
  76. //从已有的矩形和路径可以创建Region。使用Graphics.FillRegion方法绘制Region。该类指示由矩形和由路径构成的图形形状的内部。无法继承此类。
  77. //渐变色填充
  78. //需要使用两个刷子:
  79. //线性梯度刷子(LinearGradientBrush)
  80. //路径梯度刷子(PathGuadientBrush)
  81. private void button4_Click(object sender, System.EventArgs e)
  82. {
  83. //绘图表面
  84. Graphics g = this.pictureBoxII1.CreateGraphics();
  85. g.FillRectangle(Brushes.White, this.pictureBoxII1.ClientRectangle);
  86. //定义一个线性梯度刷子
  87. LinearGradientBrush lgbrush =
  88. new LinearGradientBrush(
  89. new Point(0, 10),
  90. new Point(150, 10),
  91. Color.FromArgb(255, 0, 0),
  92. Color.FromArgb(0, 255, 0));
  93. Pen pen = new Pen(lgbrush);
  94. //用线性笔刷梯度效果的笔绘制一条直线段并填充一个矩形
  95. g.DrawLine(pen, 10, 130, 500, 130);
  96. g.FillRectangle(lgbrush, 10, 150, 370, 30);
  97. //定义路径并添加一个椭圆
  98. GraphicsPath gp = new GraphicsPath();
  99. gp.AddEllipse(10, 10, 200, 100);
  100. //用该路径定义路径梯度刷子
  101. PathGradientBrush brush =
  102. new PathGradientBrush(gp);
  103. //颜色数组
  104. Color[] colors = {
  105. Color.FromArgb(255, 0, 0),
  106. Color.FromArgb(100, 100, 100),
  107. Color.FromArgb(0, 255, 0),
  108. Color.FromArgb(0, 0, 255)};
  109. //定义颜色渐变比率
  110. float[] r = {0.0f, 0.3f, 0.6f, 1.0f};
  111. ColorBlend blend = new ColorBlend();
  112. blend.Colors = colors;
  113. blend.Positions = r;
  114. brush.InterpolationColors = blend;
  115. //在椭圆外填充一个矩形
  116. g.FillRectangle(brush, 0, 0, 210, 110);
  117. //用添加了椭圆的路径定义第二个路径梯度刷子
  118. GraphicsPath gp2 = new GraphicsPath();
  119. gp2.AddEllipse(300, 0, 200, 100);
  120. PathGradientBrush brush2 = new PathGradientBrush(gp2);
  121. //设置中心点位置和颜色
  122. brush2.CenterPoint = new PointF(450, 50);
  123. brush2.CenterColor = Color.FromArgb(0, 255, 0);
  124. //设置边界颜色
  125. Color[] color2 = {Color.FromArgb(255, 0, 0)};
  126. brush2.SurroundColors = color2;
  127. //用第二个梯度刷填充椭圆
  128. g.FillEllipse(brush2, 300, 0, 200, 100);
  129. }
  130. //GDI+的坐标系统
  131. //通用坐标系——用户自定义坐标系。
  132. //页面坐标系——虚拟坐标系。
  133. //设备坐标系——屏幕坐标系。
  134. //当页面坐标系和设备坐标系的单位都是象素时,它们相同。
  135. private void button10_Click(object sender, System.EventArgs e)
  136. {
  137. Graphics g = this.pictureBoxII1.CreateGraphics();
  138. g.Clear(Color.White);
  139. this.Draw(g);
  140. }
  141. private void Draw(Graphics g)
  142. {
  143. g.DrawLine(Pens.Black, 10, 10, 100, 100);
  144. g.DrawEllipse(Pens.Black, 50, 50, 200, 100);
  145. g.DrawArc(Pens.Black, 100, 10, 100, 100, 20, 160);
  146. g.DrawRectangle(Pens.Green, 50, 200, 150, 100);
  147. }
  148. private void button5_Click(object sender, System.EventArgs e)
  149. {
  150. //左移
  151. Graphics g = this.pictureBoxII1.CreateGraphics();
  152. g.Clear(Color.White);
  153. g.TranslateTransform(-50, 0);
  154. this.Draw(g);
  155. }
  156. private void button6_Click(object sender, System.EventArgs e)
  157. {
  158. //右移
  159. Graphics g = this.pictureBoxII1.CreateGraphics();
  160. g.Clear(Color.White);
  161. g.TranslateTransform(50, 0);
  162. this.Draw(g);
  163. }
  164. private void button7_Click(object sender, System.EventArgs e)
  165. {
  166. //旋转
  167. Graphics g = this.pictureBoxII1.CreateGraphics();
  168. g.Clear(Color.White);
  169. g.RotateTransform(-30);
  170. this.Draw(g);
  171. }
  172. private void button8_Click(object sender, System.EventArgs e)
  173. {
  174. //放大
  175. Graphics g = this.pictureBoxII1.CreateGraphics();
  176. g.Clear(Color.White);
  177. g.ScaleTransform(1.2f, 1.2f);
  178. this.Draw(g);
  179. }
  180. private void button9_Click(object sender, System.EventArgs e)
  181. {
  182. //缩小
  183. Graphics g = this.pictureBoxII1.CreateGraphics();
  184. g.Clear(Color.White);
  185. g.ScaleTransform(0.8f, 0.8f);
  186. this.Draw(g);
  187. }
  188. //全局坐标——变换对于绘图表面上的每个图元都会产生影响。通常用于设定通用坐标系。
  189. //一下程序将原定移动到控件中心,并且Y轴正向朝上。
  190. //先画一个圆
  191. Graphics g = e.Graphics;
  192. g.FillRectangle(Brushes.White, this.ClientRectangle);
  193. g.DrawEllipse(Pens.Black, -100, -100, 200, 200);
  194. //使y轴正向朝上,必须做相对于x轴镜像
  195. //变换矩阵为[1,0,0,-1,0,0]
  196. Matrix mat = new Matrix(1, 0, 0, -1, 0, 0);
  197. g.Transform = mat;
  198. Rectangle rect = this.ClientRectangle;
  199. int w = rect.Width;
  200. int h = rect.Height;
  201. g.TranslateTransform(w/2, -h/2);
  202. //以原点为中心,做一个半径为100的圆
  203. g.DrawEllipse(Pens.Red, -100, -100, 200, 200);
  204. g.TranslateTransform(100, 100);
  205. g.DrawEllipse(Pens.Green, -100, -100, 200, 200);
  206. g.ScaleTransform(2, 2);
  207. g.DrawEllipse(Pens.Blue, -100, -100, 200, 200);
  208. //局部坐标系——只对某些图形进行变换,而其它图形元素不变。
  209. protected override void OnPaint(PaintEventArgs e)
  210. {
  211. Graphics g = e.Graphics;
  212. //客户区设置为白色
  213. g.FillRectangle(Brushes.White, this.ClientRectangle);
  214. //y轴朝上
  215. Matrix mat = new Matrix(1, 0, 0, -1, 0, 0);
  216. g.Transform = mat;
  217. //移动坐标原点到窗体中心
  218. Rectangle rect = this.ClientRectangle;
  219. int w = rect.Width;
  220. int h = rect.Height;
  221. g.TranslateTransform(w/2, -h/2);
  222. //在全局坐标下绘制椭圆
  223. g.DrawEllipse(Pens.Red, -100, -100, 200, 200);
  224. g.FillRectangle(Brushes.Black, -108, 0, 8, 8);
  225. g.FillRectangle(Brushes.Black, 100, 0, 8, 8);
  226. g.FillRectangle(Brushes.Black, 0, 100, 8, 8);
  227. g.FillRectangle(Brushes.Black, 0, -108, 8, 8);
  228. //创建一个椭圆然后在局部坐标系中进行变换
  229. GraphicsPath gp = new GraphicsPath();
  230. gp.AddEllipse(-100, -100, 200, 200);
  231. Matrix mat2 = new Matrix();
  232. //平移
  233. mat2.Translate(150, 150);
  234. //旋转
  235. mat2.Rotate(30);
  236. gp.Transform(mat2);
  237. g.DrawPath(Pens.Blue, gp);
  238. PointF[] p = gp.PathPoints;
  239. g.FillRectangle(Brushes.Black, p[0].X-2, p[0].Y+2, 4, 4);
  240. g.FillRectangle(Brushes.Black, p[3].X-2, p[3].Y+2, 4, 4);
  241. g.FillRectangle(Brushes.Black, p[6].X-4, p[6].Y-4, 4, 4);
  242. g.FillRectangle(Brushes.Black, p[9].X-4, p[9].Y-4, 4, 4);
  243. gp.Dispose();
  244. //base.OnPaint (e);
  245. }
  246. //Alpha混合
  247. //Color.FromArgb()的A就是Alpha。Alpha的取值范围从0到255。0表示完全透明,255完全不透明。
  248. //当前色=前景色×alpha/255+背景色×(255-alpha)/255
  249. protected override void OnPaint(PaintEventArgs e)
  250. {
  251. Graphics g = e.Graphics;
  252. //创建一个填充矩形
  253. SolidBrush brush = new SolidBrush(Color.BlueViolet);
  254. g.FillRectangle(brush, 180, 70, 200, 150);
  255. //创建一个位图,其中两个位图之间有透明效果
  256. Bitmap bm1 = new Bitmap(200, 100);
  257. Graphics bg1 = Graphics.FromImage(bm1);
  258. SolidBrush redBrush =
  259. new SolidBrush(Color.FromArgb(210, 255, 0, 0));
  260. SolidBrush greenBrush =
  261. new SolidBrush(Color.FromArgb(210, 0, 255, 0));
  262. bg1.FillRectangle(redBrush, 0, 0, 150, 70);
  263. bg1.FillRectangle(greenBrush, 30, 30, 150, 70);
  264. g.DrawImage(bm1, 100, 100);
  265. //创建一个位图,其中两个位图之间没有透明效果
  266. Bitmap bm2 = new Bitmap(200, 100);
  267. Graphics bg2 = Graphics.FromImage(bm2);
  268. bg2.CompositingMode = CompositingMode.SourceCopy;
  269. bg2.FillRectangle(redBrush, 0, 0, 150, 170);
  270. bg2.FillRectangle(greenBrush, 30, 30, 150, 70);
  271. g.CompositingQuality = CompositingQuality.GammaCorrected;
  272. g.DrawImage(bm2, 300, 200);
  273. //base.OnPaint (e);
  274. }
  275. //反走样
  276. protected override void OnPaint(PaintEventArgs e)
  277. {
  278. Graphics g = e.Graphics;
  279. //放大8倍
  280. g.ScaleTransform(8, 8);
  281. //没有反走样的图形和文字
  282. Draw(g);
  283. //设置反走样
  284. g.SmoothingMode = SmoothingMode.AntiAlias;
  285. //右移40
  286. g.TranslateTransform(40, 0);
  287. //再绘制就是反走样之后的了
  288. Draw(g);
  289. //base.OnPaint (e);
  290. }
  291. private void Draw(Graphics g)
  292. {
  293. //绘制图形和文字
  294. g.DrawLine(Pens.Gray, 10, 10, 40, 20);
  295. g.DrawEllipse(Pens.Gray, 20, 20, 30, 10);
  296. string s = "反走样测试";
  297. Font font = new Font("宋体", 5);
  298. SolidBrush brush = new SolidBrush(Color.Gray);
  299. g.DrawString(s, font, brush, 10, 40);
  300. }
上一篇:Java编程思想 学习笔记12


下一篇:【微信Java开发 --1】内网穿透外网,使用外网域名可以访问到本地项目