首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从.Net WinForms应用程序以编程方式刷新浏览器的页面

从.Net WinForms应用程序以编程方式刷新浏览器的页面
EN

Stack Overflow用户
提问于 2012-05-03 03:01:04
回答 2查看 5.4K关注 0票数 4

从asp.net页面,通过ClickOnce部署启动.Net WinForms应用程序。在某个时刻,WinForm应用程序需要刷新启动它的网页。

我怎么能这样做呢?基于.Net的windows应用程序如何刷新已经在浏览器中打开的页面?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-03 03:51:05

要以一种健壮的方式做到这一点并不容易。例如,用户可能没有使用IE。

你唯一可以控制的东西就是你的web服务器,这对web页面和windows应用是通用的。

这个解决方案很复杂,但这是我能想到的唯一可行的方法。

1)让网页在windows应用程序运行之前打开到web服务器的长轮询连接。目前,SignalR在这方面得到了很好的报道。

2)让windows应用程序在想要更新网页时向服务器发送信号。

3)在服务器端,完成长轮询请求,向web浏览器发回信号。

4)在网页中,通过刷新页面来处理响应。

我说它很复杂!

票数 3
EN

Stack Overflow用户

发布于 2012-05-03 03:35:55

下面是完成所需操作的一些示例代码(仅包含相关部分):

代码语言:javascript
运行
复制
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // Get a handle to an application window.
        [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
        public static extern IntPtr FindWindow(string lpClassName,
            string lpWindowName);

        // Activate an application window.
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);


        private void RefreshExplorer()
        {
            //You may want to receive the window caption as a parameter... 
            //hard-coded for now.
            // Get a handle to the current instance of IE based on window title. 
            // Using Google as an example - Window caption when one navigates to google.com 
            IntPtr explorerHandle = FindWindow("IEFrame", "Google - Windows Internet Explorer");

            // Verify that we found the Window.
            if (explorerHandle == IntPtr.Zero)
            {
                MessageBox.Show("Didn't find an instance of IE");
                return;
            }

            SetForegroundWindow(explorerHandle );
            //Refresh the page
            SendKeys.Send("{F5}"); //The page will refresh.
        }
    }
}

注意:代码是对this MSDN example.的修改

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10420023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档