error(1) 严重性代码 描述 项目文件字符串抑制状态 警告 C28252 “WinMain”的注释不一致:Param(3) 在前一个实例中包含“SAL_null(__no)”。请参阅 c:\program files (x86)\windows kits\include .0.19041.0\um\winbase.h(1006)。项目1 C:\Users\setro\OneDrive\Desktop\Project1\Source.cpp 8
error(2) 严重性代码 说明 项目文件字符串抑制状态 警告 C28253 “WinMain”的注释不一致:在此实例中,Param(3) 包含“SAL_null(__maybe)”。请参阅 c:\program files (x86)\windows kits\include .0.19041.0\um\winbase.h(1006)。项目1 C:\Users\setro\OneDrive\Desktop\Project1\Source.cpp 8
error(3) 严重性代码 说明 项目文件字符串抑制状态 错误 LNK2019 引用函数“int __cdecl invoke_main(void)”(?invoke_main@@YAHXZ) 中未解析的外部主要字符。项目1 C:\Users\setro\OneDrive\Desktop\Project1\MSVCRTD.lib(exe_main.obj) 1
error(4) 严重性代码 说明 项目文件字符串抑制状态 无法解析的外部元素的 LNK1120 错误:1 Project1 C:\Users\setro\OneDrive\Desktop\Project1\x64\Debug\Project1.exe 1
#include <windows.h>
#include <GL/GL.h>
#pragma comment (lib, "opengl32.lib")
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd)
{
MSG msg = { 0 };
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wc.lpszClassName = L"oglversionchecksample";
wc.style = CS_OWNDC;
if (!RegisterClass(&wc))
return 1;
CreateWindowW(wc.lpszClassName, L"openglversioncheck", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 640, 480, 0, 0, hInstance, 0);
while (GetMessage(&msg, NULL, 0, 0) > 0)
DispatchMessage(&msg);
return 0;
}
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);
MessageBoxA(0, (char*)glGetString(GL_VERSION), "OPENGL VERSION", 0);
//wglMakeCurrent(ourWindowHandleToDeviceContext, NULL); Unnecessary; wglDeleteContext will make the context not current
wglDeleteContext(ourOpenGLRenderingContext);
PostQuitMessage(0);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
摘自网站https://www.khronos.org/opengl/wiki/Creating_an_OpenGL_Context_(WGL)
WinMain()
的第三个参数不是可选的,因此使用__in
代替__in_opt
(或者根本不注释你的WinMain()
,因为你不是一开始调用它的人) .
此外,您还缺少
WINAPI
上的 __stdcall
(WinMain()
) 调用约定。参见WinMain()
中winbase.h
的声明:
int
#if !defined(_MAC)
#if defined(_M_CEE_PURE)
__clrcall
#else
WINAPI
#endif
#else
CALLBACK
#endif
WinMain (
__in HINSTANCE hInstance,
__in_opt HINSTANCE hPrevInstance,
__in LPSTR lpCmdLine,
__in int nShowCmd
);
此外,请确保您的项目设置为 GUI 项目而不是控制台项目。 GUI 应用程序使用
WinMain()
,控制台应用程序使用 main()
。在项目设置中,Linker -> System -> SubSystem
应为 Windows
。