Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

cPaintDC.GetTextExtent LineHeight computation very bogus: alternatives???

Featured Replies

Posted

cPaintDC.GetTextExtent LineHeight computation very bogus: alternatives???

LOGFONT lf;
    memset(&lf, 0, sizeof(lf));
    HGDIOBJ hFont = ::GetStockObject(OEM_FIXED_FONT);
    ::GetObject(hFont, sizeof(lf), &lf);

    lf.lfHeight = 100;
    lf.lfPitchAndFamily = FIXED_PITCH;

    CFont font;
    font.CreatePointFontIndirect(&lf);

    pOldFont = cMemDC.SelectObject(&font);

    // Get size information
    int cWidth;
    cPaintDC.GetCharWidth('0', '0', &cWidth);
    CSize cSize = cPaintDC.GetTextExtent("0", 1);
    int nLineHeight = cSize.cy;

    ySizeChar = nLineHeight;
    xSizeChar = cWidth;


    // memorybuffered output (via a memorybitmap)
    cMemDC.CreateCompatibleDC(&cPaintDC);
    GetClientRect(cClientRect);
    xClient = cClientRect.Width();
    yClient = cClientRect.Height();

    int maxPLen = yClient/ySizeChar;  // 10

    if (yClientMax>yClient)
    {
    EnableScrollBar(SB_VERT);
    ShowScrollBar(SB_VERT, true);
    }

maxPLen is 10 so maxim 10 lines, I've entered 13 and there is still space on my editbox,
my only thoughts is that cPaintDC.GetTextExtent("0", 1); cSize.cy computation is wrong,
any alternative to it?

 

  • Author

There was a bug with my code: cPaintDC should have been instead cMemDC: the CDC for which we previously set the font.
The second thing: those are called from OnPaint method, first time yClient = cClientRect.Height() will get a wrong bigger value, second time it will return the good value.
 

  • Author

Just another way to compute size of text, will return same size as previous one:

https://github.com/cmacro/simple/blob/master/gdi_win/testscrollcalc/scrolltest.cpp
inside LRESULT CALLBACK MyTextWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    // Extract font dimensions from the text metrics
    TEXTMETRIC tm;
    GetTextMetrics(cMemDC, &tm);
    int xChar = tm.tmAveCharWidth;
    int xUpper = (tm.tmPitchAndFamily & 1 ? 3 : 2) * xChar / 2;
    int yChar = tm.tmHeight + tm.tmExternalLeading;

The error seems to be of "2 chars" in my computations, don't know if is normal!
 

  • Author

Kurapica: thank you for your help.

An example:
    SIZE textSize;
    GetTextExtentPoint32A(cMemDC, "0", 1, &textSize);

returns same size as all including the above GetTextMetrics.

All those things needed for implementing hex view editbox.
Normal editbox won't show properly hex chars (same size).
Maybe somebody know an easy way to fix editbox...
 

 

Why not use a Monospaced font so they will all be fixed-width

  • Author
1 hour ago, NOP said:

Why not use a Monospaced font so they will all be fixed-width

How? And what font?
I only could find this list:
https://en.wikipedia.org/wiki/List_of_monospaced_typefaces
 

Any of them in that list are fixed-width so choose anything that suits

Courier is one I use if I want a fixed-width font

Edited by NOP

  • Author

I forget to specify something: I am on Visual C++ 6.0
on C# I did it like this:
        textBox1.Font = new Font(FontFamily.GenericMonospace, textBox1.Font.Size);
        textBox1.Text = "44 43 5F 43 4F 4D 50 41\r\n4E 46 52 41 4D 45 0D 0A";

and it works perfectly,
https://stackoverflow.com/questions/7285166/c-sharp-net-multiline-textbox-with-same-width-characters
it given me some idea of something like getting all fonts measure their size and create a list.
 

  • Author

The font list is not interesting at all, beside Courier there is also Terminal font.
So I am once again stacked, strange enough to see that on C# all works ok.
 

My C++ isn't great but have you tried sending WM_SETFONT to the edit control?

SendMessage(hEdit, WM_SETFONT, (WPARAM)hFont, TRUE)

 

  • Author

I found the error inside my code:
font.CreatePointFontIndirect(&lf);
should be instead:
    HFONT hFontNew = CreateFontIndirect(&lf);
    ::SendMessage(handle3, WM_SETFONT, (WPARAM)hFontNew, TRUE);

Complete code:

    LOGFONT lf;
    memset(&lf, 0, sizeof(lf));
    HGDIOBJ hFont = ::GetStockObject(OEM_FIXED_FONT);
    ::GetObject(hFont, sizeof(lf), &lf);

    lf.lfHeight = 12;
    _tcscpy(lf.lfFaceName, _T("Terminal"));  // Monospace
    lf.lfPitchAndFamily = FIXED_PITCH;

    HFONT hFontNew = CreateFontIndirect(&lf);
    ::SendMessage(handle3, WM_SETFONT, (WPARAM)hFontNew, TRUE);

Now all works ok.
 

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.