標籤:
Visual Studio 2013 螢幕保護裝置程式opengl模板
ScreenSaver.cpp
#define VC_EXTRALEAN#include <windows.h>#include <commctrl.h>#include <scrnsave.h>#include <time.h>#include <math.h>#include <string>#include <time.h>#include "resource.h"#include <gl\GLU.h>#include <gl\GL.h>#pragma comment(lib, "opengl32.lib")#pragma comment(lib, "glu32.lib")#pragma comment(lib, "scrnsavw.lib")#pragma comment (lib, "comctl32.lib")bool SetupOpenGL();void KillGL();void DrawGLScene();HDChDC = NULL;HGLRChRC = NULL;HWNDhWnd = NULL;HINSTANCEhInstance;int uTimer;LONG WINAPI ScreenSaverProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){// 處理螢幕保護裝置訊息switch (message){case WM_CREATE:// 建立螢幕保護裝置表單訊息 最先運行hWnd = hwnd;SetupOpenGL();// 設定定時器 (間隔為16毫秒,為了每分鐘重新整理60次 (1000/60 = 16)uTimer = SetTimer(hwnd, 1, 16, NULL);return 0;case WM_ERASEBKGND:// 清除螢幕保護裝置背景// opengl不使用return 0;case WM_TIMER:// 定時器訊息DrawGLScene();return 0;case WM_DESTROY:// 銷毀KillTimer(hwnd, uTimer);KillGL();PostQuitMessage(0);return 0;}return DefScreenSaverProc(hwnd, message, wparam, lparam);}BOOL WINAPI ScreenSaverConfigureDialog(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){return true;}BOOL WINAPI RegisterDialogClasses(HANDLE hmodule){return true;}bool SetupOpenGL(){GLuint PixelFormat;int width;int height;width = GetDeviceCaps(GetDC(NULL), HORZRES);height = GetDeviceCaps(GetDC(NULL), VERTRES);RECTWindowRect;WindowRect.left = (long)0;WindowRect.right = (long)width;WindowRect.top = (long)0;WindowRect.bottom = (long)height;//設定opengl上下文staticPIXELFORMATDESCRIPTOR pfd ={sizeof(PIXELFORMATDESCRIPTOR),1,PFD_DRAW_TO_WINDOW |PFD_SUPPORT_OPENGL |PFD_DOUBLEBUFFER,PFD_TYPE_RGBA,24,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))){KillGL();MessageBox(NULL, TEXT("Can't Create A GL Device Context."), TEXT("ERROR"), MB_OK | MB_ICONEXCLAMATION);return FALSE;}if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))){KillGL();MessageBox(NULL, TEXT("Can't Find A Suitable PixelFormat."), TEXT("ERROR"), MB_OK | MB_ICONEXCLAMATION);return FALSE;}if (!SetPixelFormat(hDC, PixelFormat, &pfd)){KillGL();MessageBox(NULL, TEXT("Can't Set The PixelFormat."), TEXT("ERROR"), MB_OK | MB_ICONEXCLAMATION);return FALSE;}if (!(hRC = wglCreateContext(hDC))){KillGL();MessageBox(NULL, TEXT("Can't Create A GL Rendering Context."), TEXT("ERROR"), MB_OK | MB_ICONEXCLAMATION);return FALSE;}if (!wglMakeCurrent(hDC, hRC)){KillGL();MessageBox(NULL, TEXT("Can't Activate The GL Rendering Context."), TEXT("ERROR"), MB_OK | MB_ICONEXCLAMATION);return FALSE;}//透視矩陣if (height == 0){ height = 1; }glViewport(0, 0, width, height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();//初始化opengl一些參數glShadeModel(GL_SMOOTH);glClearColor(0.0f, 0.0f, 0.0f, 0.5f);glClearDepth(1.0f);glEnable(GL_DEPTH_TEST);glDepthFunc(GL_LEQUAL);glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);return TRUE;}void KillGL(GLvoid){ChangeDisplaySettings(NULL, 0);// If So Switch Back To The Desktopif (hRC){// Do We Have A Rendering Context?wglMakeCurrent(NULL, NULL);// Release The DC And RC ContextswglDeleteContext(hRC);// Delete The RChRC = NULL;// Set RC To NULL}ReleaseDC(hWnd, hDC);// Release The DC}void DrawGLScene(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glTranslatef(-1.5f, 0.0f, -6.0f);glBegin(GL_TRIANGLES);glVertex3f(0.0f, 1.0f, 0.0f);glVertex3f(-1.0f, -1.0f, 0.0f);glVertex3f(1.0f, -1.0f, 0.0f);glEnd();glTranslatef(3.0f, 0.0f, 0.0f);glBegin(GL_QUADS);glVertex3f(-1.0f, 1.0f, 0.0f);glVertex3f(1.0f, 1.0f, 0.0f);glVertex3f(1.0f, -1.0f, 0.0f);glVertex3f(-1.0f, -1.0f, 0.0f);glEnd();SwapBuffers(hDC);}
模板代碼託管在 https://github.com/tiancode/windows_opengl_screensaver_template
著作權聲明:本文博主原創文章,部落格,未經同意不得轉載。
windows螢幕保護裝置程式opengl模板