Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC

PolyBtn, A Polygon Button Class

Rate me:
Please Sign up or sign in to vote.
4.94/5 (19 votes)
23 Feb 20032 min read 187.5K   3.2K   102   21
Another non-rectangular button class

Sample Image - polybtn.gif

Introduction

CPolyBtn is a simple CButton derivative that allows you to create buttons that aren't rectangles or circles or any other typical geometric shape; with CPolyBtn, you specify the button shape by providing a series of points that define a polygon.

What Can It Do?

CPolyBtn does all of the normal button stuff: rollovers, bitmaps, tooltips, focus colors, fill colors, disabled colors, etc.

How Can I Use It?

You create a button on your dialog, then create a member variable of type CPolyBtn. You know, the standard stuff. Then, in your CDialog's InitInstance, before calling CDialog::InitInstance, you create a std::vector of CPoint objects, one for each polygon point, where the points are arranged in clockwise order, then assign the vector to the CPolyBtn. CPolyBtn will determine highlight and shadow colors for each edge, set up the regions and handle everything else.

C++
// make a triangle button
std::vector<CPoint> pts;

const int triEdgeSize = 16;

pts.push_back(CPoint(0,0));
pts.push_back(CPoint(triEdgeSize,0));
pts.push_back(CPoint(0,triEdgeSize));

m_zoomInBtn.SetPoints(pts);
m_zoomInBtn.SetBMPOffset(2,2);
m_zoomInBtn.SetBMPID(IDB_ZOOM_IN, RGB(255,255,255), IDB_ZOOM_IN_DISABLED,
                     RGB(255,255,255));

At this point, you can also add bitmaps, set text positions, etc.

For those of you who demand control, CPolyBtn also provides a way for you to specify the colors for each edge manually (normal, highlight, focused, disabled, etc.). This method takes more work, of course. But if you need that much control, the option is there.

Other Options

C++
// optionally set the colors used.
void SetColors(COLORREF highlight, COLORREF down, COLORREF focus, 
               COLORREF disabled, COLORREF rollover);
 
// set the color to fill with. default ::GetSysColor(COLOR_BTNFACE);
void SetFillColor(COLORREF fillClr);
  
// draw bitmap at this offset
void SetBMPOffset(int x, int y);
  
// draw window text at this offset
void SetTextOffset(int x, int y);
 
// optional tooltip
void SetTooltipText(LPCTSTR lpszText, BOOL bActivate = TRUE);
void ActivateTooltip(BOOL bEnable = TRUE);
 
// when using the SetPoints method, you can decide to highlight lines 
// that are more than -45 degrees below horizontal (default false)
void AllowHighlightOnGtrNeg45(bool b);
 
// should the button change color when the mouse moves over ? (default false)
void AllowRollover(bool b);

Issues

In general, I'm not crazy about the way the edges are drawn. Because there's no good way to shrink a polygon, it's hard to draw the button edge like a normal button where the edges are actually two parallel lines, with different colors. So, the edges here are always a single pixel wide. Another example of where this is an issue is in the "focus" rendering: normally, button focus is indicated by drawing a dashed rectangle inside the border. But, since there's no simple way to determine what 'inside' means for all arbitrary polygons, CPolyBtn doesn't bother; it handles focus by drawing the edges with the PS_DOT pen style. (sure, there are complex ways to find the inside of a polygon, but that's more work than I wanted to put into this, and I didn't need it anyway).

Anyway...

If you come up with any improvements on this, feel free to share!

And remember, be excellent to each other.

History

  • 24th February, 2003: Initial version

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.


Written By
Software Developer
United States United States
Chris Losinger was the president of Smaller Animals Software, Inc. (which no longer exists).

Comments and Discussions

 
GeneralRe: Help Pin
Chris Losinger23-Mar-03 6:39
professionalChris Losinger23-Mar-03 6:39 
GeneralRe: Help Pin
BoscoW23-Mar-03 9:29
BoscoW23-Mar-03 9:29 
GeneralRe: Help Pin
Chris Losinger23-Mar-03 15:19
professionalChris Losinger23-Mar-03 15:19 
GeneralGood one! Pin
Kant24-Feb-03 11:16
Kant24-Feb-03 11:16 
GeneralRe: Good one! Pin
Chris Losinger24-Feb-03 11:49
professionalChris Losinger24-Feb-03 11:49 
GeneralRe: Good one! Pin
Kant24-Feb-03 12:23
Kant24-Feb-03 12:23 
QuestionHow much spare time? Pin
Philip Fitzsimons24-Feb-03 9:25
Philip Fitzsimons24-Feb-03 9:25 
AnswerRe: How much spare time? Pin
Chris Losinger24-Feb-03 9:28
professionalChris Losinger24-Feb-03 9:28 
GeneralRe: How much spare time? Pin
Philip Fitzsimons24-Feb-03 9:33
Philip Fitzsimons24-Feb-03 9:33 
GeneralRe: How much spare time? Pin
Chris Losinger24-Feb-03 9:38
professionalChris Losinger24-Feb-03 9:38 
GeneralRe: How much spare time? Pin
Philip Fitzsimons24-Feb-03 9:45
Philip Fitzsimons24-Feb-03 9:45 

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.