c++ - 在 Windows 中使用 cpp 的窗口函数

标签 c++ windows opengl codeblocks

我一直在使用 www.cplusplus.com 使用 cpp 学习 OpenGL。我一直在使用代码块,程序是这样的

/*  Trim fat from windows*/
#define WIN32_LEAN_AND_MEAN
#pragma comment(linker, "/subsystem:windows")
/*  Pre-processor directives*/
#include "stdafx.h"
#include <windows.h>
#include <iostream>
/*  Windows Procedure Event Handler*/
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{   
PAINTSTRUCT paintStruct;
    /*  Device Context*/
    HDC hDC;
    /*  Text for display*/
    char string[] = "Hello, World!";
    /*  Switch message, condition that is met will execute*/
    switch(message)
    {
        /*  Window is being created*/
        case WM_CREATE:
            return 0;
            break;
        /*  Window is closing*/
        case WM_CLOSE:
            PostQuitMessage(0);
            return 0;
            break;
        /*  Window needs update*/
        case WM_PAINT:
            hDC = BeginPaint(hwnd,&paintStruct);
            /*  Set txt color to blue*/
            SetTextColor(hDC, COLORREF(0x00FF0000));
            /*  Display text in middle of window*/
            TextOut(hDC,150,150,string,sizeof(string)-1);
            EndPaint(hwnd, &paintStruct);
            return 0;
            break;
        default:
            break;
    }
    return (DefWindowProc(hwnd,message,wParam,lParam));
}
/*  Main function*/
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    WNDCLASSEX  windowClass;        //window class
    HWND        hwnd;               //window handle
    MSG         msg;                //message
    bool        done;               //flag saying when app is complete
    /*  Fill out the window class structure*/
    windowClass.cbSize = sizeof(WNDCLASSEX);
    windowClass.style = CS_HREDRAW | CS_VREDRAW;
    windowClass.lpfnWndProc = WndProc;
    windowClass.cbClsExtra = 0;
    windowClass.cbWndExtra = 0;
    windowClass.hInstance = hInstance;
    windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    windowClass.lpszMenuName = NULL;
    windowClass.lpszClassName = "MyClass";
    windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
    /*  Register window class*/
    if (!RegisterClassEx(&windowClass))
    {
        return 0;
    }
    /*  Class registerd, so now create window*/
    hwnd = CreateWindowEx(NULL,     //extended style
        "MyClass",          //class name
        "A Real Win App",       //app name
        WS_OVERLAPPEDWINDOW |       //window style
        WS_VISIBLE |
        WS_SYSMENU,
        100,100,            //x/y coords
        400,400,            //width,height
        NULL,               //handle to parent
        NULL,               //handle to menu
        hInstance,          //application instance
        NULL);              //no extra parameter's
    /*  Check if window creation failed*/
    if (!hwnd)
        return 0;
    done = false; //initialize loop condition variable
    /*  main message loop*/
    while(!done)
    {
        PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE);
        if (msg.message == WM_QUIT) //check for a quit message
        {
            done = true; //if found, quit app
        }
        else
        {
            /*  Translate and dispatch to event queue*/
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return msg.wParam;
}

我编译了这个,最后得到了 SetTextColor、TextOut 和 GetStockObject 是“ undefined reference ”的链接器错误。我一直在包括所有必需的文件,但最终一次又一次地陷入同样的​​麻烦。

我一直在包含“stdafx.h”,但没有成功。我再次陷入同样的​​麻烦。有什么办法可以避免这个错误?

最佳答案

由于某些原因我不能在这里发表评论,所以我会把它放在一个答案中:

你真的是说这是一个编译器错误(“未定义”)?因为如果它是一个链接器错误(“undefined reference to ...”),这意味着你没有包含相应的库(我相信是 gdi32)。

关于c++ - 在 Windows 中使用 cpp 的窗口函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17747028/

相关文章:

opengl - 计算着色器共享内存包含工件

c++ - CreateWindowEx WS_POPUP 为什么要打边框?

C++/模板/GCC 4.0 错误?

python - Python中的CSV在Windows上添加了一个额外的回车符

windows - Windows 可以处理跨越 32 位/64 位边界的继承吗?

windows - FLS 与 TLS,我可以使用光纤本地存储代替 TLS 吗?

opengl - VBO 与立即模式性能

c++ - 冒泡排序、选择排序和插入排序

c++ - 如何多线程呢?

c++ - 使用 map.end() 访问 map 的最后一个元素