使用API函数LRESULT SendMessage(
HWND hWnd, // handle to destination window
UINT Msg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);LRESULT PostMessage(
HWND hWnd, // handle to destination window
UINT Msg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);更详细的内容请查阅MSDN
HWND hWnd, // handle to destination window
UINT Msg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);LRESULT PostMessage(
HWND hWnd, // handle to destination window
UINT Msg, // message
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);更详细的内容请查阅MSDN
能不能给我一个具体的例子?
1。在自己做的项目里一般不用摸拟事件,只需要在恰当的时候调用相同的函数即可。例如:
private void MouseClick()
{
// to do something
}
private void Form1_Click(object sender, System.EventArgs e)
{
MouseClick();
}
private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
MouseClick();
}
以上示列中,按下任意键和点击鼠标的功能相同。
2。如果非要摸拟事件,可采用如下方法,这是本人的一项目实例。
[DllImport("user32",EntryPoint="PostMessage")]
public static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam); public const int WM_USER = 0x400;
public const int WM_MYKEYDOWN = WM_USER + 0x100;
public const int WM_MYKEYUP = WM_USER + 0x101;
public const int WM_MYKEYCLICK = WM_USER + 0x102;
public const int WM_MYKEYDBCLICK = WM_USER + 0x103;
public const int WM_MYKEYLONG = WM_USER + 0x104; private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//发送消息
PostMessage( this.Handle, WM_MYKEYDOWN, 5, 0 );
} #region 接收按键消息
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
// *********************************************************************
// 约定Sample:
// 键号1~4为取号,5~32为叫号
// 键号5~10号为人民币,3个柜台;11~16号为外币,3个柜台;17~22号为开户,23~28号为咨询
// 键号状态:0为取号,1为叫号,2为重叫,3为暂停
// *********************************************************************
// case "WM_MYKEYDOWN":
// case "WM_MYKEYUP":
// case "WM_MYKEYDOUBLECLICK": case WM_MYKEYLONG:
//长按
//在小屏上显示暂停服务
int keyNo1=(int)m.WParam; //键号
int CouterNo1=-1;
string s1="select 柜台号 from CodeSetting where 键号=" +keyNo1.ToString(); myReader = DataLayer.ExcuteReader( DataLayer.CONN_STRING, CommandType.Text, s1 );
while(myReader.Read())
{
if(myReader["柜台号"].ToString()!="")
CouterNo1=(int)myReader["柜台号"];
}
myReader.Close(); if(CouterNo1!=-1)
ledShow("暂停服务",CouterNo1.ToString());
break; case WM_MYKEYCLICK:
//单击
int keyNo=(int)m.WParam; //键号
int ServiceNo=-1,CouterNo=-1,KeyStatus=-1;
string s2="select * from CodeSetting where 键号=" +keyNo.ToString(); myReader = DataLayer.ExcuteReader( DataLayer.CONN_STRING, CommandType.Text, s2 );
while(myReader.Read())
{
if(myReader["服务类型"].ToString()!="")
ServiceNo=(int)myReader["服务类型"];
if(myReader["柜台号"].ToString()!="")
CouterNo=(int)myReader["柜台号"];
if(myReader["状态"].ToString()!="")
KeyStatus=(int)myReader["状态"];
}
myReader.Close();
if(KeyStatus==-1)
{
MessageBox.Show("号码未适当设置\r\n键号为:"+ keyNo);
break;
} //取号
if(KeyStatus==0)
{
this.GetNoFunc(ServiceNo);
}
//叫号
if(KeyStatus==1)
{
this.CallNoFunc(ServiceNo,CouterNo);
}
//重叫
if(KeyStatus==2)
{
this.RecallNoFunc(ServiceNo,CouterNo);
}
break;
default:
base.WndProc(ref m);
return;
}
}