Drain the Form (transfer from MSDN) with C #

xiaoxiao2021-03-06  105

PSS ID Number: Q320687

When you design an application, you may want users to drag forms through the client area, such as: When the form does not have a title bar or created an irregular form, only through the client area to drag the window. Body.

A good example of us happened is Microsoft Windows Media Player. Media Player has a functionality that can be free to replace the appearance according to the user's hobbies (skinned), this time the title bar is hidden, but you can drag the form through the customer zone.

Ok, let's go forward, start our journey.

First, you must understand the message delivery mechanism of Windows. When there is a mouse activity message, the system sends a WM_NCHITTEST message to the form as a basis for the determination message. If you click on the title bar, the message value received is htcaption, the same, if the received message is htclient, explaining the user clicks the customer area, that is, the mouse message is in the client area.

When the WndProc method of the form is overloaded, the WM_NCHITTEST message can be intercepted, and the message can be changed. When it is judged that the mouse event occurs when the mouse event occurs, the message is changed, and the HTCAPTION is sent to the form, so that the message received by the form is HTCAPTION, dragging the form in the customer area to drag the form as by the title bar.

Note: When you override WndProc and overwrite the mouse event, the entire form of mouse events will change.

Example: 1. Create a C # engineering file, the default form, FORM1.

2. Click Code on the View panel.

3. Paste the following code into the Form1 class

Private const INT WM_NCHITTEST = 0x84; private const Int htclient = 0x1; private const Int htcaption = 0x2;

4. Change the mouse message in Form1

Protected Override Void WndProc (Ref Message M) {Switch (M.MSG) {CASE WM_NCHITTEST: BASE.WndProc (Ref M); if ((int) m.Result == htclient) m.Result = (intptr) htcaption; return; Return Break;} Base.WndProc (Ref M);

5. Save and run the project.

6. Try it, click anywhere in the form, can you drag the form?

Oh, there are still many tips on MSDN, treat MSDN seriously, it is our best reference.

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

New Post(0)