Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel A WinForm Minimize?

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?

like image 905
sooprise Avatar asked Sep 17 '10 16:09

sooprise


People also ask

How do I turn off WinForms full screen?

In WinForms, you do that by setting the FormBorderStyle property to one of the following: FormBorderStyle. FixedSingle , FormBorderStyle. Fixed3D , or FormBorderStyle.

How to disable minimize button in c#?

To remove minimize button we have to set the MinimizeBox property to false of the windows form. Now when you open the windows form you will notice the windows form's minimize button is in disable mode in the windows form.


3 Answers

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.

like image 158
Jay Riggs Avatar answered Oct 18 '22 13:10

Jay Riggs


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;
   }
}
like image 39
Iain Ward Avatar answered Oct 18 '22 13:10

Iain Ward


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

like image 1
Adrian Fâciu Avatar answered Oct 18 '22 12:10

Adrian Fâciu