I read your comment regarding my answer and was able to put together a more complete solution for you. I quickly ran and seemed to do what I wanted. Instead of inferring your winform forms from the form, output from this class:
using System; using System.Windows.Forms; using System.ComponentModel; namespace NoMinimizeTest { public class MinimizeControlForm : Form { private const int WM_SYSCOMMAND = 0x0112; private const int SC_MINIMIZE = 0xf020; protected MinimizeControlForm() { AllowMinimize = true; } protected override void WndProc(ref Message m) { if (!AllowMinimize) { if (m.Msg == WM_SYSCOMMAND) { if (m.WParam.ToInt32() == SC_MINIMIZE) { m.Result = IntPtr.Zero; return; } } } base.WndProc(ref m); } [Browsable(true)] [Category("Behavior")] [Description("Specifies whether to allow the window to minimize when the minimize button and command are enabled.")] [DefaultValue(true)] public bool AllowMinimize { get; set; } } }
You can do a little more if you want to decide whether to allow minimization when sending a click, for example:
using System; using System.Windows.Forms; using System.ComponentModel; namespace NoMinimizeTest { public class MinimizeControlForm : Form { private const int WM_SYSCOMMAND = 0x0112; private const int SC_MINIMIZE = 0xf020; protected MinimizeControlForm() { } protected override void WndProc(ref Message m) { if (m.Msg == WM_SYSCOMMAND) { if (m.WParam.ToInt32() == SC_MINIMIZE && !CheckMinimizingAllowed()) { m.Result = IntPtr.Zero; return; } } base.WndProc(ref m); } private bool CheckMinimizingAllowed() { CancelEventArgs args = new CancelEventArgs(false); OnMinimizing(args); return !args.Cancel; } [Browsable(true)] [Category("Behavior")] [Description("Allows a listener to prevent a window from being minimized.")] public event CancelEventHandler Minimizing; protected virtual void OnMinimizing(CancelEventArgs e) { if (Minimizing != null) Minimizing(this, e); } } }
For more information about this window notification, see the MSDN article about it .
Rob
source share