OpenGL (1) uses Win API to create a black window and openglapi

Source: Internet
Author: User

OpenGL (1) uses Win API to create a black window and openglapi

Create a black window as the GL container.


The Light3dWinAPP class handles window creation and destruction and distribution system events.

/*********************** File: Light3dWinAPP. h; * Author: chenpeng * Data: 2015/07/21; */# include <Windows. h> # include <tchar. h> namespace Light3d {class Light3dWinAPP {public: HINSTANCE m_hInstance; // instance handle; HWND m_hWnd; // window handle; int m_width; // width; int m_height; // height; public: Light3dWinAPP (HINSTANCE hInstance): m_hInstance (hInstance) {// to create a window, there must be a window class; WNDCLASSEX winClass; // window class name; winClass. lpszClassName = _ T ("Light3dWinAPP" ); WinClass. cbSize = sizeof (winClass); // when the H horizontal direction and V vertical direction change or move, redraw the window, // assign a unique device context for each window in the class; winClass. style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // point to the function's long pointer lp, processing the message loop in the window; winClass. lpfnWndProc = wndProc; // instance handle; winClass. hInstance = hInstance; // window minimization icon; winClass. hIcon = 0; winClass. hIconSm = 0; // mouse cursor; winClass. hCursor = LoadCursor (hInstance, IDC_ARROW); // window background color; winClass. hbrBackground = (HBRUSH) GetStockObjec T (BLACK_BRUSH); // Window menu; 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_PT R) pCreate-> lpCreateParams);} return DefWindowProc (hWnd, msg, wParam, lParam);} public: // window event; 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) {ca Se 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;} // main function; int main (int width, int height) {// create a window; m_hWnd = createmediawex (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) {// jump out, exit; break;} // process the message when there is a message, rendering when there is no message; if (PeekMessage (& msg, NULL, 0, 0, PM_REMOVE) {TranslateMessage (& msg); DispatchMessage (& msg) ;}else {// rendering; Sleep (1 );}} return 0 ;}};}

The entry function for processing Win in main. cpp

/***********************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);}



Download example:

http://pan.baidu.com/s/1c0EtfR6


Copyright: This article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.