I am converting a program written using Borland Turbo C to M$ Visual C++ 2010.
I have created a Win32 application, and have now managed to write to a window. But the font that has come out is a proportional one, and I need a fixed font for everything to line up.
I have used the code that the Borland application used to change the font but I am getting an error that I cannot figure out.

void MakeFont(HDC ADC)
{
    Delete_Font(ADC);
	
    cursfont.lfHeight         =  FONTSIZE;
    cursfont.lfWidth          =  FONTSIZE;

    cursfont.lfEscapement     =  0;
    cursfont.lfOrientation    =  0;
    cursfont.lfWeight         =  FW_NORMAL;
    cursfont.lfItalic         =  FALSE;
    cursfont.lfUnderline      =  FALSE;
    cursfont.lfStrikeOut      =  FALSE;
    cursfont.lfCharSet        =  ANSI_CHARSET;
    cursfont.lfOutPrecision   =  OUT_DEFAULT_PRECIS;
    cursfont.lfClipPrecision  =  CLIP_DEFAULT_PRECIS;
    cursfont.lfQuality        =  DEFAULT_QUALITY;
    cursfont.lfPitchAndFamily =  FIXED_PITCH | FF_DONTCARE;
    strcpy_s((char *)cursfont.lfFaceName, 12, "Courier New");

    hnewsfont = CreateFontIndirect(&cursfont);

    holdsfont = (HFONT)(SelectObject(ADC, (HGDIOBJ)hnewsfont));          // latest attempt
//  holdsfont = SelectObject(ADC, (HGDIOBJ)hnewsfont);                   // attempt 1
//	holdsfont = (HFONT)SelectObject(ADC, (HGDIOBJ)(HFONT)hnewsfont); // original
    Font = TRUE;
    fprintf(fp1, "Font height and width = %d %d\n",
        cursfont.lfHeight, cursfont.lfWidth);
    fflush(fp1);

    FontX = cursfont.lfHeight;
    FontY = cursfont.lfWidth;
}

void Delete_Font(HBRUSH ADC)
{
    if (Font == TRUE)
    {
        SelectObject(ADC, holdsfont);
        DeleteObject(hnewsfont);
    }
    Font = FALSE;
}

The error I am getting is:

1>c:\users\norman\documents\visual studio 2010\projects\test5\test5\test5.cpp(815): error C2664: 'SelectObject' : cannot convert parameter 1 from 'HBRUSH' to 'HDC'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

I have tried various options as shown but they all come up with the same error.
What am I doing wrong?

Dani AI

Generated

The compile error is simply a types mismatch: SelectObject needs an HDC, not an HBRUSH — as already pointed out. Fixing the parameter types will remove the immediate compiler complaint, but there are a few more practical points that will prevent subtle bugs at runtime.

Create and destroy the HFONT at sensible times (for example create in WM_CREATE, delete in WM_DESTROY). Select the font into a device context only while you draw (WM_PAINT), then immediately restore the previous font returned by SelectObject. Never call DeleteObject on an HFONT that is still selected into a DC. Keep the HFONT handle and the previous HFONT separate so you can safely restore before deleting.

For correct sizing and a true fixed-width result follow ’s direction and let the system compute character width (set lfWidth to 0). To get a specific point size convert to device pixels and use a negative height (negative indicates character height):

cursfont.lfWidth  = 0;
cursfont.lfHeight = -MulDiv(pointSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);

Unicode/ANSI gotcha: in a UNICODE build lfFaceName is wide-character. Don’t use a plain strcpy on lfFaceName. Use the wide APIs or the TEXT macro, for example:

wcscpy_s(cursfont.lfFaceName, LF_FACESIZE, L"Courier New");

Quick checklist: (1) pass an HDC to SelectObject/Delete_Font or obtain the correct HDC inside the function, (2) store and restore the previous HFONT, (3) delete the new HFONT only after it is no longer selected, (4) verify CreateFontIndirect returned a non-null handle, and (5) measure text with GetTextExtentPoint32/GetTextMetrics if alignment still drifts. These steps will give predictable, fixed-width output and avoid the runtime pitfalls that simple casts can hide.

Recommended Answers

All 2 Replies

The error is in function Delete_Font where ADC is an argument of type HBRUSH instead of HDC.

cursfont.lfWidth = FONTSIZE;
should also be changed to
cursfont.lfWidth = 0;

Your font will look either condensed or stretched if the value you set in lfWidth does not match the average character width of that font.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.