c++ - win32 程序在 GCC 中编译但不是 G++ HDC 错误

标签 c++ c winapi

我有一个用于基本 opengl 绘图的简单 win32 程序。它会用 gcc 编译,但如果我使用 g++,它会给我错误:

minimal.c: In function 'HWND__* CreateOpenGLWindow(char*, int, int, int, int, BYTE, DWORD)':
minimal.c:130:24: error: cannot convert 'HDC {aka HDC__*}' to 'HWND {aka HWND__*}' for argument '1' to 'int ReleaseDC(HWND, HDC)'
     ReleaseDC(hDC, hWnd);
                        ^
minimal.c: In function 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)':
minimal.c:160:24: error: cannot convert 'HDC {aka HDC__*}' to 'HWND {aka HWND__*}' for argument '1' to 'int ReleaseDC(HWND, HDC)'
     ReleaseDC(hDC, hWnd);

不知道为什么用gcc可以编译,用g++就编译失败?

代码如下:

#include <windows.h>            /* must include this before GL/gl.h */
#include <GL/gl.h>          /* OpenGL header file */
#include <GL/glu.h>         /* OpenGL utilities header file */
#include <stdio.h>


void
display()
{
    /* rotate a triangle around */
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex2i(0,  1);
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex2i(-1, -1);
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex2i(1, -1);
    glEnd();
    glFlush();
}

LONG WINAPI
WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{ 
    static PAINTSTRUCT ps;

    switch(uMsg) {
    case WM_PAINT:
    display();
    BeginPaint(hWnd, &ps);
    EndPaint(hWnd, &ps);
    return 0;

    case WM_SIZE:
    glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
    PostMessage(hWnd, WM_PAINT, 0, 0);
    return 0;

    case WM_CHAR:
    switch (wParam) {
    case 27:            /* ESC key */
        PostQuitMessage(0);
        break;
    }
    return 0;

    case WM_CLOSE:
    PostQuitMessage(0);
    return 0;
    }

    return DefWindowProc(hWnd, uMsg, wParam, lParam); 
} 



HWND
CreateOpenGLWindow(char* title, int x, int y, int width, int height, 
           BYTE type, DWORD flags)
{
    int         pf;
    HDC         hDC;
    HWND        hWnd;
    WNDCLASS    wc;
    PIXELFORMATDESCRIPTOR pfd;
    static HINSTANCE hInstance = 0;

    /* only register the window class once - use hInstance as a flag. */
    if (!hInstance) {
    hInstance = GetModuleHandle(NULL);
    wc.style         = CS_OWNDC;
    wc.lpfnWndProc   = (WNDPROC)WindowProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_WINLOGO);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = NULL;
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "OpenGL";

    if (!RegisterClass(&wc)) {
        MessageBox(NULL, "RegisterClass() failed:  "
               "Cannot register window class.", "Error", MB_OK);
        return NULL;
    }
    }

    hWnd = CreateWindow("OpenGL", title, WS_OVERLAPPEDWINDOW |
            WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
            x, y, width, height, NULL, NULL, hInstance, NULL);

    if (hWnd == NULL) {
    MessageBox(NULL, "CreateWindow() failed:  Cannot create a window.",
           "Error", MB_OK);
    return NULL;
    }

    hDC = GetDC(hWnd);

    /* there is no guarantee that the contents of the stack that become
       the pfd are zeroed, therefore _make sure_ to clear these bits. */
    memset(&pfd, 0, sizeof(pfd));
    pfd.nSize        = sizeof(pfd);
    pfd.nVersion     = 1;
    pfd.dwFlags      = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | flags;
    pfd.iPixelType   = type;
    pfd.cColorBits   = 32;

    pf = ChoosePixelFormat(hDC, &pfd);
    if (pf == 0) {
    MessageBox(NULL, "ChoosePixelFormat() failed:  "
           "Cannot find a suitable pixel format.", "Error", MB_OK); 
    return 0;
    } 

    if (SetPixelFormat(hDC, pf, &pfd) == FALSE) {
    MessageBox(NULL, "SetPixelFormat() failed:  "
           "Cannot set format specified.", "Error", MB_OK);
    return 0;
    } 

    DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);

    ReleaseDC(hDC, hWnd);

    return hWnd;
}    



int APIENTRY
WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst,
    LPSTR lpszCmdLine, int nCmdShow)
{
    HDC hDC;                /* device context */
    HGLRC hRC;              /* opengl context */
    HWND  hWnd;             /* window */
    MSG   msg;              /* message */

    hWnd = CreateOpenGLWindow("minimal", 0, 0, 256, 256, PFD_TYPE_RGBA, 0);
    if (hWnd == NULL)
    exit(1);

    hDC = GetDC(hWnd);
    hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);

    ShowWindow(hWnd, nCmdShow);

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

    wglMakeCurrent(NULL, NULL);
    ReleaseDC(hDC, hWnd);
    wglDeleteContext(hRC);
    DestroyWindow(hWnd);

    return msg.wParam;
}

有人可以解释为什么 g++ 的编译器会产生 HWND HDC 错误,而 gcc 编译器会接受该代码吗?

最佳答案

ReleaseDC(hDC, hWnd);

应该是

ReleaseDC(hWnd, hDC);

关于c++ - win32 程序在 GCC 中编译但不是 G++ HDC 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54657652/

相关文章:

c++ - gdb 只显示发生错误的函数(segmentation fault)

c++ unordered_map 冲突处理、调整大小和重新散列

java - Raspberry Pi 路由器上的组播

c++ - 是否可以修改 unique_ptr 以用于纯 c?

java - JNA 按名称从进程获取模块

windows - 为什么要初始化一个对象为空

c# - 将 C# 字节数组转换为 C++

c - 如何找到 BST 中小于或等于给定值的节点数? (AVL 树)

c - 库存 GTK 按钮不显示?

c++ - 写入文件,给出不正确的名称