C++ Windows programming, full-screen
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: C++ Windows programming, full-screen

  1. #1
    Join Date
    Mar 2010
    Posts
    6

    C++ Windows programming, full-screen

    Hey, I'm trying to get a script where some C++ makes the window turn full-screen... And well, I'm finding it stupidly hard. Everything I search on Google just... Plain doesn't work, outdated code maybe?

    Anyway
    Code:
    #include "stdafx.h"
    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow);
    
    bool m_bFullScreen = true;
    DWORD dwStyle;
    
    DEVMODE best;
    
    bool FindBestDisplayMode(DEVMODE &best, DWORD width, DWORD height, DWORD bpp)
    {
        DEVMODE dm = {0};
        dm.dmSize = sizeof(DEVMODE);
    
        DWORD n, maxFreq = 0;
        bool found = false;
    
        for (n = 0; EnumDisplaySettings(NULL, n, &dm); n++)
        {
            if ((dm.dmPelsWidth == width) && 
                (dm.dmPelsHeight == height) &&
                (dm.dmBitsPerPel == bpp))
            {
                found = true;
    
                if (dm.dmDisplayFrequency > maxFreq)
                {
                    maxFreq = dm.dmDisplayFrequency;
                    best = dm;
                }//if
            }//if
        }//for
    
        return found;
    }//FindBestDisplayMode
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow){
    	FindBestDisplayMode(best, 800, 600, 32);
    	if(m_bFullScreen){
    		if(ChangeDisplaySettings(&best, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL){
    			MessageBox(NULL, L"Display mode failed", NULL, MB_OK);
    			m_bFullScreen = false;
    		}
    	}
    
    	if(m_bFullScreen){      
    		dwStyle = WS_POPUP;
    		MessageBox(NULL, L"Display mode not failed", NULL, MB_OK);
    	}
    	else{
    		dwStyle = WS_OVERLAPPEDWINDOW;
    		MessageBox(NULL, L"Display mode failed", NULL, MB_OK);
    	}
    
    
        WNDCLASS wc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hInstance = hInstance;
        wc.lpfnWndProc = WndProc;
        wc.lpszClassName = L"ClassName";
        wc.lpszMenuName = 0;
        wc.style = CS_HREDRAW | CS_VREDRAW;
    
        RegisterClass(&wc);
    
        HWND hwnd = CreateWindow(
            L"ClassName",
    		L"Title",
            dwStyle,
    		0, 0,
            800, 600,
            NULL, NULL,
            hInstance, NULL
    	);    
        ShowWindow(hwnd, iCmdShow );
        UpdateWindow(hwnd);
    
        MSG msg;
    
    	while(GetMessage(&msg, NULL, 0, 0)){
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
        return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){
        switch(message){
    		case WM_PAINT:{
                HDC hdc;
                PAINTSTRUCT ps;
                hdc = BeginPaint(hwnd, &ps);
    
                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);
    }
    That is my code. I had been putting in the DEVMODE manually, but someone told me I should get a function to find out the values for me... Either way outputs the same result, my screen goes black - and then nothing happens. I have to turn my PC off and try again.

    (I'm using Visual C++ 2008 Express Edition, running on Windows 7 - just in case that does make a difference)

    I would really appreciate a hand, thanks if anyone can help

  2. #2
    Join Date
    Mar 2010
    Posts
    6

    Re: C++ Windows programming, full-screen

    Just as a note, here's what my manually-inputted DEVMODE code looks like, that still just produced a black screen.
    Code:
    DEVMODE screen;
    DWORD dwStyle, dwExStyle;
    memset(&screen,0,sizeof(screen));
    screen.dmSize = sizeof(screen);
    screen.dmPelsWidth = 800;
    screen.dmPelsHeight = 600;
    screen.dmBitsPerPel = 32;
    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;
    }

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    Re: C++ Windows programming, full-screen

    When screen go black like that it's something really bad with the settings. Since you're setting a quite low resolution I would say that the refresh rate is probably way to high. Try hardcoding it to something like 60 Hz just to check if that's the root cause.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  4. #4
    Join Date
    Nov 2003
    Posts
    1,800

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center