【问题标题】:"hInstance" is undefined. c++ WIN32 APP“hInstance”未定义。 c++ WIN32 APP
【发布时间】:2015-03-03 12:18:10
【问题描述】:

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

代码。

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;
    }
}

【问题讨论】:

  • C++ 中不允许这样的嵌套函数。

标签: c++ winapi hwnd hinstance


【解决方案1】:

你的代码的结尾应该是:

}
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 定义上方添加右大括号

这样代码才能正确编译。

【讨论】:

    【解决方案2】:

    您的程序中有一个杂散的分号:

    ATOM MyRegisterClass(HINSTANCE hInstance); { // <-- Delete this
    

    尝试删除它,看看它是否可以解决问题。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 2014-02-28
      • 1970-01-01
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      相关资源
      最近更新 更多