C# Hold down mouse event

家住魔仙堡 提交于 2019-12-24 04:15:31

问题


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.


回答1:


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

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



回答2:


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

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




回答3:


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.



来源:https://stackoverflow.com/questions/6623775/c-sharp-hold-down-mouse-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!