Solved

C++ change font

Posted on 2011-09-11
8
400 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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