C#如何向指定窗口发键盘消息...?
我用的是VS.net05版...
模拟键盘我用sendkeys.sendwait()方法
现在想在指定的窗件上发模拟按键盘的消息,具体要怎么实现?要用到哪些命名空间??
希望懂的,能给点代码参考,最好是有比较全的代码啦
我用的是VS.net05版...
模拟键盘我用sendkeys.sendwait()方法
现在想在指定的窗件上发模拟按键盘的消息,具体要怎么实现?要用到哪些命名空间??
希望懂的,能给点代码参考,最好是有比较全的代码啦
调试欢乐多
另外一个要点是,需要通过重写(Override) 窗体的 DefWndProc() 过程来接收自定义的消息。DefWndProc 的重写:
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
switch(m.Msg)
{
case ...:
break;
default:
base.DefWndProc(ref m);
break;
}
}
下面是我的C#实践例程。
------------------------------------
/////////////////////////////////////////
///file name: Note.cs
///
public class Note
{
//声明 API 函数
[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
);
[DllImport("User32.dll",EntryPoint="FindWindow")]
private static extern int FindWindow(string lpClassName,string
lpWindowName);
//定义消息常数
public const int USER = 0x500;
public const int TEST = USER + 1;
//向窗体发送消息的函数
private void SendMsgToMainForm(int MSG)
{
int WINDOW_HANDLER = FindWindow(null,@"Note Pad");
if(WINDOW_HANDLER == 0)
{
throw new Exception("Could not find Main window!");
}
SendMessage(WINDOW_HANDLER,MSG,100,200);
}
}
/////////////////////////////////////////
/// File name : Form1.cs
/// 接收消息的窗体
///
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// 重写窗体的消息处理函数
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
switch(m.Msg)
{
//接收自定义消息 USER,并显示其参数
case Note.USER:
string message = string.Format ("Received message!
parameters are :{0},{1}",m.WParam ,m.LParam);
MessageBox.Show (message);
break;
default:
base.DefWndProc(ref m);
break;
}
//Console.WriteLine(m.LParam);
}
--
[DllImport("user32.dll")]
static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);//Send keys
Process pNotepad = Process.Start( "Notepad.exe" );SetActiveWindow( pNotepad.MainWindowHandle );
SetForegroundWindow( pNotepad.MainWindowHandle );Thread.Sleep( 2000 );
SendKeys.SendWait( "%{F4}" );//Send Alt+F4
不知道说的对不对,听高手的吧:)
按你方法试了,只有活动窗口才能接收,我想要的是在最小化时也能接收键盘按键消息。
应该是我开帖时没说清楚具体要实现的功能。本人刚学.net不久,很菜,希望高手们多多指教。
通过控件名.Handle,获取控件绑定到窗口的句柄,能否用这个给这控件发键盘按键信息呢?
麻烦大侠们给点代码要窗口在最小化时,也能按收键盘的按键消息的
应该是我开帖时没说清楚具体要实现的功能。如果接收消息的程序是你开发的,你可以参看这篇文章
http://dev.csdn.net/article/15/15598.shtm如果不是自己开发的,
通过SendKeys发送消息,可以把握的程序进行优化处理
[DllImport("user32.dll")]
static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsIconic(IntPtr hWnd);
private const int SW_RESTORE = 9;//Send keys
Process pNotepad = Process.Start( "Notepad.exe" );// if iconic, we need to restore the window
if (IsIconic(pNotepad.MainWindowHandle))
{
ShowWindowAsync(pNotepad.MainWindowHandle, SW_RESTORE);
}SetForegroundWindow( pNotepad.MainWindowHandle );
SetActiveWindow( pNotepad.MainWindowHandle );Thread.Sleep( 2000 );
SendKeys.SendWait( "%{F4}" );//Send Alt+F4
http://community.csdn.net/Expert/topicview.asp?id=1567478