Search code examples
c#.netwinformstitlebar

Prevent form from going into windowed mode


I want to create a new form, which is set to maximized. It should show the title bar and the taskbar. This wasn't a big problem so far. When i set this.MaximizeBox = false; the button on the title bar gets disabled but if I drag or double click the title bar the form goes into window mode (tested on Windows 7).

Setting MinimumSize and MaximumSize to the same value wasn't successful.


Solution

  • public class Form1 {
      public Form1(){
          InitializeComponent();
          WindowState = FormWindowState.Maximized;
          Load += (s,e) => {
             MaximizeBox = false;        
          };
      }
      bool hitControlButtons;
      protected override void WndProc(ref Message m)
      {
         if ((!hitControlButtons) && (m.Msg == 0xa3 || m.Msg == 0xa1))//WM_NCLBUTTONDBLCLK and WM_NCLBUTTONDOWN
         {                
            return;
         }
         if (m.Msg == 0xA0)//WM_NCMOUSEMOVE
         {
            int wp = m.WParam.ToInt32();                
            hitControlButtons = wp == 8 || wp == 20 || wp == 9;//Mouse over MinButton, CloseButton, MaxButton                               
         }
         base.WndProc(ref m);            
      }
    }