【问题标题】:How to Set Focus Back To Calling Form After Starting A Process (Chrome)启动进程后如何将焦点设置回调用表单(Chrome)
【发布时间】:2020-06-29 15:15:47
【问题描述】:

我使用 Process.Start() 从我的程序中打开了一个 Chrome 浏览器窗口,但新打开的 chrome 浏览器选项卡覆盖了屏幕。但我希望我的应用程序在创建这些 chrome 浏览器选项卡后保持其焦点。

我将打开一个浏览器窗口,但有几个 chrome 选项卡显示不同的 URL。

这是我尝试过的,类似于How to set focus back to form after opening up a process (Notepad)? 的答案。但对我不起作用。

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd)

// this code will get called several times with a different URL in Arguments property

Process p = new Process();
p.StartInfo = new ProcessStartInfo("Chrome.exe");
p.StartInfo.CreateNoWindow = true;

p.StartInfo.Arguments = "https:/google.com";
p.Start();

p.WaitForInputIdle();
SetForegroundWindow(this.Handle);

【问题讨论】:

    标签: c# winforms process focus


    【解决方案1】:

    只是添加一个短暂的延迟似乎对我来说很好:

    private void button1_Click(object sender, EventArgs e)
    {
        OpenUrl("https:/google.com");
    }
    
    private async void OpenUrl(string url)
    {
        Process p = new Process();
        p.StartInfo = new ProcessStartInfo("Chrome.exe");
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.Arguments = url;
        p.Start();
        p.WaitForInputIdle();
        await Task.Delay(50);
        SetForegroundWindow(this.Handle);
    }
    

    【讨论】:

    • 非常感谢 Idle_Mind。它工作得很好,已经困扰了我几个月,:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多