zaitsman
zaitsman

Reputation: 9499

Maximising borderless form covers task bar only when maximised from a normal size

I am using C# to give an application 'fullscreen mode' using borderless form and maximise method. This works perfectly when i am making the form borderless while it is not maximised - all you can see on the screen is the form, taskbar is covered.. However, if i maximise the form manually (user interaction), and then attempt to make it borderless & maximised, the task bar is drawn over the form (as i am not using WorkingArea, part of the controls on the form are hidden. It is the intended behaviour to NOT show the taskbar). I tried setting the form's property TopMost to true, but that doesn't seem to have any effect.

Is there any way to rework this to always cover the taskbar?

if (this.FormBorderStyle != System.Windows.Forms.FormBorderStyle.None)
    {        
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    }
    else
    {
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
    }
    if (this.WindowState != FormWindowState.Maximized)
    {
    this.WindowState = FormWindowState.Maximized;
    }
    else
    {
        if (this.FormBorderStyle == System.Windows.Forms.FormBorderStyle.Sizable)  this.WindowState=FormWindowState.Normal;
    }

Upvotes: 1

Views: 2088

Answers (3)

Hans Passant
Hans Passant

Reputation: 941347

However, if i maximise the form manually (user interaction)...

The issue is that your window is already internally marked as being in the maximized state. So maximizing it again will not change the current size of the form. Which will leave the taskbar exposed. You'll need to restore it first back to Normal, then back to Maximized. Yes, that flickers a bit.

    private void ToggleStateButton_Click(object sender, EventArgs e) {
        if (this.FormBorderStyle == FormBorderStyle.None) {
            this.FormBorderStyle = FormBorderStyle.Sizable;
            this.WindowState = FormWindowState.Normal;
        }
        else {
            this.FormBorderStyle = FormBorderStyle.None;
            if (this.WindowState == FormWindowState.Maximized) this.WindowState = FormWindowState.Normal;
            this.WindowState = FormWindowState.Maximized;
        }
    }

Upvotes: 1

Jens Granlund
Jens Granlund

Reputation: 5070

You could try using the WinApi SetWindowPos method, like this:

public static class WinApi
{
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, 
                                    int x, int y, int width, int height, 
                                    uint flags);

    static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
    const uint SWP_NOSIZE = 0x0001;
    const uint SWP_NOMOVE = 0x0002;
    const uint SWP_SHOWWINDOW = 0x0040;

    public static void SetFormTopMost(Form form)
    {
        SetWindowPos(form.Handle, HWND_TOPMOST, 0, 0, 0, 0, 
                     SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
    }
}

In your form call it like this:

WinApi.SetFormTopMost(this);

Upvotes: 0

John
John

Reputation: 631

Not sure why this is happening, and i hate app's that assume they can take all of my screen. While this might be acceptable for 1024x768 displays, my 30" display is wasted when the damn thing decides it owns my screen.

So my message is, maybe focus on making sure all of your controls are visible rather than focusing on maximizing the window.

You can always detect window size changes and then override the default behavior, taking care of the unexpected issue you are encountering. But rather than maximizing and taking all of a 30" display, calculate how large the window needs to be and set the size accordingly.

My 2 cents, which is precisely what my thoughts are worth ;)

Upvotes: 0

Related Questions