首页
登录 | 注册

OpenGL(一) 使用Win API 建立黑色窗口

先来创建一个黑色的窗口吧,作为GL的容器。


Light3dWinAPP 这个类来处理窗口的创建和销毁以及分发 系统事件。

/**********************
*File:Light3dWinAPP.h;
*Author:chenpeng
*Data:2015/07/21;
*/

#include<Windows.h>
#include<tchar.h>

namespace Light3d
{
	class Light3dWinAPP
	{
	public :
		HINSTANCE m_hInstance;	//实例句柄;

		HWND m_hWnd;	//窗口句柄;

		int m_width;	//宽度;

		int m_height;	//高度;

	public:
		Light3dWinAPP(HINSTANCE hInstance) :m_hInstance(hInstance)
		{
			//想要建立一个窗口,必须有一个窗口类;
			WNDCLASSEX winClass;
			//窗口类名;
			winClass.lpszClassName = _T("Light3dWinAPP");
			winClass.cbSize = sizeof(winClass);

			//当H 水平方向,V 竖直方向 大小变化或移动时,重绘窗口,
			//为该类中的每一个窗口分配一个唯一的设备上下文;
			winClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;

			//指向函数的长指针lp,处理窗口的消息循环;
			winClass.lpfnWndProc = wndProc;

			//实例句柄;
			winClass.hInstance = hInstance;
			//窗口最小化图标;
			winClass.hIcon = 0;
			winClass.hIconSm = 0;
			//鼠标光标;
			winClass.hCursor = LoadCursor(hInstance, IDC_ARROW);
			//窗口背景色;
			winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
			//窗口菜单;
			winClass.lpszMenuName = nullptr;
			winClass.cbClsExtra = 0;
			winClass.cbWndExtra = 0;
			RegisterClassEx(&winClass);
		}



		virtual ~Light3dWinAPP()
		{
			UnregisterClass(_T("CELLWinAPP"), m_hInstance);
		}

	protected:
		static LRESULT CALLBACK wndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
		{
			Light3dWinAPP* pThis = (Light3dWinAPP*)GetWindowLong(hWnd, GWL_USERDATA);
			if (pThis)
			{
				return pThis->onEvent(hWnd, msg, wParam, lParam);
			}
			if (WM_CREATE == msg)
			{
				CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
				SetWindowLong(hWnd, GWL_USERDATA,(DWORD_PTR)pCreate->lpCreateParams);

			}
			return DefWindowProc(hWnd, msg, wParam, lParam);
		}


	public:
		//窗口事件;
		virtual LRESULT onEvent(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
		{
			switch (msg)
			{
			case WM_CLOSE:
			case WM_DESTROY:
			{
							   ::PostQuitMessage(0);
			}
				break;
			case WM_MOUSEMOVE:
				break;
			case WM_LBUTTONDOWN:
				break;
			case WM_LBUTTONUP:
				break;
			case WM_RBUTTONDOWN:
				break;
			case WM_RBUTTONUP:
				break;
			case WM_KEYDOWN:
				{
							   switch (wParam)
								{
							   case VK_LEFT:
							   {
									
							   }
										break;
							   case VK_RIGHT:
								   break;
							   case VK_UP:
								   break;
									default:
										break;
								}

				}
				break;
			case WM_KEYUP:
				break;
			default:
				return DefWindowProc(hWnd, msg, wParam, lParam);
			}

			return S_OK;
		}


		//主函数;
		int main(int width, int height)
		{
			//创建窗口;
			m_hWnd = CreateWindowEx(NULL, 
				_T("Light3dWinAPP"), 
				_T("Light3D"),
				WS_OVERLAPPEDWINDOW,
				CW_USEDEFAULT,
				CW_USEDEFAULT,
				width,
				height,
				NULL,
				NULL,
				m_hInstance,
				this
				);

			if (m_hWnd == 0)
			{
				return -1;
			}

			ShowWindow(m_hWnd, SW_SHOW);
			MSG msg = { 0 };
			while (msg.message!=WM_QUIT)
			{
				if (msg.message == WM_DESTROY || msg.message == WM_CLOSE)
				{
					//跳出,退出;
					break;
				}

				//有消息的时候处理消息,没有消息的时候进行渲染绘制;
				if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
				{
					TranslateMessage(&msg);
					DispatchMessage(&msg);
				}
				else
				{
					//渲染;
					Sleep(1);
				}
			}
			return 0;
		}

	};
}

main.cpp 中处理Win的入口函数

/**********************
*File:main.cpp;
*Author:chenpeng
*Data:2015/07/21;
*/

#include "Light3dWinAPP.h"

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	UNREFERENCED_PARAMETER(hInstance);
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);
	UNREFERENCED_PARAMETER(nCmdShow);

	Light3d::Light3dWinAPP app(hInstance);
	app.main(800, 600);
}

OpenGL(一)  使用Win API 建立黑色窗口



示例下载:





2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.008 s.
京ICP备10005923号