Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / MFC
Article

XGlyphButton - a simple image button control

Rate me:
Please Sign up or sign in to vote.
4.83/5 (22 votes)
9 Mar 2004CPOL2 min read 135.4K   2.9K   78   10
XGlyphButton is a simple CButton-based control that allows you to display an image without the need for owner-draw control.

Introduction

XGlyphButton is a general-purpose button control that allows you to use images on the button without needing an ownerdraw control. With XGlyphButton, you can display any glyph from any installed font (for example, WingDings).

XGlyphButton addresses two problems with using images on buttons:

  1. When using themes on Windows XP, ownerdraw buttons will be drawn without the themed look.
  2. Bitmap buttons require use of multiple bitmaps and are overly complicated - especially when all you want to display is something simple like up or down arrows.

I wish to acknowledge the work of Geno Carman, whose article Create a Simple Image Button is what I based XGlyphButton on.

In using Carman's button class, I noticed that some glyphs were drawn with very ragged outlines. After some experimentation with GDI+ and other approaches, I came across CLEARTYPE_QUALITY, which is one of the values for the lfQuality element in the LOGFONT struct. Here is what MSDN says about CLEARTYPE_QUALITY:

Windows XP: If set, text is rendered (when possible) using ClearType antialiasing method.

On XP, for most of the glyphs in the WingDings and Symbol fonts, there is a very noticeable improvement when using CLEARTYPE_QUALITY. On other systems such as Win98 (which do not have ClearType technology), the glyphs are displayed as usual.

In summary, if you can find a glyph in an installed font that is close to the image you want to display, then using XGlyphButton may save you some time, and will produce good-looking results on XP. The drawbacks:

  • You are limited to the glyphs in the installed fonts.
  • There is no way to change the text color, since you must do an ownerdraw button for that.

Demo

Here is what XGlyphButton buttons look like:

screenshot

XGlyphButton API

Here is the complete list of CXGlyphButton methods:

void SetCharSet(BYTE bCharSet);
void SetFaceName(LPCTSTR lpszFaceName);
void SetFont(LOGFONT* plf);
void SetFont(CFont* pFont);
void SetGlyph(UINT cGlyph);
void SetGlyph(LOGFONT* plf, UINT cGlyph);
void SetGlyph(CFont* pFont, UINT cGlyph);
void SetGlyph(LONG lHeight, LONG lPointSize, LONG lWeight,
              LPCTSTR lpszFaceName, UINT cGlyph);
void SetGlyph(LONG lPointSize, LPCTSTR lpszFaceName, UINT cGlyph);
void SetHeight(LONG lHeight);
void SetPointSize(LONG lPointSize);
void SetWeight(LONG lWeight);
void SetWingDingButton(UINT nButton);

How To Use

To integrate XGlyphButton into your app, you first need to add the following files to your project:

  • XGlyphButton.cpp
  • XGlyphButton.h

Then use the resource editor to add a button to your dialog, and use Class Wizard to attach a member variable to that control.

Next, include the header file XGlyphButton.h in the dialog's header file. Then replace the CButton definition with CXGlyphButton. Now you are ready to start using XGlyphButton.

Revision History

Version 1.1 - 2004 March 10

  • Initial public release.

Usage

This software is released into the public domain. You are free to use it in any way you like, except that you may not sell this source code. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions

 
Bugwingdings broken for unicode builds Pin
umeca7423-Mar-14 21:30
umeca7423-Mar-14 21:30 
GeneralI can't show correct icon on the button! Pin
daveice4-Apr-05 20:27
daveice4-Apr-05 20:27 
GeneralRe: I can't show correct icon on the button! Pin
Hans Dietrich6-Apr-05 8:41
mentorHans Dietrich6-Apr-05 8:41 
GeneralRe: I can't show correct icon on the button! Pin
daveice6-Apr-05 17:35
daveice6-Apr-05 17:35 
GeneralRe: I can't show correct icon on the button! Pin
Hans Dietrich8-Apr-05 6:48
mentorHans Dietrich8-Apr-05 6:48 
GeneralRe: I can't show correct icon on the button! Pin
daveice8-Apr-05 18:58
daveice8-Apr-05 18:58 
GeneralMaybe a badly designed XP theme? [modified] Pin
dgendreau13-Jun-06 8:19
dgendreau13-Jun-06 8:19 
GeneralRe: I can't show correct icon on the button! Pin
tom.schultz22-May-05 19:54
tom.schultz22-May-05 19:54 
GeneralRe: I can't show correct icon on the button! Pin
daveice22-May-05 21:37
daveice22-May-05 21:37 
GeneralComponent Gallery Pin
RancidCrabtree22-Mar-04 12:34
RancidCrabtree22-Mar-04 12:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.