HwndSource调整Win32窗口大小时托管WPF用户控件的大小? [英] HwndSource resize hosted wpf usercontrol when Win32 window resized?
问题描述
大家好:
我有一个Win32窗口,该窗口以rootvisual身份托管WPF用户控件,我想在win32大小更改时重新调整usercontrol的大小.我尝试了以下代码,但无法正常工作.我错过了什么吗?
I have a Win32 window that hosts WPF usercontrol as rootvisual, I wanted to resize the usercontrol when the win32 size changed. I have tried the following code but could not get it working. Did I missing something?
//从我的C#.net应用程序创建主机Win32窗口
//create the host win32 window from my C#.net appl
公共无效CreateHostedWindow()
public void CreateHostedWindow()
{
const int WS_VISIBLE = 0x10000000;
const int WS_CHILD = 0x40000000;
const int WS_VISIBLE = 0x10000000;
const int WS_CHILD = 0x40000000;
IntPtr parentHandle = nativecallstostowinwin32window();
IntPtr parentHandle = nativecallstocreatewin32window();
...
HwndSourceParameters参数=新的HwndSourceParameters();
parameters.WindowStyle = WS_VISIBLE | WS_CHILD;
parameters.SetPosition(0,0);
parameters.SetSize(initWidth,initHeight);
parameters.ParentWindow = parentHandle;
HwndSource src =新的HwndSource(参数);
HwndSourceParameters parameters = new HwndSourceParameters();
parameters.WindowStyle = WS_VISIBLE | WS_CHILD;
parameters.SetPosition(0,0);
parameters.SetSize(initWidth, initHeight);
parameters.ParentWindow = parentHandle;
HwndSource src = new HwndSource(parameters);
MyUserControl控件=新的MyUserControl();
MyUserControl control = new MyUserControl();
src.RootVisual =控件;
src.RootVisual = control;
}
在我的用户控件中,我有:
in my usercontrol I have:
公共局部类MyUserControl:UserControl
public partial class MyUserControl: UserControl
{
私人HwndSource _hwndSource;
private HwndSource _hwndSource;
const int WM_SIZE = 0x0005;
const int WM_SIZE = 0x0005;
公共MyUserControl()
{
InitializeComponent();
this.Loaded + = new RoutedEventHandler(MyUserControl_Loaded);
}
public MyUserControl()
{
InitializeComponent();
this.Loaded+=new RoutedEventHandler(MyUserControl_Loaded);
}
私人void MyUserControl_Loaded(对象发送者,RoutedEventArgs e)
{
_hwndSource = HwndSource.FromVisual(this)作为HwndSource;
//_ hwndSource = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
_hwndSource.AddHook(new HwndSourceHook(WndProc));
}
private void MyUserControl_Loaded(object sender, RoutedEventArgs e )
{
_hwndSource = HwndSource.FromVisual(this) as HwndSource;
//_hwndSource = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
_hwndSource.AddHook(new HwndSourceHook(WndProc));
}
私有静态IntPtr WndProc(IntPtr hwnd,int msg,IntPtr wParam,IntPtr lParam,ref bool处理)
{
if(msg == WM_SIZE)
{
MessageBox.Show("WM_SIZE MESSAGE");
已处理= true;
}
返回IntPtr.Zero;
}
private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if(msg == WM_SIZE)
{
MessageBox.Show ("WM_SIZE MESSAGE");
handled = true;
}
return IntPtr.Zero;
}
}
它显示了win32主机正确的用户控件,但是当更改win32大小(通过代码)时,WM_SIZE消息永远不会到达用户控件.
It shows the win32 host the usercontrol correctly, but when the win32 size is changed (through code), the WM_SIZE message never get to the UserControl.
我了解了有关在http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cc6297db-6ed9-4d68-abe2-47769e06d93a中找到用户控件的窗口句柄的信息.基本上,用户控件没有窗口句柄-它将在元素树中查找 它可以找到的第一个Win32窗口-在我的情况下是父窗口处理程序本身.
I read about find the window handle of a user control in http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cc6297db-6ed9-4d68-abe2-47769e06d93a. basically, the user control does not have a window handle- it will walk up the element tree looking for the first Win32 Window it can find - which in my case is the parent window handler itself.
非常感谢
-rockdale
-rockdale
推荐答案
找出原因,如果有人想知道相同的问题,请在此处张贴.
Found out why, post here if somebody wondering the same problem.
当您将用户控件托管在本机win32窗口中时,它没有窗口句柄. _hwndSource.Handle与本地win32窗口的Hwnd不同.它可能是Interop添加的另一层的句柄吗? (我不知道).
The usercontrol does not have a window handle, when you host it in a native win32 window. the _hwndSource.Handle is not the same as the Hwnd of the native win32 window. It might be the handle of another layer that the Interop added? (I do not know).
仅当HwndSource满足以下所有三个条件时,才会触发WM_SIZE事件
The WM_SIZE event will be triggered only if the HwndSource satisfy all of the following 3 conditions
1.设置HwndSource.SizeToContent = SizeToContent.WidthAndHeight;
1. set the HwndSource.SizeToContent = SizeToContent.WidthAndHeight;
2.通过本地MoveWindow调用调整本地Win32窗口的大小
2. Resize the native win32 window by native MoveWindow call
3.通过设置用户控件的宽度和高度来调整其大小.
3. Resize the usercontrol by set its width and height.
然后WM_SIZE消息正在工作.
then now the WM_SIZE message is working.
这篇关于HwndSource调整Win32窗口大小时托管WPF用户控件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!