C # mobile forms and controls

Source: Internet
Author: User

In the winform program, sometimes we need to move controls in the form without a title bar or in the form, and use several events such as mouse clicks, movement, and coordinate calculation to complete this function, however, an API function has been found recently, which can be easily and conveniently completed. As follows:

Code [DllImportAttribute ("user32.dll")]
Private extern static bool ReleaseCapture ();
[DllImportAttribute ("user32.dll")]
Private extern static int SendMessage (IntPtr handle, int m, int p, int h );

 

When using these two functions, import using System. Runtime. InteropServices;

Use:

ReleaseCapture ();
SendMessage (this. Handle, 0xA1, 0x2, 0 );

This parameter does not need to be changed except the this. Handle parameter. For example, we define a MouseDown event for the form:

This. MouseDown + = new MouseEventHandler (MyBaseControl_MouseDown );

Then you can drag the form at any point. The Event code is as follows:

Code 1 protected void MyBaseControl_MouseDown (object sender, MouseEventArgs e)
2 {
3 if (e. Button = MouseButtons. Left)
4 {
5 this. Cursor = Cursors. SizeAll;
6 ReleaseCapture ();
7 SendMessage (this. Handle, 0xA1, 0x2, 0 );
8 this. Cursor = Cursors. Default;
9}
10}
11

 

If a control is used for running, you can put the above Code into the MouseDown event of the control, only in SendMessage (this. Handle, 0xA1, 0x2, 0 );

This. Handle parameter should be changed to the Handle of this control, such as this. button1.Handle.

Related Article

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.