本着对鼠标移动即时响应在画什么? [英] Line drawing with instant response to mouse movement?

查看:142
本文介绍了本着对鼠标移动即时响应在画什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何编写代码,使鼠标的移动和画线之间的响应时间是即时的。每次我这样做总是有2我在窗口形式编码本,现在我编码它在WPF之间的滞后。



问题definitly在于代码,是不是我的电脑。



Basicaly它的工作原理是这样的。
单击任意位置,点1被创建。
移动鼠标和点升级以及线从点1绘制点2这对鼠标的移动而改变。



下面是我的极端简单的代码来做到这一点..
它可能看起来瞬间,但如果你将窗口最大化,使线长和快速移动鼠标,就可以更容易地注意到它。



此外,还有将在未来根据本图像层这肯定会导致它落后,甚至更多......但现在我只是想优化这个。



当我用其他的这种方案从5+年前的直线运动竟是瞬间。这就是为什么我很困惑,为什么用这个新编码,其laggy ..

 点mLoc; 
线MYLINE =新线();


公共主窗口()
{
的InitializeComponent();
SnapsToDevicePixels = FALSE;

myLine.Stroke = System.Windows.Media.Brushes.White;
myLine.StrokeThickness = 1;

canvas1.Children.Add(MYLINE);
}

私人无效onMMove(对象发件人,MouseEventArgs E)
{
mLoc = Mouse.GetPosition(canvas1);

myLine.X2 = mLoc.X;
myLine.Y2 = mLoc.Y;


}

私人无效onMLClick(对象发件人,MouseButtonEventArgs E)
{
mLoc = Mouse.GetPosition(canvas1);

myLine.X1 = mLoc.X;
myLine.Y1 = mLoc.Y;

}


解决方案

WinAPI的呢不发送WM_MOUSEMOVE足够快,以检测鼠标的每一个动作。有一个很好的文章,说明如何检测鼠标移动非常精确(的 http://blogs.msdn.com/b/oldnewthing/archive/2012/03/14/10282406.aspx


I am trying to figure out how to write the code so the response time between the mouse movement and line drawing is instant. Everytime I do this there is always lag between the 2. I have coded this in the windows forms and now I am coding it in WPF.

The problem definitly lies in the code and is not my computer.

Basicaly it works like this. Click anywhere and point1 is created. Move the mouse and point2 is updated and a line is drawn from Point1 to Point2 which changes on mouse movement.

below is my extremly simple code to do this.. It may seem instant but if you maximize the window and make the line long and move the mouse quickly, you can notice it more easily..

Also, there will be an image layer under this in the future which for sure will cause it to lag even more.. But right now I just want to optimize this.

When I used other such programs from 5+ years ago, the line movement was actually instant. Thats why I am confused as to why with this newer coding, its laggy..

    Point mLoc;
    Line myLine = new Line();


    public MainWindow()
    {
        InitializeComponent();
        SnapsToDevicePixels = false;

        myLine.Stroke = System.Windows.Media.Brushes.White;
        myLine.StrokeThickness = 1;

        canvas1.Children.Add(myLine);
    }

    private void onMMove(object sender, MouseEventArgs e)
    {
        mLoc = Mouse.GetPosition(canvas1);

        myLine.X2 = mLoc.X;
        myLine.Y2 = mLoc.Y;


    }

    private void onMLClick(object sender, MouseButtonEventArgs e)
    {
        mLoc = Mouse.GetPosition(canvas1);

        myLine.X1 = mLoc.X;
        myLine.Y1 = mLoc.Y;

    }

解决方案

WinAPI does not send WM_MOUSEMOVE quick enough to detect every movement of mouse. There is an very good article that shows how to detect mouse movement very precise (http://blogs.msdn.com/b/oldnewthing/archive/2012/03/14/10282406.aspx)

这篇关于本着对鼠标移动即时响应在画什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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