[Original] Creating a Dragon Form with C #

xiaoxiao2021-03-06  55

Create a dragable form with C #

Original: lover_p

The so-called "dragable form" is a form that can be dragged without dragging the title bar, which is a very useful technology on a form without the title bar. About this technical skills, there are many introductions on the Internet, but they are not very detailed, and some implementations are not simple enough. Most of these introductions, most of them are the source code of large pieces, very little explanation; beginners semester will probably cost some strength (I am very painful when I study this skill). Here, I made a detailed explanation of this technique. I believe that beginners can learn how to make dragable forms.

(Note: The readers expected in this article are those who have some C # develop Windows program, but temporarily do not produce friends who can drag the form.)

First, let's review the composition of a Windows form. Please see this picture:

Figure 1 Windows Form

This is a formal standard Windows form. First, the top of the form is a title bar, and the remaining portion is the body of the form, surrounding the outer frame per circumservation of the form body, the border is not the user area of ​​the control or drawing graphics.

A method of obtaining a size of various form constructor is also labeled. For the user area, System.Windows.Forms.form provides instance attribute clientsize, I believe everyone is already familiar. For the size of the active form, such as the title bar, border, etc.), we can use a class. Net class library: system.windows.forms.systeminformation, this class provides some static Attributes, such as Captionheight, which represents the height of the title bar. Information about the SystemInformation class can be found at the .NET SDK Document Directory .NET Framework SDK -> Reference -> Class Library -> System.Windows.Forms -> SystemInformation class (Note: The hyperlink here is only installed) The .NET Framework 1.1 Simplified Chinese version is also valid when a supporting document is installed. This is a very useful class, I hope everyone can remember it (maybe you know, but I know -_-sweat ~~).

Next, let's see how to move the form when dragging the mouse in the user area. Please see this picture below:

Figure 2 Movement of the form

Let's observe the movement of the mouse in the form and the movement of the form, it is easy to find that during the process of being dragged, the relative position of the mouse in the form is always constant! Then, we only need to detect the movement of the mouse on the screen and modify the location of the form, you can reach the purpose of dragging the form!

We know that in mouse message / event processing, we can only get the position of the mouse relative to the form. So, how do you know where your mouse is in your screen? Here is to mention a class: System.Windows.Forms.Control class. Maybe you will be very surprised: Is this not a base class for all controls? Oh ~ is this di. However, even though this, the Control class is not declared as abstract classes like other widely used base classes, and it provides a static property: mousePosition, through this property, you can get the position of the mouse relative to the screen. Information about the Control class can be found in the .NET Framework SDK -> Reference -> Class Library -> System.Windows.Forms -> Control class (Note: The hyperlink here is only installed. " The .NET Framework 1.1 Simplified Chinese version is also valid when a supporting document is installed. After knowing how to get this information, the production of mobile forms is actually a very simple problem. The basic process is this: First, in the mouse (left button or a key you like), record the mouse position; due to the mobile process, the mouse screen coordinates change but the form is relatively coordinate, we You can calculate the change in the location of the form (assuming mouseposition has the system.drawing.point type, indicating the relative coordinate of the mouse in the form):

// Sample code 1FORM.TOP = Control.MousePosition.y - mouseposition.y; form.left = control.mouseposition.x - mousePosition.x;

This is not yet, because our mousePosition represents the relative coordinates of the mouse in the form user area, but in the mobile form, consider the size of the form header bar and the size of the border. On the above, we correct the code to:

Sample Code // 2Form.Top = Control.MousePosition.Y - mousePosition.Y - SystemInformation.FrameBorderSize.Height - SystemInformation.CaptionHeight; Form.Left = Control.MousePosition.Y - mousePosition.Y - SystemInformation.FrameBorderSize.Width;

That is, at a height (ordinate) to subtract the height of the title bar and the height of the border, on the width (horizontal coordinate) to subtract the width of the border. However, when making a dragable form that has neither the title bar does not have a border, it is possible to use the code shown in Example Code 1.

The above code is just an exemplary code. The specific operation is as follows:

First, add a private domain for the form:

Private system.drawing.point mousepoint ;. PRIVATEPOINT;

Then, add the mouse to the form (I am Mainform_MouseDown, don't forget to link the method to the mainform.mousedown event, don't say more?):

Private void mainform_mousedown (Object sender, system.windows e) {if (e.button == mousebuttons.left) {this.mouseposition.x = EX; this.mouseposition.y = EY;}} Screened the mouse button.

Next, add a mouse mobile event processing method for the form (I am Mainform_Mousemove):

private void MainForm_MouseMove (object sender, System.Windows.Forms.MouseEventArgs e) {if (e.Button == MouseButtons.Left) {Form.Top = Control.MousePosition.Y - mousePosition.Y - SystemInformation.FrameBorderSize.Height - SystemInformation .CaptionHeight; form.left = control.mouseposition.y - mousePosition.y - systeminformation.framebordersize.width;}}

Here, if the form does not have a title bar, you can remove "-systeminformation.captionHeight"; if the form does not have a border, you can remove "-SystemInformation.framebordersize.Height" and "-systeminformation.framebordersize.width".

Now, is you very clear about how to use C # production to drag the window? congratulations! Finally, send you a small gift - desktop clock. Very simple, very interesting ~~~

Also give small skills:

How to make an irregular form

This tip is not very rich, now introduces a simple method - by setting the form of the region property. First, declare a system.drawing.drawing2d.graphicspath variable, add your expectations as the shape combination of the form outline; then generate a system.drawing.region instance from this variable, and assign the form property I.e. Such as:

Graphicspath gp = new graphicspath (); gp.addellipse (0, 0, 120, 120); region r = new region (GP); this.region = R;

Add the above code to the form of a form, we can get a custom form, this form is a circular shape having a diameter of 120 pixels (also the appearance of my clock).

How to generate a form without a border

Set the form's FORMPORDERSTYLE to NONE. Is this simple matter? Then

How to generate a form with a border but no title bar

转载请注明原文地址:https://www.9cbs.com/read-117005.html

New Post(0)