我需要通过Handle在其他窗口中托管我的WPF UserControl。 我试过使用HwndSource:
var userControl = new MyUserControl();
var parameters = new HwndSourceParameters();
parameters.WindowStyle = 0x10000000 | 0x40000000;
parameters.SetPosition(5, 5);
parameters.SetSize(300, 300);
parameters.ParentWindow = parentWindowHwnd;
var src = new HwndSource(parameters);
src.RootVisual = userControl;
但在这种情况下,箭头和标签键不起作用。
如果我使用ElementHost,一切正常:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
var userControl = new UserControl1();
var elementHost = new ElementHost();
elementHost.Child = userControl;
elementHost.Left = 5;
elementHost.Top = 5;
elementHost.Width = 300;
elementHost.Height = 300;
SetParent(elementHost.Handle, parentWindowHwnd);
如何使用HwndSource获得完整功能?
答案 0 :(得分:0)
当您使用HwndSource时,您必须为Windows消息注册处理程序。
这可以通过电话来完成:
src.AddHook(this.messageHook);
钩子必须检查wm_getdlgcode消息。
private IntPtr messageHook(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
{
switch (msg)
{
case WmGetDlgCode:
{
handled = true;
return (IntPtr)(DlgcWantChars | DlgcWantTab | DlgcWantArrows | DlgcWantAllKeys);
}
}
return IntPtr.Zero;
}
通过Dlgc_WantChars,Dlgc_WantTab,Dlgc_WantArrows和Dlgc_WantAllKeys返回您需要的内容。
选中此消息和代码: http://msdn.microsoft.com/en-us/library/windows/desktop/ms645425(v=vs.85).aspx
private const int WmGetDlgCode = 0x0087;
private const int DlgcWantChars = 0x0080;
private const int DlgcWantTab = 0x0002;
private const int DlgcWantAllKeys = 0x0004;
private const int DlgcWantArrows = 0x0001;