You'll need to intercept the minimize command before your form fires Window_StateChanged
, to avoid the minimize/restore dance you're seeing. I believe the easiest way to do this is to have your form listen for Windows messages and when the minimize command is received, cancel it and resize your form.
Register the SourceInitialized
event, in your form constructor:
this.SourceInitialized += new EventHandler(OnSourceInitialized);
Add these two handlers to your form:
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;
}
I suspect this is the approach you were hoping to avoid but boiled down to the code I show it's not too difficult to implement.
Code based on this SO question.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…