现代opengl 设计入门,与mfc 组合

本文介绍了如何将OpenGL与MFC结合,创建一个简单的SDI工程。通过修改glfw库,添加父窗口句柄支持,实现了在MFC应用程序中嵌入OpenGL窗口。在MFC视图类中,响应OnCreate、OnEraseBkgnd、OnDestroy和OnSize事件,将初始化、绘制和窗口大小调整等操作集成。调试过程中解决了glad.c预编译头文件的错误,并指出当设置初始窗口尺寸小于屏幕尺寸时,图形显示的问题。
摘要由CSDN通过智能技术生成

在前面的入门中,都是一个main 函数,没有菜单,没有对话框,如果要的话,我就想到与mfc 的组合。mfc 有菜单,对话框,opengl做图形显示。

我先是参考 cv::namedWindow, GLFWwindow以及其他程序嵌入到MFC中的教程

1: 运行效果

2:下载glfw, 并用cmake 建立编译环境,然后做如下一些小的修改。

在前面入门中看到,glfw中窗口的创建, 就是使用函数glfwCreateWindow. 在VS中, 找到glfwCreateWindow函数的定义位置, 是在 glfw3.h文件中, 新加入一个函数glfwCreateWindowEx声明, 如下: 

 *  @reentrancy This function must not be called from a callback.
 *
 *  @thread_safety This function must only be called from the main thread.
 *
 *  @sa @ref window_creation
 *  @sa glfwDestroyWindow
 *
 *  @since Added in version 3.0.  Replaces `glfwOpenWindow`.
 *
 *  @ingroup window
 */
GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share);
GLFWAPI GLFWwindow* glfwCreateWindowEx(int width, int height, const char* title, GLFWmonitor* monitor, GLFWwindow* share,int hParent);

在原本glfwCreateWindow函数的参数列表中新加入了参数int hParent. 新加入的参数, 本应该是HWND类型, 但该类型定义于Windows.h中, 本着尽可能少的改动代码, 以int代替了HWND类型。
现在打开win32_platform.h文件, 找到其中struct _GLFWwindowWin32定义所在的位置, 新加入HWND handleParent, 用来保存父窗口的句柄作为参数传递给创建窗口的函数. 如下图所示: 

// Win32-specific per-window data
//
typedef struct _GLFWwindowWin32
{
    HWND                handle;
    HICON               bigIcon;
    HICON               smallIcon;

    GLFWbool            cursorTracked;
    GLFWbool            iconified;

    // The last received cursor position, regardless of source
    int                 lastCursorPosX, lastCursorPosY;
	HWND handleParent;

} _GLFWwindowWin32;

修改好参数结构体之后, 现在定位glfwCreateWindow函数的定义, 定义于文件window.c中. 复制glfwCreateWindow函数的定义, 粘贴在glfwCreateWindow函数的定义的下方, 更改函数名为glfwCreateWindowEx并加入参数int hParent. 在该函数的实现中找到_glfwPlatformCreateWindow函数的调用地方, 在其前方加入下述代码:

window->win32.handleParent = (HWND)hParent;  //----------liwenz-------------

效果如下: 

 // Save the currently current context so it can be restored later
    previous = _glfwPlatformGetCurrentContext();
    if (ctxconfig.client != GLFW_NO_API)
        glfwMakeContextCurrent(NULL);

	window->win32.handleParent = (HWND)hParent;  //----------liwenz-------------
    // Open the actual window and create its context
    if (!_glfwPlat
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值