Win32api设置窗口全屏的方法
首先是考虑全屏处理的时机,是在创建窗口时还是显示窗口时进行,若是前者,则可以:
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
UINT width = GetSystemMetrics(SM_CXSCREEN);
UINT height = GetSystemMetrics(SM_CYSCREEN);
//创建窗口
hWnd=CreateWindow(
szWindowClass,
szTitle,
WS_POPUP,
0,0,
width,height,
NULL,NULL,
hInstance,
NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
若是在显示窗口时进行处理:
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
HWND hDesk;
RECT rc;
hDesk = GetDesktopWindow();
GetWindowRect( hDesk, &rc );
SetWindowLong( hWnd, GWL_STYLE, WS_BORDER );
SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, rc.right, rc.bottom, SWP_SHOWWINDOW);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
也可以让用户控制全屏的时机,比如按下”ESC“键后进入全屏
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd)
return TRUE;
}
在窗口处理函数中对ESC键进行处理:
switch (message)
{
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
{
HWND hDesk;
RECT rc;
hDesk = GetDesktopWindow();
GetWindowRect( hDesk, &rc );
SetWindowLong( hWnd, GWL_STYLE, WS_BORDER );
SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, rc.right, rc.bottom, SWP_SHOWWINDOW);
}
break;
}
return 0;
}