Carlos Delgado
Carlos Delgado

Reputation: 3065

Remove borders winforms and WindowState Maximized without fullscreen

I have the following trouble and i don't find a solution.

I want to implement a Winform without top bar and if is possible, without borders. I tried several things without success, the following would do the trick perfectly :

        this.Text = string.Empty;
        this.ControlBox = false;
        this.FormBorderStyle = FormBorderStyle.SizableToolWindow;

producing the following result :

All work perfect ! But when i make it in maximized state

The little problem is when me or the user trigger the maximize state , because will make the form enter in a FULLSCREEN mode ! and i don't know how to prevent this:

See ? You can't see the windows taskbar !

See? You can't see the windows taskbar ! I'm using

WindowState = FormWindowState.Maximized; // Makes a fullscreen that i dont want !

Appreciate your help !

Upvotes: 10

Views: 11545

Answers (6)

Guesso
Guesso

Reputation: 11

hi you can to do this only with 2 instructions: this.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size; this.WindowState = FormWindowState.Maximized;

Upvotes: 1

H. Pauwelyn
H. Pauwelyn

Reputation: 14310

Remove the border

this.FormBorderStyle = FormBorderStyle.None;

I think this goes work by clicking the maximize button

// Add following namespaces
using System.Windows.Forms;
using System.Drawing;

// Retrieve the working rectangle from the Screen class using the PrimaryScreen and the 
// WorkingArea properties.
Rectangle workingRectangle = Screen.PrimaryScreen.WorkingArea;

// Set the size of the form slightly less than size of working rectangle. 
this.Size = new Size(workingRectangle.Width, workingRectangle.Height);

Sources: MSDN system.windows.forms.screen.primaryscreen and Remove the title bar in Windows Forms

Upvotes: 2

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186803

If you want to take control over form maximization, do it like that:

  public class MyForm {
    protected override void WndProc(ref Message m) {
      const int WM_SYSCOMMAND = 0x0112;
      const int SC_MAXIMIZE = 0xF030;

      // When form is going to be maximized    
      if ((m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MAXIMIZE)) {
        m.Msg = 0; // <- we don't want a standard maximization
        //TODO: tell Windows here what to do when user want to maximize the form

        // Just a sample (pseudo maximization)
        Location = new Point(0, 0);
        Size = new Size(Screen.GetWorkingArea(this).Width,
                        Screen.GetWorkingArea(this).Height);
      }

      base.WndProc(ref m);
    } 
    ...
  }

Upvotes: 2

M. Nasir Javaid
M. Nasir Javaid

Reputation: 5990

Try this to set size dynamically, it may helps you.

Do not use WindowState = FormWindowState.Maximized; Try this code on loading form

var rectangle = ScreenRectangle();
Size = new Size(rectangle.Width - 100, rectangle.Height - 100);
Location = new Point(50, 50);
// here 100 is pixel used to reserve from edges, 
// you can set lower value according to your requirements

For full size

var rectangle = ScreenRectangle();
Size = new Size(rectangle.Width, rectangle.Height);
Location = new Point(0, 0);

Screen Rectangle Method is:

public Rectangle ScreenRectangle()
{
    return Screen.FromControl(this).Bounds;
}

Upvotes: 6

Carlos Delgado
Carlos Delgado

Reputation: 3065

Well ! Thanks to all your answers I finally solved with the following two methods

    private void MaximizeWindow() 
    {
        var rectangle = Screen.FromControl(this).Bounds;
        this.FormBorderStyle = FormBorderStyle.None;
        Size = new Size(rectangle.Width, rectangle.Height);
        Location = new Point(0, 0);
        Rectangle workingRectangle = Screen.PrimaryScreen.WorkingArea;
        this.Size = new Size(workingRectangle.Width, workingRectangle.Height);
    }

    private void ResizableWindow() 
    {
        this.ControlBox = false;
        this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
    }

Thanks to X-TECH and Luïs , this was solved !

Maximized state and the taskbar still there !

Upvotes: 11

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4908

this.Text = string.Empty;
this.ControlBox = false;
this.FormBorderStyle = FormBorderStyle.SizableToolWindow;

this.Left = this.Top = 0;
this.Height = Screen.GetWorkingArea(this).Height;
this.Width = Screen.GetWorkingArea(this).Width;

GetWorkingArea: Gets the working area of the display. The working area is the desktop area of the display, excluding taskbars, docked windows, and docked tool bars.MSDN

Upvotes: -2

Related Questions