如何将消息从子用户控件传递到父级
这是一个 Windows 窗体/.Net C# 问题。
我有一个无边框窗口,其透明度键和背景颜色使其完全透明。窗口内有几个用户控件。
我希望能够移动窗户。我知道如何在父窗口上执行此操作,但我的问题是子控件是唯一可见的,因此也是唯一可单击的。
问题是:如何将某些消息传递给父级,以便当鼠标右键按下并且鼠标在任何一个子控件上移动时,父级可以移动?
或者也许你可以建议另一种方式?
感谢您的帮助。
标记
This is a Windows Forms / .Net C# question.
I have a borderless windows whose transparency key and background color make it completely transparent. Inside the window are a couple of user controls.
I want to be able to move the window. I know how to do this on the parent window, but my problem is that the child controls are the only thing visible and thus the only thing click-able.
The question is: how can I pass certain messages up to the Parent so the Parent can move when the right mouse button is down and the mouse is moving on any one of the child controls?
Or maybe you can suggest another way?
Thanks for the help.
Mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
即使没有 SendMessage,您也可以使用 System.Windows.Forms.Message 类来实现您的目标。如果您完成了拖动,我想您对
WM_NCLBUTTONDOWN
消息很熟悉。通过控件的 MouseDown 事件将其发送给您的父级。以下是单击控件 label1 移动表单的示例。请注意第一行,其中 sender 用于从单击的控件中释放捕获。通过这种方式,您可以将此处理程序设置为用于移动表单的所有控件。
这是移动表单的完整代码。不需要其他任何东西。
希望这有帮助。
You can achieve your goal even without SendMessage using
System.Windows.Forms.Message
class. If you have done dragging I guess you are familiar withWM_NCLBUTTONDOWN
message. Send it to you parent from your control's MouseDown event.Here is an example for moving the form clicking on control label1. Note the first line where sender is used to release the capture from clicked control. This way you can set this handler to all controls intended to move your form.
This is complete code to move the form. Nothing else is needed.
Hope this helps.
我认为最简单的方法是将此事件添加到您的子控件中:
之后,父控件所要做的就是订阅这些事件并进行移动父控件的操作:
I think the easiest way is to add this event to your child controls:
After, all the parent have to do is to subscribe to those events and make the operations to move the Parent:
您需要调用 SendMessage API 函数将鼠标消息发送到您的家长控制。
最简单的方法可能是覆盖控件的
WndProc
方法。You need to call the SendMessage API function to send mouse messages to your parent control.
It would probably be easiest to do this by overriding your control's
WndProc
method.我确实有这个问题......但提出了不同的答案。
如果您的 WndProc 中有消息,您只需将句柄更改为您的 Parent 的句柄,然后将其传递即可。
我需要在派生的 TextBox 中执行此操作...即使其 ScrollBars 设置为 None,TextBox 也会吃掉滚轮事件。我希望这些能够传播到表单。因此,我只是将其放入派生 TextBox 的 WndProc 中:
I had exactly this question... but came up with a different answer.
If you have the Message in your WndProc, you can just change the handle to your Parent's handle and then pass it along.
I needed to do this in our derived TextBox... TextBox eats scroll wheel events even when its ScrollBars are set to None. I wanted those to propagate on up to the Form. So, I simply put this inside the WndProc for my derived TextBox: