C# – 窗体的鼠标按下事件无效

huangapple go评论85阅读模式
英文:

C# - Mouse Down event for form is not working

问题

我正在尝试制作可拖动的窗体,但当我尝试检测鼠标按下和抬起事件时,却没有触发事件。其他功能似乎是有效的,但这两个事件没有触发。

英文:

I'm trying to make draggable window form, but when I tried to detect mouse down and up event to form, but event does not fire.
Other things seems working but those 2 events are not.

public form()
{
    InitializeComponent();
    this.BackColor = Color.Fuchsia;
    this.TransparencyKey = this.BackColor;

    Debug.Write("initialized");
}

private void form_MouseDown(object sender, MouseEventArgs e)
{
    Debug.Write("dragging..");
}

private void form_MouseUp(object sender, MouseEventArgs e)
{
    Debug.Write("drag done");
}

答案1

得分: 0

最好将一个 `panel` 添加到您的表单,并将 `Dock` 设置为 `top` 以覆盖标题栏区域。然后使用这个示例代码

    private bool isDragging = false;
    private Point lastLocation;

    private void panel_MouseDown(object sender, MouseEventArgs e)
    {
        isDragging = true;
        lastLocation = e.Location;
    }

    private void panel_MouseMove(object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            this.Location = new Point(
            (this.Location.X - lastLocation.X) + e.X, 
            (this.Location.Y - lastLocation.Y) + e.Y);

            this.Update();
        }
    }

    private void panel_MouseUp(object sender, MouseEventArgs e)
    {
        isDragging = false;
    }

不要忘记在表单构造函数中添加处理程序

    panel.MouseDown += panel_MouseDown;
    panel.MouseMove += panel_MouseMove;
    panel.MouseUp += panel_MouseUp;
英文:

it is better to add a panel to your form and set Dock to top in order to cover the title bar area. then use this sample code

private bool isDragging = false;
private Point lastLocation;

private void panel_MouseDown(object sender, MouseEventArgs e)
{
    isDragging = true;
    lastLocation = e.Location;
}

private void panel_MouseMove(object sender, MouseEventArgs e)
{
    if (isDragging)
    {
        this.Location = new Point(
        (this.Location.X - lastLocation.X) + e.X, 
        (this.Location.Y - lastLocation.Y) + e.Y);

        this.Update();
    }
 }

 private void panel_MouseUp(object sender, MouseEventArgs e)
 {
    isDragging = false;
 }

don't forget to add handlers in the form constructor

panel.MouseDown += panel_MouseDown;
panel.MouseMove += panel_MouseMove;
panel.MouseUp += panel_MouseUp;

答案2

得分: 0

在你的窗体构造函数中,注册那些用于监听鼠标按下事件的方法,就像这样。

public form() { 
 InitializeComponent(); 
 this.BackColor = Color.Fuchsia; 
 this.TransparencyKey = this.BackColor; 
 Debug.Write("initialized");
 //注册用于监听事件的方法
 this.MouseDown += form_MouseDown;
 this.MouseUp += form_MouseUp;
 }
 private void form_MouseDown(object sender, 
  MouseEventArgs e) { 

   Debug.Write("拖拽中.."); 

} 
 private void form_MouseUp(object sender, 
 MouseEventArgs e) { 

   Debug.Write("拖拽完成");
 }
英文:

In your constructor for form, register those methods your using to listen for the mouse down events like this.

public form() { 
 InitializeComponent(); 
 this.BackColor = Color.Fuchsia; 
 this.TransparencyKey = this.BackColor; 
 Debug.Write("initialized");
 //register the methods to listen for the events
 this.MouseDown += form_MouseDown;
 this.MouseUp += form_MouseUp;
 }
 private void form_MouseDown(object sender, 
  MouseEventArgs e) { 

   Debug.Write("dragging.."); 

} 
 private void form_MouseUp(object sender, 
 MouseEventArgs e) { 

   Debug.Write("drag done");
 }

huangapple
  • 本文由 发表于 2023年2月18日 19:50:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定