簡體   English   中英

使用SendMessage插入文本

[英]Inserting text with SendMessage

當我嘗試將文本粘貼到另一個程序的文本框中時,將插入文本,但是該程序無法識別它。

 [DllImport("user32.dll", CharSet = CharSet.Auto)]
 static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
 const uint WM_SETTEXT = 0x000C;

 IntPtr text = Marshal.StringToCoTaskMemUni("100");
 IntPtr thisWindow = FindWindow(null, "AnotherWindow");
 IntPtr handle = FindWindowEx(thisWindow, IntPtr.Zero, "AnotherTextBox", null);
 SendMessage(handle, WM_SETTEXT, IntPtr.Zero, text);
 Marshal.FreeCoTaskMem(text);

也許我應該向父母發送一條消息,說明文本框已更新? 像這樣:

 //Wrong code, because I do not know how correctly send a message
 SendMessage(handle, WM_COMMAND, EM_SETMODIFY, handle);

再次...幫助來自另一個站點

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
//...
IntPtr boo = new IntPtr(1);
SendMessage(handle, EM_SETMODIFY, boo, IntPtr.Zero);

您將希望執行以下操作:

PostMessage(GetParent(handle), WM_COMMAND, MAKEWPARAM(GetWindowLong(handle, GWL_ID), EN_CHANGE), (LPARAM)handle);

已經設置了一些文本框,以使您無法立即通過WM_SETTEXT設置文本,尤其是那些接受數字並根據這些數字進行計算的文本框。 我有類似的問題,並通過下面的代碼解決了。 我應用了WM_PASTE和EM_REPLACESEL來解決這個問題。

SendMessage(child, WM_SETFOCUS,0 , IntPtr.Zero);  // go to text box
System.Windows.Forms.Clipboard.SetText("1");   // set something in clipboard. it does not matter what it is.
SendMessage(child, WM_PASTE, 0, IntPtr.Zero); // paste to get control of text box
SendMessage(child, WM_SETTEXT, IntPtr.Zero, string.Empty);  // clear textbox to insert your desired text.
SendMessage(child, EM_REPLACESEL, IntPtr.Zero, "your text"); // insert your desired text. i inserted digits as text.

您首先需要導入user32.dll文件:

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    private static extern int SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, string lParam);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    private static extern int SendMessage(IntPtr hWnd, uint msg,int  wParam, IntPtr lParam);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM