取得GLUT 的glutCreateWindow的HWND

本文介绍了使用GLUT库创建窗口的过程,并通过示例代码展示了如何获取当前活动窗口句柄及如何通过GLUT创建并查找窗口。

不過跟我本來想做的有點點不一樣就是了
取得GLUT 的glutCreateWindow的HWND

1.
HWND hwnd = GetActiveWindow();


2.
char name[]="mainWindow";

glutCreateWindow(name);
HWND hwnd;
hwnd=FindWindow(NULL,name);

 

#include<GL/gl.h> #include<windows.h> #include<iostream> #include<GL/glu.h> using namespace std; LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){ switch(msg){ case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,msg,wParam,lParam); } return 0; } BOOL SetupPixelFormat(HDC hdc){ PIXELFORMATDESCRIPTOR pfd; int pixelformat; ZeroMemory(&pfd.bReserved,sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion=1; pfd.iPixelType=PFD_TYPE_RGBA; pfd.cColorBits=32; pfd.cDepthBits=32; pfd.iLayerType=PFD_MAIN_PLANE; pixelformat=ChoosePixelFormat(hdc,&pfd); if(pixelformat==0){ return FALSE; } if(SetPixelFormat(hdc,pixelformat,&pfd)==FALSE) return FALSE; return TRUE; } void InitOpenGL(HDC hdc){ HGLRC hrc=wglCreateContext(hdc); wglMakeCurrent(hdc,hrc); glClearColor(0.0f,0.0f,0.0f,1.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, 800.0f / 600.0f, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); } int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){ WNDCLASS wc; HWND hwnd; MSG msg; ZeroMemory(&wc,sizeof(WNDCLASS)); wc.lpfnWndProc=WndProc; wc.hInstance=hInstance; wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.lpszClassName="OpenGLWindow"; RegisterClass(&wc); hwnd=CreateWindow("OpenGLWindow","OpenGL 3D Window",WS_OVERLAPPEDWINDOW,100,100,800,600,NULL,NULL,hInstance,NULL); ShowWindow(hwnd, nCmdShow); HDC hdc = GetDC(hwnd); SetupPixelFormat(hdc); InitOpenGL(hdc); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -5.0f); glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(0.5f, -0.5f, 0.0f); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(0.0f, 0.5f, 0.0f); glEnd(); SwapBuffers(hdc); } wglMakeCurrent(NULL, NULL); wglDeleteContext(wglGetCurrentContext()); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); UnregisterClass("OpenGLWindow", hInstance); return msg.wParam; } 返回值:Process exited after 2.585 seconds with return value 3221225477
08-30
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值