c++ - "hInstance"未定义。 C++ WIN32 APP

标签 c++ winapi hwnd hinstance

Win32 应用程序。在 MyRegisterClass 中,wc.hInsance = hInstance。显然“hInstane 是一个未定义的标识符。这是为什么?我使用的是 Visual Studio 2013,我正在阅读 Jonathan S Harbors 关于游戏编程的书。

代码。

include <Windows.h>
include <time.h>
include <iostream>

using namespace std;

const string APPTITLE = "Game Loop";
HWND window;
HDC device;

bool gameover = false;

void DrawBitmap(char *filename, int x, int y){
    HBITMAP image = (HBITMAP)LoadImage(0, "c.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

    BITMAP bm;
    GetObject(image, sizeof(BITMAP), &bm);

    HDC hdcImage = CreateCompatibleDC(device);
    SelectObject(hdcImage, image);

    BitBlt(
           device,
           x, y,
           bm.bmWidth, bm.bmHeight,
           hdcImage,
           0, 0,
           SRCCOPY);

    DeleteDC(hdcImage);
    DeleteObject((HBITMAP)image);
}

bool Game_Init(){

    srand(time(NULL));

    return 1;
}

void Game_Run(){

    if (gameover == true)return;

    RECT rect;
    GetClientRect(window, &rect);
    int x = rand() % (rect.right - rect.left);
    int y = rand() % (rect.bottom - rect.top);
    DrawBitmap("c.bmp", x, y);
}

void Game_End(){
    ReleaseDC(window, device);
}

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
    switch (message){

        case WM_DESTROY:{
            gameover = true;
            PostQuitMessage(0);
            break;
        }
            return DefWindowProc(hWnd, message, wParam, lParam);
    }

    ATOM MyRegisterClass(HINSTANCE hInstance); {
        WNDCLASSEX wc;
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = (WNDPROC)WinProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = NULL;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = APPTITLE.c_str();
        wc.hIconSm = NULL;
    }
}

最佳答案

代码的结尾应该是:

}
ATOM MyRegisterClass(HINSTANCE hInstance) {
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WinProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = APPTITLE.c_str();
    wc.hIconSm = NULL;
    return ::RegisterClassEx(&wc);
}

注意:

  • MyRegisterClass 定义中没有分号
  • MyRegisterClass 末尾只有一个大括号
  • MyRegisterClass 定义上方添加右大括号

这样代码可以正确编译。

关于c++ - "hInstance"未定义。 C++ WIN32 APP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27788369/

相关文章:

c++ - 程序集 :are they faster than global variables? 中的局部变量

c++ - 创建具有固定顶部坐标的可调整大小的窗口

c# HwndSourceHook 与 Windows.Forms

windows - 确定窗口属于桌面还是 Windows 应用商店应用

c++ - 为什么有些语句在 "Release"中产生警告,而在GCC的 "Debug"模式编译中却没有?

c++ - std::enable_if 有条件地编译一个成员函数

c - "HANDLE"与类型为 "HINSTANCE"的参数不兼容

c++ - 有哪些用于非 MFC、Win32 C++ 应用程序的 *slick* UI 库/框架/工具?

c++ - 过滤图上的 boost::astar_search

c++ - 依赖类型 : Template argument deduction failed