asp.net ページから、ClickOnce 配置を介して、.Net WinForms アプリケーションが開始されます。ある時点で、WinForm アプリケーションは開始元の Web ページを更新する必要があります。
どうすればこれを行うことができますか?.Net ベースの Windows アプリケーションは、ブラウザーで既に開いているページをどのように更新できますか?
これを堅牢な方法で行うのは簡単ではありません。たとえば、ユーザーが IE を使用していない可能性があります。
あなたが制御し、Web ページと Windows アプリに共通する唯一のものは Web サーバーです。
この解決策は複雑ですが、私が考えることができる唯一の方法です。
1) Windows アプリを実行する前に、Web ページを取得して、Web サーバーへのロング ポーリング接続を開きます。現時点では、SignalR はこれについてよく報道されています。
2) Windows アプリが Web ページを更新するときに、サーバーに信号を送信するようにします。
3) サーバーで、ロングポーリング要求を完了し、信号を Web ブラウザーに送り返します。
4) Web ページで、ページを更新して応答を処理します。
私はそれが複雑だと言いました!
必要なことを行うためのサンプルコードを次に示します (関連する部分のみ)。
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.
}
}
}
注: コードは、この MSDN の例を変更したものです。