put wpf on a win hdl
i get a winHdl 开发者_运维技巧(which is a winForm) from a native c++ application.
Plugin implementation in c#:
public int Create(int hParentWnd ...){ ... // here i want to put wpf on this hParentWnd }
Now i want to put my WPF user control in this window. How can i do this? Any code snippets available?
thanks
You don't need WinForms to do this. You can use HwndSource directly:
public void CreateControl(IntPtr hParentWnd)
{
_userControl = new MyWPFUserControl();
var parameters =
new HwndSourceParameters("", _initialWidth, _initialHeight)
{
ParentWindow = (IntPtr)hwndParent,
WindowStyle = ..., // Win32 style bits
ExtendedWindowStyle = ..., // Win32 ex style bits
})
_hwndSource =
new HwndSource(parameters) { RootVisual = _userControl };
}
public void DestroyControl()
{
_hwndSource.Destroy();
}
The parameter to CreateControl really ought to be IntPtr, not 'int', but you can set it to 'int' if you need to and just cast it to IntPtr later on.
If you want to return the handle of the newly created window, return _hwndSource.Handle.
ElementHost is what you are looking for, it's a WinForms control that hosts WPF content.
http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.elementhost.aspx
Ok, i figured it out. Here is the result:
int WS_CHILD = 0x40000000;
int WS_VISIBLE = 0x10000000;
// paint wpf on int hParentWnd
myWindow = new MyWpfWindow();
// First it creates an HwndSource, whose parameters are similar to CreateWindow:
HwndSource otxHwndSource = new HwndSource(
0, // class style
WS_VISIBLE | WS_CHILD, // style
0, // exstyle
10, 10, 200, 400,
"MyWpfWindow", // NAME
(IntPtr)hParentWnd // parent window
);
otxHwndSource.RootVisual = ...;
// following must be set to prcess key events
myHwndSource.AddHook(new HwndSourceHook(ChildHwndSourceHook));
with the hook:
private IntPtr ChildHwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == (int)(0x0087)) //WM_GETDLGCODE
{
handled = true;
int returnCode = (int)(0x0080);
return new IntPtr(returnCode);
}
return System.IntPtr.Zero;
}
thanks to Ray Burns!
精彩评论