select window 我想发送键盘输入吗

Cant select the window i want to sent keyboard inputs

我尝试激活 window 以使用

发送键盘输入
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

private void button1_Click(object sender, EventArgs e)
{
    IntPtr calcWindow = FindWindow(null, "Calculator");

    if (SetForegroundWindow(calcWindow))
        SendKeys.Send("10{+}10=");
}

我是 c# 的新手,我看到我需要输入任务栏上显示的确切名称,但我尝试将键盘事件发送到 DOSBox,the name of the window i want to select 有一个我试图写的奇怪名称多次但我没有得到正确的东西,你知道我如何浏览已经打开的 windows 和 select 这个或者我怎样才能得到确切的名称

您可以使用System.Diagnostics.Process 来查找进程。你可以通过它的 ProcessName 找到进程,然后得到它的 MainWindowHandle.

[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

private void button1_Click(object sender, EventArgs e)
{
    // find the process by its name, this is the process name and is not the window name
    System.Diagnostics.Process process = System.Diagnostics.Process.GetProcesses()
        .FirstOrDefault(p => p.ProcessName.Equals("DOSBox"));
            
    if(process != null)
    {
        IntPtr calcWindow = process.MainWindowHandle;
        SetForegroundWindow(calcWindow);
        if (SetForegroundWindow(calcWindow))
            SendKeys.SendWait("10{+}10=");
    }           
}