2

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?

Marshal
  • 6,551
  • 13
  • 55
  • 91
rotgers
  • 1,992
  • 1
  • 15
  • 25
  • 1
    Maybe this http://stackoverflow.com/questions/2272019/how-to-display-a-windows-form-in-full-screen-on-top-of-the-taskbar or that http://stackoverflow.com/questions/505167/how-do-i-make-a-winforms-app-go-full-screen helps you. Greetings! – Muraad Nofal Jun 09 '15 at 13:39
  • Are you making the application full screen only during a click on one of the tool strip menu items? If so, the menu appears to be drawing before the resize, thereby causing your issue, as the window is resized after the menu is already drawn. – cjones26 Jun 09 '15 at 13:40
  • @MuraadNofal Thank you, but I have tried both methods and it gives an undesired effect (I have 3 screens), it overlaps on my other screens OR it is below the taskbar. The method I used, fits the window perfectly on my main screen. – rotgers Jun 09 '15 at 13:43
  • Understood, I still think this has something to do with you changing the size from the menu item click event. – cjones26 Jun 09 '15 at 13:44
  • @slashp This makes perfect sense and I tested it immediately by adding this code to the constructor of the form (nothing has been drawed in that stage if I'm correct). Unfortunately, the menu is still out of place. :( – rotgers Jun 09 '15 at 13:45

2 Answers2

1

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.

Community
  • 1
  • 1
cjones26
  • 3,459
  • 1
  • 34
  • 51
  • I'm sorry but this does not work. Neither in the constructor nor the Form_Load event. Thanks for the tip anyway! – rotgers Jun 09 '15 at 13:47
  • 1
    Lol, what works is enabling the auto-hide taskbar checkbox at the taskbar properties window! – rotgers Jun 09 '15 at 13:47
  • Yeah I'm guessing that won't work for your situation, though. I'm actually not able to reproduce the issue where the taskbar is still on top but that may have something to do with me currently being in a Citrix session. – cjones26 Jun 09 '15 at 13:50
  • I also have the taskbar at the top of my screen. That influences it. When I have the taskbar at the bottom of my screen the problem is resolved. – rotgers Jun 09 '15 at 13:51
  • Ahhhh, it very well might. Also if you look in the link posted by Muraad, you'll notice a lot of people are having the taskbar issue which seems to be dependent on the order which the properties on the form are set. Since you're testing from your dev machine, I would try seeing it works fine from one of the machines where the application will be deployed to and check whether the issue persists. Quick edit: I see the same menu behavior when I set my taskbar to the top of the screen. – cjones26 Jun 09 '15 at 13:52
  • Yes, a lot of people are having issues with it. By using the Windows API, I got it right. Also the production machine will have the taskbar on the bottom. Windows is always full of surprises! – rotgers Jun 09 '15 at 13:54
1

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)

rotgers
  • 1,992
  • 1
  • 15
  • 25
  • 1
    Yep, you appear to be running into the issue described here: http://msdn.developer-works.com/article/12805750/Toolbars+in+the+Windows+Taskbar+and+popup+menus Looks like it's just the way it is, unfortunately, since "They contain code that ensures they are shown within the screen's working area, the value of Screen.WorkingArea." – cjones26 Jun 09 '15 at 14:08
  • Ah, that explains it! With the methods described by Muraad in his comments, I still have an issue with the task bar being there. When the Windows API forces the application to go over the task bar. The 'code' still 'sees' the task bar in the calculation, thus you get the gap... Thank you for your help! – rotgers Jun 09 '15 at 14:12