5

I have a winform with the minimizeMaximizeClose buttons disabled, but still if someone presses it in the task bar, it will minimize. I want to prevent this from happening.

How can I accomplish this?

sooprise
  • 22,657
  • 67
  • 188
  • 276

4 Answers4

19

Override WndProc on your form, listen for minimize messages and cancel.

Add this code to your form:

private const int WM_SYSCOMMAND = 0x0112; 
private const int SC_MINIMIZE = 0xf020; 

 protected override void WndProc(ref Message m) 
{ 
    if (m.Msg == WM_SYSCOMMAND) 
    { 
        if (m.WParam.ToInt32() == SC_MINIMIZE) 
        { 
            m.Result = IntPtr.Zero; 
            return; 
        } 
    } 
    base.WndProc(ref m); 
} 

I modified Rob's code found in this SO thread:
How to disable the minimize button in C#?

Works great: no flickering, no nothing when the user attempts to minimize.

Community
  • 1
  • 1
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
4

You could probably catch them changing it in the SizeChanged event and check the WindowState, if its been set to Minimized then set it back Normal. Not the most elegant solution but should work.

eg.

private void myForm_SizeChanged(object sender, System.EventArgs e)
{
   if (myForm.WindowState == FormWindowState.Minimized)
   {
       myForm.WindowState = FormWindowState.Normal;
   }
}
Iain Ward
  • 9,850
  • 5
  • 34
  • 41
  • This should also work, but since the event is triggered after the window was minimized it's slightly possible that a flickering effect will appear. – Adrian Fâciu Sep 17 '10 at 16:23
  • @Adrian: yeah I was just thinking that, depends what is on the form and how much redrawing would need to take place. – Iain Ward Sep 17 '10 at 16:24
  • And what if the window was maximized? You need to know the window state prior to the event firing. – dynamichael Jul 25 '20 at 20:06
1

If it's suitable for you, just hide it from the taskbar: ShowInTaskbar=false

Adrian Fâciu
  • 12,414
  • 3
  • 53
  • 68
1

you can simply remove the minimize button from the window:

add the code below to the private void InitializeComponent() method of the Form class:

            this.MinimizeBox = false;