How does a borderless form implement form size changes by dragging the edges of a form with the mouse? This can be done in a few steps:
1. Implement WM_NCHITTEST message, achieve four border simulation
2. Implement Wm_nclbuttondown, send the message of the form drag change
Specific elaboration as follows:
1. Implement WM_NCHITTEST message, achieve four border simulation, the reference code is as follows:
LRESULT cnoborderwndchangesizedlg::onnchittest (CPoint point) {//TODO: Add the message Handler code here and/or call the default value CRect rect; GetWindowRect (&rect); CRect rect1 = rect; Rect1. Deflaterect (10, 10,-10,-10); Rect1. NormalizeRect (); if (point.x <= rect.left+3) return htleft; else if (point.x >= rect.right-3) return htright; else if (point.y <= rect.top+3) return httop; else if (point.y >= rect.bottom-3) return htbottom; else if (point.x <= rect.left+10 && point.y <= rect.top+10) return httopleft; else if (point.x >= rect.right-10 && point.y <= rect.top+10) return httopright; else if (point.x <= rect.left+10 && point.y >= rect.bottom-10) return htbottomleft; else if (point.x >= rect.right-10 && point.y >= rect.bottom-10) return htbottomright; else if (!rect. Isrectempty ()) {LRESULT Uret = cwnd::onnchittest (point); Uret = (Uret = = htclient)? Htcaption:uret; return uRet; } else {return cwnd::onnchittest (point); } return 0; }
2. Implement Wm_nclbuttondown, send the message of the form drag change
void Cnoborderwndchangesizedlg::onnclbuttondown (UINT nhittest, CPoint point) {//TODO: Add Message Handler code here and/or call default value if (nhittest = = httop) {SendMessage (Wm_syscommand, Sc_size | Wmsz_top, Makelparam (Point.x, Point.y)); } else if (nhittest = = Htbottom) SendMessage (Wm_syscommand, Sc_size | Wmsz_bottom, Makelparam (Point.x, Point.y)); else if (nhittest = = Htleft) SendMessage (Wm_syscommand, Sc_size | Wmsz_left, Makelparam (Point.x, Point.y)); else if (nhittest = = htright) SendMessage (Wm_syscommand, Sc_size | Wmsz_right, Makelparam (Point.x, Point.y)); else if (nhittest = = Httopleft) SendMessage (Wm_syscommand, Sc_size | Wmsz_topleft, Makelparam (Point.x, Point.y)); else if (nhittest = = httopright) SendMessage (Wm_syscommand, Sc_size | Wmsz_topright, Makelparam (Point.x, Point.y)); else if (nhittest = = Htbottomleft) SendMessage (Wm_syscommand, Sc_size | Wmsz_bottomleft, MakelPARAM (Point.x, Point.y)); else if (nhittest = = htbottomright) SendMessage (Wm_syscommand, Sc_size | Wmsz_bottomright, Makelparam (Point.x, Point.y)); else if (nhittest==htcaption) SendMessage (Wm_syscommand, Sc_move | 4, Makelparam (Point.x, Point.y)); }
In general, you can implement the requirements using the code described above.
Let's take a look at the extension. When the size of a form changes, there are two forms of representation: one is a dashed box, and one does not. This option relies on the operating system and you can right-click on the Desktop/properties/skins/effects to express both forms by selecting or canceling the "show window contents when dragging". So how to implement in a program without relying on the settings of the current machine (this means not relying on manually selecting the "Show window content when dragging" item), we can do this in the Onnclbuttondown function by adding the following code:
if (nhittest = = httop) { SystemParametersInfo (spi_setdragfullwindows, TRUE, NULL, 0); SendMessage (Wm_syscommand, Sc_size | Wmsz_top, Makelparam (Point.x, Point.y)); SystemParametersInfo (Spi_setdragfullwindows, FALSE, NULL, 0); }
This code means to set "show window contents when dragging" before dragging the top edge action, and restore the original settings when the action ends. Of course, the better thing is that you first get the machine's current options, the function is still systemparametersinfo, and the options change, just look at MSDN.
Implement drag and form size changes for borderless forms