繁体   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