我想取消自然最小化行为并改为改变WPF表单大小.
我有一个Window_StateChanged的解决方案,但它看起来不那么好 - 窗口首先最小化然后跳回并进行大小更改.有没有办法实现这个目标?我用Google搜索了Window_StateChanging但是无法弄清楚,某些我不想使用的外部库.
这就是我的意思:
private void Window_StateChanged(object sender, EventArgs e)
{
switch (this.WindowState)
{
case WindowState.Minimized:
{
WindowState = System.Windows.WindowState.Normal;
this.Width = 500;
this.Height = 800;
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢,
EP
Jay*_*ggs 10
你需要在表单触发前拦截最小化命令Window_StateChanged,以避免你看到的最小化/恢复舞蹈.我认为最简单的方法是让你的表单监听Windows消息,当收到最小化命令时,取消它并调整表单大小.
SourceInitialized在表单构造函数中注册事件:
this.SourceInitialized += new EventHandler(OnSourceInitialized);
Run Code Online (Sandbox Code Playgroud)
将这两个处理程序添加到您的表单:
private void OnSourceInitialized(object sender, EventArgs e) {
HwndSource source = (HwndSource)PresentationSource.FromVisual(this);
source.AddHook(new HwndSourceHook(HandleMessages));
}
private IntPtr HandleMessages(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
// 0x0112 == WM_SYSCOMMAND, 'Window' command message.
// 0xF020 == SC_MINIMIZE, command to minimize the window.
if (msg == 0x0112 && ((int)wParam & 0xFFF0) == 0xF020) {
// Cancel the minimize.
handled = true;
// Resize the form.
this.Width = 500;
this.Height = 500;
}
return IntPtr.Zero;
}
Run Code Online (Sandbox Code Playgroud)
我怀疑这是你希望避免的方法,但归结为我所展示的代码并不太难实现.
基于此SO问题的代码.
| 归档时间: |
|
| 查看次数: |
3130 次 |
| 最近记录: |