如何在按下按钮时向WndProc发送消息?

编程入门 行业动态 更新时间:2024-10-11 09:23:44
本文介绍了如何在按下按钮时向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发送消息?

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

发布评论

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

>www.elefans.com

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