How to scale and hold a borderless form in winform

Source: Internet
Author: User

A borderless form is often used in winform because you wantProgramThe interface is completely customized, and there have been a lot of related information on the Internet

Today, I sorted out the lunch time and thought it was good. I posted it to check it out.

In order to improve the efficiency of drag-and-drop operations and reduce system overhead, the software generally supports displaying only the dotted lines around the form, which is also the default setting of windows.

First, let's take a look at the form support of other software. First, let's talk about qq2009. If the main form of qq2009 is quickly supported on the screen when it is being held, you will find it when it stops, it is very likely that the position of the mouse in the form is not the position of the press. let's talk about cool music box. when you move the mouse over the frame of the form, the shape of the mouse changes. in addition, even if the system is set to show the content of the form when it is set to torch, the content will not be displayed during operation and scaling, and it will still be a dotted line. this dotted line may be messy in some situations. For example, when playing an animated movie, the screen DC is used to draw the line directly, and the interface of other forms is damaged after being placed, for other re-painting operations, he may not display the following several types of support methods.

Let's talk about the two common ones first, and then add additional instructions.

1. Move and zoom together with the mousemove, mousedown, and mouseup events,CodeHigh performance and low efficiency, which can be written by beginners.

2. It is to use an existing system method. Generally, it is to overwrite the window process and use sendmessage to cheat the system.

A small example of rewriting a callback function

Protected override void wndproc (ref message m)
{
Switch (M. msg)
{
Case wm_nclbuttondblclk: // wm_nclbuttondblclk = 163 <0xa3> intercept the message by clicking the left-click button of a non-customer area to determine whether to maximize the display of the form.
If (this. maximizebox)
{
Base. wndproc (ref m); // The advantage of this method is that you do not have to handle the Mouse shape.
This. invalidate ();
}
Return;
Case wm_nchittest: // wm_nchittest = 132 <0x84>
Base. wndproc (ref m); // if this line of code is removed, the form will lose events such as mousemove ..
Point lpint = new point (INT) M. lparam); // you can get the mouse coordinates, so that you can decide how to handle the message, whether to move the form, scale, and to which direction the message is scaled.

M. Result = (intptr) 0x2; // support htcaption = 2 <0x2>

// Of course, you can support or change the size.

// Htleft = 10 <0xa> left border

// Httop = 12 <0xc> upper border

// Httopleft = 13 <0xd>

// Httopright = 14 <0xe>
// Htright = 11 <0xb>

// Htbottom = 15 <0xf>

// Htbottomleft = 16 <0x10>

// Htbottomright = 17 <0x11>

// Htborder = 18 <0x12>

// Htminbutton = 8 <0x8> minimization button

// Htmaxbutton = 9 <0x9> maximize button

// Htclose = 20 <0x14> close button

Return;
Default:
Base. wndproc (ref m );
Return;
}

}

In the example of sending a message, you can also add the method for sending the message in the Response Message above,

You can also send messages in events such as the form mousedown .. and other controls.

Remember to release the mouse first and use the API function releasecapture (); or the capture attribute of the control or form. Capture = false;

I will not mention the Declaration of API functions. There are three APIs used here. setwindowlong can be used but is used as an example.

[Dllimport ("user32.dll", charset = charset. Auto)]
Public static extern int sendmessage (intptr hwnd, int MSG, int wparam, int lparam );
[Dllimport ("user32.dll", charset = charset. Auto)]
Public static extern bool releasecapture ();
[Dllimport ("user32.dll", charset = charset. Auto)]
Public static extern int setwindowlong (intptr hwnd, int index, long value );

// Support

Sendmessage (this. Handle,/* wm_syscommand */274,/* Move SC _move = */61456 +/* htcaption = 2 */2, 0 );

// Scale the Left Border

Sendmessage (this. handle,/* wm_syscommand */274,/* Mobile Information SC _size = */61440 +/* wmsz_left = 1 <0x1> can be replaced with other parameters */1, 0 );

// Wmsz_left = 1 <0x1>

// Wmsz_right = 2 on the right <0x2>

// Above wmsz_top = 3 <0x3>

// Wmsz_topleft = 4 <0x4>

// Wmsz_topright = 5 <0x5>

// Wmsz_bottom = 6 <0x6>

// Wmsz_bottomleft = 7 <0x7>

// Wmsz_bottomright = 8 <0x8>

Here we can complete the operation and scaling of the two methods, but it is still different from that of the border, there is a border during the operation and scaling of the dotted line can be very thick, but now it is very fine.

In this case, you can use the setwindowlong mentioned above,

Setwindowlong (this. Handle, gwl_style, ws_sysmenu | ws_sizebox | ws_minimizebox | ws_maximizebox | ws_clipchildren | ws_clipsiblings );

This is not just a rough border, and the system menu is available, but it is a little bad. If you are interested, you can study it yourself.

I can use another method to write it in the form of mousedown.

Private void form=mousedown (Object sender, mouseeventargs E)
{

This. Capture = false;
Form FF = new form ();
Ff. startposition = formstartposition. Manual;
Ff. size = This. size;
Ff. Location = This. location;
// Ff. Show (); do not display the form

Sendmessage (FF. Handle, 274,614 40 + 1, 0); // send the mobile message, or you can send other messages, such as scaling messages.

This. size = ff. size;
This. Location = ff. location;

Ff. Dispose ();

}

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.