我正在尝试使用sendkeys,但是将其发送到非焦点程序。例如,我想使用sendkeys来记事本 - 无标题,而不是它被聚焦。对不起,如果不清楚,我会详细说明。我目前的代码是:
string txt = Regex.Replace(richTextBox3.Text, "[+^%~()]", "{$0}");
System.Threading.Thread.Sleep(1000);
SendKeys.Send(txt + "{ENTER}");
如果我使用SendMessage,是否有人愿意向我展示我应该做些什么的例子?我发现网上找不到任何对我的问题非常有用的东西。
答案 0 :(得分:2)
我从previous SO answer找到以下代码。 PostMessage API将Windows消息发送到特定的Windows句柄。下面的代码将导致所有Internet Explorer实例触发按键事件,而不会给出焦点,模拟按下了F5键。可以找到其他密钥代码列表here。
static class Program
{
const UInt32 WM_KEYDOWN = 0x0100;
const int VK_F5 = 0x74;
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
[STAThread]
static void Main()
{
while(true)
{
Process [] processes = Process.GetProcessesByName("iexplore");
foreach(Process proc in processes)
PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_F5, 0);
Thread.Sleep(5000);
}
}
}
答案 1 :(得分:0)
我花了一段时间才为自己弄明白
另外here 虚拟密钥代码
的有用列表 [DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
const int WM_SYSKEYDOWN = 0x0104;
const int VK_RETURN = 0x0D;
IntPtr WindowToFind = FindWindow(null, "Notepad - Untitled"); // Window Titel
PostMessage(WindowToFind, WM_SYSKEYDOWN, VK_RETURN, 0);
}