CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

OpenGL – How to draw on MFC dialog? – Complete working code

In our previous articles, we have discussed linking OpenGL-related files to the Project and preparing the MFC Dialog to display the OpenGL graphics. You must read the below articles before you continue reading this article.

OpenGL – How to draw on MFC dialog? – Linking opengl32.lib

OpenGL – How to draw on MFC dialog? – Prepare to draw the Graphics

Through this article, I want to put it all together with a working code example. And also you will see the screenshot of it, at the end.

Include OpenGL header file(s)

Step 1. OpenGL function declarations are declared in the header file “gl.h” and it has to be included in the Project to enable to use of the OpenGL functions.

#include <gl/gl.h>

Link opengl32.lib file to the Project

Step 2. To load, opengl32.dll to the Project; where OpenGL functions are defined; we need to add opengl32.lib file to our Project.

Open Project properties, then expand Configuration properties; and then expand Linker. Select Input option; then add “opengl32.lib” into “Additional Dependencies” field. Press the OK button to apply the changes.

Prepare to draw OpenGL graphics

Step 3. Prepare the MFC dialog to draw the OpenGL graphics.

BOOL COpenGLDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	//{{ OpenGL - Prepare to draw
	PIXELFORMATDESCRIPTOR pfd = { 
		sizeof(PIXELFORMATDESCRIPTOR),   // size of this pfd  
		1,                     // version number  
		PFD_DRAW_TO_WINDOW |   // support window  
		PFD_SUPPORT_OPENGL     // support OpenGL  
		PFD_TYPE_RGBA,         // RGBA type  
		24,                    // 24-bit color depth  
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   
		32,                    // 32-bit z-buffer  
		0, 0, 0, 0, 0, 0, 0 
	};

	CDC *pDC = this->GetDC();
	if ( pDC != NULL )
	{
		HDC hdc = pDC->GetSafeHdc();
		if ( hdc != NULL )
		{
			int ipf = ChoosePixelFormat(hdc, &pfd);
			if ( ipf != 0 )
			{
				if ( SetPixelFormat(hdc, ipf, &pfd) == TRUE )
				{
					HGLRC hglrc = wglCreateContext(hdc);
					if ( hglrc != NULL )
						wglMakeCurrent(hdc, hglrc);
				}
			}
		}

		this->ReleaseDC(pDC);
	}
	//}} OpenGL

	return TRUE;  // return TRUE  unless you set the focus to a control
}

Draw the scene

Step 4. Using gl functions we are going to draw graphics on the MFC dialog. glRectf function is used to draw the rectangle. Observe the values passed to this function; all are float values.

glColor3f function is used to set the color value; it considers R (Red), G (Green), and B (Blue) components to display the color.

Finally, we used glFlush function to empty the commands in the command buffer, and execute them immediately.

void COpenGLDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();

		//{{ OpenGL - Draw the scene
		{
			glColor3f(0.0f, 1.0f, 0.0f);
			glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
			glFlush();
		}
		//}} OpenGL		
	}
}

Delete rendering context

Step 5. Delete the rendering context. This we can delete only when NO current rendering context is selected for the calling thread.

void COpenGLDlg::OnDestroy()
{
	//{{ OpenGL - Clean up
	CDC *pDC = this->GetDC();
	if ( pDC != NULL )
	{
		HDC hdc = pDC->GetSafeHdc();
		if ( hdc != NULL )
		{
			HGLRC hglrc = wglGetCurrentContext();
			if ( hglrc != NULL )
			{
				if ( wglMakeCurrent(NULL, NULL) == TRUE )
					wglDeleteContext(hglrc);
			}
		}

		this->ReleaseDC(pDC);
	}
	//}} OpenGL

	CDialogEx::OnDestroy();
}

Step 6. Now we have the complete code, in-place. Build the application and run the project. You will see a green color-filled rectangle; drawn on the MFC dialog. The screenshot of it is, below;

OpenGL graphics on MFC dialog
OpenGL graphics on MFC dialog

// Malin

OpenGL – How to draw on MFC dialog? – Complete working code

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top