简体   繁体   English

使用C#移动Winform的问题

[英]Problem with moving a winform using C#

My form doesn't have a title bar, so I am implementing the code to drag the entire form around the screen. 我的表单没有标题栏,因此我正在实现代码以在屏幕上拖动整个表单。 I am using the below code to do it, which works fine. 我正在使用下面的代码来做到这一点,它工作正常。 I have two panels in my form, PanelA and PanelB . 我的表单中有两个面板PanelAPanelB During the startup I show PanelA where the dragging works perfectly. 在启动过程中,我向PanelA展示了拖动效果PanelA地方。 Later when the user clicks the button in PanelA , I need to make PanelA invisible and show PanelB However, the dragging does not work when PanelB is shown. 后来,当用户点击该按钮PanelA ,我需要让PanelA无形的,并显示PanelB然而,在拖动时不工作PanelB所示。 What's the problem here? 这是什么问题

private void SerialPortScanner_MouseUp(object sender, MouseEventArgs e)
{
    this.drag = false; 
}

private void SerialPortScanner_MouseDown(object sender, MouseEventArgs e)
{
    this.drag = true;
    this.start_point = new Point(e.X, e.Y);
}

private void SerialPortScanner_MouseMove(object sender, MouseEventArgs e)
{
    if (this.drag)
    {
        Point p1 = new Point(e.X, e.Y);
        Point p2 = this.PointToScreen(p1);
        Point p3 = new Point(p2.X - this.start_point.X,
                             p2.Y - this.start_point.Y);
        this.Location = p3;
    }
} 

Edit: I've realized that you're not asking about drag and drop , but rather about moving your form around the screen. 编辑:我已经意识到您并不是在问拖放 ,而是在屏幕上移动表单。 (Thanks to @Veer.) I've edited your question to help clarify this. (感谢@Veer。)我已经编辑了您的问题,以帮助阐明这一点。 Drag and drop is a completely different thing, since it is dragging information from one control to another. 拖放是完全不同的事情,因为它将信息从一个控件拖放到另一个控件。

The same principle of my answer still applies though, since mouse events are also handled at the Control level - you might need to handle the mouse events from PanelB as well. 不过,我的答案仍然适用相同的原则,因为鼠标事件也在控件级别进行处理-您可能还需要处理PanelB的鼠标事件。

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

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