C# 窗口最大化/未最大化时的事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1295999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Event when a window gets maximized/un-maximized
提问by Michael Stum
Is there an event that is fired when you maximize a Form or un-maximize it?
当您最大化或取消最大化表单时,是否会触发事件?
Before you say Resize
or SizeChanged
: Those get only fired if the Size
actually changes. If your window happens to be equal in size to the maximized window, they do not fire. Location looks like the next best bet, but that again feels like gambling on a coincidence.
在你说Resize
or之前SizeChanged
:只有在Size
实际发生变化时才会被解雇。如果您的窗口恰好与最大化窗口的大小相等,则它们不会触发。位置看起来是下一个最好的选择,但这又感觉像是在赌巧合。
采纳答案by Reed Copsey
You can do this by overriding WndProc:
您可以通过覆盖 WndProc 来做到这一点:
protected override void WndProc( ref Message m )
{
if( m.Msg == 0x0112 ) // WM_SYSCOMMAND
{
// Check your window state here
if (m.WParam == new IntPtr( 0xF030 ) ) // Maximize event - SC_MAXIMIZE from Winuser.h
{
// THe window is being maximized
}
}
base.WndProc(ref m);
}
This should handle the event on any window. SC_RESTORE
is 0xF120
, and SC_MINIMIZE
is 0XF020
, if you need those constants, too.
这应该处理任何窗口上的事件。 SC_RESTORE
is0xF120
和SC_MINIMIZE
is 0XF020
,如果你也需要这些常量的话。
回答by Matt
If there's no obvious event to listen for, you're probably going to need to hook into the Windows API and catch the appropriate message (Google turns up that you'll want to intercept the WM_SYSCOMMAND message: http://www.codeguru.com/forum/archive/index.php/t-234554.html).
如果没有明显的事件要侦听,则您可能需要连接到 Windows API 并捕获适当的消息(Google 发现您要拦截 WM_SYSCOMMAND 消息:http://www.codeguru。 com/forum/archive/index.php/t-234554.html)。
回答by Lorenzo Melato
Another little addition in order to check for the restore to the original dimension and position after the maximization:
为了检查最大化后恢复到原始尺寸和位置的另一个小补充:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
// WM_SYSCOMMAND
if (m.Msg == 0x0112)
{
if (m.WParam == new IntPtr(0xF030) // Maximize event - SC_MAXIMIZE from Winuser.h
|| m.WParam == new IntPtr(0xF120)) // Restore event - SC_RESTORE from Winuser.h
{
UpdateYourUI();
}
}
}
Hope this help.
希望这有帮助。
回答by 816-8055
I had the same problem, and I could solve it without overriding. Because I have a PictureBoxin dock mode "Fill" I could use it's SizeChangedevent, which fired also on maximizing the window.
我有同样的问题,我可以在不覆盖的情况下解决它。因为我在停靠模式“填充”中有一个PictureBox,所以我可以使用它的SizeChanged事件,该事件也在最大化窗口时触发。
回答by TheFlash
Suprising that no one mentioned the inbuilt .NET method.
令人惊讶的是,没有人提到内置的 .NET 方法。
This way you don't need to override the Window Message Processing handler.
这样您就不需要覆盖窗口消息处理处理程序。
It even captures maximize/restore events caused by double-clicking the window titlebar, which the WndProc method does not.
它甚至可以捕获由双击窗口标题栏引起的最大化/恢复事件,而 WndProc 方法则没有。
Copy this in and link it to the "Resize" event handler on the form.
将其复制并链接到表单上的“调整大小”事件处理程序。
FormWindowState LastWindowState = FormWindowState.Minimized;
private void Form1_Resize(object sender, EventArgs e) {
// When window state changes
if (WindowState != LastWindowState) {
LastWindowState = WindowState;
if (WindowState == FormWindowState.Maximized) {
// Maximized!
}
if (WindowState == FormWindowState.Normal) {
// Restored!
}
}
}
回答by Hobis
' Great tip. So if it helps to VisualBasic In Code
Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_MAXIMIZE As Integer = &HF030
' # WndProcess ????
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg.Equals(WM_SYSCOMMAND) Then
If (m.WParam.ToInt32.Equals(SC_MAXIMIZE)) Then
Me.p_FullScreen()
Return
End If
End If
MyBase.WndProc(m)
End Sub
回答by Eske Rahn
I'm a newbie here so comments not allowed, but this IS a comment to the clean answer by GeoTarget:
我是这里的新手,因此不允许发表评论,但这是对GeoTarget干净答案的评论:
The first line OUGHT to be slightly changed to nullable, to catch if the form is started Minimized:
第一行应该稍微更改为可空,以捕获表单是否已启动 最小化:
FormWindowState? LastWindowState = null;
And a banal suggestion: Move the assignment of LastWindowState to afterthe "if"s, so the user can easily check not only what you go to, but also what it came from:
还有一个平庸的建议:将 LastWindowState 的分配移到“if”之后,这样用户不仅可以轻松检查您要去的内容,还可以轻松检查它的来源:
FormWindowState? LastWindowState = null;
private void Form1_Resize(object sender, EventArgs e) {
// When window state changes
if (WindowState != LastWindowState) {
if (WindowState == FormWindowState.Maximized) {
// Maximized!
}
if (WindowState == FormWindowState.Normal) {
// Restored!
}
LastWindowState = WindowState;
}
}
回答by Gabriel Marius Popescu
I believe the code is even simpler than that. You don't need to save the lastState because the WindowState is checked anytime when the event is fired.
我相信代码比那更简单。您不需要保存 lastState,因为在触发事件时会随时检查 WindowState。
private void MainForm_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Maximized)
{
spContainer.SplitterDistance = 1000;
}
if (WindowState == FormWindowState.Normal)
spContainer.SplitterDistance = 500;
}
回答by Track
I hope this part of code will be useful.
我希望这部分代码会有用。
if (m.Msg == User32.WM_WINDOWPOSCHANGING && IsHandleCreated)
{
User32.WINDOWPLACEMENT wp = new User32.WINDOWPLACEMENT();
wp.length = Marshal.SizeOf(typeof(User32.WINDOWPLACEMENT));
User32.GetWindowPlacement(Handle, ref wp);
switch (wp.showCmd)
{
case User32.SW_RESTORE:
case User32.SW_NORMAL:
case User32.SW_SHOW:
case User32.SW_SHOWNA:
case User32.SW_SHOWNOACTIVATE:
_windState = FormWindowState.Normal;
if (wp.showCmd == User32.SW_RESTORE)
Update();
break;
case User32.SW_SHOWMAXIMIZED:
_windState = FormWindowState.Maximized;
SetMaximumSize();
break;
case User32.SW_SHOWMINIMIZED:
case User32.SW_MINIMIZE:
case User32.SW_SHOWMINNOACTIVE:
_windState = FormWindowState.Minimized;
break;
}
}
private void SetMaximumSize()
{
Screen screen = Screen.FromControl(this);
if (screen != null && !screen.WorkingArea.IsEmpty)
{
int sizeDiff = this.Size.Width - this.ClientSize.Width;
var maxSize = new Size(screen.WorkingArea.Width + sizeDiff, screen.WorkingArea.Height + sizeDiff);
this.MaximumSize = maxSize;
}
}
#region Window State
public const int SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9;
#endregion Window State