我正在制作一个程序,让我点击两个同心圆的中心,并通过鼠标移动,改变它的位置,我可以用它的半径做同样的事情。 问题是鼠标移动之后是圆形绘制的延迟响应,使半径跟随鼠标而不是在移动过程中完全处于相同位置。
你们知道怎么让它像那样工作吗?图纸后面的针脚点。
一些处理鼠标点击和移动的代码:
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();
}
答案 0 :(得分:2)
根据MSDN(http://msdn.microsoft.com/en-us/library/dd145002%28v=vs.85%29.aspx)InvalidateRect不会导致窗口重新绘制,直到下一个WM_PAINT和“系统发送WM_PAINT消息到窗口,只要它的更新区域不为空且没有其他该窗口的应用程序队列中的消息。“所以不是立竿见影的。
我在MSDN上找到了一个可能的解决方案Drawing Without the WM_PAINT Message
答案 1 :(得分:1)
我找到了解决这个问题的方法!
这比预期的简单,你要做的就是在创建渲染目标时添加一个标志,这样鼠标移动的响应速度会更快: 标志是: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
);