简体   繁体   English

C#WinForms用鼠标拖动控件

[英]C# WinForms dragging controls with mouse

I'm making a calendar From in C# using WinForms. 我正在使用WinForms在C#中制作日历。 I've put it together using a two-dimensional array of Panels, and inside them I have a List<> of custom controls which represent appointments. 我使用一个二维的Panels数组把它放在一起,在里面我有一个List <>的自定义控件代表约会。

The user needs to be able to drag appointment controls from one Panel to another (from day to day). 用户需要能够将约会控件从一个面板拖动到另一个面板(从一天到一天)。

The custom control has a MouseDown and MouseUp event, which passes a message up from the control to the Parent.Parent (custom control -> day panel -> calendar form) and calls public methods StartDragging() and StopDragging() respectively. 自定义控件有一个MouseDown和MouseUp事件,它将一个消息从控件传递到Parent.Parent(自定义控件 - >日面板 - >日历表单),并分别调用公共方法StartDragging()和StopDragging()。

Inside these methods, I make a clone of the custom control and add it to the Form, and store it in a global variable in the form which is called DraggedControl. 在这些方法中,我复制了自定义控件并将其添加到Form中,并将其存储在名为DraggedControl的表单中的全局变量中。

The Form has an event handler for MouseMove which goes like this: Form有一个MouseMove的事件处理程序,如下所示:

    void Calendar_MouseMove(object sender, MouseEventArgs e)
    {
        if (DraggedControl == null)
            return;

        DraggedControl.Location = PointToClient(MousePosition);
        Refresh();
    }

There are two problems, however: 但是有两个问题:

  • First of all, the custom control is under everything else. 首先,自定义控件是其他所有内容。 I can see it being added and removed on MouseDown and MouseUp, but it is being added at 0,0 under the panels and day Labels. 我可以在MouseDown和MouseUp上看到它被添加和删除,但它在面板和日标签下添加到0,0。
  • Secondly, it does not appear to be moving with the MouseMove at all. 其次,它似乎根本没有与MouseMove一起移动。 I have a feeling this might be because I am moving the mouse with the button pressed, and this would represent a drag action rather than a basic MouseMove. 我有一种感觉,这可能是因为我按下按钮移动鼠标,这将代表拖动动作而不是基本的MouseMove。

If I remove the MouseUp code, the control does drag with the mouse, however as soon as the mouse enters the panels (which the control is, sadly, underneath), the drag action stops. 如果我删除了MouseUp代码,控件确实会用鼠标拖动,但是只要鼠标进入面板(控件就在下面),拖动动作就会停止。

What would you suggest I do? 你有什么建议我这样做? I suspect there is probably a better way to do what I am trying to do. 我怀疑可能有更好的方法来做我想做的事情。

custom control is under everything else 自定义控制在其他一切之下

bring it on top: 把它放在首位:

DraggedControl.BringToFront();

it does not appear to be moving with the MouseMove at all 它似乎根本没有与MouseMove一起移动

Control, which handled MouseDown event, captures mouse input and receives all following MouseMove events until it releases mouse input on MouseUp event, that's why Calendar_MouseMove() is not called. Control,处理MouseDown事件,捕获鼠标输入并接收所有后续MouseMove事件,直到它在MouseUp事件上释放鼠标输入,这就是为什么不调用Calendar_MouseMove() Handle MouseMove event for the same control, which generated MouseDown event. 处理同一控件的MouseMove事件,该事件生成MouseDown事件。

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

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