標籤:os ar for sp on art bs ad ef
1、相關聲明函數,SendMessage可定義兩種格式。
[DllImport("User32.DLL", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
[DllImport("User32.DLL")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);
2、相關訊息
public const uint WM_SETTEXT = 0x000C;
public const uint WM_CHAR = 0x0102;
public const uint EM_SETSEL = 0x00B1;
3、
用spy++觀察是哪個視窗處理訊息,然後用FindWindow找到視窗控制代碼
IntPtr main = FindWindow("TFrmMain", null);
或
自己啟動,比如記事本
Process vProcess = Process.Start("notepad.exe");
while (vProcess.MainWindowHandle == IntPtr.Zero) vProcess.Refresh();
註:啟動之後可以直接發索引值
SendKeys.Send("01234/n");SendKeys.Send("56789/n");
或
枚舉所有進程
Process[] vP2 = Process.GetProcesses();
用FindWindowEx找子視窗控制代碼,一層層找下去。
IntPtr panel1 = FindWindowEx(main, IntPtr.Zero, "TPanel", null);
IntPtr panel2 = FindWindowEx(panel1, IntPtr.Zero, "TPanel", null);
如果有多個同類子視窗,FindWindowEx第二個參數,控制從哪個子視窗開始找何開始找。
IntPtr edit1 = FindWindowEx(panel2, (IntPtr)null, "TEdit", null);
IntPtr edit2 = FindWindowEx(panel2, edit1, "TEdit", null);
逐個發訊息
SendMessage(edit1, WM_CHAR, (uint)Keys.F3, 0);
用迴圈發
string end="hello";
for(int i=0;i<5;i++)
SendMessage(edit1, WM_CHAR, (uint)end[i], 0);
發字串
SendMessage(vHandle, WM_SETTEXT, IntPtr.Zero, "測試/r/n換行");
選中7個文本
SendMessage(vHandle, EM_SETSEL, 0, 7);
C#給其他程式發訊息