简体   繁体   English

C# 按住鼠标事件

[英]C# Hold down mouse event

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.我有一个 mousemove 事件,它采用 cursor 的 position 并将其输出到两个标签(X 和 Y),该值随着我 hover 左右而动态变化。 I have a mousedown event that when clicked, the same values are outputted to a textbox.我有一个 mousedown 事件,单击该事件时,相同的值会输出到文本框。 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.如何组合 mousedown 和 mousemove 事件,以便当我 hover 并按住鼠标按钮时,文本框值会随着我的移动而动态变化。

You can interrogate the mouse buttons in your Move event handler, ie :您可以在 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);
    }
}

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.aspx for an example有关示例,请参见http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousebuttons.aspx

Use利用

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

        }
 }

like this and in second if you will have a condition when your mosue moved and mouse Left button is down.像这样,第二个if你的鼠标移动并且鼠标左键按下时你会有一个条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM