Chris Rasmussen · Photographer · Infrastructure Guy · Code Dabbler · Traveller

How to make a full-screen Windows app using VB.NET

Recently I had to write a Windows application that required the main form to run in full-screen mode. This means no title bar and with the window appearance above everything else, i.e. Start Button, taskbar, system tray and all other apps.

2009/08/29 Update – Complete application example & source now available for download from the Digital Formula Downloads page!

This requires an API call to the SetWindowPos function which you need to create an alias to before you can call it. The first part of the code, which should be placed in the declarations part of the form, looks like this :

Private Declare Function SetWindowPos Lib “user32.dll” Alias “SetWindowPos” (ByVal hWnd As IntPtr, ByVal hWndIntertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean

You also need an alias to the API function called GetSystemMetrics, like this :

Private Declare Function GetSystemMetrics Lib “user32.dll” Alias “GetSystemMetrics” (ByVal Which As Integer) As Integer

Following this you need to declare 4 constants :

Private Const SM_CXSCREEN As Integer = 0
Private Const SM_CYSCREEN As Integer = 1
Private Shared HWND_TOP As IntPtr = IntPtr.Zero
Private Const SWP_SHOWWINDOW As Integer = 64

Then 2 public properties :

Public ReadOnly Property ScreenX() As Integer
Get
Return GetSystemMetrics(SM_CXSCREEN)
End Get
End Property

Public ReadOnly Property ScreenY() As Integer
Get
Return GetSystemMetrics(SM_CYSCREEN)
End Get
End Property

After this you need to write a simple sub routine to use the constants you declared above and that calls the SetWindowPos API function. The function’s code looks like this :

Private Sub FullScreen()
Me.WindowState = FormWindowState.Maximized
Me.FormBorderStyle = FormBorderStyle.None
Me.TopMost = True
SetWindowPos(Me.Handle, HWND_TOP, 0, 0, ScreenX, ScreenY, SWP_SHOWWINDOW)
End Sub

Beyond that it’s just a case of calling the FullScreen sub routine whenever you want the application to show in full-screen mode.

banner ad

40 Responses to “How to make a full-screen Windows app using VB.NET”

  1. Firstimeer says:

    Thanks, I’ve been looking all over for this

  2. Chris says:

    No problem, thanks for the comment. :)

  3. Thyscorpion says:

    Perfect.. bless u vb guru’s… :-)

    i love google! ;-)

  4. yoosuf says:

    good code man, but u dident make for restore mode :(

  5. Fake says:

    This is Maximize mode not Full-screen mode.

  6. Chris says:

    As far as I’m aware this IS full-screen mode. There are no titlebars and the Windows taskbar doesn’t show up at all. If anyone has further comments about this article please leave your email address so I can respond – thanks.

    - Chris

  7. Eze says:

    How do you do to return to normal mode??

  8. Chris says:

    I’ve had a couple of requests for the method to change back to normal/restore mode for the application window. The method below will do this for you.

    Public Sub NormalMode()
    ‘ restore the window size back to default
    Me.WindowState = FormWindowState.Normal
    ‘ change the form’s border style so the border is visible
    ‘ note that SizableToolWindow is just what I’ve used – there are other options available if you have code auto-completion enabled
    Me.FormBorderStyle = FormBorderStyle.SizableToolWindow
    ‘ change the window so it’s not the on top
    Me.TopMost = False
    End Sub

    Note that this method is basically the reverse of the FullScreen() method described above but without the use of the SetWindowPos call.

    Please ask if you have any questions about the above code.

  9. Mahroof says:

    Thanks Chris, It works fine.. but i don’t want the form to be on the topmost, Bcz I am writing a program that plays Microsoft Character agents.. i want the agent to be on the top.. Is there a way to solve this??
    thank you

    Mahroof

  10. Mahroof says:

    Yeah, I got it.. It was there in ur last comment

    me.topmost = false

    Thanks

  11. Gryzor says:

    Now, if only someone made a small program that would make other applications run in full screen non-natively… :)

  12. Dante Q says:

    Thanx, both the fullscreen() and normalmode() work out perfectly!

  13. Zinzin says:

    very thanx fullscreen perfect !

  14. duckling says:

    Thanks for your helpful code. It works well if my application on the primary monitor. However, when I moved the application on the extent monitor, the application disappeared. How can I get the screenX, ScreenY for the extent monitor?

  15. duckling says:

    Yeash! I solved the problem.

  16. Vincent says:

    Hey.. for long days i was looking for this code.. but i dont know how to use it..
    can i get already u written project?
    anybody. plz send me. if you have
    plz mail me jinuem@gmail.com

  17. Vincent says:

    thanks alot.. its very usefull

  18. KK says:

    Thanks for this, But, How do you do a fullscreen in Dual Monitor?

  19. Chris says:

    KK: Do you mean an application that is full-screen across both monitors? Or full-screen only on the 2nd monitor?

    • KK says:

      Hi Chris.. sorry for the late reply.. it should be full-screen across both monitors.

    • KK says:

      Hi Chris..
      Your code works perfectly.. the reason it gave me some problems was because i was not set to “Horizontal span” – My Bad

      Thank you for this code. :D

    • KK says:

      ok.. no i have a problem… teacher doesn’t want a “HORIZONTAL VIEW” and that she’s sticking with the “DUALVIEW”

      your code…
      1. in Horizontal Span – it treats it like a single monitor only.
      2. in DUALVIEW – it treat 1 monitor as a single monitor and the 2nd monitor another monitor which in this case is not showing anything.

      • Chris says:

        I’m not sure I understand what you mean. On my 2nd monitor the application still displays full-screen just fine. Do you want to have an application that is full-screen across *both* monitors?

  20. john says:

    Thank you for your code.

    I have learnt to develop a full screen web browser. If a page try to open another window, how could I do to bring or show this window back to my browser window?

    Thanks.

    • Chris says:

      john: The sample application contains code for a button to return the application from full-screen mode to window mode. You could use this although I’m not sure if this is exactly what you want. Can you explain more?

      • john says:

        Thank you for your answer, Chris.

        My problem is:

        Most web pages have some links inside. Some linked pages are set to be shown in _self page, in this way, the new web page will not open a new window. Some other pages will show in _blank page, which will open a new window for the new page. The first sort of pages will be good for my full screen web browser, the second ones cause some problems. They will open another window, which will not stay in my full screen window any more. My question is how we could prevent those pages from opening another window. Any pages have to be shown in _self window.

        • Chris says:

          john: Ok, I see what you mean. _blank is an HTML standard identifier and unless you write code to specifically block it I don’t think you’ll be able to it them opening in a new window. If you really wanted to start hacking web page’s code to pieces you could load the page source into a string (or StringBuilder even) and replace all occurrences of _blank with _top or _self … you’d be changing how the designer of the web page wanted the site/page to behave though. Are you using one of the .NET web browser controls or have you written your own to handle the entire HTTP protocol conversation?

  21. john says:

    Chris,

    Thanks a lot.

    I use vb webbrowser control. If I could not make those _blank link to go back to _self, my whole project will be failed. You suggest about StringBuilder is a very good idea, however, I have not found a way to do it.

  22. john says:

    Chris,

    Thank you very much.

    Your links are very useful.

  23. john says:

    It is a bad practice.

    I recorded the downloaded web file in a file, and then use replace method in msdn to replace those _blank to _self. This part is OK, but after reload this web page, I find that we go to a wrong direction. The page is reloaded from my computer, not from its own server, it looks OK, but not.

    I am trying another direction now. I think when those _blank pages clicked, there will be new pages opened. I should get their name, url, hwnd; and then reload page by using url to my browser, at the same time, send a message to close the opened web page.

    I do not familiar with API programming. I am learning it now. Tell you what is going on later.

    Thank you again for your help.

  24. john says:

    Hi, Chris,

    Someone help me to find this?

    http://msdn.microsoft.com/en-us/library/aa770041(VS.85).aspx#Controlling_New_Windows

    I do not understand it. Could you help me?

  25. Fean says:

    Thx dude i’ve been looking for this all those other sites have no true fullscreen examples but you did it thx again

  26. Handy143 says:

    Thanks, you did a wonderful job.

  27. Alex says:

    Hey, wonderful, but can I ask you how to do a fullscreen mode like the one that appear clicking on the square button next to the red “X” on top-right on the application? I mean the “X” that close the windows. Sorry for bad English, I’m italian :S

  28. [...] I posted a quick update to an article I wrote back in 2007 entitled How to make a full-screen Windows app using VB.NET. Aside from 1 or 2 people saying “Oh man why don’t you be a man and write it using C#?” I [...]

Leave a Reply

Powered by Wordpress | Designed by Elegant Themes