实现效果需要处理以下这三个消息:
WM_NCHITTEST WM_SETCURSOR WM_NCLBUTTONDOWN
很多人在问三个函数是做什么用的,下面我大概介绍一下:
OnNcHitTest(UINT nHitTest, CPoint point)是用来测试鼠标的位置,Nc表示非客户区
OnSetCursor(HWND hWnd, UINT nHitTest, UINT message) 是用来设置鼠标状态
OnNcLButtonDown(UINT nHitTest, CPoint point)是用来处理非客户区的鼠标左键点击
有不懂的可以留言交流一下,希望本文章对大家有所帮助
消息处理函数:
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/2b4fc9fac813ba91b48f318f.jpg)
UINT CXXXDlg::OnNcHitTest(UINT nHitTest, CPoint point)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/1908c7deddcdc51b94ee378f.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/75a4c80336079fc7d43f7c8f.jpg)
...{
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/241ce288727dcedba4c2728f.jpg)
CRect rect;
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/6266b3940c803451d21b708f.jpg)
GetWindowRect(&rect);
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/129b491e785947bb1bd5768f.jpg)
if(point.x<=rect.left+3)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/8c7019f58fbcd06abc31098f.jpg)
returnHTLEFT;
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/56a7ad25cd73152f34a80f8f.jpg)
elseif(point.x>=rect.right-3)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/64423af4b797e421ddc4748f.jpg)
returnHTRIGHT;
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/ad8c996e7386189581cb4a8f.jpg)
elseif(point.y<=rect.top+3)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/6b9d65da31897094b6fd488f.jpg)
returnHTTOP;
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/37920ddb04bf1b29d0164e8f.jpg)
elseif(point.y>=rect.bottom-3)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/00a466666454406bab184c8f.jpg)
returnHTBOTTOM;
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/6701f8bfedc3c94519d81f8f.jpg)
elseif(point.x<=rect.left+10&&point.y<=rect.top+10)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/dad8b40a9a9af571b1351d8f.jpg)
returnHTTOPLEFT;
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/af81883e34777ab5828b138f.jpg)
elseif(point.x>=rect.right-10&&point.y<=rect.top+10)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/076c07fa05c9ef51a8d3118f.jpg)
returnHTTOPRIGHT;
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/34d2921e2191b4504134178f.jpg)
elseif(point.x<=rect.left+10&&point.y>=rect.bottom-10)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/1e8ac91fca76de45304e158f.jpg)
returnHTBOTTOMLEFT;
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/f76da30abd0c6c5095ca6b8f.jpg)
elseif(point.x>=rect.right-10&&point.y>=rect.bottom-10)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/8617111f1a889887a786698f.jpg)
returnHTBOTTOMRIGHT;
else
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/8617111f1a889887a786698f.jpg)
return CDialog::OnNcHitTest(point);
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/c96753095c7f39803bc7638f.jpg)
}
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/7f6444cfac85d672f8dc618f.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/8d9eab3d619e984fbaa1678f.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/4e85e50021e3845e728b658f.jpg)
BOOL CXXXDlg::OnSetCursor(HWND hWnd, UINT nHitTest, UINT message)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/c4d27310265081b3c3ce798f.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/0f4bfcfc628c8ab2fc037f8f.jpg)
...{
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/7597f7fd5f41db0ad6887d8f.jpg)
if(nHitTest==HTCAPTION||nHitTest==HTSYSMENU||
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/4a5aa64573ca9d708794738f.jpg)
nHitTest==HTMENU||nHitTest==HTCLIENT)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/60d1e03f20d6ed8d7d1e718f.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/31cd90c2d45c9b540ef4778f.jpg)
...{
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/cb47e61ba5b6529aad6e758f.jpg)
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/b8ad2fd5002c948b51da4b8f.jpg)
}
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/1b37e9c4fe98c88439db498f.jpg)
elseif(nHitTest==HTTOP||nHitTest==HTBOTTOM)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/e783b5cb8899ffb252664f8f.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/8f8282fde124995909244d8f.jpg)
...{
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/671a994b15513abf82025c8f.jpg)
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/2172e0078cb8af827b89478f.jpg)
}
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/83a3d2cdc0cbd71e0fb3458f.jpg)
elseif(nHitTest==HTLEFT||nHitTest==HTRIGHT)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/cdd0aa51b2f1466b42a75b8f.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/b9ea3b24fde5907cc895598f.jpg)
...{
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/f4feed3309d7980cad4b5f8f.jpg)
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/7ecce5436e09985c73f05d8f.jpg)
}
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/1b12e513b6b2cb4d5aaf538f.jpg)
elseif(nHitTest==HTTOPLEFT||nHitTest==HTBOTTOMRIGHT)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/c5a9b6029deded404bfb518f.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/ecf6900f82b9b2a2ab64578f.jpg)
...{
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/fda2cfed60264a0078f0558f.jpg)
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/1d3d374fb5b2be79afc3ab88.jpg)
}
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/cea9c3366081307e0a55a988.jpg)
elseif(nHitTest==HTTOPRIGHT||nHitTest==HTBOTTOMLEFT)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/199a4d31db17fa56ebc4af88.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/bc0c87193886fe0a43a9ad88.jpg)
...{
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/5d9d834596ebcb69cffca388.jpg)
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/f5f0b62618be57588a82a188.jpg)
}
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/79a52a1753c0f04e4a90a788.jpg)
else
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/3cdb8d0191d2bf4c738da588.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/fcc9c203aecfe0383912bb88.jpg)
...{
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/c5d49d77e650fa54b051b988.jpg)
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/8f4b871b5113b1568618bf88.jpg)
}
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/0608cc19655a7f35dab4bd88.jpg)
}
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/3041027a3ff603a22e73b388.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/982abfd45a5ece4ea18bb788.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/17d2b3d6ca3dd76007088b88.jpg)
voidCXXXDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/9126aa2fe84a61771f308988.jpg)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/b1511c38ee72a06bb8998f88.jpg)
...{
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/a969dd244bdbe04e4c088d88.jpg)
if(nHitTest==HTTOP)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/fa515a06ead7433402088188.jpg)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_TOP, MAKELPARAM(point.x, point.y));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/abcc3e7bfb4a32bb0ad18788.jpg)
elseif(nHitTest==HTBOTTOM)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/b4514ff4f1932b96f2d38588.jpg)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_BOTTOM, MAKELPARAM(point.x, point.y));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/bc8856d90f911e6311df9b88.jpg)
elseif(nHitTest==HTLEFT)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/448a632cee9de0a38a139988.jpg)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_LEFT, MAKELPARAM(point.x, point.y));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/a7869dec8b502a6862d09f88.jpg)
elseif(nHitTest==HTRIGHT)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/27d694ec0fb9187226979188.jpg)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_RIGHT, MAKELPARAM(point.x, point.y));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/cf683e55615ba28bb745ae88.jpg)
elseif(nHitTest==HTTOPLEFT)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/011c77a9546486b31e17a288.jpg)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_TOPLEFT, MAKELPARAM(point.x, point.y));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/90ce6dee362e1cb4b2fb9588.jpg)
elseif(nHitTest==HTTOPRIGHT)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/04a277166261060520a4e988.jpg)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_TOPRIGHT, MAKELPARAM(point.x, point.y));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/db7b7b4a31e70f6c09f7ef88.jpg)
elseif(nHitTest==HTBOTTOMLEFT)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/bfaea4528d613b410cf3e388.jpg)
SendMessage( WM_SYSCOMMAND, SC_SIZE|WMSZ_BOTTOMLEFT, MAKELPARAM(point.x, point.y));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/0f587e902a413ac2a977a488.jpg)
elseif(nHitTest==HTBOTTOMRIGHT)
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/2e7b460e1bb09e857bcbe188.jpg)
SendMessage(WM_SYSCOMMAND, SC_SIZE|WMSZ_BOTTOMRIGHT, MAKELPARAM(point.x, point.y));
![](http://hiphotos.baidu.com/%C1%E8%D4%C6%D7%B7%B7%E7/pic/item/baaae3ca62884309f21fe788.jpg)
}