?
Solved

C++ change font

Posted on 2011-09-11
8
Medium Priority
?
406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

762 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