在上一个问题中,我询问了how to send text to Notepad。它极大地帮助了我。对于第2部分,这是相同的应用mIRC的简化版本:

[DllImport("User32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

IntPtr mainHandle = FindWindow("mIRC", null);
IntPtr serverHandle = FindWindowEx(mainHandle, new IntPtr(0), "MDIClient", null);
IntPtr chanHandle = FindWindowEx(serverHandle, new IntPtr(0), "mIRC_Channel", null);
IntPtr editHandle = FindWindowEx(chanHandle, new IntPtr(0), "Edit", null);

SendMessage(editHandle, 0x000C, 0, textBox1.Text);

对我来说这似乎是正确的,除了它不起作用!窗口名称是否不正确(MDIClient,mIRC_Channel和Edit)?这些是我在Google网站上通过搜索“FindWindowEx mIRC”找到的值。

1.)我在上面做错了什么?

2.)作为引用,通常是否有一种简单的方法来查找所有与FindWindowEx()一起使用的窗口名称?

最佳答案

这段代码对我有用(镜像6.31):

IntPtr mainHandle = FindWindow("mIRC", null);
IntPtr serverHandle = FindWindowEx(mainHandle, new IntPtr(0), "MDIClient", null);
IntPtr chanHandle = FindWindowEx(serverHandle, new IntPtr(0), "mIRC_Channel", null);
IntPtr editHandle = FindWindowEx(chanHandle, new IntPtr(0), "richEdit20A", null);
SendMessage(editHandle, 0x000C, 0, "Hello World");

注意更改的窗口类(richedit20A而不是edit)。刚刚使用Spy++找到了正确的类。

至于窗口句柄,一种可能性是使用EnumWindowsEnumChildWindows API。

关于c# - 如何在C#/Win32中编写文本并将其发送到mIRC?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/523669/

10-13 06:46