Solved

C++ change font

Posted on 2011-09-11
8
395 Views
Last Modified: 2012-06-27
I am converting a program from Borland Turbo C to Visual Studio C++ 2010.
So far, I have succeeded in writing text to my window, but I need the text to be in a fixed, not proportional font. To that end I have the code  
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, 7, "System");

    hnewsfont = CreateFontIndirect(&cursfont);

    holdsfont = (HFONT)(SelectObject(ADC, (HGDIOBJ)hnewsfont));
//  holdsfont = SelectObject(ADC, (HGDIOBJ)hnewsfont);
//	holdsfont = (HFONT)SelectObject(ADC, (HGDIOBJ)(HFONT)hnewsfont);
    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;
}

Open in new window


However, no matter what alternative I try (others shown commented out) I get the error message:
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
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Just what am I missing?
0
Comment
Question by:norman1312
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 36520086
You should use a monospaced font as 'lfFaceName', e.g. Courier
0
 

Author Comment

by:norman1312
ID: 36520199
Fine, so I  change the font name - to Courier New, for example.
How, exactly, has that made any difference to the problem with SelectObject?
0
 
LVL 7

Expert Comment

by:tampnic
ID: 36522290
Shouldn't the Delete_Font function be using an HDC parameter, not a HBRUSH?

i.e. Delete_Font(HDC ADC) not Delete_Font(HBRUSH ADC)

ADC was passed into MakeFont() as a device context handle, so why is it passed into Delete_Font() as a brush handle?

Cheers,
  Chris


0
 

Author Comment

by:norman1312
ID: 36522443
I lifted this code directly from the Borland manual - without understanding what it did - but it worked!
Now I can see that there are type clashes all over the place and VC C++ is showing them up.
I have now done the following:
1. Corrected the call to DeleteFont to be HDC not HBRUSH
2. Corrected MakeFont to be an HDC
3. The MakeFont call was actually commented out, so I have removed the comment.
The program now compiles and executes but still gives me a window with the proportional spacing - as if the MakeFont hasn't done anything.
How can I get the font I want?

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:tampnic
ID: 36522515
Where are you getting the device context handle ADC that is passed into MakeFont()? The type of window that the device context was retrieved from is important. Is it the application window, a child window, a dialog, a control? I need a bit more information about the general application to help you further.

The device context should be manipulated in response to a WM_PAINT message usually, is this the case in your code? Have you tried sticking a break point into the MakeFont() function to see if it is called before drawing the window - is it called at all?

Cheers,
   Chris
0
 

Author Comment

by:norman1312
ID: 36522905
Oh, I've just found what was stooping the font from changing. The call to MakeFont was using the wrong HDC variable. It was using DC instead of hDc. Once I changed that,the font changed to what I wanted. Been fiddling with sizes and fonts to get an acceptably large/reasonable display.
Now I'm trawling through posts trying to work out how to change the text color in a Win32 application, not a console one.
I think that ends this post, does it not?
0
 
LVL 7

Accepted Solution

by:
tampnic earned 500 total points
ID: 36523630
SetTextColor(ADC,RGB(255,0,0)); 
SetBkMode(ADC,TRANSPARENT);

Open in new window


plonked somewhere into the MakeFont() function should get you in the right direction for changing the font colour. You can alter the RGB formula to suit which colour you want.

I believe that we've sorted this out now.

Cheers,
  Chris
0
 

Author Closing Comment

by:norman1312
ID: 36523716
It turned out that there were some coding errors that, once put right, made the thing work, but I'm sure many have found that talking it through with others enables you to see the problem(s) much faster.
An excellent result on my part.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now