Use Visual C # To create a scalable personalized form

Source: Internet
Author: User
Introduction: Everyone wants their applications to impress others, and it is a good way to make their forms stand out. Imagine: In a lot of ordinary windows suddenly jump out of a very cool interface, it will make people eye bright and then generate interest. In VB and Vc, how to customize a scalable personalized window is no secret for a long time. There have been a lot of related articles to introduce how to call system APIs and other methods, but in.. net, but it is relatively troublesome to call the API. net also has some articles for creating personalized forms. They generally use transparent background and images, so they cannot be moved or scaled out. Is there a way to implement scalable and personalized forms without calling system APIs? Of course, the. NET Framework provides a very powerful system class library. We will create a scalable and personalized form using "pure". net. We need to replace all the "skins" of the form with our own defined ones, including the title bar, border, and system buttons. Therefore, we need to first create a set of our own skin graphics files. Because the form is scalable, we cannot simply take the entire image as the form skin, but cut the image into different parts as needed. Generally, show a few parts (the red line is the cutting line): name each part of the image as bottom_left, bottom, bottom_right, middle_left, middle_right, top_left, top_middle, top_right, sysbutton_min, sysbutton_close and sysbutton_restore. Note that some images can be scaled. For example, images in middle_left and bottom_middle can be a small part. You need to repeat the texture later. Some fixed-size images, such as bottom_left and top_left, will be pasted only once. In actual application, you must differentiate them. With the above principles, you can create skin images, as shown in the figure below: You can then put these images in the imagelist control or resource file for the program to call. (For details about how to create a resource file, see Visual C # resource file programming-create a resource file.) Next, we use Visual Studio. net to create a Windows application project. In the form attribute settings, set the formborderstyle attribute of the form to none (no border style), as shown in: Define a resource manager: private ResourceManager Rm; then, use the following method to retrieve the image in the form Constructor (the resource file name is skin. resources): Rm = new ResourceManager ("skinwindow. skin ", assembly. getexecutingassembly (); bottom_left = (Bitmap) Rm. getObject ("bottom_left ");... (Other images can also be retrieved using this method) The onpaint event of the heavy-load form: Graphics G = E. graphics; // manually draw each part of the form drawmiddle_left (E. graphics); // draw the Left Border drawbottom_middle (E. graphics); // draw the bottom border drawmiddle_right (E. graphics); // draw the right border drawbottom_left (E. graphics); // draw drawbottom_right (E. graphics); // draw drawtop_left (E. graphics); // draw drawtop_right (E. graphics); // draw drawtop_middle (E. graphics); // draw the drawsys_button (E. graphics); // The painting system is subject to the specific implementation of the above skin painting method. I will only give a code example for drawing the left border. For other parts, please give them a return:
private void DrawMiddle_Left(Graphics g){Brush brush = new TextureBrush(Middle_Left, new Rectangle(0, 0, Middle_Left.Width, Middle_Left.Height));g.FillRectangle(brush, 0, TITLE_WIDTH, Middle_Left.Width, Height - Bottom_Middle.Height - TITLE_WIDTH);}
When we put on our clothes, our program now has a different appearance: It looks pretty cool, but it's just a fancy, because of the border, title bar, system buttons are fake borders, title bars, and system buttons drawn by ourselves. Therefore, this form cannot be moved or scaled up or down freely, and it is useless to close points. In the past, we never had to worry about this when writing programs. Are these basic functions of forms? I never thought it would be a problem? What should we do? The answer is to do it by ourselves, but it will be troublesome. Because the border is canceled, Windows will not send system events for you. If you cannot capture what happened to the system, there is no way to write the response code, so we need to detect the coordinates of the mouse, and send the event message by ourselves Based on the mouse action, and then respond. First, we define some code to respond to events. I have defined an abstract base class mouseaction to indicate all mouse events. It has an abstract method action: public abstract class mouseaction {public abstract void action (INT screenx, int screeny, system. windows. forms. form);} then define its derived classes to indicate the code that reflects the response of each mouse event. Below is a code response for a window event stretching to the right:
public class MouseSizeRight : MouseAction{private int lx;public MouseSizeRight(int LocationX){lx = LocationX;}public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form){form.Width = ScreenX - lx;form.Invalidate();}}
It is very simple and easy to understand, so I will not repeat it again. All other events are equally simple. Here we do not provide the implementation code for all events, just to list the code response class to be implemented: mousesizeleft: stretch the Left Border mousesizebottom: stretch the lower border mousesizetop: stretch the Upper Border border tip: stretch the upper left corner mousesizetopright: stretch the upper right corner mousesizebottomright: dragging mousedrag in the lower right corner: it is also very easy to drag the mouse, but it is slightly different from the scaling and stretching of the window. The implementation code is as follows:
public class MouseDrag : MouseAction{private int x, y;public MouseDrag(int hitX, int hitY){x = hitX;y = hitY;}public override void Action(int ScreenX, int ScreenY, System.Windows.Forms.Form form){form.Location = new Point(ScreenX - x, ScreenY - y);}}
Next, we will write the code for sending events. We will first define several variables: Private int left = 5, Right = 5, Bottom = 5, Top = 5, title_width = 45; // size of the border and title bar private int x = 0, y = 0; // Save the temporary coordinates of the mouse private mouseaction mouse; // The mouse Event Response object and record the current coordinates of the mouse in the form mousedown event: x = E. x; y = E. y; Appendix: E is system. windows. forms. mouseeventargs then defines the Event Response object based on the coordinates of the mouse:
// Click the upper-left border if (E. x <= left + 10 & E. Y <= Top) | (E. Y <= Top + 10 & E. x <= left) {mouse = new mousesizetopleft (location. x, location. y, width, height); return ;}
Of course, some events can also respond directly:
// Click system close button if (E. x> width-20 & E. y> 6 & E. x <width-20 + sysbutton_min.width & E. Y <6 + sysbutton_min.height) {close (); return ;}
Most event responses are actually completed in the mousemove event:
Private void form_mousemove (Object sender, system. windows. forms. mouseeventargs e) {This. parent. cursor = checkcursortype (E. x, E. y); // change the pointer shape of the mouse if (mouse! = NULL) {mouse. action (control. mouseposition. x, control. mouseposition. y, this); // execution time response // note that the coordinates are control. the mouseposition static variable is given, and its value is the global coordinate of the mouse on the desktop }}
Give the pointer shape for each different part:
private Cursor CheckCursorType(int X, int Y){if(((X <= LEFT + 10 && Y <= TOP) || (Y <= TOP + 10 && X <= LEFT)) || ((X >= Width - RIGHT - 10 && Y >= Height - BOTTOM) || (Y >= Height - BOTTOM - 10 && X >= Width - RIGHT))){return Cursors.SizeNWSE;}else if(((Y <= TOP + 10 && X >= Width - RIGHT) || (Y <= TOP && X >= Width - RIGHT - 10)) || ((X <= LEFT && Y >= Height - BOTTOM - 10) || (Y >= Height - BOTTOM && X <= LEFT + 10))){return Cursors.SizeNESW;}else if(X >= Width - RIGHT || X <= LEFT){return Cursors.SizeWE;}else if(Y >= Height - BOTTOM || Y <= TOP){return Cursors.SizeNS;}else{return Cursors.Arrow;}}
Finally, release the mouse variable in the mouseup event:
private void Form_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e){mouse = null;}
To be more realistic, you can add a double-click on the title bar to maximize or restore the event:
private void Form_DoubleClick(object sender, System.EventArgs e){if(y > TOP && y < TITLE_WIDTH){if(WindowState == FormWindowState.Normal){WindowState = FormWindowState.Maximized;SysButton = SysButton_Restore;Invalidate();}else if(WindowState == FormWindowState.Maximized){WindowState = FormWindowState.Normal;SysButton = SysButton_Max;Invalidate();}}}

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.