Search code examples
c++windowswinapiopenglgdi

How can I make OpenGL and GDI both working properly at the same time?


I have four windows, which are one parent window and three child windows. I want to make two of those child windows to draw stuff. The first child window is using OpenGL, and the second child window is using GDI. If I just draw some stuff on my OpenGL window and everything works fine, but when I try to draw(or just try to process WM_PAINT message) something else on my GDI window, those child windows become weird. Firstly, some parts(like buttons, statics, title bar) of my third child window will be missing. Secondly, if I try to resize any of those windows, one or two child windows will be missing. But once I click those windows they will be back. Thirdly, PeekMessage does not work anymore, it always return a FALSE, MessageBox seems not to work properly(I cannot see the popup window, but I can hear the sound), either.

My third child window just created. enter image description here

After I click it. enter image description here

I thought there might be something wrong with the message processing, but I cannot figure out what is actually happening. Is here anyone has any suggestions?

Message Loop:

MSG msg;


DWORD dwLast;
DWORD dwCurrent;
DWORD dwInterval = 20;//50 FPS 1000 / 50
dwLast = GetTickCount();

while (1) {

    while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == FALSE)
    {
        dwCurrent = GetTickCount();
        if (dwCurrent - dwLast < dwInterval)
            continue;

        SendMessage(Handle_Child_Demo, WM_PAINT, 0, 0);

        dwLast = dwCurrent;
    }
    if (GetMessage(&msg, NULL, 0, 0) != TRUE) {
        break;
    }
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

I am not using MDI to create my child windows, I just used CreateWindow to create all my child windows, and I think that must be something wrong with it, what else do I need to do to make sure those child windows work properly?


Solution

  • MUST USE MDI to create child windows, just remember, USE MDI!!!! Or there will be something really weird, this is bacese those messages are not processed properly, and MDI will help us to do it.