windows C#按住鼠标事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6623775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:20:59  来源:igfitidea点击:

C# Hold down mouse event

c#windowsmouseevent

提问by Hyman

I have a mousemove event that takes the position of the cursor and outputs it to two labels (X and Y), the value dynamically changes as I hover around. I have a mousedown event that when clicked, the same values are outputted to a textbox. How can I combine the mousedown and mousemove events so that when I hover AND hold down the mouse button, the textbox value dynamically changes as I move.

我有一个 mousemove 事件,它获取光标的位置并将其输出到两个标签(X 和 Y),当我悬停时,该值会动态变化。我有一个 mousedown 事件,当单击时,相同的值会输出到文本框。如何组合 mousedown 和 mousemove 事件,以便当我悬停并按住鼠标按钮时,文本框值会随着我的移动而动态变化。

回答by Lazarus

You can interrogate the mouse buttons in your Move event handler, i.e. :

您可以在 Move 事件处理程序中询问鼠标按钮,即:

void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left) {
        String tipText = String.Format("({0}, {1})", e.X, e.Y);
        trackTip.Show(tipText, this, e.Location);
    }
}

回答by Bob Vale

Track the mouse down and mouse up events to set a variable determining whether or not the mouse button is pressed (ie set in down unset in mouse up) then just check this variable in mouse_move

跟踪鼠标按下和鼠标抬起事件以设置一个变量,确定是否按下鼠标按钮(即设置为向下未设置鼠标向上)然后只需在 mouse_move 中检查此变量

see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousebuttons.aspxfor an example

有关示例,请参见http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousebuttons.aspx

回答by Tigran

Use

 private void OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
 {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {

        }
 }

like this and in second ifyou will have a condition when your mosue moved andmouse Leftbutton is down.

像这样,然后if当您的鼠标移动并且鼠标左键按下时,您将有一个条件。