从asp.net页面,通过ClickOnce部署启动.Net WinForms应用程序。在某个时刻,WinForm应用程序需要刷新启动它的网页。
我怎么能这样做呢?基于.Net的windows应用程序如何刷新已经在浏览器中打开的页面?
发布于 2012-05-02 19:51:05
要以一种健壮的方式做到这一点并不容易。例如,用户可能没有使用IE。
你唯一可以控制的东西就是你的web服务器,这对web页面和windows应用是通用的。
这个解决方案很复杂,但这是我能想到的唯一可行的方法。
1)让网页在windows应用程序运行之前打开到web服务器的长轮询连接。目前,SignalR在这方面得到了很好的报道。
2)让windows应用程序在想要更新网页时向服务器发送信号。
3)在服务器端,完成长轮询请求,向web浏览器发回信号。
4)在网页中,通过刷新页面来处理响应。
我说它很复杂!
发布于 2012-05-02 19:35:55
下面是完成所需操作的一些示例代码(仅包含相关部分):
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.的修改
https://stackoverflow.com/questions/10420023
复制