C++ 什么发布WM_退出消息?
我刚刚开始学习NeHe的OpenGl教程,我已经编写了第一个教程中的所有代码。 该程序没有按我希望的方式运行,所以我开始调试它,发现有一条WM_退出消息发布在某处 我没有把它贴在任何地方,除了“案件结束:”(这从来没有发生过,我没有走那么远) 所以我只是想知道,有什么可能会发布WM_退出消息。我不是很熟悉C++,但是以前我做过很多java。 提前谢谢,对我的英语不好表示歉意 代码:C++ 什么发布WM_退出消息?,c++,windows,winapi,C++,Windows,Winapi,我刚刚开始学习NeHe的OpenGl教程,我已经编写了第一个教程中的所有代码。 该程序没有按我希望的方式运行,所以我开始调试它,发现有一条WM_退出消息发布在某处 我没有把它贴在任何地方,除了“案件结束:”(这从来没有发生过,我没有走那么远) 所以我只是想知道,有什么可能会发布WM_退出消息。我不是很熟悉C++,但是以前我做过很多java。 提前谢谢,对我的英语不好表示歉意 代码: #包括 #包括 #包括 #包括 #包括 //窗口/渲染变量 HGLRC hRC=NULL;//渲染上下文的句柄 H
#包括
#包括
#包括
#包括
#包括
//窗口/渲染变量
HGLRC hRC=NULL;//渲染上下文的句柄
HDC HDC=NULL;//设备上下文句柄
HWND HWND=NULL;//窗把手
HINSTANCE HINSTANCE=NULL;//应用程序实例的句柄
bool active=true;//窗口是否处于活动状态
bool fullscreen=true;//是全屏的吗
布尔键[256];//按键阵列
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);//窗口程序的声明
GLvoid ResizeGLScene(GLsizei width,GLsizei height)//调整和初始化GL窗口的大小
{
如果(高度==0)
{
高度=1;
}
glViewport(0,0,宽度,高度);
glMatrixMode(GL_投影);//这之后的代码将影响投影矩阵
glLoadIdentity();//重置当前矩阵(投影)
gluPerspective(45.0f,宽度/高度,0.1f,100.0f);//设置透视图并计算纵横比
glMatrixMode(GL_MODELVIEW);//之后的代码将影响MODELVIEW矩阵
glLoadIdentity();//重置当前矩阵(modelview)
}
int InitGL(GLvoid)//设置并初始化openGL
{
glShadeModel(GL_平滑);//将shademodel设置为平滑着色
glClearColor(1.0f,0.5f,0.5f,0.0f);//设置屏幕默认颜色rgb
glClearDepth(1.0f);//将默认深度设置为1并进行设置
glEnable(GL_DEPTH_TEST);//启用深度测试
glDepthFunc(GL_LEQUAL);//指示深度测试的类型
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);//最佳透视计算
返回true;
}
int DrawGLScene(GLvoid)//绘图
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//将屏幕设置为清晰颜色并清除深度缓冲区
glLoadIdentity();
glBegin(GL_QUADS);
GL3F(1.0f,0.0f,0.0f);
glVertex2f(100200);
GL3F(0.0f,1.0f,0.0f);
glVertex2f(200200);
GL3F(0.0f、0.0f、1.0f);
glVertex2f(200100);
GL3F(0.0f,0.0f,0.0f);
glVertex2f(100100);
格伦德();
返回true;
}
GLvoid KillGLWindow(GLvoid)
{
if(全屏)//是否全屏激活
{
ChangeDisplaySettings(NULL,0);//使用默认的DisplaySettings(无全屏)
ShowCursor(true);
}
if(hRC)//是否存在呈现上下文?
{
如果(!wglMakeCurrent(NULL,NULL))//可以释放设备上下文和呈现上下文吗?
{
消息框(NULL,“无法释放DC和RC”,“关机错误”,MB|U OK | MB|U图标信息);
}
如果(!wglDeleteContext(hRC))//可以删除呈现上下文吗
{
MessageBox(NULL,“无法释放DC和RC”,“关机错误”,MB|U OK | MB|图标信息);MessageBox(NULL,“无法删除渲染上下文”,“关机错误”,MB|U OK | MB|图标信息);
}
hRC=零;
}
如果(hDC&!ReleaseDC(hWnd,hDC))//可以释放设备上下文吗
{
MessageBox(空,“无法释放设备上下文”,“关机错误”,MB|U OK | MB|U图标信息);
}
如果(hWnd&!DestroyWindow(hWnd))//可以销毁窗口吗
{
MessageBox(NULL,“无法销毁窗口句柄”,“关闭错误”,MB|U OK | MB|U图标信息);
hWnd=NULL;
}
如果(!UnregisterClass(“OpenGL”,hInstance))
{
MessageBox(NULL,“无法注销类”,“关机错误”,MB|U OK | MB|U图标信息);
hInstance=NULL;
}
}
BOOL CreateGLWindow(字符*标题、整型宽度、整型高度、整型位、BOOL fullscreenflag)
{
GLuint像素格式;
WNDCLASS wc;
DWORD dwExStyle;//窗口扩展样式
DWORD dwStyle;//窗口样式
矩形窗口矩形;
WindowRect.left=0;
WindowRect.right=宽度;
WindowRect.top=0;
WindowRect.bottom=高度;
全屏=全屏标志;
hInstance=GetModuleHandle(NULL);
wc.style=CS|HREDRAW | CS|VREDRAW | CS|OWNDC;
wc.lpfnWndProc=(WNDPROC)WNDPROC;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(空,IDI_WINLOGO);
wc.hCursor=LoadCursor(空,IDC_箭头);
wc.hbrBackground=NULL;
wc.lpszMenuName=NULL;
wc.lpszClassName=“OpenGL”;
如果(!注册表类(&wc))
{
MessageBox(NULL,“无法注册窗口类”,“错误”,MB|U OK | MB|U IConnexClation);
返回false;
}
如果(全屏)
{
DEVMODE dmScreenSettings;//设备模式
memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
dmScreenSettings.dmSize=sizeof(dmScreenSettings);
dmScreenSettings.dmPelsHeight=宽度;
dmScreenSettings.dmPelsHeight=高度;
dmScreenSettings.dmBitsPerPel=位;
dmScreenSettings.dmFields=DM|u BITSPERPEL | DM|u PELSWIDTH | DM|PELSHEIGHT;
if(更改显示设置(&dmScreenSettings,CD\u全屏)!=显示更改成功)
{
if(MessageBox(NULL,“图形卡不支持全屏模式。是否要使用窗口模式?”,“OpenGL”,MB|YESNO | MB|ICONECLATION)=idies)
{
全屏=假;
}
其他的
{
消息框(NULL,“程序正在关闭”,“正在关闭”,MB|U OK | MB|U ICONSTOP);
返回false;
}
}
}
如果(全屏)
{
dwExStyle=WS_EX_APPWINDOW;
dwStyle=WS_弹出窗口;
ShowCursor(假);
}
其他的
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle=WS_重叠窗口;
}
调整WindowRectex(&WindowRect,dwStyle,false,dwExStyle);
如果(!(hWnd=CreateWindowEx(dwExStyle,
“OpenGL”,
标题
WS_CLIPSIBLINGS|
威斯康辛州
#include <Windows.h>
#include <gl\GL.h>
#include <gl\GLU.h>
#include <glaux.h>
#include <iostream>
// Window/Rendering vars
HGLRC hRC = NULL; // Handle to the rendering context
HDC hDC = NULL; // Handle to Device context
HWND hWnd = NULL; // Handle to the Window
HINSTANCE hInstance = NULL; // Handle to a instance of the application
bool active = true; // Is window active
bool fullscreen = true; // Is window fullscreen
bool keys[256]; // Array for keypresses
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration of the window procedure
GLvoid ResizeGLScene(GLsizei width, GLsizei height) //resize and initialize GL window
{
if(height == 0)
{
height = 1;
}
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); // Code after this will affect the projection matrix
glLoadIdentity(); // Reset the current matrix(projection)
gluPerspective(45.0f, width/height, 0.1f, 100.0f); // setting the perspective and calculating aspect ratio
glMatrixMode(GL_MODELVIEW); // Code after this will affect the modelview matrix
glLoadIdentity(); // Reset the current matrix(modelview)
}
int InitGL(GLvoid) //Setup and initialize openGL
{
glShadeModel(GL_SMOOTH); //Set shademodel to smooth shading
glClearColor(1.0f, 0.5f, 0.5f, 0.0f); //set the screen default color rgb
glClearDepth(1.0f); // set default depth to 1 and setup
glEnable(GL_DEPTH_TEST); // Enables depth test
glDepthFunc(GL_LEQUAL); // indicates the type of depth test
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // nicest perspective calculations
return true;
}
int DrawGLScene(GLvoid) //DRAWING
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // set the screen to the clear color and clears the depth buffer
glLoadIdentity();
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(100, 200);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(200, 200);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(200, 100);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(100, 100);
glEnd();
return true;
}
GLvoid KillGLWindow(GLvoid)
{
if(fullscreen) // is fuulscreen active
{
ChangeDisplaySettings(NULL, 0); // use default displaysetting(no fullscreen)
ShowCursor(true);
}
if(hRC) // is a rendering context present?
{
if(!wglMakeCurrent(NULL, NULL)) // can device context and rendering context be released?
{
MessageBox(NULL, "Couldn't Release DC And RC", "Shutdown Error", MB_OK | MB_ICONINFORMATION);
}
if(!wglDeleteContext(hRC)) // can rendering context be deleted
{
MessageBox(NULL, "Couldn't Release DC And RC", "Shutdown Error", MB_OK | MB_ICONINFORMATION);MessageBox(NULL, "Couldn't Delete Rendering Context", "Shutdown Error", MB_OK | MB_ICONINFORMATION);
}
hRC = NULL;
}
if(hDC && !ReleaseDC(hWnd, hDC)) // Can device context be released
{
MessageBox(NULL, "Couldn't Release Device Context", "Shutdown Error", MB_OK | MB_ICONINFORMATION);
}
if(hWnd && !DestroyWindow(hWnd)) // can window be destroyed
{
MessageBox(NULL, "Couldn't destroy window handle", "Shutdown Error", MB_OK | MB_ICONINFORMATION);
hWnd = NULL;
}
if(!UnregisterClass("OpenGL", hInstance))
{
MessageBox(NULL, "Couldn't Unregister Class", "Shutdown Error", MB_OK | MB_ICONINFORMATION);
hInstance = NULL;
}
}
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat;
WNDCLASS wc;
DWORD dwExStyle; //Window Extended Style
DWORD dwStyle; //Window Style
RECT WindowRect;
WindowRect.left = 0;
WindowRect.right = width;
WindowRect.top = 0;
WindowRect.bottom = height;
fullscreen = fullscreenflag;
hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "OpenGL";
if(!RegisterClass(&wc))
{
MessageBox(NULL, "Couldn't Register The Window Class", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return false;
}
if(fullscreen)
{
DEVMODE dmScreenSettings; //Device Mode
memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
dmScreenSettings.dmSize = sizeof(dmScreenSettings);
dmScreenSettings.dmPelsHeight = width;
dmScreenSettings.dmPelsHeight = height;
dmScreenSettings.dmBitsPerPel = bits;
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
if(MessageBox(NULL, "Fullscreen mode not supported by Graphics Card. Do you want to use windowed mode?", "OpenGL", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
{
fullscreen = false;
}
else
{
MessageBox(NULL, "Program is closing.", "CLOSING", MB_OK | MB_ICONSTOP);
return false;
}
}
}
if(fullscreen)
{
dwExStyle = WS_EX_APPWINDOW;
dwStyle = WS_POPUP;
ShowCursor(false);
}
else
{
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle = WS_OVERLAPPEDWINDOW;
}
AdjustWindowRectEx(&WindowRect, dwStyle, false, dwExStyle);
if(!(hWnd = CreateWindowEx(dwExStyle,
"OpenGL",
title,
WS_CLIPSIBLINGS |
WS_CLIPCHILDREN |
dwStyle,
0, 0,
WindowRect.right - WindowRect.left,
WindowRect.bottom - WindowRect.top,
NULL,
NULL,
hInstance,
NULL)))
{
KillGLWindow();
MessageBox(NULL, "Window creation error", "Error", MB_OK | MB_ICONEXCLAMATION);
return false;
}
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
bits,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
if(!(hDC = GetDC(hWnd)))
{
KillGLWindow();
MessageBox(NULL, "Can't create a Gl Device Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return false;
}
if(!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
{
KillGLWindow();
MessageBox(NULL, "Couldn't find the rigth pixelformat", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return false;
}
if(!SetPixelFormat(hDC, PixelFormat, &pfd))
{
KillGLWindow();
MessageBox(NULL, "Couldn't set pixelformat", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return false;
}
if(!(hRC = wglCreateContext(hDC)))
{
KillGLWindow();
MessageBox(NULL, "Can't create a Gl rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return false;
}
if(!wglMakeCurrent(hDC, hRC))
{
KillGLWindow();
MessageBox(NULL, "Can't activate a Gl rendering Context.", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return false;
}
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
ResizeGLScene(width, height);
if(!InitGL())
{
KillGLWindow();
MessageBox(NULL, "GL Initialization Failed", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return false;
}
return true;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_ACTIVATE:
{
if(!HIWORD(wParam))
{
active = true;
}
else
{
active = false;
}
return 0;
}
case WM_SYSCOMMAND:
{
switch(wParam)
{
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;
}
case WM_CLOSE:
{
std::cout << "Close" << std::endl;
PostQuitMessage(0);
return 0;
}
case WM_KEYDOWN:
{
keys[wParam] = true;
return 0;
}
case WM_KEYUP:
{
keys[wParam] = false;
return 0;
}
case WM_SIZE:
{
ResizeGLScene(LOWORD(lParam), HIWORD(lParam));
return 0;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
bool done = false;
if(MessageBox(NULL, "Run application in fullscreen mode?", "Initate Fullscreen", MB_YESNO | MB_ICONQUESTION) == IDNO)
{
fullscreen = false;
}
if(!CreateGLWindow("NeHe's OpenGl Framework", 800, 600, 16, fullscreen))
{
MessageBox(NULL, "framwork failed", "ERROR", MB_OK | MB_ICONEXCLAMATION);
return 0;
}
while(!done)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message = WM_QUIT)
{
std::cout << "EXIT" << std::endl;
done = false;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
std::cout << "Translate and Dispatch Msg" << std::endl;
}
}
else
{
if(active)
{
if(keys[VK_ESCAPE])
{
done = true;
}
else
{
DrawGLScene();
SwapBuffers(hDC);
}
}
if(keys[VK_F1])
{
keys[VK_F1] = false;
KillGLWindow();
fullscreen =! fullscreen;
if(!CreateGLWindow("NeHe's OpenGL Framework", 800, 600, 16, fullscreen))
{
return 0;
}
}
}
}
KillGLWindow();
return (msg.wParam);
}
if(msg.message = WM_QUIT)
if(msg.message == WM_QUIT)