我同意马特的观点。设置“MaximizedBounds”不是一个好主意。正如在窗口最大化/未最大化时的事件中所写,我将覆盖 WndProc 方法。在那里,您可以自己处理从窗口接收到的不同命令。
要做的主要事情是为“SC_MAXIMIZE”-window 命令编写自己的代码(如上面引用的文章中所写)。在那里您可以手动设置表单的大小,例如在这种情况下,表单不会真正最大化。实际上它是否仍然处于正常的WindowState。为了防止用户改变这个状态,你需要“捕捉”一些其他的窗口命令。
重写的 WndProc 方法可能是这样的:
protected override void WndProc(ref Message m)
{
if(m.Msg == 0x0112) // WM_SYSCOMMAND
{
if(m.WParam == new IntPtr(0xF012)) //TITLE_CLICK_ONCE
{
// catch, this command can occur, when form starts to move
}
if(m.WParam == new IntPtr(0xF001) // RESIZE_ON_EDGE
|| m.WParam == new IntPtr(0xF002)
|| m.WParam == new IntPtr(0xF003)
|| m.WParam == new IntPtr(0xF004)
|| m.WParam == new IntPtr(0xF005)
|| m.WParam == new IntPtr(0xF006)
|| m.WParam == new IntPtr(0xF007)
|| m.WParam == new IntPtr(0xF008))
{
// catch the resizing
}
if(m.WParam == new IntPtr(0xF032)) // SECOND_CLICK_ON_TITLEBAR
{
// catch. causes a maximization (or resuming to normal window-mode)
}
if(m.WParam == new IntPtr(0xF030)) //SC_MAXIMIZE
{
// the actual point, where to enter your code
// this command occurs, when the "Maximize"-button is pressed
}
}
// maybe abort calling of the base-method at specified window-commands,
// when you want to make your own code by simply "return;"
base.WndProc(ref m);
}