Solved

C++ change font

Posted on 2011-09-11
8
393 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 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

20 Experts available now in Live!

Get 1:1 Help Now