Windows C++ 窗口全屏

1、在一些场景中我们需要将应用程序窗口进行全屏显示,比如一些会议程序,为了观看的清晰度,会将整个渲染窗口全屏显示。下面主要就实现一下这个功能

2、首先创建一个窗口

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_LBUTTONDOWN:
        break;
    case WM_PAINT:
        break;
    case WM_CLOSE:
        DestroyWindow(hwnd);  //销毁窗口
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

int main(void) {
    HINSTANCE hInstance = GetModuleHandle(0);
    WNDCLASS wndcls;
    wndcls.cbClsExtra = 0;
    wndcls.cbWndExtra = 0;
    wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndcls.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndcls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndcls.hInstance = hInstance;
    wndcls.lpfnWndProc = WndProc;
    wndcls.lpszClassName = "TestWindow";
    wndcls.lpszMenuName = NULL;
    wndcls.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClass(&wndcls);
    hwnd = CreateWindow("TestWindow", "TestWindow", WS_OVERLAPPEDWINDOW,
        100, 100, 600, 400, NULL, NULL, hInstance, NULL);
    ShowWindow(hwnd, SW_SHOWNORMAL);
    UpdateWindow(hwnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

3、这里使用 Esc 来控制窗口全屏和恢复,在 WndProc 回调里面接收键盘消息

case WM_KEYDOWN:
    if (VK_ESCAPE == wParam) {
        if (full_screen_) {
            RestoreFullScreen();
        }
        else {
            FullScreen();
        }
    }
break;

4、实现全屏函数和恢复函数

全屏函数

void FullScreen() {
    last_style = GetWindowLong(hwnd, GWL_STYLE); //存储上次的窗口风格
    GetWindowRect(hwnd, &last_rect);             //存储上次的窗口位置和大小
    int w = GetSystemMetrics(SM_CXSCREEN);
    int h = GetSystemMetrics(SM_CYSCREEN);       // 获取最大化的窗口大小
    SetWindowLongPtr(hwnd, GWL_STYLE, WS_VISIBLE | WS_POPUP); // 去掉标题栏
    SetWindowPos(hwnd, NULL, 0, 0, w, h, SWP_FRAMECHANGED); // 设置位置和大小
    full_screen_ = true;
}

恢复全屏前大小的函数

void RestoreFullScreen() {
    SetWindowLong(hwnd, GWL_STYLE, last_style); // 恢复最大化之前的窗口风格
    SetWindowPos(hwnd, NULL, last_rect.left, last_rect.top, last_rect.right - last_rect.left, last_rect.bottom - last_rect.top, NULL); // 恢复最大化之前的窗口位置和大小
    full_screen_ = false;
}

启动程序,按下 Esc 键就可以观察效果了

5、所有代码

#include <windows.h>

#include <iostream>

bool full_screen_ = false;
HWND hwnd;
DWORD last_style;
RECT last_rect;
void RestoreFullScreen() {
    SetWindowLong(hwnd, GWL_STYLE, last_style); // 恢复最大化之前的窗口风格
    SetWindowPos(hwnd, NULL, last_rect.left, last_rect.top, last_rect.right - last_rect.left, last_rect.bottom - last_rect.top, NULL); // 恢复最大化之前的窗口位置和大小
    full_screen_ = false;
}

void FullScreen() {
    last_style = GetWindowLong(hwnd, GWL_STYLE); //存储上次的窗口风格
    GetWindowRect(hwnd, &last_rect);             //存储上次的窗口位置和大小
    int w = GetSystemMetrics(SM_CXSCREEN);
    int h = GetSystemMetrics(SM_CYSCREEN);       // 获取最大化的窗口大小
    SetWindowLongPtr(hwnd, GWL_STYLE, WS_VISIBLE | WS_POPUP); // 去掉标题栏
    SetWindowPos(hwnd, NULL, 0, 0, w, h, SWP_FRAMECHANGED); // 设置位置和大小
    full_screen_ = true;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_LBUTTONDOWN:
        break;
    case WM_PAINT:
        break;
    case WM_CLOSE:
        DestroyWindow(hwnd);  //销毁窗口
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_KEYDOWN:
        if (VK_ESCAPE == wParam) {
            if (full_screen_) {
                RestoreFullScreen();
            }
            else {
                FullScreen();
            }
        }
        break;
    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

int main(void) {
    HINSTANCE hInstance = GetModuleHandle(0);
    WNDCLASS wndcls;
    wndcls.cbClsExtra = 0;
    wndcls.cbWndExtra = 0;
    wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndcls.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndcls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndcls.hInstance = hInstance;
    wndcls.lpfnWndProc = WndProc;
    wndcls.lpszClassName = "TestWindow";
    wndcls.lpszMenuName = NULL;
    wndcls.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClass(&wndcls);
    hwnd = CreateWindow("TestWindow", "TestWindow", WS_OVERLAPPEDWINDOW,
        100, 100, 600, 400, NULL, NULL, hInstance, NULL);
    ShowWindow(hwnd, SW_SHOWNORMAL);
    UpdateWindow(hwnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

 

    原文作者:光头小杨
    原文地址: https://blog.csdn.net/yp18792574062/article/details/115336270
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞