C# 外部应用程序中文本框的 SetText。Win32 API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1100605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SetText of textbox in external app. Win32 API
提问by Kirschstein
Using Winspector I've found out the ID of the child textbox I want to change is 114. Why isn't this code changing the text of the TextBox?
使用 Winspector 我发现我想要更改的子文本框的 ID 是 114。为什么这段代码不更改文本框的文本?
[DllImport("user32.dll")]
static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int Param, string s);
const int WM_SETTEXT = 0x000c;
private void SetTextt(IntPtr hWnd, string text)
{
IntPtr boxHwnd = GetDlgItem(hWnd, 114);
SendMessage(boxHwnd, WM_SETTEXT, 0, text);
}
采纳答案by Gregyski
The following is what I've used successfully for that purpose w/ my GetLastError error checking removed/disabled:
以下是我为此目的成功使用的内容,删除/禁用了我的 GetLastError 错误检查:
[DllImport("user32.dll", SetLastError = false)]
public static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, string lParam);
public const uint WM_SETTEXT = 0x000C;
private void InteropSetText(IntPtr iptrHWndDialog, int iControlID, string strTextToSet)
{
IntPtr iptrHWndControl = GetDlgItem(iptrHWndDialog, iControlID);
HandleRef hrefHWndTarget = new HandleRef(null, iptrHWndControl);
SendMessage(hrefHWndTarget, WM_SETTEXT, IntPtr.Zero, strTextToSet);
}
I've tested this code and it works, so if it fails for you, you need to be sure that you are using the right window handle (the handle of the Dialog box itself) and the right control ID. Also try something simple like editing the Find dialog in Notepad.
我已经测试了这段代码并且它可以工作,所以如果它对你来说失败了,你需要确保你使用了正确的窗口句柄(对话框本身的句柄)和正确的控件 ID。还可以尝试一些简单的操作,例如在记事本中编辑“查找”对话框。
I can't comment yet in the post regarding using (char *) but it's not necessary. See the second C# overload in p/Invoke SendMessage. You can pass String or StringBuilder directly into SendMessage.
我还不能在帖子中就使用 (char *) 发表评论,但这不是必需的。请参阅p/Invoke SendMessage 中的第二个 C# 重载。您可以将 String 或 StringBuilder 直接传递给 SendMessage。
I additionally note that you say that your control ID is 114. Are you certain WinSpector gave you that value in base 10? Because you are feeding it to GetDlgItem as a base 10 number. I use Spy++ for this and it returns control IDs in base 16. In that case you would use:
我还注意到您说您的控件 ID 是 114。您确定 WinSpector 以 10 为基数为您提供了该值吗?因为您将它作为基数为 10 的数字提供给 GetDlgItem。我为此使用 Spy++,它返回基数为 16 的控件 ID。在这种情况下,您将使用:
IntPtr boxHwnd = GetDlgItem(hWnd, 0x0114);
回答by smok1
Are you sure you are passing text right? SendMessage last param should be a pointer to char* containing text you want to set.
Look at my "crude hack" of setting text in
How to get selected cells from TDBGrid in Delphi 5
this is done in Delphi 5, where PChar is char* alias, and I simply cast it as int (Integer in Delphi).
你确定你在传递文本吗?SendMessage last param 应该是指向 char* 的指针,其中包含要设置的文本。
看看我在如何从 Delphi 5 中的 TDBGrid 中获取选定单元格中设置文本的“粗略黑客”,
这是在 Delphi 5 中完成的,其中 PChar 是 char* 别名,我只是将其转换为 int(Delphi 中的整数)。
回答by Gautam Jain
You must make sure that "text" is allocated in the external app's memory space. You will not be able to allocate text in the caller app and pass it to another app as each of them will have their own private memory space.
您必须确保在外部应用程序的内存空间中分配了“文本”。您将无法在调用方应用程序中分配文本并将其传递给另一个应用程序,因为每个应用程序都有自己的私有内存空间。
回答by Chandrasekhar Telkapalli
Please convert your control id (obtained from spy ++) from Hexdecimal Number to Decimal Number and pass that value to the GetDlgItem function.With this
you will get the handle of Text box.This worked for me.
请将您的控件 ID(从 spy ++ 获得)从十六进制数转换为十进制数并将该值传递给 GetDlgItem 函数。这样
您将获得文本框的句柄。这对我有用。
[DllImport("user32.dll")]
static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int Param, string s);
const int WM_SETTEXT = 0x000c;
private void SetTextt(IntPtr hWnd, string text)
{
IntPtr boxHwnd = GetDlgItem(hWnd, 114);
SendMessage(boxHwnd, WM_SETTEXT, 0, text);
}