无边框窗口实现拖垃效果

(1)实现效果需要处理以下这三个消息:

WM_NCHITTEST  WM_SETCURSOR  WM_NCLBUTTONDOWN
WM_NCHITTEST参数:
xPos = LOWORD(lParam);  // horizontal position of cursor (相对于屏幕坐标)
yPos = HIWORD(lParam);  // vertical position of cursor (相对于屏幕坐标)

(2)消息处理函数:

 

UINT CXXXDlg::OnNcHitTest(UINT nHitTest, CPoint point)
{
    CRect rect;
    GetWindowRect(
&rect);
    
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;
    }

    
return HTCLIENT;//指示当前鼠标在客户区,将响应OnLButtonDown消息。
}


BOOL CXXXDlg::OnSetCursor(HWND hWnd, UINT nHitTest, UINT message)
{
    
if(nHitTest == HTCAPTION || nHitTest == HTSYSMENU ||
        nHitTest 
== HTMENU || nHitTest == HTCLIENT)
    
{
        SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
    }

    
else if(nHitTest == HTTOP || nHitTest == HTBOTTOM)
    
{
        SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
    }

    
else if(nHitTest == HTLEFT || nHitTest == HTRIGHT)
    
{
        SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
    }

    
else if(nHitTest == HTTOPLEFT || nHitTest == HTBOTTOMRIGHT)
    
{
        SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
    }

    
else if(nHitTest == HTTOPRIGHT || nHitTest == HTBOTTOMLEFT)
    
{
        SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
    }

    
else
    
{
        SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
    }

    
return TRUE;
}



void  CXXXDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
    
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));
}

(3)消息返回值

HTBORDER In the border of a window that does not have a sizing border(在一个没有边框的窗口边缘上)
HTBOTTOM In the lower horizontal border of a window(在窗口下面的水平边缘上)
HTBOTTOMLEFT In the lower-left corner of a window border(在窗口左下方的角点上)
HTBOTTOMRIGHT In the lower-right corner of a window border(在窗口右下方的角点上)
HTCAPTION In a title bar(在标题栏上)
HTCLIENT In a client area(在客户区域中)
HTERROR On the screen background or on a dividing line between windows (same as HTNOWHERE, except that the DefWindowProc function produces a system beep to

indicate an error)
HTGROWBOX In a size box (same as HTSIZE)
HTHSCROLL In a horizontal scroll bar(在水平滚动条上)
HTLEFT In the left border of a window(在窗口的左边缘上)
HTMENU In a menu(在一个菜单上)
HTNOWHERE On the screen background or on a dividing line between windows(在窗口和屏幕背景的边缘线上)
HTREDUCE In a Minimize button(在最小化按钮上)
HTRIGHT In the right border of a window(在窗口的右边缘上)
HTSIZE In a size box (same as HTGROWBOX)
HTSYSMENU In a System menu or in a Close button in a child window
HTTOP In the upper horizontal border of a window(在窗口上面的水平边缘上)
HTTOPLEFT In the upper-left corner of a window border(在窗口左上方的角点上)
HTTOPRIGHT In the upper right corner of a window border(在窗口右上方的角点上)
HTTRANSPARENT In a window currently covered by another window
HTVSCROLL In the vertical scroll bar(在垂直滚动条上)
HTZOOM In a Maximize button(在最大化按钮上)

你可能感兴趣的:(windows,null,System,button,border,scroll)