如何将消息从子用户控件传递到父级

发布于 2024-08-16 19:50:12 字数 253 浏览 1 评论 0原文

这是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晨光如昨 2024-08-23 19:50:12

即使没有 SendMessage,您也可以使用 System.Windows.Forms.Message 类来实现您的目标。如果您完成了拖动,我想您对 WM_NCLBUTTONDOWN 消息很熟悉。通过控件的 MouseDown 事件将其发送给您的父级。

以下是单击控件 label1 移动表单的示例。请注意第一行,其中 sender 用于从单击的控件中释放捕获。通过这种方式,您可以将此处理程序设置为用于移动表单的所有控件。

这是移动表单的完整代码。不需要其他任何东西。


public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

private void label1_MouseDown(object sender, MouseEventArgs e)
{
      (sender as Control).Capture = false;
      Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
      base.WndProc(ref msg);
}

希望这有帮助。

You can achieve your goal even without SendMessage using System.Windows.Forms.Message class. If you have done dragging I guess you are familiar with WM_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.


public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

private void label1_MouseDown(object sender, MouseEventArgs e)
{
      (sender as Control).Capture = false;
      Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
      base.WndProc(ref msg);
}

Hope this helps.

拔了角的鹿 2024-08-23 19:50:12

我认为最简单的方法是将此事件添加到您的子控件中:

/// <summary>
/// The event that you will throw when the mouse hover the control while being clicked 
/// </summary>
public event EventHandler MouseRightClickedAndHoverChildControl;

之后,父控件所要做的就是订阅这些事件并进行移动父控件的操作:

ChildControl.MouseRightClickedAndHoverChildControl += OnMouseHoverChildControl;

private void OnMouseHoverChildControl(object sender, EventArgs e)
{
    //do foo...
}

I think the easiest way is to add this event to your child controls:

/// <summary>
/// The event that you will throw when the mouse hover the control while being clicked 
/// </summary>
public event EventHandler MouseRightClickedAndHoverChildControl;

After, all the parent have to do is to subscribe to those events and make the operations to move the Parent:

ChildControl.MouseRightClickedAndHoverChildControl += OnMouseHoverChildControl;

private void OnMouseHoverChildControl(object sender, EventArgs e)
{
    //do foo...
}
心碎无痕… 2024-08-23 19:50:12

您需要调用 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.

梦冥 2024-08-23 19:50:12

我确实有这个问题......但提出了不同的答案。
如果您的 WndProc 中有消息,您只需将句柄更改为您的 Parent 的句柄,然后将其传递即可。

我需要在派生的 TextBox 中执行此操作...即使其 ScrollBars 设置为 None,TextBox 也会吃掉滚轮事件。我希望这些能够传播到表单。因此,我只是将其放入派生 TextBox 的 WndProc 中:

            case 0x020A: // WM_MOUSEWHEEL
            case 0x020E: // WM_MOUSEHWHEEL
                if (this.ScrollBars == ScrollBars.None && this.Parent != null)
                    m.HWnd = this.Parent.Handle; // forward this to your parent
                base.WndProc(ref m);
                break;

            default:
                base.WndProc(ref m);
                break;

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:

            case 0x020A: // WM_MOUSEWHEEL
            case 0x020E: // WM_MOUSEHWHEEL
                if (this.ScrollBars == ScrollBars.None && this.Parent != null)
                    m.HWnd = this.Parent.Handle; // forward this to your parent
                base.WndProc(ref m);
                break;

            default:
                base.WndProc(ref m);
                break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文