visualc&x2B+;窗口创建失败 最近我完成了一个关于C++的入门课,决定跳进Visual C++中,尝试做一个小应用程序。
到目前为止,我想做的就是至少创建并显示一个窗口,没有做进一步的工作。 我使用了许多Microsoft教程来获取我的代码 请注意,我有很多意见来帮助我理解我在做什么。 我正在使用Microsoft Visual Studio 2015visualc&x2B+;窗口创建失败 最近我完成了一个关于C++的入门课,决定跳进Visual C++中,尝试做一个小应用程序。,c++,winapi,visual-c++,visual-studio-2015,C++,Winapi,Visual C++,Visual Studio 2015,到目前为止,我想做的就是至少创建并显示一个窗口,没有做进一步的工作。 我使用了许多Microsoft教程来获取我的代码 请注意,我有很多意见来帮助我理解我在做什么。 我正在使用Microsoft Visual Studio 2015 #include <tchar.h> //A Win32 character string that can be used to describe ANSI, DBCS, or Unicode strings
#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);
}
#包括//一个Win32字符串,可用于描述ANSI、DBCS或Unicode字符串
//如果发生不兼容错误或(LPCWSTR)L“”,请将字符串括在_T()中
#包括
//全局变量
HINSTANCE hinst;
//窗口程序的函数原型,以便程序内部了解wazup
LRESULT回调WinProc(HWND-HWND,UINT消息,WPARAM-WPARAM,LPARAM-LPARAM);
BOOL初始化应用程序(HINSTANCE);
BOOL InitInstance(HINSTANCE,int);
//使用WinMain()代替int main(int argc,char*argv[]),而不是通常的控制台程序
INT WINAPI wWinMain(HINSTANCE hInst,//正在执行的程序的实例
HINSTANCE hPrevInst,//以前的实例,不再经常使用
LPWSTR lpCmdLine,//保存命令行参数
INT nShowCmd)//告诉程序应该如何启动(最小化、最大化…)
{
/*创建窗口的步骤:
-注册窗口类
-创建窗口
-显示窗口
-设置回调过程
*/
//如果RegisterClassEx()返回0,我们可以判断window类是否失败。
//幸运的是,还有另一个方便的函数,即GetLastError()。
//GetLastError()将返回一个错误代码,然后可以使用该代码跟踪错误原因。
//https://msdn.microsoft.com/en-us/library/ms681381.aspx
if(!InitApplication(hInst))
{
int nResult=GetLastError();
MessageBox(空,
_T(“窗口类创建失败”),
_T(“窗口类失败”),
MB_(错误);
返回1;
}
//窗口创建和显示:
if(!InitInstance(hInst,nShowCmd)){
int nResult=GetLastError();
MessageBox(空,
_T(“窗口创建失败”),
_T(“窗口创建失败”),
MB_(错误);
返回1;
}
//要继续,必须执行windows“回调过程”
MSG;//消息处理程序
零内存(&msg,sizeof(msg));
//程序的主循环
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);//将虚拟密钥消息转换为字符消息
DispatchMessage(&msg);//将任何消息发送到callvack过程,以便对其进行处理
}
//仍然返回一个int!
返回0;
}
BOOL初始化应用程序(HINSTANCE HINSTANCE)
{
//window类定义了我们想要创建的窗口的整体“外观”。
//首先,我们创建一个窗口类的实例
WNDCLASSEX WCLASSE;
//有关WNDCLASSEX的更多信息:https://msdn.microsoft.com/en-us/library/ms633577(第85节)
//将WNDCLASSEX实例wClass的所有参数清除为零
零内存(&wClass,sizeof(WNDCLASSEX));
//现在,我们正在为我们的实例设置所有SORTS参数:
wClass.cbClsExtra=NULL;//cbClsExtra其他参数
wClass.cbSize=sizeof(WNDCLASSEX);//cbSize指定窗口类的大小
wClass.cbWndExtra=NULL;//cbWndExtra其他参数
wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;//hbrBackground设置窗口的背景色
wClass.hCursor=LoadCursor(NULL,IDC_箭头);//hCursor将显示在窗口中的光标
wClass.hIcon=NULL;//窗口的hIcon图标
wClass.hIconSm=NULL;//窗口的hIconSm小图标
wClass.hInstance=hInstance;//应用程序实例的hInstance句柄
wClass.lpfnWndProc=(WNDPROC)WinProc;//lpfnWndProc回调过程(稍后详细介绍)
wClass.lpszClassName=_T(“窗口类”);//lpszClassName窗口类的唯一名称
wClass.lpszMenuName=NULL;//用于菜单的lpszMenuName
wClass.style=CS|HREDRAW | CS|u VREDRAW;//为窗口的外观和感觉设置样式
//注册窗口类。
返回寄存器类(&W类);
}
BOOL InitInstance(HINSTANCE HINSTANCE,int nCmdShow)
{
//保存应用程序实例句柄。
hinst=hinstance;
//创建主窗口。
//CreateWindowEx返回一个HWND
HWND HWND=CreateWindowEx(//句柄是一个通用标识符+类似于“new”
0,//DWORD,正在创建的窗口的扩展窗口样式https://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx
_T(“MainsWClass”),//LPCTSTR,一个以null结尾的字符串或一个由先前调用RegisterClass或RegisterClass函数创建的类原子。
_T(“Sample”),//LPCTSTR,窗口名。
WS_OVERLAPPEDWINDOW,//DWORD,创建的窗口的样式
CW_usefault,//int,X-默认水平位置
CW\u usefault,//int,Y-默认垂直位置
CW_usefault,//int,nWidth-默认宽度
CW_usefault,//int,nHeight-默认高度
(HWND)NULL,//HWND,hWndParent-正在创建的窗口的parrent或owner窗口的句柄(弹出选项)
(humenu)NULL,//humenu,humenu
hinstance,//hinstance,hinstance-要关联的模块实例的句柄