• 无边框对话框拖动改变大小的实现总结


    相同是项目遇到的问题,要求是无边框的对话框要实现鼠标的拖动改变大小。无边框对话框跟有边框的实现肯定不一样喽。

    我上网搜到一种方法是:


    须要处理下面这三个消息:
     
    WM_NCHITTEST  WM_SETCURSOR  WM_NCLBUTTONDOWN 
     
    消息处理函数:
     
    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 0;
    }
     
     
    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)));
        }
    }
     
     
    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));
    }



    上面的做法有点问题,像在推断时, 左右,上下先推断了。那左上,左下,右上,右下就没法做了。
    全部就有了改进的方法:
    UINT CTestDlg::OnNcHitTest( CPoint point )
    {
    CPoint ptCur;
    CRect rect;
    GetCursorPos( &ptCur );
    GetWindowRect( &rect );
    
    if( CRect(rect.left, rect.top, rect.left+3, rect.top+3).PtInRect( ptCur ) )  
    return HTTOPLEFT;
    else if( CRect(rect.right-3, rect.top, rect.right, rect.top+3).PtInRect( ptCur ) )
    return HTTOPRIGHT;
    else if( CRect(rect.left, rect.bottom-3, rect.left+3, rect.bottom).PtInRect( ptCur ) )
    return HTBOTTOMLEFT;
    else if( CRect(rect.right-3, rect.bottom-3, rect.right, rect.bottom).PtInRect( ptCur ) )
    return HTBOTTOMRIGHT;
    else if ( CRect(rect.left, rect.top, rect.left+3, rect.bottom).PtInRect( ptCur ) )
    return HTLEFT;
    else if( CRect(rect.right-3, rect.top, rect.right, rect.bottom).PtInRect( ptCur ) )
    return HTRIGHT;
    else if( CRect(rect.left, rect.top, rect.right-128, rect.top+3).PtInRect( ptCur ) ) // 128 Min,Max,Close
    return HTTOP;
    else if( CRect(rect.left, rect.bottom-3, rect.right, rect.bottom).PtInRect( ptCur ) )
    return HTBOTTOM;
    
        return CDialog::OnNcHitTest(point);
    }
    
    BOOL CTestDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
    {
    CPoint ptCur;
    CRect rect;
    GetCursorPos( &ptCur );
    GetWindowRect( &rect );
    
    if ( rect.Width() >= m_nCxFullScreen-3 && rect.Height() >= m_nCyFullScreen-3 )
    return CDialog::OnSetCursor(pWnd, nHitTest, message);
    
    if( CRect(rect.left, rect.top, rect.left+3, rect.top+3).PtInRect( ptCur ) || CRect(rect.right-3, rect.bottom-3, rect.right, rect.bottom).PtInRect( ptCur ) )
    {
    SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
    return 0;
    }
    else if( CRect(rect.left, rect.bottom-3, rect.left+3, rect.bottom).PtInRect( ptCur ) || CRect(rect.left, rect.bottom-3, rect.left+3, rect.bottom).PtInRect( ptCur ) )
    {
    SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
    return 0;
    }
    else if( CRect(rect.left, rect.top, rect.right-128, rect.top+3).PtInRect( ptCur ) || CRect(rect.left, rect.bottom-3, rect.right, rect.bottom).PtInRect( ptCur ) )
    {
    SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
    return 0;
    }
    else if( CRect(rect.left, rect.top, rect.left+3, rect.bottom).PtInRect( ptCur ) || CRect(rect.right-3, rect.top, rect.right, rect.bottom).PtInRect( ptCur ) )
    {
    SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
    return 0;
    }
    
    return CDialog::OnSetCursor(pWnd, nHitTest, message);
    }
    
    void CTestDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
    {
    switch( nHitTest )
    {
    case HTTOP:
    SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOP, MAKELPARAM(point.x, point.y));
    return;
    case HTBOTTOM:
    SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOM, MAKELPARAM(point.x, point.y));
    return;
    case HTLEFT:
    SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_LEFT, MAKELPARAM(point.x, point.y));
    return;
    case HTRIGHT:
    SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_RIGHT, MAKELPARAM(point.x, point.y));
    return;
    case HTTOPLEFT:
    SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPLEFT, MAKELPARAM(point.x, point.y));
    return;
    case HTTOPRIGHT:
    SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPRIGHT, MAKELPARAM(point.x, point.y));
    return;
    case HTBOTTOMLEFT:
    SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMLEFT, MAKELPARAM(point.x, point.y));
    return;
    case HTBOTTOMRIGHT:
    SendMessage( WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMRIGHT, MAKELPARAM(point.x, point.y));
    return;
    default:
    CDialog::OnNcLButtonDown( nHitTest, point );
    }



  • 相关阅读:
    Java1:Chapter2
    Java1:Chapter1
    Java1:Chapter11
    Java1:Chapter8
    Java1:Chapter6
    Android day 03
    Android day02
    Android day01
    二进制文件的读写
    字符流
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5091590.html
Copyright © 2020-2023  润新知