using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int WM_Lbutton = 0x201; //定义了鼠标的左键点击消息
const int WM_RBUTTONDOWN = 0x204;//定义鼠标右键
const int WM_KEYDOWN = 0x100;
public const int USER = 0x0400;// 是windows系统定义的用户消息
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
string message = "";
switch (m.Msg)
{
case WM_Lbutton:
///string与MFC中的CString的Format函数的使用方法有所不同
message = string.Format("收到消息!参数为:{0},{1}", m.WParam, m.LParam);
MessageBox.Show(message);///显示一个消息框
break;
case 0:
message = string.Format("收到鼠标点击按钮消息!参数为:{0},{1}", m.WParam, m.LParam);
MessageBox.Show(message);
break;
case WM_RBUTTONDOWN:
MessageBox.Show("你按下了鼠标右键!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
case WM_KEYDOWN:
MessageBox.Show("你按下了键盘!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
default:
base.DefWndProc(ref m);///调用基类函数处理非自定义消息。
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.Message m = new Message();
m.Msg = 0;
m.LParam = (IntPtr)10;
m.WParam = (IntPtr)11;
SendMessage((int)this.Handle, (int)m.Msg, (int)m.WParam, (int)m.LParam);
}
}
}