MFC重绘非客户区

1.去除窗口边框和标题栏

OnInitDialog函数,去除窗口边框和标题栏

DWORD dwStyle = GetStyle();
DWORD dwNewStyle = WS_OVERLAPPED | WS_VISIBLE| WS_SYSMENU |WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
dwNewStyle&=dwStyle;
SetWindowLong(m_hWnd,GWL_STYLE,dwNewStyle);
DWORD dwExStyle = GetExStyle();
DWORD dwNewExStyle = WS_EX_LEFT |WS_EX_LTRREADING |WS_EX_RIGHTSCROLLBAR;
dwNewExStyle&=dwExStyle;
SetWindowLong(m_hWnd,GWL_EXSTYLE,dwNewExStyle);


2.解决窗口拖动

bool                        m_bIsZoomed;        //是否最大化

解决窗口不可以拖动,添加WM_LBUTTONDOWN消息

void CDialogEx::OnLButtonDown(UINT nFlags, CPoint point)
{
    //模拟标题
    if (!m_bIsZoomed)
    {
        //模拟标题
        PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
   
        return;
    }
   
    CDialog::OnLButtonDown(nFlags, point);
}

3.拉伸窗口

添加WM_NCHITTESTWM_NCLBUTTONDOWN

我们先添加一个变量,是否可以拉伸,默认初始化为true即可,毕竟窗口拉伸是窗口基本的属性之一

bool                        m_bExtrude;         //是否可以拉伸
LRESULT CDialogEx::OnNcHitTest(CPoint point)
{
    if( m_bExtrude )
    {
        CRect rcWindow;
        GetWindowRect(&rcWindow);
   
        if ((point.x <= rcWindow.left+BORDERWIDTH) && (point.y>BORDERWIDTH) && (point.y<rcWindow.bottom-BORDERWIDTH*2) )
            return HTLEFT;
        else if ((point.x >= rcWindow.right-BORDERWIDTH) && (point.y>BORDERWIDTH) && (point.y<rcWindow.bottom-BORDERWIDTH*2) )
            return HTRIGHT;
        else if ((point.y <= rcWindow.top+BORDERWIDTH) && (point.x>BORDERWIDTH) && (point.x<rcWindow.right-BORDERWIDTH*2))
            return HTTOP;
        else if ((point.y >= rcWindow.bottom-BORDERWIDTH) && (point.x>BORDERWIDTH) && (point.x<rcWindow.right-BORDERWIDTH*2))
            return HTBOTTOM;
        else if ((point.x <= rcWindow.left+BORDERWIDTH*2) && (point.y <= rcWindow.top+BORDERWIDTH*2))
            return HTTOPLEFT;
        else if ((point.x >= rcWindow.right-BORDERWIDTH*2) && (point.y <= rcWindow.top+BORDERWIDTH*2))
            return HTTOPRIGHT;
        else if ((point.x <= rcWindow.left+BORDERWIDTH*2) && (point.y >= rcWindow.bottom-BORDERWIDTH*2))
            return HTBOTTOMLEFT;
        else if ((point.x >= rcWindow.right-BORDERWIDTH*2) && (point.y >= rcWindow.bottom-BORDERWIDTH*2))
            return HTBOTTOMRIGHT;
        else
            return CWnd::OnNcHitTest(point);
   
        return 0;
    }
    else
        return CDialog::OnNcHitTest(point);
}
   
void CDialogEx::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
    if( m_bExtrude )
    {
        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));
    }
   
    CDialog::OnNcLButtonDown(nHitTest, point);
}

 

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页