如何使用不可见的 System.Windows.Forms.WebBrowser? [英] How to use an invisible System.Windows.Forms.WebBrowser?

查看:28
本文介绍了如何使用不可见的 System.Windows.Forms.WebBrowser?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在没有容器表单的情况下运行它,但是 DocumentCompleted 事件没有触发.

I tried to run it without a container form, but the DocumentCompleted event doesn't fire.

我尝试在不透明度设置为 0% 的表单中运行它,但该过程并未完全隐藏,因为当用户使用 Alt 时它会出现在用户面前+Tab

I tried to run it in a Form with opacity set to 0% but the process isn't completely hidden, since it appears to the user when he uses Alt+Tab

我不介意进程是否出现在任务管理器上.

I don't mind if the proccess appears on the Task Manager though.

推荐答案

要防止窗口显示,请将此代码粘贴到您的表单中:

To prevent the window from being shown, paste this code into your form:

    protected override void SetVisibleCore(bool value) {
        if (!this.IsHandleCreated) {
            CreateHandle();
            value = false;
        }
        base.SetVisibleCore(value);
    }

请注意,Load 事件在您明确使表单可见之前不会运行,因此请移动您在 if 语句中获得的任何代码.

Beware that the Load event won't run until you explicitly make your form visible so move any code you've got there inside the if statement.

未运行 DocumentCompleted 事件通常是由于未运行消息循环 (Application.Run) 造成的.WebBrowser 需要一个线程和一个标记为 [STAThread] 的线程,以便触发其事件.消息循环对于 COM 组件非常重要.

Not getting the DocumentCompleted event to run is usually caused by not running a message loop (Application.Run). WebBrowser requires one, and a thread that's marked with [STAThread], in order to fire its events. The message loop is very important for COM components.

防止隐形形式偷取焦点也很重要,代码如下:

Is it also important to prevent the invisible form from stealing focus, with the code below:

protected override bool ShowWithoutActivation
{
    get { return true; } // prevents form creation from stealing focus
}

form1.Enabled = false; // prevents inner controls from stealing focus

这篇关于如何使用不可见的 System.Windows.Forms.WebBrowser?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆