【问题标题】:C# - Sending Keys to another Window without focus [duplicate]C# - 将键发送到另一个没有焦点的窗口[重复]
【发布时间】:2016-12-12 15:33:33
【问题描述】:

我想制作一个应用程序,允许我将键或一些消息发送到未聚焦的窗口。

我的代码允许我在焦点集中时向窗口发送消息:

string processName = "notepad";
Process[] processList = Process.GetProcesses();
foreach (Process P in processList)
{
    if (P.ProcessName.Equals(processName))
    {
        IntPtr edit = P.MainWindowHandle;
        SendKeys.Send("Hello");
    }
}

我希望我能找到一些帮助。

【问题讨论】:

  • 找到窗口句柄,然后给它发送消息
  • 我想将文本发送到非焦点窗口。

标签: c#


【解决方案1】:

这是一个可能的解决方法:

[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);
string processName = "notepad";

    Process [] notepads=Process.GetProcessesByName("notepad");
    if(notepads.Length==0)return;            
    if (notepads[0] != null)
    {
        IntPtr child= FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
        SendMessage(child, 0x000C, 0, "Hello");
    }

【讨论】:

  • 不起作用。无法从程序集 'processbackground,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null' 加载类型 'processbackground.Form1',因为方法 'SendMessage' 没有实现(没有RVA)。
猜你喜欢
  • 2020-09-14
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-16
  • 1970-01-01
  • 2019-06-10
相关资源
最近更新 更多