我打电话HwndSource.AddHook()是为了让消息在我的 WPF 窗口中处理。我想知道我是否需要HwndSource.RemoveHook()在窗口被销毁时调用 - 这个窗口不是 MainWindow。如果是这样,称它为 ( Closing()) 的正确位置是什么?当窗口被销毁时,似乎 Hooks 被删除了。
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (NativeMethods.UWM_SHOWMYAPP == msg)
{
if (this.WindowState == WindowState.Minimized)
this.WindowState = WindowState.Normal;
this.Activate();
handled = true;
}
return IntPtr.Zero;
}
void StatusWindow_Closing(object sender, CancelEventArgs e)
{
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.RemoveHook(WndProc);
}
在这里删除时我又得到了HwndSource。这个可以吗?还是我应该保留该source对象Add()并将其用于Remove()?
蓝山帝景
相关分类