如何在winforms应用程序中单击并拖动

时间:2023-01-17 04:14:37

I have a simple winforms application with a number of controls in it. I want to be able to click and drag from anywhere on the form and move the whole set of stuff around (panning). I have figured out the respositioning bit already. What I still need to figure out is how to tie the click and drag into updating my offsets.

我有一个简单的winforms应用程序,其中包含许多控件。我希望能够从表单上的任何位置单击并拖动并移动整组内容(平移)。我已经找到了重新定位的位。我还需要弄清楚的是如何将点击和拖动绑定到更新我的偏移量。

What I have tried so far is adding stuff to the main form's MouseDown, MouseUp and MouseMove events (sort of looks something like what this guy shows) , but they aren't getting called (breakpoints in them don't get hit)

我到目前为止尝试的是在主窗体的MouseDown,MouseUp和MouseMove事件中添加东西(看起来像这个人显示的东西),但它们没有被调用(它们中的断点不会被击中)

What am I doing wrong?

我究竟做错了什么?


Edit: I'm not looking for drag and drop as I don't necessarily have anything to be dragged or dropped. What I want would work withe the following spec:

编辑:我不是在寻找拖放,因为我不一定要拖放任何东西。我想要的是符合以下规范:

  • create a form with no controls
  • 创建一个没有控件的表单

  • set up an x and y variable that are printed to the form title whenever they update
  • 设置一个x和y变量,每次更新时都会打印到表单标题

  • when the user click and drags anywhere on the form, the x and y get updated to reflect the motion of the pointer (click and drag down and to the right will result in x>0 and y>0)
  • 当用户单击并拖动窗体上的任何位置时,x和y会更新以反映指针的运动(单击并向下拖动,向右拖动将导致x> 0且y> 0)


Based on Eric's comment I think I'm hooking up to the events on the wrong control.

根据埃里克的评论,我想我正在接受错误控制的事件。

Is there a way to (with a setup like controls use) catch all events in one place regardless of what they are supposed to be sent to? Or how about have any otherwise unhanded events get chained up to the parent? In my case I will never care what got the event so this would be what I want to get.

有没有办法(使用像控件一样的设置)在一个地方捕获所有事件,而不管它们应该被发送到什么地方?或者如果有任何其他无法处理的事件被链接到父母?在我的情况下,我永远不会关心什么事情,所以这将是我想要的。

2 个解决方案

#1


How do you handle mouse events exactly? If you have panel covering the form, form's mouse events won't be called. Only panel's mouse events would.

你如何处理鼠标事件?如果您有面板覆盖表单,则不会调用表单的鼠标事件。只有面板的鼠标事件会。

#2


There are a few tutorials online covering Drag-and-Drop such as this one

有一些教程在线涵盖拖拽这样的教程

the fact that the events arent triggering is not normal. If you have controls over the form, you should hook events of those controls. Another way of doing it would be to have a transparent panel over your whole form which will handle the events

事件没有触发的事实并不正常。如果您对表单有控制权,则应该挂钩这些控件的事件。另一种方法是在整个表单上放置一个透明面板来处理事件

#1


How do you handle mouse events exactly? If you have panel covering the form, form's mouse events won't be called. Only panel's mouse events would.

你如何处理鼠标事件?如果您有面板覆盖表单,则不会调用表单的鼠标事件。只有面板的鼠标事件会。

#2


There are a few tutorials online covering Drag-and-Drop such as this one

有一些教程在线涵盖拖拽这样的教程

the fact that the events arent triggering is not normal. If you have controls over the form, you should hook events of those controls. Another way of doing it would be to have a transparent panel over your whole form which will handle the events

事件没有触发的事实并不正常。如果您对表单有控制权,则应该挂钩这些控件的事件。另一种方法是在整个表单上放置一个透明面板来处理事件