Implement variable-size untitled window dragging in. Net Environment

Source: Internet
Author: User
In. the Untitled window dragging in the. Net environment seems simple. I believe many people have previously implemented it in environments such as VB, VC, and Delphi, of course, I don't think it's a big deal. However, the actual situation is still a little unexpected ~~

Generally, there are two common implementation methods for untitled window dragging:

Method 1:

  

During OnMouseDown event processing, record the oldPos-> at the time of OnMouseMove event processing, get the current mouse coordinate and move the form to the current position;

Method 2:

In OnMouseDown event processing, use WinAPI to send the WM_NCLBUTTONDOWN message to the form. In this way, you can click the left mouse button in the Form title bar;

The typical calling code may be as follows:

Private const int WM_NCLBUTTONDOWN = 0xA1;

Private const int HTCAPTION = 2;

[DllImport ("user32.dll")]

Private static extern int SendMessage (int hwnd, int wMsg, int wParam, int lParam );


[DllImport ("user32.dll")]

Private static extern int ReleaseCapture ();


Private void form_MouseDown (object sender, System. Windows. Forms. MouseEventArgs e)

{

// Release Mouse capture for the current application

ReleaseCapture ();

// Send the message so that the system will mistakenly assume that you have clicked the mouse on the title bar

SendMessage (int) this. Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0 );

}

}

The following describes the effects of the above two methods:



Obviously, method 1 is generally not recommended, because the border shadow of the form is generated during dragging, which is not beautiful. method 2, because it simulates mouse clicks, the effect is basically perfect.

But what if I want the form to be variable?



Why? Because, when we implement the Untitled form, the FormBorderStyle of the form is set to None. However, in this way, the form cannot be changed with the mouse (you cannot drag the border of the form with the mouse to make the form larger and smaller), does not meet our requirements.

Fortunately



A smart programmer finds a solution to set the FormBorderStyle attribute of the form to Sizable, The ControlBox attribute to False, and set the Text of the Form in OnLoad event processing of the form, that is, the title content is string. empty, Good, looks really Good, the form has no title, can be dragged, variable size, perfect solution !?

However

Note that the solution above sets the title content of the form to string. Empty. Although the effect is achieved, the form button displayed on the taskbar during running becomesBlank behind the iconThis is not what I want. Hey, when I minimize the form, I don't want the taskbar to display an empty hundred ~~! However, if Form. Text is not set to string. Empty, the title bar is displayed ~~ Of course, it's barely acceptable ~~ This form is not displayed on the taskbar ~~

However, there are even more terrible events:If the Text attribute value of the form is string. empty, the form cannot be used as a mode dialog box. That is to say, it cannot be called for any instance of the Form. ShowDialog () otherwise, the form is automatically closed once called ~~

My God! When I find this, I am deeply depressed and mourn my previous efforts!



Therefore, no perfect solution has been found for the following results.

How to implement a "form with no title, drag, variable size, and title displayed on the taskbar "?

Compromise



Currently, this compromise can only be achieved: When I need to display a title on the taskbar, while the form is displayed with no title, can be dragged, and the form may be used as a mode dialog box, discard the variable size function of the form (that is, you cannot drag the border of the form to make the form larger and smaller). Of course, the change to the above "perfect solution" is, set the FormBorderStyle attribute of the form to None. In this case, you can set the Text attribute of the form, that is, the title value.

I complained deeply about Uncle Bill, and then started VS. Net to write the code ~~~

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.