在前面的入门中,都是一个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