如何使鼠标移动工作没有延迟? [英] How to make mouse movement work with no delay?

查看:242
本文介绍了如何使鼠标移动工作没有延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个程序,让我点击两个同心圆的中心,通过鼠标移动,改变它的位置,我可以做同样的半径。
事情是,鼠标移动后是一个延迟响应从圆圈绘制使半径跟随鼠标,而不是在运动过程中完全在相同的位置。


$ b $你会知道如何使它这样工作吗?



代码的一小部分,用于处理鼠标点击和移动:

  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 =真正;
centerMove = false;
maximumRadiusCircleMove = false;
}
else if((distanceToCenter>(maximumRadius - 1.0f))&&(distanceToCenter<(maximumRadius + 1.0f)))
{
maximumRadiusCircleMove =真正;
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();
}


解决方案

对于这个问题!



它的方式比预期简单,你需要做的是在创建渲染目标时添加一个标志,这样mousemove会更快地响应:
标记为:D2D1_PRESENT_OPTIONS_IMMEDIATELY。

  //创建Direct2d渲染目标。 
hr = m_pD2DFactory-> CreateHwndRenderTarget(
D2D1 :: RenderTargetProperties(),
D2D1 :: HwndRenderTargetProperties(m_hwnd,size,D2D1_PRESENT_OPTIONS_IMMEDIATELY),
& m_pRenderTarget
);


I'm making a program that let me click on the center of two concentric circles and, by mouse move, change it's position and i can do the same with it's radii. The thing is that the mouse movement is followed by a delay response from the circles drawing making the radius follow the mouse instead of being exactly in the same position during the movement.

Would you guys know how to make it work like that? pin point following by the drawing.

a bit of the code that treats the mouse clicking and movements:

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(); 
}

解决方案

i've found a solution to that problem!

It's way simple than expected, all you got to do is add a flag while creating the render target, that way the mousemove will respond way faster: the flag is: 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
            );

这篇关于如何使鼠标移动工作没有延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆