4

2つのWPFアプリケーションと1つのプロセスマネージャーがあり、最初のWPFアプリケーションから2番目のWPFアプリケーションに、またはその逆にデータを渡します。あるユースケースでは、最初のアプリケーションのウィンドウ(メインウィンドウ)を、モーダルモードの2番目のアプリケーションのウィンドウ(メインウィンドウ)の上に表示する必要があります。したがって、2番目のWPFアプリケーションのウィンドウが無効になり、最初のWPFアプリケーションのウィンドウの上に表示されます。必要な動作は、単一のWPFアプリケーションでモーダルモードでウィンドウを表示するのと同じです。別のWPFアプリケーションから1つのWPFアプリケーションのウィンドウにアクセスするにはどうすればよいですか?

Winformアプリケーションの場合、ウィンドウハンドル(intPtr)を別のアプリケーションに渡し、モーダルモードでウィンドウを表示しているときに、次のようなハンドルを使用します。

System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window)

WPFアプリケーションの場合、どのように同様のことが達成できますか?前もって感謝します。

4

2 に答える 2

4

===========================更新の開始===================== ================

コード:

using System.Windows; // Window, WindowStartupLocation
using System.Windows.Interop; // WindowInteropHelper
using System.Runtime.InteropServices; // DllImport
...

// Instantiate the owned WPF window
CenteredWindow cw = new CenteredWindow();
// Get the handle to the non-WPF owner window
IntPtr hWnd = ...

CenteredWindow cw = new CenteredWindow();

EnableWindow(hWnd, false); // disable parent window
try
{
    // Set the owned WPF window’s owner with the non-WPF owner window
    WindowInteropHelper helper = new WindowInteropHelper(cw);
    helper.Owner = hWnd;
    cw.ShowDialog();
}
finally
{
    EnableWindow(hWnd, true); // enable parent window
}

...
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool EnableWindow(IntPtr hwnd, bool enable);

@kamal- nayanのコメントにあるMSConnectLinkの助けを借りて、上記のようにコードを変更しました。これは私にとってはうまく機能します。

重要なのは、親ウィンドウを無効にすることです。モーダルダイアログを閉じたら、親ウィンドウを有効にします。

===========================更新の終了===================== ================

using System.Windows; // Window, WindowStartupLocation
using System.Windows.Interop; // WindowInteropHelper
...
// Instantiate the owned WPF window
CenteredWindow cw = new CenteredWindow();

// Get the handle to the non-WPF owner window
IntPtr ownerWindowHandle = ...; // Get hWnd for non-WPF window

// Set the owned WPF window’s owner with the non-WPF owner window
WindowInteropHelper helper = new WindowInteropHelper(cw);
helper.Owner = ownerWindowHandle;
cw.ShowDialog();

これは私が見つけた唯一の解決策です。これは実際のモーダルではありません。つまり、親をアクティブ化することはできますが、子ウィンドウが親の上にあることは良いことです。

http://blogs.msdn.com/b/wpfsdk/archive/2007/04/03/centering-wpf-windows-with-wpf-and-non-wpf-owner-windows.aspx

于 2014-03-27T02:54:16.847 に答える
0
_parameters = new HwndSourceParameters("myWindow");

_parameters.WindowStyle = WindowStyles.WS_SYSMENU | WindowStyles.WS_VISIBLE | WindowStyles.WS_CAPTION | WindowStyles.WS_CHILD | WindowStyles.WS_POPUP;
_parameters.SetPosition(50, 50);

_parameters.ParentWindow = ParentWindowHandle;           
_hwndSource = new HwndSource(_parameters);

_hwndSource.SizeToContent = SizeToContent.WidthAndHeight;
_hwndSource.RootVisual = modalWindowContent;

このようにして、あるプロセスのウィンドウを他のプロセスのウィンドウへのモーダルとして表示することができました。

于 2014-03-27T11:07:58.013 に答える