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.