Menu out of place when application is in full screen by Windows API

325 views Asked by At

I have developed an application in C# and I want to show it in full screen mode. It should also cover up the taskbar. To accomplish this I have used the Windows API. You can find the class below:

public sealed class WinAPI
{
    [DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
    public static extern int GetSystemMetrics(int which);

    [DllImport("user32.dll")]
    public static extern void
        SetWindowPos(IntPtr hwnd, IntPtr hwndInsertAfter,
                     int X, int Y, int width, int height, uint flags);

    private const int SM_CXSCREEN = 0;
    private const int SM_CYSCREEN = 1;
    private static IntPtr HWND_TOP = IntPtr.Zero;
    private const int SWP_SHOWWINDOW = 64; // 0×0040

    public static int ScreenX
    {
        get { return GetSystemMetrics(SM_CXSCREEN); }
    }

    public static int ScreenY
    {
        get { return GetSystemMetrics(SM_CYSCREEN); }
    }

    public static void SetWinFullScreen(IntPtr hwnd)
    {
        SetWindowPos(hwnd, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW);
    }
}

I am using this class in conjunction with the following form settings to go in full screen mode:

private void terminalModeToolStripMenuItem_Click(object sender, EventArgs e)
{
    // Remove the border.
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.WindowState = FormWindowState.Maximized;
    this.Bounds = Screen.PrimaryScreen.Bounds;

    // Full screen windows API hack.
    WinAPI.SetWinFullScreen(this.Handle);
}

Now comes the funny part. If I click a button in my menu bar it will show up with a gap between the button and the menu as you can see in the image below:

menuItem offset

Does anyone knows how to fix this issue? I would like it to show up like this:

menuItem without offset

And why does this happen anyway?

2

There are 2 answers

2
rotgers On BEST ANSWER

This problem is caused by having the task bar set to the top of the screen.

It can be resolved by either moving the task bar to the bottom of the screen, or by enabling the Auto-hide task bar check box in the Properties window of the task bar.

EDIT: As stated by @slashp 's comments. The root cause of this issue comes from some inner mechanics in drawing the menu. The menu has a safety to be always drawn within the working area. Which seems to be your screen - task bar. Because the application is placed over the task bar, the calculation is placing the menu below the task bar. (you can't see it, but it's still there)

6
cjones26 On

As Muraad pointed you to in his comment, try moving the following block of code into your Form load event:

// Remove the border.
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.Bounds = Screen.PrimaryScreen.Bounds;

And see if the issue still persists.