消息参数的含义根据消息的不同而不同,每个消息的参数含义都要到msdn里去查,比如这个是wm_keyup 的wParam Specifies the virtual-key code of the nonsystem key. lParam Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table. 0-15 Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. The repeat count is always one for a WM_KEYUP message. 16-23 Specifies the scan code. The value depends on the OEM. 24 Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0. 25-28 Reserved; do not use. 29 Specifies the context code. The value is always 0 for a WM_KEYUP message. 30 Specifies the previous key state. The value is always 1 for a WM_KEYUP message. 31 Specifies the transition state. The value is always 1 for a WM_KEYUP message.楼主要做的事不太适合C#,用C++比较好
比如说依次向后台运行的notepad发送“F1”、回车、“123”。
谢谢。
[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd,
uint Msg, int wParam, string lParam);
[DllImport("User32.DLL")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent,
IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
public const uint WM_SETTEXT = 0x000C;
private void button1_Click(object sender, EventArgs e)
{
Process vProcess = Process.Start("notepad.exe");
while (vProcess.MainWindowHandle == IntPtr.Zero) vProcess.Refresh();
IntPtr vHandle = FindWindowEx(vProcess.MainWindowHandle,
IntPtr.Zero, "Edit", null);
SendMessage(vHandle, WM_SETTEXT, 0, "Zswang 路过");
}
不过发现SendMessage会覆盖上一个SendMessage的内容,该怎么解决呢?
还有能否通过已经在运行的进程的程序路径找到进程的Handle呢?(因为应用中的状况是不同路径中有两个同名程序,Process.GetProcessesByName会得到两个Process,而且他们的StartInfo.FileName,也没法通过比较这个属性知道要控制的是哪一个程序)
{
this.richTextBox1.Focus(); //焦点请求 for (int i = 0; i < 10; i++)
{ SendKeys.Send(i.ToString()); System.Threading.Thread.Sleep(100); SendKeys.Flush (); } }
分不够我会另开新帖加
顺便请教一下SendMessage各参数可取值范围与含义,如:
UINT Msg=0x000C,
WPARAM wParam=0,
LPARAM lParam="test"
向指定进程发送字符串。
如果想发送 Tab、F1、Alt、回车 等命令该怎么办?还有怎么防止SendMessage覆盖前面发送的消息?怎么依据程序路径找到已经运行的程序句柄(内存中可能有两个同名但路径不同的程序)?
谢谢。
Specifies the virtual-key code of the nonsystem key. lParam
Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.
0-15
Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. The repeat count is always one for a WM_KEYUP message.
16-23
Specifies the scan code. The value depends on the OEM.
24
Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
25-28
Reserved; do not use.
29
Specifies the context code. The value is always 0 for a WM_KEYUP message.
30
Specifies the previous key state. The value is always 1 for a WM_KEYUP message.
31
Specifies the transition state. The value is always 1 for a WM_KEYUP message.楼主要做的事不太适合C#,用C++比较好
主要是考虑到这个东西的很多其它功能要大量应用.net Framework的东西,C++没有合适的库(没有用过C++/CLR,不知道这种情况下C++/CLR相对纯.net环境是否有什么特殊优势,比如对系统API的调用上)。