【发布时间】:2017-04-03 00:26:45
【问题描述】:
我目前正在研究一种非托管外部代码自动化的解决方案,因此在将文本设置为 RichEdit 控件时遇到了一些麻烦。
我尝试发送 WM_SETTEXT,但它只将字符串的第一个字母设置为控件。
我尝试过的其他东西:PostMessage、EM_SETTEXTEX、SetWindowText,我也尝试过 EM_STREAMIN,但没有成功,但没有足够简单的消息示例。
richEdit的具体类是: WindowsForms10.RichEdit20W.app.0.141b42a_r14_ad1
我的代码:
IntPtr Text;
string bar;
...
//Function call
setRichEditText(Text, bar);
...
//Function declaration
private static int setRichEditText(IntPtr hWnd, string text) {
StringBuilder sb = new StringBuilder(text);
int result = SendMessage(hWnd, WM_SETTEXT, (IntPtr)sb.Length, sb.ToString());
return result;
}
...
//Imported function
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);
有没有办法让它设置整个字符串或解决方法?
【问题讨论】:
-
闻起来像您通过 UTF 16 但控件将其解释为 ANSI
-
但是使用 UI 自动化不是更容易
-
[MarshalAs(UnmanagedType.LPStr)]- 这是一个错误。 -
我正在使用 UIAVerify、winSpy、spy++ 和 AutomationSpy。我不能只使用一种工具,因为一种工具不能提供我需要的一切。
-
没有人建议使用工具。 UI Automation 是一个编程接口。