Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ change font

Posted on 2011-09-11
8
Medium Priority
?
411 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Industry Leaders: 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!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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.

610 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