Thursday, 19 November 2015

C# WPF Window to be Dialogue to Win32

The Windows Form Method

This is how its done on Windows Forms forms, fairly straight forward.

//apphandle  is the application handle for your Win32 application
IntPtr windowHandle = new IntPtr(Convert.ToInt32(apphandle));
WindowHandleWrapper windowWrapper = new WindowHandleWrapper(windowHandle);
MyWinForm = new MyForm();


But WPF forms don't support 

public DialogResult ShowDialog(IWin32Window owner);

Doing this for WPF Forms

You do this by setting the owner of the WPF window/form.

// apphandle  is the application handle for your Win32 application
IntPtr FormHandle = new IntPtr(Convert.ToInt32(apphandle));

// Create new WPF Form
var app = new MyWpfWindow();

// New Helper 
var helper = new WindowInteropHelper(app);

// Point the FormHandle to owner of the 
helper.Owner = FormHandle;

Easy eh ?  

No comments:

Post a Comment

Comments are welcome, but are moderated and may take a wee while before shown.