Thread: C++ Full screen

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    20

    C++ Full screen

    Hey, I'm trying to make a little window in C++ that goes full screen.
    I've been browsing the internet for a solution... And well, somewhere just said to change the bit in CreateWindow to WS_MAXIMIZE... That did nothing.
    I have a code at the moment...
    Code:
    #include "stdafx.h"
    
    
    #include <windows.h>
    
    
    int WINAPI WinMain( HINSTANCE hInstance,    // HANDLE TO AN INSTANCE.  This is the "handle" to YOUR PROGRAM ITSELF.  More in the GLOSSARY at the bottom.
                        HINSTANCE hPrevInstance,// USELESS on modern windows (totally ignore hPrevInstance)
                        LPSTR szCmdLine,        // Command line arguments.  Explained near BOTTOM of this file.
                        int iCmdShow )          // Start window maximized, minimized, etc.
    {
    
        #pragma region part 1 - STARTUP STUFF
        WNDCLASS wc;
        wc.cbClsExtra = 0;  // ignore for now
        wc.cbWndExtra = 0;  // ignore for now
        wc.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );   // I want the window to have a white background
        wc.hCursor = LoadCursor( NULL, IDC_ARROW );            // I want it to have an arrow for a cursor
        wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );        // I want it to have that envelope like icon
        wc.hInstance = hInstance;           
        wc.lpfnWndProc = WndProc;           // Give name of WndProc function here.
        wc.lpszClassName = TEXT("Philip");
        wc.lpszMenuName = 0;    // no menu - ignore
        wc.style = CS_HREDRAW | CS_VREDRAW; 
    	DEVMODE screen;
    	memset(&screen,0,sizeof(screen));
    	screen.dmSize = sizeof(screen);
    	screen.dmPelsWidth = 1024;
    	screen.dmPelsHeight = 768;
    	screen.dmBitsPerPel = 32;
    	screen.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
    	ChangeDisplaySettings(&screen, CDS_FULLSCREEN);
    
        RegisterClass( &wc ); 
        HWND hwnd = CreateWindow(
            TEXT("Philip"),         // THIS IS THE LINK
                                    // to the WNDCLASS structure that
                                    // we created earlier.
    
            TEXT("Window ittle"),// appears in title of window
    
            WS_POPUP,    
            50, 50,                 // x, y start coordinates of window
            500, 500,               // width, height of window
            NULL, NULL,             // nothing and nothing (ignore to start out)
            hInstance, NULL );      // hInstance -- (see glossary), nothing
    
    
        ShowWindow(hwnd, iCmdShow );
        UpdateWindow(hwnd);
        #pragma endregion
    
        #pragma region part 2 - ENTER A LOOP TO CONTINUALLY KEEP CHECKING WITH WIN O/S FOR USER INTERACTION
        while( GetMessage( &msg, NULL, 0, 0 ) )
        {
            
    
            DispatchMessage( &msg );    
    
        }
        #pragma endregion
    
        return msg.wParam;    // return from WinMain
    }
    
    LRESULT CALLBACK WndProc(   HWND hwnd,      // "handle" to the window that this message is for
                                UINT message,   // TYPE of message (e.g. WM_PAINT is a message asking to paint the window)
                                WPARAM wparam,  // information about the actual message
                                LPARAM lparam ) // MORE info about the message
    {
    
        switch( message )
        {
    
        case WM_PAINT:
            {
                // we would place our Windows painting code here.
                HDC hdc;
                PAINTSTRUCT ps;
                hdc = BeginPaint( hwnd, &ps );
    
                // draw a circle and a 2 squares
                Ellipse( hdc, 20, 20, 160, 160 );
                Rectangle( hdc, 50, 50, 90, 90 );
                Rectangle( hdc, 200, 50, 140, 90 );
    
                EndPaint( hwnd, &ps );
            }
            return 0;
            break;
    
        case WM_DESTROY:
            PostQuitMessage( 0 ) ;
            return 0;
            break;
    
        }
        return DefWindowProc( hwnd, message, wparam, lparam );
    }
    But umm... Yeah that just makes my PC show a black screen, forcing me to turn it off and on again.

    So I'm really stumped :P

    I'd really appreciate any comments, thank you

    (Sorry if the code is a bit messy... It's a mixture between mine and someone else's from a tutorial with lots of comments in, basically the full screen bit is this bit..)

    Code:
    DEVMODE screen;
    memset(&screen,0,sizeof(screen));
    screen.dmSize = sizeof(screen);
    screen.dmPelsWidth = 1024;
    screen.dmPelsHeight = 768;
    screen.dmBitsPerPel = 32;
    screen.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
    ChangeDisplaySettings(&screen, CDS_FULLSCREEN);
    Well... Supposedly.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Maybe your screen doesn't support 32 bit color depth or 1024x768 resolution...
    Devoted my life to programming...

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Code:
    wc.lpszClassName = TEXT("Philip");
    the TEXT macro will input this: ""Philip"" ( I think ), so there's your error, an untitled window class...
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    20
    If I take out out the full-screen section of the code, it runs fine - so I don't think the error is within the TEXT macro.

    And I've tried changing the bit color from "32" to "64"... It doesn't black-screen, but it does nothing other than a regular window. I wasn't sure whether the bit-number was meant to be my OS's bit-rate (64-bit) or the program's (Win32).

    I'll try fiddling with some things whilst it's on "64", and the TEXT macro... But I could still use a definitive answer.

    EDIT: Okay, if I take all that stuff out (DEVMODE screen etc.) and just set it to a WS_POPUP with the exact width and height of my screen resolution, that looks right... Is this a good way to do it or not?
    Last edited by MinatureCookie; 03-10-2010 at 11:45 AM.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Is there 64 bit color depth??

    ChangeDisplaySettings(/*...*/) is of bool type ( or int type that converts to bool )
    You should check what that returns before proceeding!

    Okay, if I take all that stuff out (DEVMODE screen etc.) and just set it to a WS_POPUP with the exact width and height of my screen resolution, that looks right... Is this a good way to do it or not?
    Well, i'll look like fullscreen, but it won't be. DEVMODE changes the screen resolution and color depth and removes the Windows bar.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    20
    Okay, right then...

    I do want a proper full screen, so I've been trying to fiddle about with it. I've changed CreateWindow() to CreateWindowEx(), added in two variables dwStyle and dwExStyle which I set in the DEVMODE code...

    Code:
    DEVMODE screen;
    	DWORD dwStyle, dwExStyle;
    	memset(&screen,0,sizeof(screen));
    	screen.dmSize = sizeof(screen);
    	screen.dmPelsWidth = 800;
    	screen.dmPelsHeight = 600;
    	screen.dmBitsPerPel = 64;
    	screen.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
    	if(ChangeDisplaySettings(&screen, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL){
    		dwExStyle = WS_EX_APPWINDOW;      
    		dwStyle = WS_POPUP;                 
    		ShowCursor(FALSE);
    	}
    	else{
    		dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
    		dwStyle = WS_OVERLAPPEDWINDOW;
    	}
    But all I get is the window 800x600 large, with no window bar... So I'm guessing that the DEVMODE variable isn't actually being set? (I thought ChangeDisplaySettings was a function not a boolean... Or is it both?)

    But umm, yeah... Stuck again

    EDIT: (Forgot to say, thank you Sipher - you have been very helpful so far)

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    I'll say it in another way, i don't think a 64-bit color depth exists!! ( I don't know though, i'm using 32-bit system )

    It's ok the way you do it, just try to change the following:
    Code:
    if(ChangeDisplaySettings(&screen, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL){
    		dwExStyle = WS_EX_APPWINDOW;      
    		dwStyle = WS_POPUP;                 
    		ShowCursor(FALSE);
    	}
    to:

    Code:
    if(ChangeDisplaySettings(&screen, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL){
    		dwExStyle = 0; // <-- YOU DON'T NEED AN EXTENDED STYLE WHEN IN FULLSCREEN      
    		dwStyle = WS_POPUP;                 
    		ShowCursor(FALSE);
    	}
    Devoted my life to programming...

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by MinatureCookie View Post
    But all I get is the window 800x600 large, with no window bar... So I'm guessing that the DEVMODE variable isn't actually being set? (I thought ChangeDisplaySettings was a function not a boolean... Or is it both?)
    Sorry, now i remember! It's a function that returns DWORD ( unsigned long ) and you use one of Windows macros, here DISP_CHANGE_SUCCESSFUL, to check it.

    You set the DEVMODE variable everytime! The function just takes your set values and informs you if the change was successful, failed or an error occured.
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    20
    So what would the code for that be?

    EDIT: Okay so I've worked out the problem is almost definitely in bitsPerPel.
    I added in an error box, and if I have bitsPerPel = 64, it throws an error. If I have it on 32 or 16, it just goes into a black-screen and does nothing...?

    (My screen is running on 32 bit)

    Might it be that I am running on Windows 7? I'm trying to work out why there is so little information about a black-screen appearing when trying this.

    I have taken out the dwExStyle, I've made 800x600, a size I know my monitor can handle, I've made it 32-bit, what I know my monitor is running off... But it just black screens.
    Last edited by MinatureCookie; 03-11-2010 at 03:18 AM.

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by MinatureCookie View Post
    I have taken out the dwExStyle, I've made 800x600, a size I know my monitor can handle, I've made it 32-bit, what I know my monitor is running off... But it just black screens.
    Then it may be something wrong with the way you manipulate Windows messages. Try this:
    Code:
    while( GetMessage( &msg, NULL, 0, 0 ) )
        {
            
            TranslateMessage(&msg);  
            DispatchMessage( &msg );    
    
        }
    But i insist: Remove the TEXT macro from your code! I think that it produces different results that you'd like it to!!!

    I don't know, i never had this problem!...
    Devoted my life to programming...

  11. #11

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    20
    @Sipher - done both of those things, neither work.

    @Codeplug - What parameters am I meant to put into that function (FindBestDisplayMode)? And is it just the function I need to pay attention to? To get the correct data, do you think?

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Quote Originally Posted by MSDN
    ChangeDisplaySettings Remarks:

    To ensure that the DEVMODE structure passed to ChangeDisplaySettings is valid and contains only values supported by the display driver, use the DEVMODE returned by the EnumDisplaySettings function.
    The point is that you do not fill a DEVMODE structure yourself (other than it's size). Instead, you find the DEVMODE you want to use by calling EnumDisplaySettings(). Once you find the DEVMODE you want to use, pass that to ChangeDisplaySettings().

    Since many display drivers support "rotated" displays, you should also check the dmDisplayOrientation member for DMDO_DEFAULT (but only if dmFields has the DM_ORIENTATION bit set of course).
    DEVMODE Structure (Windows)

    >> What parameters am I meant to put into that function (FindBestDisplayMode)?
    >> bool FindBestDisplayMode(DEVMODE &best, DWORD width, DWORD height, DWORD bpp)
    So "best" is an output only reference parameter, which should only be used if the function returns true. If it does return true, then that's the DEVMODE to pass to ChangeDisplaySettings(). "width", "height", and "bpp" are all "desired settings". So if you want 800x600 with 32bit color, you would pass in "800, 600, 32".

    gg

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    20
    That's what I would have guessed for the parameters, but Visual C++ throws back this error:
    Code:
    error C2660: 'FindBestDisplayMode' : function does not take 3 arguments

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I have to agree with the compiler - I count 4 parameters.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Full Screen
    By Unregistered in forum C++ Programming
    Replies: 25
    Last Post: 06-12-2009, 01:33 PM
  2. Opening in full screen
    By MaGaIn in forum C++ Programming
    Replies: 14
    Last Post: 08-21-2007, 11:12 AM
  3. Full Screen
    By Rainer in forum C++ Programming
    Replies: 4
    Last Post: 08-08-2003, 05:56 AM
  4. !!!!!!Full screen!!!!!
    By Lukas in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2003, 04:43 AM
  5. Full Screen
    By JamMan in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2001, 03:10 PM