|
|||||||||||||||
介绍This article mainly explains how to use a custom Paint event handler to draw your own Windows Control (in this case a form control). The 这篇文章主要是解释了如何使用用户自定义PAINT事件句柄来绘制自己的窗口控件(这个例子是FORM控件)GoogleTalkForm 类继承和扩展了System.Windows.Forms.Form 控件(系统控件),以便提供Google Talk 窗口表单的样式和风格。 The class properties 类的属性IsWindowSnappable,
这个类做了什么?
How it works这个类的工作原理 First of all we need to add custom event handlers for the form paint event and mouse events. The following code is added to the 首先我们需要给PAINT事件和鼠标事件添加一些处理函数,下面的代码被加入到了InitializeComponent 方法 this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseDown); this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OnMouseUp); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMove); this.DoubleClick += new System.EventHandler(this.OnMouseDoubleClick); this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnFormPaint); The form's style must be set using the method 窗口的风格必须要在GoogleTalkForm 类的构造函数里调用SetStyle方法,才能起到作用 this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.Selectable, true); this.SetStyle(ControlStyles.StandardClick, true); this.SetStyle(ControlStyles.StandardDoubleClick, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.Opaque, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, false); this.UpdateStyles(); The method isMousePointerInArea 方法用来检查特定区域内的鼠标位置。MousePosition 返回鼠标相对于桌面的位置。
private bool isMousePointerInArea(Point mousePosition, Rectangle area) { Point relativePoint = new Point(0, 0); relativePoint.X = mousePosition.X - this.Location.X; relativePoint.Y = mousePosition.Y - this.Location.Y; if (relativePoint.X < area.X) { return false; } else if(relativePoint.Y < area.Y) { return false; } else if(relativePoint.X > area.X + area.Width) { return false; } else if(relativePoint.Y > area.Y + area.Height) { return false; } return true; } The custom form painting is all done by the method 自定义的界面绘制是在OnFormPaint 中创建了一个和FORM同样大小的位图对象,然后再利用这个位图创建一个Graphics对象,并且使用了画线,画字符串等函数来实现的。在画完之后,利用DrawImageUnscaled方法 把这个位图复制到graphics实例。 // Create a new Pen object p = new Pen(this.OutlineColor, this.OutlineSize); // Draw the form outline g.DrawArc(p, rectLeftCorner, 180, 90); g.DrawArc(p, rectRightCorner, 270, 90); g.DrawLine(p, edgeRadius, 0, this.Width - edgeRadius, 0); g.DrawLine(p, 0, edgeRadius, 0, this.Height); g.DrawLine(p, this.Width - 1, edgeRadius, this.Width - 1, this.Height); g.DrawLine(p, 0, this.Height - 1, this.Width, this.Height - 1); // Dispose the Pen object p.Dispose(); p = null; A custom 自定义区域创建并应用到窗体,可以形成非矩形的窗口效果。为了创建我们需要的形状,我们不得不把非透明色的点一个一个添加到GraphicsPath 对象。窗口左右上角的颜色被认为是要被透明的颜色。 // Create GraphicsPath to be used to crop the region required gpRegion = new GraphicsPath(); // Loop through every pixel in the top left corner. // Create a 1 x 1 rectangle regions of pixels that do not match the transparent color for (int x = rectLeftCorner.X; x < rectLeftCorner.Width; x++) { for (int y = rectLeftCorner.Y; y < rectLeftCorner.Height / 2; y++) { if (isSameColor(bmp.GetPixel(x, y), this.transparentColor) == false) { gpRegion.AddRectangle(new Rectangle(x, y, 1, 1)); } } } // Loop through every pixel in the top right corner. // Create a 1 x 1 rectangle regions of pixels that do not match the transparent color for (int x = rectRightCorner.X + 1; x < rectRightCorner.X + rectRightCorner.Width + 1; x++) { for (int y = rectRightCorner.Y; y < rectRightCorner.Y + rectRightCorner.Height / 2; y++) { if (isSameColor(bmp.GetPixel(x, y), this.transparentColor) == false) { gpRegion.AddRectangle(new Rectangle(x, y, 1, 1)); } } } // Create the remaining rectangular regions to complete cover all the windows form area gpRegion.AddRectangle(new Rectangle(rectLeftCorner.Width, 0, this.Width - (edgeRadius * 4), rectLeftCorner.Height / 2)); gpRegion.AddRectangle(new Rectangle(0, rectLeftCorner.Height / 2, this.Width, this.Height)); // Apply region this.Region = new Region(gpRegion); History
NiaWs
|