我在WindowsFormsHost中有一个Windows窗体地图,我需要它来调整窗口大小。
我只是不确定要听哪个事件,这样做。我需要地图只在鼠标启动时调整大小,否则它会滞后,当你非常缓慢地调整窗口大小时,它会尝试绘制一百万次。
答案 0 :(得分:7)
等待计时器是一个非常非常糟糕的主意,很简单,这是一种启发式方法,你猜测何时完成了调整大小操作。
更好的想法是从WindowsFormsHost
派生一个类并覆盖WndProc
方法,处理WM_SIZE
消息。这是在大小操作完成时发送到窗口的消息(与在此过程中发送的WM_SIZING
相反)。
您还可以处理WM_SIZING
消息,并在收到此消息时不调用WndProc
的基本实现,以防止消息被处理并让地图在所有这些时间重绘。
WndProc
课程中Control
方法的文档显示了如何覆盖WndProc
方法:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc(VS.71).aspx
即使它是针对不同的类,它也是完全相同的原则。
此外,您需要WM_SIZING
和WM_SIZE
常量的值,您可以在此处找到:
http://www.pinvoke.net/default.aspx/Enums/WindowsMessages.html
请注意,您不需要上面链接中的所有内容,只需要这两个值的声明:
/// <summary>
/// The WM_SIZING message is sent to a window that
/// the user is resizing. By processing this message,
/// an application can monitor the size and position
/// of the drag rectangle and, if needed, change its
/// size or position.
/// </summary>
const int WM_SIZING = 0x0214;
/// <summary>
/// The WM_SIZE message is sent to a window after its
/// size has changed.
/// </summary>
const int WM_SIZE = 0x0005;
答案 1 :(得分:-3)
根据这里的建议:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8453ab09-ce0e-4e14-b30b-3244b51c13c4
他们建议使用计时器,每次触发SizeChanged事件时,只需重新启动计时器即可。这样,定时器不会一直调用“Tick”来检查大小是否已经改变 - 定时器只在每次改变尺寸时熄灭一次。也许不太理想,但我不必处理任何低级别的Windows东西。这种方法也适用于任何wpf用户控件,而不仅仅是wpf windows。
public MyUserControl()
{
_resizeTimer.Tick += _resizeTimer_Tick;
}
DispatcherTimer _resizeTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 1500), IsEnabled = false };
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
_resizeTimer.IsEnabled = true;
_resizeTimer.Stop();
_resizeTimer.Start();
}
int tickCount = 0;
void _resizeTimer_Tick(object sender, EventArgs e)
{
_resizeTimer.IsEnabled = false;
//you can get rid of this, it just helps you see that this event isn't getting fired all the time
Console.WriteLine("TICK" + tickCount++);
//Do important one-time resizing work here
//...
}