我在WindowsFormsHost中有一個Windows窗體地圖,我需要它用窗口調整大小。如何知道WindowsFormsHost何時在WPF中調整大小?
我只是不確定要聽什麼事,要做到這一點。我需要地圖只在鼠標上移時重新調整大小,否則會延遲,並且在您調整窗口大小時慢慢嘗試繪製自己一百萬次。
我在WindowsFormsHost中有一個Windows窗體地圖,我需要它用窗口調整大小。如何知道WindowsFormsHost何時在WPF中調整大小?
我只是不確定要聽什麼事,要做到這一點。我需要地圖只在鼠標上移時重新調整大小,否則會延遲,並且在您調整窗口大小時慢慢嘗試繪製自己一百萬次。
等待一個定時器是一個非常非常糟糕的主意,很簡單,它是一種啓發式的方法,你在猜測調整操作完成的時間。
一個更好的主意是從WindowsFormsHost
派生一個類並覆蓋WndProc
方法,處理WM_SIZE
消息。這是大小操作完成時發送到窗口的消息(與在此過程中發送的WM_SIZING
相反)。
您還可以處理WM_SIZING
消息,並且當您收到此消息時不會調用基本實現WndProc
,以防止消息被處理並讓地圖重繪本身。
有關Control
類WndProc
方法的文檔演示如何重寫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;
每建議在這裏:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8453ab09-ce0e-4e14-b30b-3244b51c13c4
他們建議使用計時器,每次觸發SizeChanged事件時,只需重新啓動計時器即可。這樣,定時器不會一直調用「Tick」來檢查大小是否發生了變化 - 定時器只會在每次更改大小時關閉。也許不太理想,但我不必處理任何低級窗口的東西。這種方法也適用於任何wpf用戶控件,而不僅僅是wpf窗口。
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
//...
}
我刪除了我的答案,我並沒有試圖假設你應該一直運行計時器。無論如何,我對這種方法很感興趣。任何鏈接或樣本? – bendewey 2009-02-05 01:07:16