如何在按下按钮时向WndProc发送消息? [英] how to send a message to WndProc when button pressed?

查看:128
本文介绍了如何在按下按钮时向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事件。不过,它比这复杂一点。请参阅此处给出的解决方案: http://stackoverflow.com/questions/ 10635518 / capture-wndproc-message-of-a-button-click-click [ ^ ]


这篇关于如何在按下按钮时向WndProc发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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