使用WndProc在WinForms Designer中捕获窗口消息(WM)

编程入门 行业动态 更新时间:2024-10-11 05:20:54
本文介绍了使用WndProc在WinForms Designer中捕获窗口消息(WM)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在用.NET Windows窗体编写自定义控件.考虑以下代码:

I am writing a custom control in .NET Windows Forms. Consider the following code:

protected override void WndProc(ref Message m) { base.WndProc(ref m); switch(m.Msg) { case WM_LBUTTONDOWN: // Yes, it's defined correctly. MessageBox.Show("Left Button Down"); break; } }

它在运行时可以工作,但是我需要它在设计器中工作.我该如何实现?

It works when running, but I need it to work in the designer. How can I achieve this?

注意:

我猜可能有人会说:"您无法在设计器中检测到点击,因为设计表面会捕获这些点击并将其作为设计过程的一部分进行处理"

I guess someone might say that "You can't detect clicks in the designer because the design surface captures them and processes them as part of the design process"

...例如以TabControl为例.添加新选项卡时,可以单击以浏览选项卡,然后单击选项卡的可设计区域以开始设计选项卡页面的内容.怎么运作的?

...Take for example the TabControl. When you add a new tab, you can click to navigate through the tabs, and then click the tab's designable area to begin designing the tab page's content. How does that work?

推荐答案

设计人员会吃一些消息.如果要将所有消息发送到Control,则需要创建一个自定义控件设计器并将其发送到控件.

Well, designer eats some of the messages. If you want all messages to be sent to the Control, you need to create a custom control designer and send them to the control.

引用 ControlDesigner.WndProc

public class CustomDesigner : ControlDesigner { protected override void WndProc(ref Message m) { DefWndProc(ref m);//Passes message to the control. } }

然后将DesignerAttribute应用于自定义控件.

Then apply the DesignerAttribute to your custom control.

[Designer(typeof(CustomDesigner))] public partial class MyUserControl : UserControl { public MyUserControl() { InitializeComponent(); } protected override void WndProc(ref Message m) { base.WndProc(ref m); const int WM_LBUTTONDOWN = 0x0201; switch (m.Msg) { case WM_LBUTTONDOWN: // Yes, it's defined correctly. MessageBox.Show("Left Button Down"); break; } } }

将控件拖到Form,单击它.现在您还应该在设计器中看到消息框:)

Drag your control to the Form, click on it. Now you should see the message box in designer also :)

更多推荐

使用WndProc在WinForms Designer中捕获窗口消息(WM)

本文发布于:2023-11-11 05:24:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577498.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗口   消息   WinForms   WndProc   WM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!