本文介绍了如何在按下按钮时向WndProc发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hellow 在我的表格中我有一个按钮(它的名称=按钮1)当我按下按钮我希望去WndProc并使用MessageBox来显示Hellow Word
Hellow in my form i have a button (its NAME=button1 ) when i pressed the button i wish to go to WndProc and use MessageBox to show "Hellow Word"
namespace MyWndProc { public partial class Form1 : Form { private const int WM_ACTIVATEAPP = 0x001C; private bool appActive = true; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // how can i call WndProc from here } [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] protected override void WndProc(ref Message m) { // Listen for operating system messages. switch (m.Msg) { // The WM_ACTIVATEAPP message occurs when the application // becomes the active application or becomes inactive. case WM_ACTIVATEAPP: // The WParam value identifies what is occurring. appActive = (((int)m.WParam != 0)); // Invalidate to get new text painted. this.Invalidate(); break; case button1://not sure <---------- MessageBox.Show("Hellow World"); break; } base.WndProc(ref m); } } }推荐答案
无论如何,这有效: Anyway, this works: using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; namespace SendMessageWndProc { /// <summary> /// Description of MainForm. /// </summary> public partial class MainForm : Form { private const int WM_USER = 0x0400; private const int WM_BTNCLICKED = WM_USER + 1; [DllImport("user32.dll")] public static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam); public MainForm() { InitializeComponent(); } [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] protected override void WndProc(ref Message m) { // Listen for operating system messages. switch (m.Msg) { case WM_BTNCLICKED: MessageBox.Show("Hellow World"); break; } base.WndProc(ref m); } void Button1Click(object sender, EventArgs e) { SendMessage(this.Handle, WM_BTNCLICKED, IntPtr.Zero, IntPtr.Zero); } } }
但您实际上可以捕获WndProc中的click事件。不过,它比这复杂一点。请参阅此处给出的解决方案: stackoverflow/questions/ 10635518 / capture-wndproc-message-of-a-button-click-click [ ^ ]
更多推荐
如何在按下按钮时向WndProc发送消息?
发布评论