23

I'm using Windows Vista and C#.net 3.5, but I had my friend run the program on XP and has the same problem.

So I have a C# program that I have running in the background with an icon in the SystemTray. I have a low level keyboard hook so when I press two keys (Ctr+windows in this case) it'll pull of the application's main form. The form is set to be full screen in the combo key press even handler:

this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;

So it basically works. When I hit CTR+Windows it brings up the form, no matter what program I have given focus to. But sometimes, the taskbar will still show up over the form, which I don't want. I want it to always be full screen when I hit that key combo.

I figure it has something to do with what application has focus originally. But even when I click on my main form, the taskbar sometimes stays there. So I wonder if focus really is the problem. It just seems like sometimes the taskbar is being stubborn and doesn't want to sit behind my program.

Anyone have any ideas how I can fix this?

EDIT: More details- I'm trying to achieve the same effect that a web browser has when you put it into fullscreen mode, or when you put powerpoint into presentation mode.

In a windows form you do that by putting the border style to none and maximizing the window. But sometimes the window won't cover the taskbar for some reason. Half the time it will.

If I have the main window topmost, the others will fall behind it when I click on it, which I don't want if the taskbar is hidden.

dario_ramos
  • 7,118
  • 9
  • 61
  • 108
Joel
  • 16,474
  • 17
  • 72
  • 93

5 Answers5

20

Try this (where this is your form):

this.Bounds = Screen.PrimaryScreen.Bounds;
this.TopMost = true;

That'll set the form to fullscreen, and it'll cover the taskbar.

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
  • 1
    For this to work, you want to make sure the form is *not* maximized. – Ryan Lundy Sep 23 '08 at 12:43
  • Oh, the only problem is I can't have it be TopMost, because I have other windows that are over it that are TopMost, and so if I click on 'this' then all the other windows will go behind it. – Joel Sep 23 '08 at 20:23
  • 2
    Heh. Minor detail there. I have my doubts about whether what you want can be done *without* TopMost if the user has "Keep the taskbar on top of other windows" checked. But you can also have multiple TopMost windows, and the active one will be the, uh, TopMostest. – Ryan Lundy Sep 23 '08 at 21:06
2

I've tried so many solutions, some of them works on Windows XP and all of them did NOT work on Windows 7. After all I write a simple method to do so.

private void GoFullscreen(bool fullscreen)
    {
        if (fullscreen)
        {
            this.WindowState = FormWindowState.Normal;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Bounds = Screen.PrimaryScreen.Bounds;
        }
        else
        {
            this.WindowState = FormWindowState.Maximized;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
        }
    }

the order of code is important and will not work if you change the place of WindwosState and FormBorderStyle.

One of the advantages of this method is leaving the TOPMOST on false that allow other forms to come over the main form.

It absolutely solved my problem.

mammadalius
  • 3,263
  • 6
  • 39
  • 47
1
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.F11)
        if (FormBorderStyle == FormBorderStyle.None)
        {
            FormBorderStyle = FormBorderStyle.Sizable;
            WindowState = FormWindowState.Normal;
        }
        else
        {
            SuspendLayout();
            FormBorderStyle = FormBorderStyle.None;
            WindowState = FormWindowState.Maximized;
            ResumeLayout();
        }
}
SeriousSamP
  • 436
  • 3
  • 18
Ronen
  • 11
  • 1
0

As far as I know, the taskbar is either above or below windows based on the "Keep the taskbar on top of other windows" setting. (At least, that's the wording in XP.) I suppose you could try to see if you can detect this setting and toggle it if needed?

jdmichal
  • 10,984
  • 4
  • 43
  • 42
  • 4
    It's bad practice to change the user's chosen settings based on YOUR preferences. It's better to find out how to use the APIs to get the behavior you desire in the right way. – Wedge Sep 22 '08 at 23:42
  • That setting only applies to windowed apps, not full screen apps. Because when I play games or put my web browser to full screen it'll cover up the task bar. – Joel Sep 23 '08 at 03:43
  • Full screen apps like games are switching the display mode to DirectX or OpenGL or something else completely outside the windowing system, that gives the program raw video buffer access. This is from within the windowing system, so the setting I mentioned applies. – jdmichal Dec 17 '09 at 19:13
0

Try resizing the form and bringing it to the front of the z-order like so:

        Rectangle screenRect = Screen.GetBounds(this);
        this.Location = screenRect.Location;
        this.Size = screenRect.Size;
        this.BringToFront();
Paul Beesley
  • 767
  • 2
  • 10
  • 22