Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6110919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:34:15+00:00 2026-05-23T14:34:15+00:00

I work on an MFC app containing OpenGL context.I am new to MFC that

  • 0

I work on an MFC app containing OpenGL context.I am new to MFC that is why I am asking it.OpenGL works fine ,but when I want to draw a text above the 3D window using this code inside WindowProc:

        case WM_PAINT:

      hDC=BeginPaint(window,&paintStr);
      GetClientRect(window,&aRect);
      SetBkMode(hDC,TRANSPARENT);
      DrawText(hDC,L"He He I am a text on top of OpenGL",-1,&aRect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
      EndPaint(window,&paintStr);

      return 0;

it is shown beneath the OpenGL context.I can see it only when resizing the window as the OpenGL rendering pauses than.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:34:16+00:00Added an answer on May 23, 2026 at 2:34 pm

    What you’re doing is wrong and also harder than doing it all in OpenGL. To solve the problem of adding text to an OpenGL-drawn window, it’s better to just make OpenGL draw the text. You can even use the exact same font you were using in MFC by creating a CFont instance when you handle WM_CREATE, selecting the font into the DC, and calling wglUseFontBitmaps, which will make a series of rasterized bitmaps that you can use with glCallLists. (While you’re at it, call GetCharABCWidths and GetTextMetrics to determine the width and height of each glyph, respectively.)

    ABC glyphInfo[256]; // for font widths
    TEXTMETRIC tm;  // for font heights
    
    // create a bitmap font
    CFont myFont;
    myFont.CreateFont(
        16,                        // nHeight
        0,                         // nWidth
        0,                         // nEscapement
        0,                         // nOrientation
        FW_NORMAL,                 // nWeight
        FALSE,                     // bItalic
        FALSE,                     // bUnderline
        0,                         // cStrikeOut
        ANSI_CHARSET,              // nCharSet
        OUT_DEFAULT_PRECIS,        // nOutPrecision
        CLIP_DEFAULT_PRECIS,       // nClipPrecision
        DEFAULT_QUALITY,           // nQuality
        DEFAULT_PITCH | FF_SWISS,  // nPitchAndFamily
        _T("Arial")                // lpszFacename
    );
    
    // change the current font in the DC
    CDC* pDC = CDC::FromHandle(hdc);
    
    // make the system font the device context's selected font  
    CFont *pOldFont = (CFont *)pDC->SelectObject (&myFont); 
    
    // the display list numbering starts at 1000, an arbitrary choice  
    wglUseFontBitmaps (hdc, 0, 255, 1000); 
    VERIFY( GetCharABCWidths (hdc, 0, 255, &glyphInfo[0]) );
    pDC->GetTextMetrics(&tm);
    
    if(pOldFont)
        pDC->SelectObject(pOldFont);
    
    myFont.DeleteObject(); 
    

    Then when you handle WM_PAINT, reset your matrices and use glRasterPos2d to put the text where you need it to go. I suggest calculating the exact width of your string using code similar to the one below if you want it to be horizontally centered.

    // indicate start of glyph display lists  
    glListBase (1000); 
    
    CRect r;
    GetWindowRect(r);
    
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(0, r.Width(), 0, r.Height());
    
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    
    CString formattedString;
    formattedString.Format("Pi is about %1.2f", 3.1415);
    
    int stringWidth=0; // pixels
    for(int j=0; j < formattedString.GetLength(); ++j)
        stringWidth += glyphInfo[ formattedString.GetAt(j) ].abcA + glyphInfo[ formattedString.GetAt(j) ].abcB + glyphInfo[ formattedString.GetAt(j) ].abcC;
    
    double textXPosition, textYPosition;
    textXPosition = r.Width()/2-stringWidth/2; // horizontally centered
    textYPosition = r.Height()/2-tm.tmHeight/2; // vertically centered
    
    glRasterPos2d(textXPosition,textYPosition);
    
    // this is what actually draws the text (as a series of rasterized bitmaps)
    glCallLists (formattedString.GetLength(), GL_UNSIGNED_BYTE, (LPCSTR)formattedString);
    
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    

    While the setup is annoying, you only have to do it once, and I think it’s less frustrating than dealing with GDI. Mixing GDI and OpenGL is really asking for trouble, and OpenGL does a very good job of displaying text — you get sub-pixel accuracy for free, among other benefits.

    Edit: In response to your request for including GUI elements, I will assume that you meant that you want to have both OpenGL-drawn windows and also standard Windows controls (edit boxes, check boxes, buttons, list controls, etc.) inside the same parent window. I will also assume that you intend OpenGL to draw only part of the window, not the background of the window.

    Since you said you’re using MFC, I suggest that you create a dialog window, add all of your standard Windows controls to it, and then add in a CWnd-derived class where you handle WM_PAINT. Use the resource editor to move the control to where you want it. Effectively, you’re making an owner-draw custom control where OpenGL is doing the drawing. So OpenGL will draw that window, and the standard MFC classes (CEdit, CButton, etc.) will draw themselves. This works well in my experience, and it’s really not much different from what GDI does in an owner-draw control.

    What if instead you want OpenGL to draw the background of the window, and you want standard Windows controls to appear on top of it? I don’t think this is a great idea, but you can handle WM_PAINT and WM_ERASE for your CDialog-derived class. In WM_ERASE, call OpenGL to draw your 3D content, which will be overwritten by the standard Windows controls when WM_PAINT is called. Alternatively in WM_PAINT you could call OpenGL before calling CDialog::OnDraw, which would be similar.

    Please clarify your statement “I want to add some 2s graphics overlay (like labels ,gui elements)” if you want me to write more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm architecting an application that does necessarily C++ work, but MFC/ATL is too
I work for a very large organization that is looking for new ideas. Currently
We are in the process of writing a native windows app (MFC) that will
I have been learning MFC these days.I want to draw lines with MoveTo() and
From a modal MFC dialog, I want to extract text from an edit box
I have an MFC legacy app that I help to maintain. I'm not quite
I have a C++ MFC app that stores all of its system wide configuration
I work in the ui of a quite big MFC application. We tried to
This code below doesn't work correctly since my MFC program is in unicode circumstance.
work on asp.net vs 05 C#.Master page header contain the bellow code <script type=text/javascript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.