C#給其他程式發訊息

來源:互聯網
上載者:User

標籤: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#給其他程式發訊息

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.