You are here:

C++/(MFC)one application will make opengl drawing to the other application's window


QUESTION: Dear Bill,
I have 2 applications (Visual Studio 6.0). One of them is GUI. It has a picture component. Other application is a console application(MFC based), which makes an opengl drawing. I want to make this console application draw opengl in the picture component of GUI. For that, I am giving HANDLE of picture component to console application. In main of console application, I am getting the HDC of picture component using the available HANDLE. I am doing that as follows:
Firstly I am creating an object from CWnd class.
CWnd *cwnd = new CWnd();
Then I am getting HDC as below:
HDC h = (cwnd->GetDC())->GetSafeHdc();

After these, I am calling ChoosePixelFormat function passing h to set the pixel format. But I am taking error when I come to ChoosePixelFormat. What can be the problem? Can a console application manage HDC kind of things given by another dialog based MFC application? If my console application is not MFC based, how can I do all of these things?

Thank you.

ANSWER: Hello,

Is your PIXELFORMATDESCRIPTOR structure all OK (test by filling it in the same way in the MFC program and making the same call to ChoosePixelFormat.

It's possible that a console mode program is built such that calls to update the display are blocked?  I'm guessing since I've never done what you're doing.

I guess there's a reason to structure the program this way, but did you consider passing a pointer to the drawing function and have the console mode call back into the GUI?


---------- FOLLOW-UP ----------

Actually, I forgot to say that it runs correctly  when two applications are MFC(dialog based). That is if I use MFC instead of my console application, it runs as I want.That is, it can set pixel format using received HANDLE from GUI. But, when I do the same things with console application, there is a problem. As a result, some of things supported by MFC application may not be supported by console application for HDC kinds of things. I console mode, calls to update the display can be blocked as you said. If such thing occurs, how can I manage this? As written in the previous question I have a console application which is MFC based. If I have a console application which is NOT MFC based, how can create an object of CWnd class? (CWnd class is not defined in console applications which are NOT MFC based.)

Thank you.

When you created the Console mode application did you check:

Add support for: [X] MFC [  ] ATL.  

I would think you'd have to check MFC.

I have not done what you are trying to do (I've created DLLs that create and access Windows) but will help as much as I can.



All Answers

Answers by Expert:

Ask Experts


Bill A


I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.


I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

BS Computer Engineering

©2017 All rights reserved.