8

我正在尝试创建一个用 OpenGL 视口填充屏幕的无边框窗口。问题是,当我将窗口和视口设置为与桌面相同的大小时,窗口在失去和获得焦点、退出和创建时会闪烁黑色。这可能与 Windows 将窗口设置为某种真正的“全屏”模式有关。这似乎不会发生在使用这种类型的无边框全屏窗口的其他应用程序(DirectX?)中。

我相信SFML 有/有过这个问题的变种

有一个解决方法:不要将窗口设置为桌面的确切大小。例如,使窗口的宽度比桌面的宽度大一个像素。

下面是一个没有错误检查的SSCCE,它显示了我在说什么。这将创建一个无边框的全屏窗口,其桌面大小的 OpenGL 视口显示出难看的绿色(使用 退出AltF4)。您可以更改仅向窗口宽度添加一个额外像素的行#define FIX_BUG 0#define FIX_BUG 1以查看我想要的行为是什么样的。

#define WIN32_LEAN_AND_MEAN
#include "windows.h"

#pragma comment(lib, "OpenGL32.lib")

#define FIX_BUG 0

void MyRegisterClass(HINSTANCE hInstance);
HWND MyCreateWindow(HINSTANCE hInstance);
void MySetupOpenGLContext(HWND hWnd);
void MySetPixelFormat(HDC hDC);
RECT MyGetDesktopRect();
void MyLoadOpenGLFunctions();

#define APIENTRYP APIENTRY *
#define GLAPI extern
#define GL_COLOR 0x1800

typedef int GLint;
typedef int GLsizei;
typedef unsigned int GLenum;
typedef float GLfloat;

typedef void (APIENTRYP PFNGLVIEWPORTPROC) (GLint x, GLint y, GLsizei width, GLsizei height);
PFNGLVIEWPORTPROC glViewport;

typedef void (APIENTRYP PFNGLCLEARBUFFERFVPROC) (GLenum buffer, GLint drawbuffer, const GLfloat *value);
PFNGLCLEARBUFFERFVPROC glClearBufferfv;

static const char *windowClass = "CLASS";
static const char *windowTitle = "TITLE";

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
    MyRegisterClass(hInstance);
    const HWND hWnd = MyCreateWindow(hInstance);
    MySetupOpenGLContext(hWnd);
    ShowWindow(hWnd, nCmdShow);
    HDC hDC = GetDC(hWnd);

    MSG msg;
    while (IsWindow(hWnd)) {
        while (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) DispatchMessage(&msg);

        const GLfloat color[] = {0.5f, 0.5f, 0.0f, 1.0f};
        glClearBufferfv(GL_COLOR, 0, color);

        SwapBuffers(hDC);
    }

    return 0;
}

void MyRegisterClass(HINSTANCE hInstance) {
    WNDCLASSEX wcex = {0};

    wcex.cbSize = sizeof wcex;
    wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wcex.lpfnWndProc = &DefWindowProc;
    wcex.hInstance = hInstance;
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH) (COLOR_WINDOWFRAME + 1);
    wcex.lpszClassName = windowClass;

    RegisterClassEx(&wcex);
}

HWND MyCreateWindow(HINSTANCE hInstance) {
    RECT dRect = MyGetDesktopRect();
    return CreateWindow(windowClass, windowTitle, WS_POPUP,
                        0, 0, dRect.right + FIX_BUG, dRect.bottom,
                        NULL, NULL, hInstance, NULL);
}

void MySetupOpenGLContext(HWND hWnd) {
    HDC hDC = GetDC(hWnd);
    MySetPixelFormat(hDC);
    HGLRC hGLRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hGLRC);
    MyLoadOpenGLFunctions();
    RECT dRect = MyGetDesktopRect();
    glViewport(0, 0, dRect.right, dRect.bottom);
}

void MySetPixelFormat(HDC hDC) {
    PIXELFORMATDESCRIPTOR pfd = {0};

    pfd.nSize = sizeof pfd;
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 24;
    pfd.cStencilBits = 8;
    pfd.iLayerType = PFD_MAIN_PLANE;

    int format = ChoosePixelFormat(hDC, &pfd);
    SetPixelFormat(hDC, format, &pfd);
}

RECT MyGetDesktopRect() {
    RECT desktopRect;
    const HWND hDesktop = GetDesktopWindow();
    GetWindowRect(hDesktop, &desktopRect);
    return desktopRect;
}

void MyLoadOpenGLFunctions() {
    glViewport = (PFNGLVIEWPORTPROC) GetProcAddress(GetModuleHandleA("OpenGL32.dll"), "glViewport");
    glClearBufferfv = (PFNGLCLEARBUFFERFVPROC) wglGetProcAddress("glClearBufferfv");
}

Github 要点| 原始文本

为什么会这样?有没有办法通过将窗口设置为桌面的确切大小来获得我想要的行为?

我的平台:Visual Studio 2013 / 64 位 Windows 8 / AMD Radeon HD6850。

4

0 回答 0