Click here to Skip to main content
Licence 
First Posted 23 Feb 2003
Views 152,562
Bookmarked 98 times

PolyBtn, a polygon button class

By | 23 Feb 2003 | Article
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.
   // 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, <BR>                        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

// optionally set the colors used.
void SetColors(COLORREF highlight, COLORREF down, COLORREF focus, <BR>               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.

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

About the Author

Chris Losinger

Software Developer

United States United States

Member

Chris Losinger is the president of Smaller Animals Software, Inc..

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMessage Automatically Removed PinmemberBill SerGio, The Infomercial King4:37 30 Aug '11  
GeneralProblem with resizing the buttons PinmemberLexaja834:53 19 Jun '07  
Hi all!
I hope someone can help me! I use this class in my project to draw non-rectangular buttons. I thought everything is perfect, but I have one Problem. When I resize my dialog where the buttons are shown in, some buttons don't redraw themselves and so they are positioned where they were before resizing the dialog. What can I do?
 
When I use RedrawWindow() in my CDialog::OnPaint-Method instead of CDialog::OnPaint() (call of the base class), the buttons redraw themselves, but now the dialog-background doesn't redraw, when you pop up another window in the client area of my dialog!
 
Help!!!
GeneralHelp PinmemberSnowbluey13:04 6 Mar '06  
GeneralEasy to use. PinmemberColinDavies18:22 17 Jul '04  
GeneralHelp me PinmemberChuThaiDuong23:43 25 Apr '04  
GeneralCreating regions Pinmembermuzzle_flash7:45 29 Oct '03  
GeneralRe: Creating regions PinmemberChris Losinger7:55 29 Oct '03  
GeneralHelp PinmemberBoscoW5:42 23 Mar '03  
GeneralRe: Help PinmemberChris Losinger5:45 23 Mar '03  
GeneralRe: Help PinmemberBoscoW6:38 23 Mar '03  
GeneralRe: Help PinmemberChris Losinger6:39 23 Mar '03  
GeneralRe: Help PinmemberBoscoW9:29 23 Mar '03  
GeneralRe: Help PinmemberChris Losinger15:19 23 Mar '03  
GeneralGood one! PinmemberKant11:16 24 Feb '03  
GeneralRe: Good one! PinmemberChris Losinger11:49 24 Feb '03  
GeneralRe: Good one! PinmemberKant12:23 24 Feb '03  
QuestionHow much spare time? PinmemberPhilip Fitzsimons9:25 24 Feb '03  
AnswerRe: How much spare time? PinmemberChris Losinger9:28 24 Feb '03  
GeneralRe: How much spare time? PinmemberPhilip Fitzsimons9:33 24 Feb '03  
GeneralRe: How much spare time? PinmemberChris Losinger9:38 24 Feb '03  
GeneralRe: How much spare time? PinmemberPhilip Fitzsimons9:45 24 Feb '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 24 Feb 2003
Article Copyright 2003 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid