问题描述
我是C ++和WinApi的新手.我无法在WinApi中创建一个简单的窗口. CreateWindow()函数返回null. GetLastError()函数返回错误1813.但是在创建窗口之前,GetLastError()返回0.对不起,我的英语.这是我的完整代码:
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
LPCWSTR szWindowClass = TEXT("WndClass");
LPCWSTR szTitle = TEXT("Main window");
DWORD dwError;
WNDCLASS wc;
wc.style = CS_OWNDC;
wc.hInstance = hInstance;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.lpfnWndProc = WndProc;
wc.lpszClassName = szWindowClass;
wc.lpszMenuName = L"MenuName";
dwError = GetLastError(); //0
RegisterClass(&wc);
dwError = GetLastError();//0
HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);//NULL
dwError = GetLastError();//1813 =(
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
return 0;
}
首先,您的错误处理错误.文档告诉您仅在CreateWindow
失败时才调用GetLastError
.并且CreateWindow
失败由返回值NULL
指示.调用GetLastError
之前必须检查CreateWindow
的返回值.请确保您仔细阅读文档. >
您在调用RegisterClass
时犯了完全相同的错误.为了您的辩护,这是Win32程序员最常犯的错误.
错误代码1813是ERROR_RESOURCE_TYPE_NOT_FOUND
. 文档说:
同样,一旦您知道要看的地方,就可以通过阅读文档来学习此信息.
这意味着CreateWindow
试图查找文件中不存在的资源.也许您没有设法链接菜单资源.
您的窗口操作也有问题.应该是:
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
当您开始为某些消息添加定制处理时,请确保您仍对其他消息调用DefWindowProc
.
I'm new in C++ and WinApi. I can't create a simple window in WinApi. CreateWindow() function returns null. GetLastError() func returns error 1813.But before creating window GetLastError() returns 0. Sorry for my English. Here is my full code:
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
LPCWSTR szWindowClass = TEXT("WndClass");
LPCWSTR szTitle = TEXT("Main window");
DWORD dwError;
WNDCLASS wc;
wc.style = CS_OWNDC;
wc.hInstance = hInstance;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.lpfnWndProc = WndProc;
wc.lpszClassName = szWindowClass;
wc.lpszMenuName = L"MenuName";
dwError = GetLastError(); //0
RegisterClass(&wc);
dwError = GetLastError();//0
HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);//NULL
dwError = GetLastError();//1813 =(
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
return 0;
}
First of all, your error handling is wrong. The documentation tells you to call GetLastError
only if CreateWindow
failed. And CreateWindow
failure is indicated by a return value of NULL
. You must check the return value of CreateWindow
before calling GetLastError
. Please make sure you read the documentation carefully.
You make the exact same mistake in your call to RegisterClass
. In your defence, this is the most common mistake made by novice Win32 programmers.
Error code 1813, is ERROR_RESOURCE_TYPE_NOT_FOUND
. The documentation says:
Again, you can learn this information by reading the documentation, once you know where to look.
What this means is that CreateWindow
is trying to find a resource that is not present in the file. Perhaps you did not manage to link a menu resource.
Your window procedure is also defective. It should be:
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
When you start to add bespoke handling for certain messages, ensure that you still call DefWindowProc
for any other messages.
这篇关于调用CreateWindow()函数WinApi时出现错误1813的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!