如何从孩子的用户控制父传递消息 [英] How to pass messages from a child user-control to the parent

查看:137
本文介绍了如何从孩子的用户控制父传递消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个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?

感谢您的帮助。

标记

推荐答案

您可以实现自己的目标,即使没有使用SendMessage函数 System.Windows.Forms.Message 类。如果你做拖着我猜你所熟悉的 WM_NCLBUTTONDOWN 消息。它从你的控件的MouseDown事件发送到你的父母。

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.

下面是有关控制LABEL1移动形式点击一个例子。记下发送者用来释放从点击控制捕获的第一行。这样,您可以设置此处理程序,以意动窗体的所有控件。

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.

这篇关于如何从孩子的用户控制父传递消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆