[C #] use the Label control to simulate the movement of the Form title,
This article is an original article and the source code is the original code. For example, repost/copy the original code, please clearly mark the original name, author, and URL on the webpage/code. Thank you!
Development Tool: VS2017
Language: C #
DotNet version:. Net FrameWork 4.0 or later
1. Two WIN32 APIs are used. One is ReleaseCapture and the other is SendMessage. The two functions are described as follows:
ReleaseCapture Function: To describe the use of ReleaseCapture, you must first know the use of SetCapture. MSDN describes the SetCapture function as follows:
This function sets Mouse capture in the specified window of the current thread. Once a window captures the mouse, all mouse inputs are directed at the window, regardless of whether the cursor is within the window boundary. Only one window can capture the mouse at a time. If the mouse cursor is in the window created by another thread, the system only points the mouse input to the specified window when the mouse key is pressed.
ReleaseCapture, on the contrary, functions are used to release Mouse capture.
Why is this ReleaseCapture function used? The reason is that you need to release the Mouse capture when moving the Form title. Otherwise, you cannot move the Form title.
SendMessage Function: This function is used to send Windows messages to a form,
In this article, this function is used to simulate sending Windows messages to non-form customer regions (such as the form title, maximize, minimize, and close button areas, enables a specific area to receive the message of dragging the Form title.
The definitions of the two in C # are as follows:
[DllImport("user32.dll")]static extern void ReleaseCapture();
[DllImport("user32.dll")]static extern void SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);
For more information about hwnd, msg, wParam, and lParam, you can use Baidu.
2. Construct an application that simulates the title of a mobile form. Here we use a label (label1 on the left) to move the mouse over the control and drag it to move the form,
The other label (label2 on the right) is used to close the form, as shown in:
The following code is written in the MouseDown event of label1:
private void label1_MouseDown(object sender, MouseEventArgs e){ ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);}
WM_NCLBUTTONDOWN (0x00A1) is used to send left-click messages to non-clients,
HT_CAPTION (0x0002), which is the region to be applied. This is the form title.
Write the following code in the MouseEnter, MouserLeave, and Click events of label2:
Private void label2_MouseEnter (object sender, EventArgs e) {label2.BackColor = Color. fromArgb (255,192,191); toolTip1.SetToolTip (label2, "close");} private void label2_MouseLeave (object sender, EventArgs e) {label2.BackColor = Color. silver; toolTip1.SetToolTip (label2, "");} private void label2_Click (object sender, EventArgs e) {this. close ();}
You must enter the "close" button prompt for the "close" button. Therefore, you must use the ToolTip control.
3. The constructed program interface is as follows:
Isn't it easy? This article only needs to master the ReleaseCapture and SendMessage functions.
4. source code and EXE are as follows:
Source code:
https://pan.baidu.com/s/1bo3SJan
EXE:
https://pan.baidu.com/s/1gfITSSN