视觉C++窗口创建失败
Visual C++ Window creation failed
我最近完成了一门关于C++的入门课程,并决定跳入Visual C++尝试做一些应用程序。
到目前为止,我想要的只是至少创建和显示一个窗口,没有进一步。我使用了许多Microsoft教程来获取我拥有的代码。
请注意,我有很多评论来帮助我理解我在做什么。我正在使用Microsoft Visual Studio 2015
#include <tchar.h> //A Win32 character string that can be used to describe ANSI, DBCS, or Unicode strings
//Enclose the strings in _T() if an incompatibility error occures or (LPCWSTR)L""
#include <windows.h>
// Global variable
HINSTANCE hinst;
//Function prototype for the window procedure so the inside of the program knows wazup
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
//Instead of your usual console program, you get WinMain() instead of int main(int argc,char *argv[])
INT WINAPI wWinMain(HINSTANCE hInst, //Instance of the program being executed
HINSTANCE hPrevInst, //Previous instance, not used much anymore
LPWSTR lpCmdLine, //Holds command line arguments
INT nShowCmd) //Tells how the program should start(minumised, maximised...)
{
/*Steps to create a window:
- Register a window class
- Create the window
- Show the window
- Setup a callback procedure
*/
//We can tell if the window class has failed if RegisterClassEx() returns 0.
//Fortunatley, there is another handy function, namely GetLastError().
//GetLastError() will return an error code, which can then be used to track down the cause of the error.
//https://msdn.microsoft.com/en-us/library/ms681381.aspx
if (!InitApplication(hInst))
{
int nResult = GetLastError();
MessageBox(NULL,
_T("Window class creation failed"),
_T("Window Class Failed"),
MB_ICONERROR);
return 1;
}
//Window creation & display:
if (!InitInstance(hInst, nShowCmd)){
int nResult = GetLastError();
MessageBox(NULL,
_T("Window creation failed"),
_T("Window Creation Failed"),
MB_ICONERROR);
return 1;
}
//The windows 'callback procedure' is necessary to continue
MSG msg; //Message handler
ZeroMemory(&msg, sizeof(MSG));
//Main loop for the program
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);//translates virtual key messages to character messages
DispatchMessage(&msg); //Sends any messages to the callvack procedure, so it can be handled
}
//Still returns an int!
return 0;
}
BOOL InitApplication(HINSTANCE hinstance)
{
//The window class defines the overall 'look and feel' of the window that we want to create.
//First we create an instance of a window class
WNDCLASSEX wClass;
//For more about WNDCLASSEX: https://msdn.microsoft.com/en-us/library/ms633577(VS.85).aspx
//Clearing all the parameters of the WNDCLASSEX instance wClass to zero
ZeroMemory(&wClass, sizeof(WNDCLASSEX));
//Now we are setting up all sotrts of parameters for our instance:
wClass.cbClsExtra = NULL; // cbClsExtra Additional parameters
wClass.cbSize = sizeof(WNDCLASSEX); // cbSize Specifies the size of the window class
wClass.cbWndExtra = NULL; // cbWndExtra Additional parameters
wClass.hbrBackground = (HBRUSH)COLOR_WINDOW; // hbrBackground Sets background color for the window
wClass.hCursor = LoadCursor(NULL, IDC_ARROW); // hCursor The cursor that will appear in the window
wClass.hIcon = NULL; // hIcon Icon for the window
wClass.hIconSm = NULL; // hIconSm Small icon for the window
wClass.hInstance = hinstance; // hInstance Handle to the application instance
wClass.lpfnWndProc = (WNDPROC)WinProc; // lpfnWndProc The callback procedure (more on that later)
wClass.lpszClassName = _T("Window Class"); // lpszClassName The unique name of the window class
wClass.lpszMenuName = NULL; // lpszMenuName Used for menus
wClass.style = CS_HREDRAW | CS_VREDRAW; // style The look and feel of the window
// Register the window class.
return RegisterClassEx(&wClass);
}
BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
{
// Save the application-instance handle.
hinst = hinstance;
// Create the main window.
//CreateWindowEx returns a HWND
HWND hwnd = CreateWindowEx( // A "handle" is a generic identifier + like "new"
0, // DWORD, extended window style of the window being created https://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx
_T("MainsWClass"), // LPCTSTR, A null-terminated string or a class atom created by a previous call to the RegisterClass or RegisterClassEx function.
_T("Sample"), // LPCTSTR, The window name.
WS_OVERLAPPEDWINDOW, // DWORD, Style of the window created
CW_USEDEFAULT, // int, X - default horizontal position
CW_USEDEFAULT, // int, Y - default vertical position
CW_USEDEFAULT, // int, nWidth - default width
CW_USEDEFAULT, // int, nHeight - default height
(HWND)NULL, // HWND, hWndParent - handle to the parrent or owner window of the window being created (opptional for pop-up)
(HMENU)NULL, // HMENU, hMenu
hinstance, // HINSTANCE, hInstance - A handle to the instance of the module to be associated with the window.
(LPVOID)NULL); // LPVOID, Pointer to a value to be passed to the window through the CREATESTRUCT structure
//If the creation fails, returns FALSE
if (!hwnd)
return FALSE;
else{
// Show the window and send a WM_PAINT message to the window procedure.
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
}
//callback procedure
LRESULT CALLBACK WinProc(
HWND hWnd, //Handle of the window that we want to monitor
UINT msg, //Message we are receiving
WPARAM wParam, //Additionnal info for the received message
LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY: //Message sent to the application if the program was closed using the X on the top of the window
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
您正在注册一个名为 "Window Class"
的窗口类,但尝试创建类 "MainsWClass"
的窗口。该类是未知的。您需要创建以前已注册的窗口类的窗口(由您自己创建,或像 "BUTTON"
这样的预定义窗口类之一)。
相关文章:
- 链接到自行创建的dll失败
- C/C++:socket() 创建在循环中失败,打开的文件太多
- 创建服务失败0x6f7存根收到错误数据
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- 使用 SQLConfig数据源创建 SQL Server DSN 失败:关键字-值对无效
- 创建进程 API 失败,在窗口 122 上出现错误代码 10
- fopen 在创建大量文件后因 EFBIG "File too large" 而失败
- C++从 std::string 创建 CString 失败
- 创建进程失败,意外错误
- lib OSMesa 屏幕外上下文创建在C++中失败,但仅在静态链接时失败
- C++ - 创建进程失败代码 2
- C++ Mingw32 创建进程() 失败,错误代码 2:系统找不到指定的文件
- 创建 ./config.data 在编译 Mozilla Firefox 时失败
- Qt OpenGL帧缓冲在一台PC上创建,但在另一台PC上失败
- 视觉C++窗口创建失败
- 用户创建程序失败
- 使用创建进程函数创建"dir"命令失败,错误代码为 2
- Vulkan-加载扩展时实例创建失败
- OpenGL 创建渲染上下文失败
- 创建对象时调试断言失败