Anyway, this works:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace SendMessageWndProc
{
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)
{
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);
}
}
}
But you can actually capture the click event in the WndProc. Still, it is a little bit more complicated than this. See the solution given here:
http://stackoverflow.com/questions/10635518/capturing-wndproc-message-of-a-certain-button-click[
^]