2011-03-18 29 views
1

我在Visual C++ 2010中使用了wxWidgets。
我的目標之一是能夠移動我用窗口的任何部分(客戶端或其他)創建的框架。爲此,我在過去使用過WM_NCHITTEST來欺騙Windows,認爲我的窗口的每個部分都是標題欄。
wxWidgets應該怎麼做?wxWidgets和WM_NCHITTEST

回答

2

廣泛的研究,由於對應答部門活動後,我發現有點接受(雖然不便攜式)解決方案:

WXLRESULT [your-wxWindow-inheriting-objectname-here]::MSWWindowProc(WXUINT message,WXWPARAM wParam,WXLPARAM 
lParam) 
{ 
    if(message==WM_NCHITTEST) { return HTCAPTION; } 

    return wxFrame::MSWWindowProc(message,wParam,lParam); 
} 

這可以用於任何WINAPI消息。

0

另一便攜式解決方案也許是這樣的:

//assume your frame named wxUITestFrame 
//headers 
class wxUITestFrame : public wxFrame 
{ 
    DECLARE_EVENT_TABLE() 

protected: 
    void OnMouseMove(wxMouseEvent& event); 
    void OnLeftMouseDown(wxMouseEvent& event); 
    void OnLeftMouseUp(wxMouseEvent& event); 
    void OnMouseLeave(wxMouseEvent& event); 

private: 
    bool  m_isTitleClicked; 
    wxPoint  m_mousePosition; //mouse position when title clicked 
}; 


//cpp 
BEGIN_EVENT_TABLE(wxUITestFrame, wxFrame) 
    EVT_MOTION(wxUITestFrame::OnMouseMove) 
    EVT_LEFT_DOWN(wxUITestFrame::OnLeftMouseDown) 
    EVT_LEFT_UP(wxUITestFrame::OnLeftMouseUp) 
    EVT_LEAVE_WINDOW(wxUITestFrame::OnMouseLeave) 
END_EVENT_TABLE() 


void wxUITestFrame::OnMouseMove(wxMouseEvent& event) 
{ 
    if (event.Dragging()) 
    { 
     if (m_isTitleClicked) 
     { 
      int x, y; 
      GetPosition(&x, &y); //old window position 

      int mx, my; 
      event.GetPosition(&mx, &my); //new mouse position 

      int dx, dy; //changed mouse position 
      dx = mx - m_mousePosition.x; 
      dy = my - m_mousePosition.y; 

      x += dx; 
      y += dy; 

      Move(x, y); //move window to new position 
     } 
    } 
} 

void wxUITestFrame::OnLeftMouseDown(wxMouseEvent& event) 
{ 
    if (event.GetY() <= 40) //40 is the height you want to set for title bar 
    { 
     m_isTitleClicked = true; 
     m_mousePosition.x = event.GetX(); 
     m_mousePosition.y = event.GetY(); 
    } 
} 

void wxUITestFrame::OnLeftMouseUp(wxMouseEvent& event) 
{ 
    if (m_isTitleClicked) 
    { 
     m_isTitleClicked = false; 
    } 
} 

void wxUITestFrame::OnMouseLeave(wxMouseEvent& event) 
{ 
    //if mouse dragging too fase, we will not get mouse move event 
    //instead of mouse leave event here. 
    if (m_isTitleClicked) 
    { 
     int x, y; 
     GetPosition(&x, &y); 

     int mx, my; 
     event.GetPosition(&mx, &my); 

     int dx, dy; 
     dx = mx - m_mousePosition.x; 
     dy = my - m_mousePosition.y; 

     x += dx; 
     y += dy; 

     Move(x, y); 
    } 
} 

事實上,由約翰·洛克在一樓提到的解決方案是more建議在wxMSW,以及像Linux系統,我們可以模擬ALT按鈕按下信息時,標題點擊。