Thread: Checking and changing window states

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    Checking and changing window states

    Initially, my window appears as a full-screen popup. I want to add a case in the message handler that detects ALT + ENTER to change the state to a normal window if it is in popup mode, and to revert back to popup mode if it is already in normal mode. In my book I can't find a way to check which state the window is currently in......also, how do I tell Windows to check if the ALT key is pressed BEFORE the ENTER key?

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Well, I'm not entirely sure how you're setting your window to full screen. Forgive me, but it's been a really, really long time since I've coded any windows for my programs (I hate those things). Could you provide a bit of code to show us how you're doing it?

    But, I can probably shed some light onto how you can solve the ALT before ENTER problem. It's entirely dependent on the structure of your program, but here goes. You'll want to have two variables that say whether or not the ALT and ENTER keys are pressed down.

    You'll want to check for two things, one after the other:

    1. Enter is not pressed down, and alt is pressed down.
    2. Alt is pressed down, and enter is pressed down.

    So, something like this.
    Code:
    if(!enterPressed && altPressed)
    {
      if(altPressed && enterPressed)
      {
        //code here~
      }
    }
    If the keypresses don't follow this sequence, then you don't change the size of your window.

    EDIT: Hmm... seems like they wouldn't have much time to press the enter key. o_O, no clue how to fix that yet. *pokes at his brain for the answer*
    Last edited by Tronic; 01-02-2006 at 12:34 PM.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    When I call the CreateWindow function, initially I supply WS_POPUP and WS_VISIBLE as parameters.
    Code:
    CreateWindow(WINDOW_CLASS_NAME,"Window Class",WS_POPUP | WS_VISIBLE, 0,0, GetSystemMetrics(SM_CXSCREEN),
    GetSystemMetrics(SM_CYSCREEN),NULL,NULL,hinstance,NULL)))
    That works great for creating the window. It displays the menu at the top (something I have not developed into a useful item yet). I know that I can use ShowWindow() to CHANGE the window state, but I need a way to check to see what state the window is in and do something different given that, such as ignoring a certain keypress, or something.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Ahh. Here's an idea. You can just check what state the user is in by how many times he's pressed the alt+enter keys (successfully). Toggle on and off a bool or something.

    I'm not sure how to go at it from the opposite angle though.. I mean, how do you turn off the WS_POPUP style? ...!WS_POPUP? o_O Would that even work? I have no idea. I don't see a window state on MSDN that relates to the window being non-popup. So I'm not sure how you would approach this from the angle you want.
    Last edited by Tronic; 01-02-2006 at 03:12 PM.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Use the GetWindowPlacement() API function. The showCmd member of the returned WINDOWPLACEMENT structure will tell you if your window is minimized, or maximized.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    What I have in mind is to set up the window like a lot of computer games do: you have your full screen mode (no window border, i.e. popup) and your windowed mode, which of course looks like a regular window (emulators do that too......Gens comes to mind). Alt+Enter is normally how it is done in my experience. When you go from full screen (not maximized) to window, the window remains active and you can Alt+Enter to revert to fullscreen. I'm having some trouble finding the code to do that.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What part are you having trouble with? Getting the window to go full screen, or trapping the Alt+Enter combo?

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    Both, actually.

    The window creates full screen popup (no borders).

    I want the option of playing it in a window versus playing a full screen game like pretty much any computer game out there. I actually discovered that the compiler already has an built in use for Alt in the program; so instead I was thinking of using F1 to go from full screen to windowed, and Shift + F1 to go from windowed to full screen. I still can't get it to read the combo though......

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well since I have some spare time on my hands....
    Code:
    #include <windows.h>
    
    int g_fs = 0;
    RECT g_r;
    
    void ToggleFullscreen(HWND hwnd)
    {
    	int cx, cy;
    	HDC DC = GetDC(NULL);
    	cx = GetDeviceCaps(DC,HORZRES)+GetSystemMetrics(SM_CXBORDER)+1;
    	cy = GetDeviceCaps(DC,VERTRES)+GetSystemMetrics(SM_CYBORDER)+1;
    	ReleaseDC(0,DC);
    
    	g_fs = !g_fs;
    	if(g_fs)
    	{
    		GetWindowRect(hwnd,&g_r);
    		SetWindowLong(hwnd, GWL_STYLE,
    				GetWindowLong(hwnd, GWL_STYLE) & (~WS_CAPTION & ~WS_BORDER));
    		// go full screen
    		SetWindowPos(hwnd, HWND_TOPMOST, -GetSystemMetrics(SM_CXBORDER)-5,
    						-GetSystemMetrics(SM_CYBORDER)-5, cx+10,cy+10, SWP_NOZORDER);
    
    	}
    	else
    	{
    		SetWindowLong(hwnd, GWL_STYLE,
    						GetWindowLong(hwnd, GWL_STYLE) | WS_CAPTION | WS_BORDER);
    		SetWindowPos(hwnd, HWND_NOTOPMOST,
    						g_r.left,
    						g_r.top,
    						g_r.right-g_r.left,
    						g_r.bottom-g_r.top,
    						SWP_SHOWWINDOW);
    	}
    	
    }
    
    LONG CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_CLOSE:
    		DestroyWindow(hwnd);
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	case WM_COMMAND:
    		if(HIWORD(wParam) == 1 && LOWORD(wParam) == 0x11)
    		{
    			ToggleFullscreen(hwnd);
    		}
    		break;
    	case WM_PAINT:
    		{
    			PAINTSTRUCT		ps;
    			HDC				hdc;
    			HBRUSH			hOldBrush;
    
    			hdc = BeginPaint(hwnd,&ps);
    			/* Set a color to fill the rectangle in */
    			hOldBrush = SelectObject(hdc,GetSysColorBrush(1));
    			/* Draw the rectangle */
    			Rectangle(hdc,100,80,300,280);
    			EndPaint(hwnd,&ps);
    			SelectObject(hdc,hOldBrush);
    		}
    		break;
    	}
    	return DefWindowProc(hwnd,msg,wParam,lParam);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance,  HINSTANCE hPrevInstance,  LPSTR lpCmdLine,  int nShowCmd)
    {
    	WNDCLASS	wndClass;
    	BOOL		bRet;
    	MSG			msg;
    	HWND		hwnd;
    	HACCEL		hAccel;
    	ACCEL		accelTable;
    
    	accelTable.fVirt = FALT;
    	accelTable.key = VK_RETURN;
    	accelTable.cmd = 0x11;
    
    	wndClass.style			= 0;
    	wndClass.lpfnWndProc	= WndProc;
    	wndClass.cbClsExtra		= 0;
    	wndClass.cbWndExtra		= 0;
    	wndClass.hInstance		= hInstance;
    	wndClass.hIcon			= LoadIcon(hInstance, IDI_APPLICATION);
    	wndClass.hCursor		= LoadCursor(NULL, IDC_ARROW);
    	wndClass.hbrBackground	= (HBRUSH)(COLOR_WINDOW + 1);
    	wndClass.lpszMenuName	= NULL;
    	wndClass.lpszClassName	= "MyClass";
    
    	if(!RegisterClass(&wndClass))
    	{
    		MessageBox(NULL,"Error registering class","Error",0);
    		return 0;
    	}
    
    	if(!(hwnd = CreateWindow("MyClass","My Window", WS_VISIBLE | WS_OVERLAPPEDWINDOW,
    			CW_USEDEFAULT, CW_USEDEFAULT,400,400,NULL, NULL,hInstance,0)))
    	{
    		MessageBox(NULL,"Error creating window","Error",0);
    		return 0;
    	}
    
    	hAccel = CreateAcceleratorTable(&accelTable,1);
    	if(!hAccel)
    	{
    		MessageBox(NULL,"Error creating accel","Error",0);
    		return 0;
    	}
    
    	while((bRet = GetMessage(&msg,NULL,0,0)) != 0)
    	{
    		if(bRet == -1)
    		{
    			/* Error with GetMessage */
    			return 0;
    		}
    		else
    		{
    			if(!TranslateAccelerator(hwnd,hAccel,&msg))
    			{
    				TranslateMessage(&msg);
    				DispatchMessage(&msg);
    			}
    		}
    	}
    	return 0;
    }
    Alt+Enter will toggle fullscreen in this little application. I used a keyboard accelerator to trap the Alt+Enter combo. To switch to Shift + F1, just make the appropriate changes to the CreateAcceleratorTable() function call.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Why do you need a keyboard accelerator? o_O.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    To catch Alt+Enter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error checking
    By hawaiian75 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2005, 01:56 AM