http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 12073|回复: 7

[综合] 关于webbrowser自动弹出的问题,百度很久找不到解决方法

[复制链接]
发表于 2013-3-13 16:30:04 | 显示全部楼层 |阅读模式
本帖最后由 yisa 于 2013-3-13 16:34 编辑

想做一款刷PV的工具,用httpwebrequest 只是下载HTML,不执行JS,无法达到刷PV的目的
无奈之下只有用webbrowser,结果发现,不能后台运行,一旦跳转网页就会自动弹出来跑到屏幕前方,就没法操作其他的了。

我自己试的几个方法
1、在加载完毕后最小化,还是会闪出来在最小化。
2、想动态创建webbrowser,但是动态加上Cookie给webbrowser在访问的方法更不会,而且具体动态创建会不会弹出也不清楚


求助老大解决方法,百度了很久都没解决!有什么办法让webbrowser打开网页不自动抢焦点,可以完全后台运行?


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-3-13 17:24:26 | 显示全部楼层
有两种方法解决,第一种你了解网站的URL规则,直接建一个方案去循环比如我的网站
http://www.sufeinet.com/thread-2140-1-1.html
http://www.sufeinet.com/thread-2141-1-1.html
http://www.sufeinet.com/thread-2142-1-1.html
这是这种格式的,那个只需要修改一下后面的数字就行,这样可以动态生成网址,不需要执行单击操作

第二种是你在加载页面之后把页面所有的 target="_blank"都替换为空,呵呵。你看看需要怎么处理吧,
或者还有其它的方式只是我现在没有见过,有空了帮你看看你先试试这咱看看行不行
 楼主| 发表于 2013-3-13 17:38:44 | 显示全部楼层
老大可能理解错了,我是想做一个可以刷网站PV的工具,暂时还没有考虑到多页面PV

下午我一直用的QQ空间测试,因为可以测试到访问记录来看看程序是否可以实现PV记录。

结果我发现,循环不断访问 "http://user.qzone.qq.com/123456/mood/"  会老弹出来winform到屏幕最顶层,无法后台运行,刚才在群里@☆心的启航☆ 的提醒下,我换了下www.baidu.com  却没有出现这个情况,应该是QQ空间某些js代码造成的。你可以测试,访问上面QQ空间的地址,焦点会一直抢在屏幕最前面。
发表于 2013-3-13 17:41:56 | 显示全部楼层
或者可以试试我的思路但是我的方法不一定全,可以安这个方法改进一下
希望得到解决后给大家分享一部分代码
[code=csharp]  //当有新的连接时
        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;

            //WebBrowser1.Navigate WebBrowser1.Document.activeelement.href
            webBrowser1.Navigate(webBrowser1.Document.ActiveElement.GetAttribute("href"));
        }[/code]
发表于 2013-3-13 17:58:25 | 显示全部楼层

            
    EDIT: complete question rewritten, I misunderstood original question
Let's generalize the problem: a control or component that you don't have control about, can call FlashWindow (the Win32 API function) to get attention from the user. You don't want that.
There are generally two solutions for this: use API hooking or Message hooking. Since API hooking is complex and involved, I'll present a solution for Message hooking.
FlashWindowMicrosoft doesn't explain in so many words what FlashWindow does. Unfortunately, it doesn't send a specific message (say WM_FLASH or similar), which would've made it easier to capture and annul this behavior. Instead, FlashWindow does three things:
  • It sets a system timer for the flashing intervals
  • It sends a WM_NCACTIVATE message for the first flash
  • It sends a WM_NCACTIVATE message when the timer expires (on receiving WM_SYSTIMER)
Depending on how the component calls FlashWindow, this can be indefinite, until another timeout occurs, until it has focus or just once. Each WM_NCACTIVATE message activates or deactivates the NC-zone (caption bar, button on taskbar). It doesn't change the input the focus.
ChallengeAny solution for preventing the flashing is a bit involved. The main challenges are:
  • the WM_SYSTIMER event is sent asynchronously with PostMessage and is not received by the WndProc method of the Form (it only processes synchronous messages)
  • the WM_NCACTIVATE messages are also used when the user clicks on the title bar or taskbar button to set input focus, simply canceling these messages will have unwanted side effects
  • FlashWindow will always flash at least once, regardless of the WM_SYSTIMER firing or not.
The WM_SYSTIMER message is undocumented. It has the value 0x0118 and is used internally by Windows to time such things as the blinking of the caret, the delay in a menu opening etc. Here it is used for the time between the flashes.
SolutionThe solution I present here is a basis for further development. It is not a complete solution, but it solves the issue in many cases. Place the following in your form code:
[code=csharp]protected override void WndProc(ref Message m)
{
    bool messageHandled = false;
    if (m.Msg == WM_NCACTIVATE)
    {
        // add logic here to determine user action, losing focus etc and set
        // messageHandled and m.Result only when user action is not the cause
        // of triggering WM_NCACTIVATE
        m.Result = IntPtr.Zero;
        messageHandled = true;
    }

    if(!messageHandled)
        base.WndProc(ref m);
}[/code]The above code already prevents flashing completely. You'll have to add some logic to change the title bar, because totally ignoring WM_NCACTIVATE means that the title bar will look active all the time, even when it isn't.
The following code gives you more control. You can use it to react to the flashing itself. Normally, a main window does not receive WM_SYSTIMER events so often, but you'll have to experiment whether you should make exceptions. It seems that for FlashWindow, the wParam is always set to 0xFFF8, but do experiment with it, as this is not documented anywhere.
[code=csharp]public class MyMessageFilter : IMessageFilter
{
    // an application can have many windows, only filter for one window at the time
    IntPtr FilteredHwnd = IntPtr.Zero;

    public MyMessageFilter(IntPtr hwnd)
    {
        this.FilteredHwnd = hwnd;
    }

    public bool PreFilterMessage(ref Message m)
    {
        if (this.FilteredHwnd == m.HWnd && m.Msg == WM_SYSTIMER)
            return true;     // stop handling the message further
        else
            return false;    // all other msgs: handle them
    }
}[/code]To activate this messagefilter, simply add the following line somewhere in your form load event:
Application.AddMessageFilter(new MyMessageFilter(this.Handle));The following constants should be placed at class level. They are used in both code sections above:
[code=csharp]public const UInt32 WM_SYSTIMER = 0x0118;public const UInt32 WM_NCACTIVATE = 0x86;[/code]ConclusionThough the problem itself is solvable, it is by far not easy. With the above handles, you should get quite far. Use the filter to prevent the flashing, but then the first "flash" still happens. Use the WinProc override to prevent the first one too, but add some logic to prevent your application from behaving too oddly (i.e.: always inactive title bar, or always active). You already have some code that you can combine with this to set some boolean flags.


发表于 2013-4-30 20:36:52 | 显示全部楼层
上面的看不懂。。没有中文的呐?
发表于 2013-11-22 16:54:36 | 显示全部楼层
我没懂苏菲大哥的好办法。 我以前只用API把弹出的窗口自动关掉。
发表于 2014-5-6 19:58:00 | 显示全部楼层
MS提供的WebBrowser控件是个单标签浏览器,容易出现你说说的问题,建议找一个多标签浏览器,弹出新窗口只是增加了一个新的Tab,加载完毕后close这个Tab就行了.
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-15 18:25

© 2014-2021

快速回复 返回顶部 返回列表