Click here to Skip to main content
15,742,316 members
Articles / Desktop Programming / MFC
Posted 25 Dec 2005


26 bookmarked

CUniButton - Universal Button for Pocket PC

Rate me:
Please Sign up or sign in to vote.
3.00/5 (4 votes)
25 Dec 2005CPOL3 min read
An owner drawn button class in C++.


I tried to find a ready to use button class that would work with Pocket PC and look better than the dull Microsoft buttons. I thought, it would be very easy to find one. Well, it didn't work out. The best code I could find still required a lot of modifications to meet my expectations.

The objectives were.... Well, let's take a look at the list of features.


  • Owner drawn button class.
  • Fully transparent option.
  • Transparent bitmap support for up and down buttons.
  • Plain or 3D text on top of the button.
  • WAV file association with the button.

These are the basic requirements that allow you to make simple and professionally looking/acting buttons.

Using the code

  • Add UniButton.cpp and UniButton.h to the project.
  • Add #include "UniButton.h" to xxxxxxDlg.h file or wherever required.
  • Add a background bitmap to the resources and a member variable m_hBmpBkgnd as a handle to the bitmap. Load the bitmap and assign the result to handle in OnInitDialog(). This member variable value will be later passed to the UniButton class. Draw the background in OnPaint().
  • Add buttons to the dialog resources as desired. For each button, add a member variable of CButton type using the Class Wizard. In the xxxxxDlg.h file, replace CButton type with CUniButton. In the resource editor, check the "Owner Draw" property.
  • Add the required bitmaps to the resources. For instance, IDB_BITMAP_BTNUP and IDB_BITMAP_BTNDOWN.
  • All the button configurations are done in OnInitDialog() using the public member functions of CUniButton class. This is where the bitmap handle m_hBmpBkgnd is passed to CUniButton. The reason it is done this way is to minimize memory usage due to multiple copies of the background.

Public methods

All these methods are self-explanatory. A few of them are listed below:

void    SetTransparent(BOOL Enable = FALSE);
void    SetTextColor(COLORREF TextColor = RGB(0,0,0));
BOOL    FontStyle(CString sFont = "MS Sans Serif", 
                  int iHeight = 10, int iWidth = 6,
                  BOOL bFont3D = FALSE, BOOL bConcave = FALSE);

void    HideText(BOOL bHide = TRUE);
void    SetBtnDownImg(int bmpID);        //Assign bitmap ID from resources
void    SetBtnUpImg(int bmpID);          //Assign bitmap ID from resources
BOOL    LoadWAV(CString fileName, BOOL loadMEM);
BOOL    WavIsSupported(void);
BOOL    WAVLoaded(void);
void    SetBkgndHandle(HBITMAP hBitmap); //Pass a background bitmap
void    SetTopEdgeColor(COLORREF TopEdge = RGB(50,50,50));
void    SetLeftEdgeColor(COLORREF LeftEdge = RGB(70,70,70));
void    SetTranspColor(COLORREF transpCREF);


When CUniButton is created, the availability of WAV support is done automatically. The main function can check the availability by calling WavIsSipported() before calling LoadWAV(). However, since LoadWAV() does similar checking itself, it is safe to call it without prior checking.

LoadWAV() has two parameters. fileName specifies the path to the WAV file. loadMEM, if true, will load the WAV file into the memory buffer therefore making it faster at the expense of memory used. When it is false, each time the file is to be played, the buffer will be created and the the WAV file will be copied into it, and then played. This will be slower but it requires less memory.

Transparency, in this class, has two meanings. When transparency is set with SetTransparent(TRUE) button bitmaps are ignored and the button is drawn with a background. When transparency is off button bitmaps are used. Although, it might sound confusing they are painted with transparency on. The bitmap's color (255,255,255) - white will be made transparent. Transparent color can be changed with SetTranspColor().

In order to try out the included Test.wav file it has to be placed in the "My Device" directory. Since, I was unable to figure out how to do it with an emulator I tested the sound on the actual device. The pre-built demo can be downloaded from here.


This code is provided "AS IS" without any warranties and for non-commercial use only. For all questions, please contact Advatronix.


  • 12/25/2005: Version 1.0 - Initial release.


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

Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

NewsYou could also make it free... Pin
PatLeCat28-Dec-05 10:21
PatLeCat28-Dec-05 10:21 
GeneralYou also could check... Pin
Davide Calabro26-Dec-05 10:24
Davide Calabro26-Dec-05 10:24 

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.