2012-01-30 81 views
3

我正在製作一個程序,讓我點擊兩個同心圓的中心,然後通過鼠標移動,改變它的位置,我可以用它的半徑做同樣的操作。 問題在於鼠標移動之後是來自圓圈的延遲響應,使得半徑跟隨鼠標,而不是在移動過程中完全處於相同位置。如何讓鼠標移動毫不拖延地工作?

你們會知道如何使它像那樣工作嗎?針點跟在繪圖之後。

一個位,用於對待鼠標點擊和動作的代碼:

void DemoApp::OnLButtonDown(FLOAT pixelX, FLOAT pixelY) 
{ 
    SetCapture(m_hwnd); 

    mouseRegion = DPIScale::PixelsToDips(pixelX, pixelY); 
    FLOAT xDifference = centerCircles.x - mouseRegion.x; 
    FLOAT yDifference = centerCircles.y - mouseRegion.y; 
    FLOAT distanceToCenter = sqrtf(xDifference*xDifference + yDifference*yDifference); 

    if(distanceToCenter < 10.0f) 
    { 
     centerMove = true; 
     minimumRadiusCircleMove = false; 
     maximumRadiusCircleMove = false; 
    } 
    else if((distanceToCenter > (minimumRadius - 1.0f)) && (distanceToCenter < (minimumRadius + 1.0f))) 
    { 
     minimumRadiusCircleMove = true; 
     centerMove = false; 
     maximumRadiusCircleMove = false; 
    } 
    else if((distanceToCenter > (maximumRadius - 1.0f)) && (distanceToCenter < (maximumRadius + 1.0f))) 
    { 
     maximumRadiusCircleMove = true; 
     centerMove = false; 
     minimumRadiusCircleMove = false; 
    } 
    else 
    { 
     centerMove = false; 
     minimumRadiusCircleMove = false; 
     maximumRadiusCircleMove = false; 
    } 


    InvalidateRect(m_hwnd, NULL, FALSE); 
} 

void DemoApp::OnMouseMove(int pixelX, int pixelY, DWORD flags) 
{ 
    if (flags & MK_LBUTTON) 
    { 
     if(centerMove) 
     { 
      centerCircles = DPIScale::PixelsToDips(pixelX, pixelY); 

      FLOAT distanceLeftToCenterCircles = abs(centerCircles.x - bitmapTopLeft); 
      FLOAT distanceTopToCenterCircles = abs(centerCircles.y - bitmapTopRight); 

      percentageFromLeft = distanceLeftToCenterCircles/displaySizeWidth; 
      percentageFromTop = distanceTopToCenterCircles/displaySizeHeight; 

     } 
     else if(minimumRadiusCircleMove) 
     { 
      radiusSelection = DPIScale::PixelsToDips(pixelX, pixelY); 
      FLOAT xDifference = centerCircles.x - radiusSelection.x; 
      FLOAT yDifference = centerCircles.y - radiusSelection.y; 
      minimumRadius = sqrtf(xDifference*xDifference + yDifference*yDifference); 

      minimumRadiusPercentage = minimumRadius/(displaySizeWidth/2); 
     } 
     else if(maximumRadiusCircleMove) 
     { 
      radiusSelection = DPIScale::PixelsToDips(pixelX, pixelY); 
      FLOAT xDifference = centerCircles.x - radiusSelection.x; 
      FLOAT yDifference = centerCircles.y - radiusSelection.y; 
      maximumRadius = sqrtf(xDifference*xDifference + yDifference*yDifference); 

      maximumRadiusPercentage = maximumRadius/(displaySizeWidth/2); 
     } 

     InvalidateRect(m_hwnd, NULL, FALSE); 
    } 
} 

void DemoApp::OnLButtonUp() 
{ 
    ReleaseCapture(); 
} 

回答

1

我已經找到了解決這一問題!

它比預期的簡單,你所要做的就是在創建渲染目標時添加一個標誌,這樣mousemove會以更快的方式響應: 標誌是:D2D1_PRESENT_OPTIONS_IMMEDIATELY。

// Create Direct2d render target. 
     hr = m_pD2DFactory->CreateHwndRenderTarget(
      D2D1::RenderTargetProperties(), 
      D2D1::HwndRenderTargetProperties(m_hwnd, size, D2D1_PRESENT_OPTIONS_IMMEDIATELY), 
      &m_pRenderTarget 
      ); 
2

根據MSDN(http://msdn.microsoft.com/en-us/library/dd145002%28v=vs.85%29.aspx)InvalidateRect不引起重新繪製窗口,直到下一個WM_PAINT和「系統發送一個只要WM_PAINT消息的更新區域不是空的,並且該窗口的應用程序隊列中沒有其他消息,WM_PAINT消息「。所以它不是直接的。

我發現了一個可能的解決方案在MSDN這裏Drawing Without the WM_PAINT Message

+0

這不僅僅是WM_PAINT,WM_MOUSEMOVE只在消息隊列沒有更好的事情時纔會被髮送。雙重打擊。 – 2012-01-30 16:18:11

+0

我調用了渲染函數來重畫屏幕,所以它不需要從mousemove函數中調用WM_PAINT,但這沒有什麼區別。由於我在direct2d上,而不是在GDI上,我不能使用HDC。還有什麼想法?感謝你的努力。 – Leonardo 2012-01-30 16:33:35

相關問題