Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

XGlyphButton - a simple image button control

0.00/5 (No votes)
9 Mar 2004 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here