精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。
With the advent of GDI+ in .Net, graphics programming is made very easy. One of the main functionality provided by Microsoft in .net is the transparency, yet this transparency can be applied to only to forms. For having our cool transparent control, Bob Powell in article at http://www.bobpowell.net/transcontrols.htm
随着.net中GDI +的出现,图形编程变得非常容易。在.NET中由微软提供的一个主要功能是透明度,然而这种透明度只能适用于窗体。在Bob Powell文章http://www.bobpowell.net/transcontrols.htm中有我们很酷的透明控件。
I will just continue this article to write a control which provides magnification functionality as well.本文我将继续写具有放大功能的控件。
We will develop this magnifier control in two steps, first we will write a transparent control and then change it to add magnifying functionality.
我们将分两步开发这个放大镜控件,首先,我们将编写一个透明的控件,然后改变它添加放大功能。
I have written a class TransControl inheriting from System.Windows.Forms.Form 我写了一个TransControl类从System.Windows.Forms.Form 继承
namespace TransControl { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { <CODE><FONT color=#0000ff size=2><FONT size=2></FONT>
Now to make this control I will have to make couple of things as, 现在完成控件我必须做一些事情,
First, I will have to change the behavior of the window by giving it a <SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: Georgia; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA">WS_EX_TRANSPARENT style. This is accomplished by overriding the CreateParams property so that the correct window style is included when the control is instantiated. The listing below shows this property override.
首先,我必须通过一个透明的样式来改变窗口的属性<SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: Georgia; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"> 。这是通过覆盖CreateParams属性来实现的,以便于控件实例化时能够包含正确的窗口样式。下面的清单显示了这个覆盖属性。
protectedoverrideCreateParams CreateParams { get { CreateParams cp=base.CreateParams; cp.ExStyle|=0x00000020; //WS_EX_TRANSPARENT returncp; } }
Second, just to make sure the control background is not painted, we will have a blank OnPaintBackground event, as follows. 其次,为了确保控件背景透明,我们会有一个空白的OnPaintBackground事件,如下。
protected override void OnPaintBackground(PaintEventArgs pevent) { //Do not write anything here, and do not remove it. }
So with this our transparent control is ready now we need to make this control movable, so we will add these events in the class as follows, 这样我们这个透明的控件就准备就绪了,我们需要让这个控件移动,所以我们会在类中添加这些事件,如下,
protected void this_MouseMove(object sender, MouseEventArgs e) { if(isMouseDown) { this.Left = this.Left + (e.X-x); this.Top = this.Top + (e.Y-y); //Refresh the parent to ensure that whatever is behind the control gets painted //before we need to do our own graphics output. this.Parent.Refresh(); } } protected void this_MouseDown(object sender, MouseEventArgs e) { x= e.X; y=e.Y; left=this.Left; top=this.Top; isMouseDown=true; this.Cursor= Cursors.SizeAll; } protected void this_MouseUp(object sender, MouseEventArgs e) { isMouseDown=false; this.Cursor= Cursors.Default; this.Refresh(); } <FONT size=2></FONT>
Here ends our movable transparent control, now we will add the magnifier functionality. 这里结束可移动的透明控件,现在我们将添加放大镜的功能。
We have written the transparent control, now we will change it to add Magnification functionality 我们已经写好了透明的控件,现在我们将改变它,添加放大功能。
This control should do two things, 这种控件应该做两件事,
1) It should magnify the selected background 它应该放大所选的背景
2) It should support, setting X & Y offset, to see magnification of a particular point. 它应该支持看到某一特定点的放大倍率,设置X和Y偏移,。
For Magnification, I have used very simple logic as , whenever you draw an image of small size on bigger canvas, it looks like magnified although the quality is not very good. 对于放大倍率,我用很简单的逻辑,每当你在大的画布上画一个小尺寸的图片,它看起来就像放大虽然质量不是很好。
I have declared three properties in TransControl, 我在TransControl中声明了三个属性,as
</FONT>
These three properties will be used by TransControl to provide magnification. Remember, in first step we overrided the OnPaintBackground method and kept it blank, now we will the code in it as follows, 这三个属性将由TransControl使用,用于提供放大。记得在第一步中,我们覆盖OnPaintBackground方法并且保持空白,现在我们将代码如下,
protected override void OnPaintBackground(PaintEventArgs pevent) { pevent.Graphics.DrawImage(this.Parent.BackgroundImage, new Rectangle(0,0,this.Width,this.Height),this.Left + XOffset, this.Top + YOffset,this.Width-(MagnificationFactor/10), this.Height-(MagnificationFactor/10), GraphicsUnit.Pixel); }
Thats all, we are ready with Movable magnifier control.这一切做完,我们就准备好了一个透明且可以移动的放大镜控件。