Windows C++获取显卡支持的OpenGL最高版本

用C++代码获取显卡支持的OpenGL最高版本的代码很简单:

const GLubyte* OpenGLVersion = glGetString(GL_VERSION); //返回当前OpenGL实现的版本号

但是这行代码必须在windows desktop application建立起图形化环境并启用opengl以后才能被获取到,因此我们可以做一个一闪而过的windows desktop application来将获取到的版本信息写入一个文件,其它需要这个信息的代码可以从这个文件来读取。

具体实现过程如下:

1. visual studio 2022 community 新建项目,选择C++, Windows, Desktop, Desktop Application

2. 在项目的源文件<project_name>.cpp的WndProc函数中添加写入版本到文件(C:\idealand\sysinfo\opengl_version.txt)的代码:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{

//-------------以下为添加的代码 -------------------
case WM_CREATE:
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //Flags
PFD_TYPE_RGBA, //The kind of framebuffer. RGBA or palette.
32, //Colordepth of the framebuffer.
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24, //Number of bits for the depthbuffer
8, //Number of bits for the stencilbuffer
0, //Number of Aux buffers in the framebuffer.
PFD_MAIN_PLANE,
0,
0, 0, 0
};

HDC ourWindowHandleToDeviceContext = GetDC(hWnd);

int letWindowsChooseThisPixelFormat;
letWindowsChooseThisPixelFormat = ChoosePixelFormat(ourWindowHandleToDeviceContext, &pfd);
SetPixelFormat(ourWindowHandleToDeviceContext, letWindowsChooseThisPixelFormat, &pfd);

HGLRC ourOpenGLRenderingContext = wglCreateContext(ourWindowHandleToDeviceContext);
wglMakeCurrent(ourWindowHandleToDeviceContext, ourOpenGLRenderingContext);

char* opengl_version = (char*)glGetString(GL_VERSION);
std::ofstream opengl_version_file("C:\\idealand\\sysinfo\\opengl_version.txt"); opengl_version_file.write(opengl_version, strlen(opengl_version)); opengl_version_file.close();
//MessageBoxA(0, opengl_version, "OPENGL VERSION", 0);

wglDeleteContext(ourOpenGLRenderingContext);
PostQuitMessage(0);
}
break;

//-------------添加代码结束 -------------------

case WM_COMMAND:
{

.................

build 执行这个项目就会在文件(C:\idealand\sysinfo\opengl_version.txt)中得到版本号,比如:

---------------------------

4.6.0 NVIDIA 512.15

---------------------------

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qiuzen

您的资助将帮助我创作更好的作品

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值