2011-08-12 25 views
2

我有一個基於對話框的MFC應用程序,它從文本文件讀取高度和半徑的座標,並將其顯示爲圖片控制窗口上的點圖。現在,繪製點後,我需要能夠將點拖放到窗口中的任何特定位置,以便將點座標更改爲新位置。所有這些都應該通過用我的右鍵單擊按鈕來完成。我明白,我應該使用的事件是OnRButtonDown()和OnRButtonUp(),但我無法理解如何在應用程序中包含拖放功能。爲了您的信息,我已經完成了對點的繪圖,我只需要了解拖放功能的實現。在我的圖形中拖放功能 - 基於對話框的MFC

在此先感謝。

回答

2

的阻力&降幾件事情:

  1. 在OnRButtonDown(),你需要確定你拿起來看,RButtonDown標誌設置爲true。
  2. 檢查該標誌,如果爲true,則發佈繪製消息以根據OnMouseMove()中該點的新位置動態繪製繪圖,使其儘可能平滑(不閃爍),不會使所有動作失效並重繪某個地區。
  3. 在OnRButtonUp()中,將該標誌更新爲false。

您可能還需要在OnRButtonDown使用SetCapture/ReleaseCapture()/ OnRButtonUp()爲您拖動和移動鼠標,你的對話框窗口的情況。

+0

非常感謝布蘭登。真的很有幫助。它在我實現你所說的之後起作用。 乾杯。 – Neophile

+1

@ Nerds.Dont.Swear不客氣,我很高興它幫助:-) – BrandonSun

2

您需要繼承CWndCStatic並自己做繪畫。然後當拖動完成時,您需要自己移動繪圖對象。使用設備上下文(CDC,CClientDC)會出現在屏幕上。您需要使用CDC::SetROP2和其他方法繪製圖形對象。

1

我已經想出瞭如何讓這個工作。所以,如果人們想知道如何在他們的程序中實現這一點,可以從這段代碼中獲得一個想法。

代碼:

void CRangemasterGeneratorDlg::OnRButtonDown(UINT nFlags, CPoint point) 
{ 
    // TODO: Add your message handler code here and/or call default 

    GetCursorPos(&point); 

    int mx = point.x; 
    int my = point.y; 

    float cursR, cursH; 

    cursR = (mx - 312)/7.2;// records the current cursor's radius(x) position 
    cursH = (641 - my)/5.3;// records the current cursor's height(y) position 

    CString Hgt,Rds; 
    Hgt.Format("%.3f",cursH);// Rounding off Height values to 3 decimal places 
    Rds.Format("%.3f",cursR);// Rounding off Radius values to 3 decimal places 

    curR = (float)atof(Rds); 
    curH = (float)atof(Hgt); 

    // I had limits on my grid from 0 - 100 on both x and y-axis 
     if(curR < 0 || curR >100 || curH < 0 || curH > 100) 
     return; 

    SetCapture(); 

    SetCursor(::LoadCursor(NULL, IDC_CROSS)); 

    //snap the point, compare the point with your array and save position on 'y' 
    for(int i=0; i < 100; i++) 
    { 
     if(curH < m_Points[i+1].m_height_point && curH >m_Points[i-1].m_height_point) 
     { 
      curH = m_Points[i].m_height_point; 
      curR = m_Points[i].m_radius_point; 
      y = i; 
     } 
    } 

    CDialog::OnRButtonDown(nFlags, point); 
    UpdateData(false); 
    Invalidate(); 
} 

void CRangemasterGeneratorDlg::OnRButtonUp(UINT nFlags, CPoint point) 
{ 
    // TODO: Add your message handler code here and/or call default 
    ReleaseCapture(); 

    GetCursorPos(&point); 

    int mx1 = point.x; 
    int my1 = point.y; 

    float curR1,curH1; 

    curR1 = (mx1 - 312)/7.2;// records the current cursor's radius(x) position 
    curH1 = (641 - my1)/5.3;// records the current cursor's height(y) position 

    m_Points[y].m_radius_point = curR1; 
    m_Points[y].m_height_point = curH1; 

    Invalidate(); 

    CDialog::OnRButtonUp(nFlags, point); 
    UpdateData(false); 
} 

...

我已經跑這個代碼,它出色的作品很好。這段代碼中的變量與我在程序中使用的有關。如果你不明白,請隨時問我。