Application of Virtual controls in GUI Programming

Source: Internet
Author: User
Tags bmp image

In graphical user interface programming, user controls are an important concept. Essentially, our graphical user interface is composed of various controls. Some of these controls are provided by IDE developers and some are provided by third parties, if none of these controls can complete the function, you need to write the control yourself. Writing a control in C # is very simple. The following code can implement a user control.

Public class UserControl1: System. windows. forms. userControl {protected override void OnMouseMove (MouseEventArgs e) {// handle the mouse movement code} protected override void OnPaint (PaintEventArgs e) {// code for drawing the user interface} // other code for the user control}

The younger brother proposed the concept of "Virtual Control". The so-called virtual control is to place a proxy layer between the control and the code of the operation control. All the Code uses this proxy to operate the control, the control event also calls the relevant control code through this proxy layer. The control and Code do not have a direct relationship. The following code implements the proxy layer in a simple "virtual control"

Public class VirtualControlBase {protected System. windows. forms. control myControl = null; public System. windows. forms. paintEventHandler _ Paint = null; public System. windows. forms. mouseEventHandler _ MouseMove = null; public VirtualControlBase () {_ Paint = new System. windows. forms. paintEventHandler (myControl_Paint); _ MouseMove = new System. windows. forms. mouseEventHandler (myControl_MouseMove);} // The bound public control System. Windows. Forms. Control BindControl {get {return myControl;} set {if (value! = MyControl) {if (myControl! = Null) {myControl. Paint-= _ Paint; myControl. MouseMove-= _ MouseMove;} myControl = value; if (myControl! = Null) {myControl. paint + = _ Paint; myControl. mouseMove + = _ MouseMove ;}}// public System. windows. forms. control BindControlprivate void myControl_Paint (object sender, System. windows. forms. paintEventArgs e) {HandlePaint (e. graphics, e. clipRectangle);} private void myControl_MouseMove (object sender, System. windows. forms. mouseEventArgs e) {HandleMouseMove (e. x, e. y, e. button);} protected virtual void HandlePaint (System. drawing. graphics, System. drawing. rectangle ClipRectangle) {} protected virtual void HandleMouseMove (int x, int y, System. windows. forms. mouseButtons Button) {} public System. drawing. size ClientSize {get {return System. drawing. size. empty;} public System. drawing. rectangle Bounds {get {return myControl. bounds;} public System. drawing. graphics CreateGraphics () {return myControl. createGraphics ();} public System. windows. forms. cursor {get {return myControl. cursor;} set {myControl. cursor = value ;}} public System. drawing. font {get {return myControl. font;} set {myControl. font = value ;}} public void Refresh () {myControl. refresh ();} public void Invalidate (System. drawing. rectangle Rect) {myControl. invalidate (Rect, true);} public void Update () {myControl. update () ;}// public class VirtualControlBase

With this proxy layer, the development of user controls will not be inherited based on System. Windows. Form. UserControl. Instead, it inherits from VirtualControlBase. At this time, the developed virtual controls only reload HandlePaint and HandleMouseMove to draw the control user interface and process mouse movement events. The user control code is as follows:

Public class UserControl2: VirtualControlBase {protected override void HandleMouseMove (int x, int y, MouseButtons Button) {// code for processing mouse movement} protected override void HandlePaint (Graphics, Rectangle ClipRectangle) {// code for drawing the user interface} // other code for the user control}

This mode is called the "virtual control" mode, while the traditional control is called the "real control" mode.

The well-designed proxy layer of virtual controls makes code compilation and real-control code compilation slightly different.

Virtual controls cannot be directly used. They must be attached to other controls. First, a standard control, such as Panel, must be placed in the form. Instantiate a virtual control and call the VirtualControlBase. BindControl attribute to bind the control.

Why do we need so much to develop a user control. In fact, there are many benefits to doing so.

  • Conducive to code separation and modularization
  • The real control can only be used in graphical user interface programs, while the virtual control can be used in other types of programs, including command line programs and ASP. NET, WebService, and Windows Services. This feature can be applied to porting certain programs. For example, porting a GUI program to ASP. NET, the GUI program compiled the complex graphics drawn by the virtual control, and then in ASP.. NET program, it creates a BMP image, and then uses the library function System. drawing. graphics. fromImage creates a Graphics object and calls the HandlePaint of the Virtual Control to draw the complex image. Then, it can be published. In this way, the virtual control can be used in both GUI programs and ASP. NET programs to achieve code reuse. Using this feature, you can also better modify the Old GUI code, maintain the original functions, and provide graphic Support to non-GUI programs such as ASP. NET.
  • Virtual controls can reduce the number of controls. In some GUI applications, many widgets may accumulate in the form. For example, a form has a TabControl and many pages, each page contains a user control of the same type. You need to switch back and forth between these controls. As you know,. NET controls are based on Win32 forms. Creating a control is laborious and consumes resources. Virtual controls are essentially just a class. It is very easy to create a class in. NET. In this way, you only need to place a control in the form, and then generate many virtual controls. After switching back and forth between the controls, you can convert them to bind to and unbind them. In addition, placing a large number of controls in the form is not conducive to the work of the Form Designer. If an error occurs in the design status of a control, the normal operation of the Form Designer is affected. (In VB, if a user control fails to be loaded, it is automatically converted to a PictureBox control, and all design attributes will be lost)
  • Before using a control, you must. NET toolbox to add the control icon, this operation is troublesome, and open other projects that do not need the control, the toolbox will also display the control icon. This is not the case when Virtual controls are used.

In fact, the concept of virtual controls can also be implemented using VB. The following code is a simple example.

'Class myControl. private WithEvents myControl As VB. pictureBoxPublic Sub SetBindControl (ByVal p As VB. pictureBox) Set myControl = pEnd SubPrivate Sub myControl_MouseMove (Button As Integer, Shift As Integer, X As Single, Y As Single) 'process the mouse movement event End SubPrivate Sub myControl_Paint () 'Operation End Sub of Drawing Graphics

The following is the complete code of a virtual control proxy layer compiled by C.

/// <Summary> /// module bound to the control /// </summary> public class VirtualControlBase {/// <summary> // control bound to the object /// </summary> protected System. windows. forms. control myControl = null; // <summary> // delegate object for processing the Control repainting event /// </summary> public System. windows. forms. paintEventHandler _ Paint = null; // <summary> // delegate object for processing the event by pressing the mouse key /// </summary> public System. windows. forms. mouseEventHandler _ MouseDown = null; // <summary> // process mouse movement Event Delegate object // </summary> public System. windows. forms. mouseEventHandler _ MouseMove = null; // <summary> // handle the mouse button to release the event's delegate object /// </summary> public System. windows. forms. mouseEventHandler _ MouseUp = null; // <summary> // process the delegate object for the mouse cursor to enter the control event /// </summary> public System. eventHandler _ MouseEnter = null; // <summary> // delegate object for processing the mouse cursor to exit the control event /// </summary> public System. eventHandler _ MouseLeave = null; // <summary> // Process The delegate object of the scroll wheel event // </summary> public System. windows. forms. mouseEventHandler _ MouseWheel = null; /// <summary> /// delegate object for handling keyboard-pressing events /// </summary> public System. windows. forms. keyEventHandler _ KeyDown = null; // <summary> // delegate object for handling keyboard input character events /// </summary> public System. windows. forms. keyPressEventHandler _ KeyPress = null; // <summary> // delegate object for handling the keyboard key release event /// </summary> public System. windows. forms. keyEventHandler _ K EyUp = null; // <summary> // The delegate object for handling the control to obtain the focus event // </summary> public System. eventHandler _ GotFocus = null; // <summary> // delegate object for handling control focus loss events /// </summary> public System. eventHandler _ LostFocus = null; // <summary> // process the delegate object of the mouse-clicked event // </summary> public System. eventHandler _ Click = null; // <summary> // delegate object for handling double-Click events with the mouse /// </summary> public System. eventHandler _ DoubleClick = null; // <summary> // delegate to handle the event of widget size change Like /// </summary> public System. eventHandler _ Resize = null; // <summary> // initialize the object // </summary> public VirtualControlBase () {_ Paint = new System. windows. forms. paintEventHandler (myControl_Paint); _ MouseDown = new System. windows. forms. mouseEventHandler (myControl_MouseDown); _ MouseMove = new System. windows. forms. mouseEventHandler (myControl_MouseMove); _ MouseUp = new System. windows. forms. mouseEventHandler (my Control_MouseUp); _ MouseEnter = new System. eventHandler (myControl_MouseEnter); _ MouseLeave = new System. eventHandler (myControl_MouseLeave); _ MouseWheel = new System. windows. forms. mouseEventHandler (myControl_MouseWheel); _ KeyDown = new System. windows. forms. keyEventHandler (myControl_KeyDown); _ KeyPress = new System. windows. forms. keyPressEventHandler (myControl_KeyPress); _ KeyUp = new System. windows. forms. K EyEventHandler (myControl_KeyUp); _ GotFocus = new System. eventHandler (myControl_GotFocus); _ LostFocus = new System. eventHandler (myControl_LostFocus); _ Click = new System. eventHandler (myControl_Click); _ DoubleClick = new System. eventHandler (myControl_DoubleClick); _ Resize = new System. eventHandler (myControl_Resize);} // <summary> // re-bind the control /// </summary> public virtual void ReBind () {this. bindControl = this. MyControl;} // <summary> // bound control /// </summary> public virtual System. windows. forms. control BindControl {get {return myControl;} set {if (myControl! = Null) {myControl. paint-= _ Paint; myControl. mouseDown-= _ MouseDown; myControl. mouseMove-= _ MouseMove; myControl. mouseUp-= _ MouseUp; myControl. mouseEnter-= _ MouseEnter; myControl. mouseLeave-= _ MouseLeave; myControl. mouseWheel-= _ MouseWheel; myControl. keyDown-= _ KeyDown; myControl. keyPress-= _ KeyPress; myControl. keyUp-= _ KeyUp; myControl. gotFocus-= _ GotFocus; myControl. lostFocus-= _ LostFocus; myContr Ol. Click-= _ Click; myControl. DoubleClick-= _ DoubleClick; myControl. Resize-= _ Resize;} myControl = value; if (myControl! = Null) {myControl. paint + = _ Paint; myControl. mouseDown + = _ MouseDown; myControl. mouseMove + = _ MouseMove; myControl. mouseUp + = _ MouseUp; myControl. mouseEnter + = _ MouseEnter; myControl. mouseLeave + = _ MouseLeave; myControl. mouseWheel + = _ MouseWheel; myControl. keyDown + = _ KeyDown; myControl. keyPress + = _ KeyPress; myControl. keyUp + = _ KeyUp; myControl. gotFocus + = _ GotFocus; myControl. lostFocus + = _ LostFocus; myContr Ol. click + = _ Click; myControl. doubleClick + = _ DoubleClick; myControl. resize + = _ Resize;} OnAfterBindControl () ;}// public System. windows. forms. control BindControl /// <summary> // processing after the Control is bound /// </summary> protected virtual void OnAfterBindControl () {} private void myControl_KeyDown (object sender, System. windows. forms. keyEventArgs e) {HandleKeyDown (e. keyCode, e. alt, e. shift, e. control);} private void myContro Rochelle keypress (object sender, System. windows. forms. keyPressEventArgs e) {HandleKeyPress (e. keyChar);} private void myControl_KeyUp (object sender, System. windows. forms. keyEventArgs e) {HandleKeyUp (e. keyCode, e. alt, e. shift, e. control);} private void myControl_MouseLeave (object sender, System. eventArgs e) {HandleMouseLeave ();} private void myControl_MouseWheel (object sender, System. windows. forms. mouseEventArgs E) {HandleMouseWheel (e. x, e. y, e. button, e. delta);} private void myControl_GotFocus (object sender, System. eventArgs e) {HandleGotFocus ();} private void myControl_LostFocus (object sender, System. eventArgs e) {HandleLostFocus ();} private void myControl_MouseEnter (object sender, System. eventArgs e) {HandleMouseEnter ();} private void myControl_Click (object sender, System. eventArgs e) {System. drawing. point p = System. windows. forms. control. mousePosition; p = myControl. pointToClient (p); HandleClick (p. x, p. y, System. windows. forms. control. mouseButtons);} private void myControl_DoubleClick (object sender, System. eventArgs e) {System. drawing. point p = System. windows. forms. control. mousePosition; p = myControl. pointToClient (p); HandleDoubleClick (p. x, p. y, System. windows. forms. control. mouseButtons);} private void myCon Trol_Paint (object sender, System. windows. forms. paintEventArgs e) {HandlePaint (e. graphics, e. clipRectangle);} private void myControl_MouseDown (object sender, System. windows. forms. mouseEventArgs e) {HandleMouseDown (e. x, e. y, e. button);} private void myControl_MouseMove (object sender, System. windows. forms. mouseEventArgs e) {HandleMouseMove (e. x, e. y, e. button);} private void myControl_MouseUp (object sender, System. windows. forms. mouseEventArgs e) {HandleMouseUp (e. x, e. y, e. button);} private void myControl_Resize (object sender, System. eventArgs e) {HandleResize ();} # region control event handling group /// <summary> /// process the keyboard press event /// </summary> /// <param name = "KeyCode"> button encoding </param> /// <param name = "Alt"> whether to press the Alt key </param> /// <param name = "Shift"> whether to press the Shift key </param> // <param name = "Control"> whether to press the Control key </param> protected virtual void HandleKeyDown (System. windows. forms. keys KeyCode, bool Alt, bool Shift, bool Control) {}/// <summary> /// handle keyboard character events /// </summary> /// <param name = "KeyChar"> character data </param> protected virtual void HandleKeyPress (char KeyChar) {}/// <summary> /// handle the keyboard button release event // </summary> /// <param name = "KeyCode"> key encoding </param> /// <param name = "Alt"> whether to press the Alt key </param> // <param name = "Shift"> whether to press the Shift key </param> // /<param name = "Control "> Whether to press the Control key </param> protected virtual void HandleKeyUp (System. windows. forms. keys KeyCode, bool Alt, bool Shift, bool Control) {}/// <summary> /// handle the event when the mouse cursor leaves the control workspace /// </summary> protected virtual void HandleMouseLeave () {}/// <summary> /// process the scroll wheel event // </summary> /// <param name = "x"> X coordinate of the mouse cursor </param> /// <param name = "y"> Y coordinate of the mouse cursor </param> /// <param name = "Button"> key value </param> // /<param name = "Delta"> scroll wheel value </param> Protected virtual void HandleMouseWheel (int x, int y, System. windows. forms. mouseButtons Button, int Delta) {}/// <summary> // process the control to obtain the focus event // </summary> protected virtual void HandleGotFocus () {}/// <summary> /// process the event where the control loses focus /// </summary> protected virtual void HandleLostFocus () {}/// <summary> /// process the mouse cursor entering the control workspace event // </summary> protected virtual void HandleMouseEnter () {}/// <summary> /// process the mouse click event /// </summary> /// <Param name = "x"> X coordinate of the mouse cursor </param> /// <param name = "y"> Y coordinate of the mouse cursor </param>/ // <param name = "Button"> mouse key value </param> protected virtual void HandleClick (int x, int y, System. windows. forms. mouseButtons Button) {}/// <summary> /// process the double-click mouse event // </summary> /// <param name = "x"> X coordinate of the mouse cursor </param> /// <param name = "y"> Y coordinate of the mouse cursor </param> /// <param name = "Button"> protected virtual void HandleDoubleClick (int x, int Y, System. windows. forms. mouseButtons Button) {}/// <summary> /// process the mouse button press event // </summary> /// <param name = "x"> X coordinate of the mouse cursor </ param> /// <param name = "y"> Y coordinate of the mouse cursor </param> /// <param name = "Button"> protected virtual void HandleMouseDown (int x, int y, System. windows. forms. mouseButtons Button) {}/// <summary> /// process the mouse movement event /// </summary> /// <param name = "x"> X coordinate of the mouse cursor </param> /// <param name = "y"> Y coordinate of the mouse cursor </param> /// <Param name = "Button"> mouse key value </param> protected virtual void HandleMouseMove (int x, int y, System. windows. forms. mouseButtons Button) {}/// <summary> /// handle the mouse button release event // </summary> /// <param name = "x"> X coordinate of the mouse cursor </ param> /// <param name = "y"> Y coordinate of the mouse cursor </param> /// <param name = "Button"> protected virtual void HandleMouseUp (int x, int y, System. windows. forms. mouseButtons Button) {}/// <summary> // process the re-painting user Interface event // </summary> /// <param name = "Graphics"> graphic drawing object </param> /// <param name = "ClipRectangle"> cut the rectangle region </param> protected virtual void HandlePaint (System. drawing. graphics, System. drawing. rectangle ClipRectangle) {}/// <summary> // handle the event of widget size change /// </summary> protected virtual void HandleResize () {}# endregion // <summary> // create a bitmap object for the displayed content /// </summary> /// <param name = "bounds"> region </param> /// <returns> Create a BMP bitmap object </returns> public System. drawing. bitmap CreateBitmap (System. drawing. rectangle bounds) {System. drawing. bitmap bmp = new System. drawing. bitmap (bounds. width, bounds. height); using (System. drawing. graphics g = System. drawing. graphics. fromImage (bmp) {g. translateTransform (bounds. left, bounds. top); this. handlePaint (g, bounds);} return bmp;} // <summary> // whether the object is bound with a control /// </summary> public bool HasBindC Ontrol {get {return myControl! = Null; }}/// <summary> // create a Drawing Object // </summary> /// <returns> </returns> public System. drawing. graphics CreateGraphics () {if (myControl = null) return null; elsereturn myControl. createGraphics () ;}/// <summary> /// set the input Focus /// </summary> public void Focus () {if (myControl! = Null) myControl. focus () ;}/// <summary> /// mouse cursor /// </summary> public System. windows. forms. cursor {get {if (myControl = null) return System. windows. forms. cursors. default; elsereturn myControl. cursor;} set {if (myControl! = Null) myControl. Cursor = value ;}/// <summary> // obtain a value that indicates whether the control has an input focus. /// </Summary> public bool Focused {get {return myControl = null? False: myControl. Focused ;}/// <summary> // force the control to invalidate the workspace and redraw itself and any child controls immediately. /// </Summary> public virtual void Refresh () {if (myControl! = Null) myControl. refresh () ;}//< summary> /// obtain the control set contained in the control /// </summary> public System. windows. forms. control. controlCollection Controls {get {return myControl = null? Null: myControl. controls ;}}/// <summary> // invalidate the specified area in the control, and send the draw message to the control /// </summary> /// <param name = "Rect"> </param> public virtual void Invalidate (System. drawing. rectangle Rect) {if (myControl! = Null) myControl. invalidate (Rect, true );} /// <summary> /// determines whether the mouse has been captured by the control. /// </summary> public bool Capture {get {return myControl = null? False: myControl. capture ;}/// <summary> // make the control re-paint the invalid area in the workspace /// </summary> public void Update () {if (myControl! = Null) myControl. Update () ;}// public class VirtualControlBase

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.