gpt4 book ai didi

c++ - 在 WC_TABCONTROL 中渲染 OpenGL Canvas

转载 作者:太空狗 更新时间:2023-10-29 21:13:11 30 4
gpt4 key购买 nike

我的目标是使用包含用于渲染 OpenGL 的 Canvas 的 Win32 API 创建一个基本的选项卡控件。我的选项卡包含一个用于呈现 OpenGL 的静态控件。但是,让 Canvas 出现在 GUI 中的唯一方法是排除选项卡控件(在我的示例中注释掉 CREATE_TAB_PANE 宏来执行此操作)。

我的例子如下:

// OpenGlTabWin32.cpp
// NOTE: canvas displays fine if TabPane creation is commented out

#include <windows.h>
#include <commctrl.h>
#include <gl/GL.h>

#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "opengl32.lib")

#define CREATE_TAB_PANE

enum { IDC_TAB = 200, IDC_CANVAS = 201 };

static HWND TabPaneId;
static HWND CanvasId;

static WNDPROC CanvasWndProc;
static HGLRC CanvasRc;
static HDC CanvasDc;

////////////////////////////////////////////////////////////////////////////////
// WndProc
////////////////////////////////////////////////////////////////////////////////
static LRESULT CALLBACK WndProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam) {

switch (msg) {
case WM_CREATE:
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}

return 0;
}

////////////////////////////////////////////////////////////////////////////////
// OpenGlCanvasProc
////////////////////////////////////////////////////////////////////////////////
static LRESULT CALLBACK OpenGlCanvasProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam) {

if (msg == WM_PAINT) {
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);

wglMakeCurrent(CanvasDc, CanvasRc);
SwapBuffers(CanvasDc);

return 0;
}

return CallWindowProc(CanvasWndProc, hwnd, msg, wParam, lParam);
}

////////////////////////////////////////////////////////////////////////////////
// CanvasInit
////////////////////////////////////////////////////////////////////////////////
static void CanvasInit() {

glClearColor(0.0, 0.0, 0.0, 0.0);
}

////////////////////////////////////////////////////////////////////////////////
// CanvasResize
////////////////////////////////////////////////////////////////////////////////
static void CanvasResize(int width, int height) {

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0, 1, 0, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

////////////////////////////////////////////////////////////////////////////////
// CanvasDisplay
////////////////////////////////////////////////////////////////////////////////
static void CanvasDisplay() {

glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();
glColor3d(1, 0, 0);

glBegin(GL_LINE_STRIP);
glVertex2d(0.2, 0.2);
glVertex2d(0.8, 0.8);
glEnd();

SwapBuffers(CanvasDc);
}

////////////////////////////////////////////////////////////////////////////////
// WinMain
////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd) {

// Create window

const char *className = "OpenGlTab";

WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = className;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if (!RegisterClassEx(&wc)) {
exit(0);
}

HWND winId = CreateWindowEx(0,
className,
"Tab Pane w/ OpenGL",
WS_OVERLAPPEDWINDOW,
100, 100, 600, 400,
0,
0,
hInstance,
0);
if (!winId) {
exit(0);
}

ShowWindow(winId, nShowCmd);
UpdateWindow(winId);

#ifdef CREATE_TAB_PANE

// Create tab pane

TabPaneId = CreateWindow(WC_TABCONTROL,
0,
WS_CHILD | WS_VISIBLE,
10, 10, 566, 343,
winId,
HMENU(IDC_TAB),
hInstance,
0);

TCITEM tabItem = {0};
tabItem.mask = TCIF_TEXT;
tabItem.pszText = "Tab";

SendMessage(TabPaneId, TCM_INSERTITEM, 0, LPARAM(&tabItem));

#endif

// Create OpenGL canvas

int w = 200;
int h = 200;

CanvasId = CreateWindow("static",
"",
WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
20, 100, w, h,
winId,
HMENU(IDC_CANVAS),
hInstance,
0);

CanvasWndProc = (WNDPROC)SetWindowLongPtr(CanvasId,
GWLP_WNDPROC,
(LONG_PTR)OpenGlCanvasProc);

CanvasDc = GetDC(CanvasId);

static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1, // version
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32, // color depth
0, 0, 0, 0, 0, 0,
0, 0,
0, 0, 0, 0, 0,
16, // depth buffer
0,
0,
0,
0,
0, 0, 0
};

int pixelFormat = ChoosePixelFormat(CanvasDc, &pfd);
SetPixelFormat(CanvasDc, pixelFormat, &pfd);

CanvasRc = wglCreateContext(CanvasDc);

// Render OpenGL canvas

wglMakeCurrent(CanvasDc, CanvasRc);
CanvasResize(w, h);
CanvasInit();
CanvasDisplay();
SwapBuffers(CanvasDc);

// Execute GUI

MSG msg;

while (GetMessage(&msg, 0, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}

我根据 Chris 所做的更改更新了示例:

// OpenGlTabWin32.cpp
// NOTE: canvas displays fine if TabPane creation is commented out

#include <windows.h>
#include <commctrl.h>
#include <gl/GL.h>

#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "opengl32.lib")

#define CREATE_TAB_PANE

enum { IDC_TAB = 200, IDC_CANVAS = 201 };

static HWND TabPaneId;
static HWND CanvasId;

static WNDPROC CanvasWndProc;
static HGLRC CanvasRc;

////////////////////////////////////////////////////////////////////////////////
// WndProc
////////////////////////////////////////////////////////////////////////////////
static LRESULT CALLBACK WndProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam) {

switch (msg) {
case WM_CREATE:
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}

return 0;
}

////////////////////////////////////////////////////////////////////////////////
// CanvasInit
////////////////////////////////////////////////////////////////////////////////
static void CanvasInit() {

glClearColor(0.0, 0.0, 0.0, 0.0);
}

////////////////////////////////////////////////////////////////////////////////
// CanvasResize
////////////////////////////////////////////////////////////////////////////////
static void CanvasResize(int width, int height) {

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0, 1, 0, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

////////////////////////////////////////////////////////////////////////////////
// CanvasDisplay
////////////////////////////////////////////////////////////////////////////////
static void CanvasDisplay() {

glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();
glColor3d(1, 0, 0);

glBegin(GL_LINE_STRIP);
glVertex2d(0.2, 0.2);
glVertex2d(0.8, 0.8);
glEnd();
}

////////////////////////////////////////////////////////////////////////////////
// OpenGlCanvasProc
////////////////////////////////////////////////////////////////////////////////
static LRESULT CALLBACK OpenGlCanvasProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam) {

switch (msg) {
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);

wglMakeCurrent(hdc, CanvasRc);
CanvasDisplay();
SwapBuffers(hdc);
wglMakeCurrent(NULL, NULL);
ReleaseDC(hwnd, hdc);

EndPaint(hwnd, &ps);

return 0;
}

case WM_SIZE:
{
HDC hdc = GetDC(hwnd);
wglMakeCurrent(hdc, CanvasRc);
CanvasResize(LOWORD(lParam), HIWORD(lParam));
wglMakeCurrent(NULL, NULL);
ReleaseDC(hwnd, hdc);

return 0;
}
}

return CallWindowProc(CanvasWndProc, hwnd, msg, wParam, lParam);
}

////////////////////////////////////////////////////////////////////////////////
// WinMain
////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd) {

// Create window

const char *className = "OpenGlTab";

WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = className;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if (!RegisterClassEx(&wc)) {
exit(0);
}

HWND winId = CreateWindowEx(0,
className,
"Tab Pane w/ OpenGL",
WS_OVERLAPPEDWINDOW,
100, 100, 600, 400,
0,
0,
hInstance,
0);
if (!winId) {
exit(0);
}

ShowWindow(winId, nShowCmd);
UpdateWindow(winId);

#ifdef CREATE_TAB_PANE

// Create tab pane

TabPaneId = CreateWindow(WC_TABCONTROL,
0,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
10, 10, 566, 343,
winId,
HMENU(IDC_TAB),
hInstance,
0);

TCITEM tabItem = {0};
tabItem.mask = TCIF_TEXT;
tabItem.pszText = "Tab";

SendMessage(TabPaneId, TCM_INSERTITEM, 0, LPARAM(&tabItem));

#endif

// Create OpenGL canvas

int w = 200;
int h = 200;

CanvasId = CreateWindow("static",
"",
WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
20, 100, w, h,
winId,
HMENU(IDC_CANVAS),
hInstance,
0);

CanvasWndProc = (WNDPROC)SetWindowLongPtr(CanvasId,
GWLP_WNDPROC,
(LONG_PTR)OpenGlCanvasProc);

HDC hdc = GetDC(CanvasId);

static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1, // version
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32, // color depth
0, 0, 0, 0, 0, 0,
0, 0,
0, 0, 0, 0, 0,
16, // depth buffer
0,
0,
0,
0,
0, 0, 0
};

int pixelFormat = ChoosePixelFormat(hdc, &pfd);
SetPixelFormat(hdc, pixelFormat, &pfd);

CanvasRc = wglCreateContext(hdc);

// Render OpenGL canvas

wglMakeCurrent(hdc, CanvasRc);
CanvasResize(w, h);
CanvasInit();
CanvasDisplay();
SwapBuffers(hdc);
wglMakeCurrent(NULL, NULL);
ReleaseDC(CanvasId, hdc);

// Execute GUI

MSG msg;

while (GetMessage(&msg, 0, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}

最佳答案

您在此样本中发生的许多事情都可能有所贡献:

  • 最直接的问题可能只是选项卡窗口覆盖了 Canvas 窗口,而您实际上并没有绘制任何内容来响应 WM_PAINT。选项卡控件将在其失效后立即在您的 Canvas 上绘制,因为 Windows 通常允许子窗口相互绘制;因此将 WS_CLIPSIBLINGS 添加到选项卡控件可能会有所帮助。

  • 在将它与当前 wgl 上下文相关联后,您正在将 HDC 抓取到静态控件并保留它。你真的不应该这样做,除非你正在使用一个带有 CS_OWNDC 的窗口类,尤其是不使用一个可能有 CS_PARENTDC 的窗口类(因为那样,一旦父窗口或另一个子窗口绘制,DC 就会重新与一个从未关联过 SetPixelFormat 的窗口)。

  • 您只是让您的 opengl 上下文成为当前上下文,并期望稍后设置它。这很好 - 假设你有一个带有 HDC 的 CS_OWNDC 窗口,你可以提前捕获并保留 - 并且假设你永远不想出于任何原因创建第二个 GL 上下文。

因此,当在您不控制窗口类样式(或者可能有多个 OpenGL 上下文)的应用程序中执行 OpenGL 时,您需要确保始终清除当前上下文并在您一完成就释放 DC完成了。

例如,您的 CanvasWindowProc 应该看起来更像这样:

case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd,&ps);
wglMakeCurrent(glrc,hdc);
CanvasDisplay();
SwapBuffers();
wglMakeCurrent(NULL,NULL);
EndPaint(&ps);
}
return 0;
case WM_SIZE:
{
HDC hdc = GetDC(hwnd);
wglMakeCurrent(glrc,hdc);
CanvasResize(LOWORD(lParam),HIWORD(lParam));
wglMakeCurrent(NULL,NULL);
ReleaseDC(hwnd,hdc);
}
break;

关于c++ - 在 WC_TABCONTROL 中渲染 OpenGL Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45114305/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com