"hInstance"未定义。C++ WIN32 应用程序
"hInstance" is undefined. c++ WIN32 APP
Win32 应用程序。在MyRegisterClass中,wc.hInsance = hInstance.显然"hInstane是一个未定义的idetifier。这是为什么呢?我使用的是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;
}
}
代码的结尾应该是:
}
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
这样代码就可以正确编译。
程序中有一个杂散的分号:
ATOM MyRegisterClass(HINSTANCE hInstance); { // <-- Delete this
尝试删除它,看看它是否能解决问题。
希望这有帮助!
相关文章:
- WM_CTLCOLORSTATIC从未在WIN32应用程序中触发
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- SWIG和Mingw-64与Python一起使用...不是有效的 Win32 应用程序
- 在 Win32 应用程序中承载 GTK+3 上下文
- 开发如何解决错误193:%1不是Dev C 中的Win32应用程序
- 您如何使用Winrt创建经典的Win32应用程序窗口
- 链接错误时,使用CLION/CMAKE/MSVC 2015编译WIN32应用程序
- 如何在VS 2017 C++Win32应用程序中打印出gethostname结果
- 我如何将Win32应用程序迁移到X64
- 如何制作一个不依赖msvc dll的win32应用程序
- 空的Win32 C++应用程序内存消耗过多
- 是否可以在 Visual Studio 中的 Win32 C++应用程序中的控制台中进行调试
- 从头开始创建C 背景Win32应用程序
- 托管C#Win32应用程序的非托管DLL.与课堂入口
- Exe不是有效的win32应用程序,但在64位上运行
- 具有控制台输出且没有新窗口的 Win32 应用程序
- 使用 C++11 编写 Win32 应用程序
- 使用类设置 win32 应用程序的更好方法
- 用于 Win32 应用程序的 WriteFile() 函数
- 调用 C 代码的 Win32 应用程序