CHyperLink: a simple MFC HyperLink button class with any size 32bpp icons support

HyperLink Demo

Introduction

I know that there are already a lot of classes written for implementing a hyperlink but none satisfied me so I decided to write one on my own. It's only a simple button class usable both for web page redirection or as a normal button with parent notifications. It inherits from CStatic. It's possible to specify a quadruple icon set (when the link is in normal, hovered, clicked or disabled state) and to put these icons on the left, on the right or both sides. It's possible to change the font face, size and weight as you can see in the image above. You can now also change background color, put a border, using multiline text and chose to automatically generate the disabled state icon. To see completely what it can do, download the demo executable.

Using the code

The class is very simple to use. Anyway, I'll explain the class' public methods and their syntax.

First and most important are: SetLinkText and SetLinkUrl which let you respectively set the link text and the link URL (the last one will be used as tooltip text too). You can also set e-mail address links using the syntax "mailto:[email protected]" as you surely already know.

TCHAR* GetLinkText();

void SetLinkText(TCHAR* lpText);

TCHAR* GetLinkUrl();

void SetLinkUrl(TCHAR* lpUrl);

I think these don't need explanations.

Now come methods used to specify optional behaviour of the hyperlink.

Collapse
void LockInPosition(BOOL bLockInPosition = FALSE);

BOOL IsMultiline() { return m_bMultiline; }

void SetMultiline(BOOL bMultiline);

void IsLink(BOOL bIsLink = TRUE);

void UnderlineAlways(BOOL bUnderlineAlways = FALSE);

COLORREF GetUpColor() { return m_crLinkUp; }

COLORREF GetHoverColor() { return m_crLinkHover; }

COLORREF GetDownColor() { return m_crLinkDown; }

COLORREF GetBackgroundColor() { return m_crBackGround; }

void SetColors(COLORREF crLinkUp, COLORREF crLinkHover, COLORREF crLinkDown,

COLORREF crBackGround = ::GetSysColor(COLOR_BTNFACE));

void SetBackgroundColor(COLORREF crBackGround = ::GetSysColor(COLOR_BTNFACE));

COLORREF GetBorderColor() { return m_crBorder; }

void SetBorderColor(COLORREF crBorder = RGB(0,0,0));

int GetBorderSize() { return m_nBorderSize; }

void SetBorderSize(int nSize);

int GetBorderStyle() { return m_nBorderStyle; }

void SetBorderStyle(int nStyle);

UINT GetShowIconsOpt() { return m_uShowIcon; }

void SetIcons(HICON hIconUp, HICON hIconHover, HICON hIconDown,

UINT uShowIcons, HICON hIconDisabled = NULL);

void SetIconUp(int nIconUp, int dx = 0, int dy = 0);

void SetIconHover(int nIconHover, int dx = 0, int dy = 0);

void SetIconDown(int nIconDown, int dx = 0, int dy = 0);

void SetIconDisabled(int nIconDisabled, int dx = 0, int dy = 0);

void SetShowIcons(UINT uShowIcons);

BOOL GetAutoGenerateDisabled() { return m_bAutoGenerateDisabled; }

void SetAutoGenerateDisabled(BOOL bAutoGenerate);

BOOL SetFont(TCHAR* lpFaceName = "Arial", int nSize = 14,

int nWeight = FW_NORMAL, BOOL bItalic = FALSE);

BOOL SetFont(const LPLOGFONT lpFont);

void Disable(BOOL bDisable = FALSE);

CToolTipCtrl* GetTooltip();

void SetTooltip(CToolTipCtrl* pToolTip);

  • LockInPosition is called to specify if the link has to be drawn leaving space for icons although icons are not visible. Try the demo application to understand how it works.
  • UnderlineAlways specifies if the hyperlink will keep the underline style always, even when the link is not hovered.
  • IsLink says if the hyperlink is a link referring to a web page and if the URL string is a web URL or if it's only a tooltip text and the button is a regular dialog button. A point to mention about this method: if you call IsLink(FALSE) and hence the link is considered a regular button, the class will automatically notify the clicks to the parent; instead if the hyperlink is used as a link, the notification will be send only if the static style SS_NOTIFY is set for the control.
  • SetColors sets the hyperlink colors. The parameters are simple to understand. The last parameter specifies the background color. I've added it to solve the problems of the transparent background simulation if you use dialogs in which the background color is different from the default COLOR_BTNFACE. I added specific methods for the background color.
  • SetBorderColor, SetBorderSize, SetBorderStyle are all meant to change border behaviour. To disable border simply call SetBorderSize(0).
  • GetShowIconsOpt simply retrieves the UINT flag specifying how to show icons. This is useful if you need to add a visualization to others already existing instead of setting all once (for example: SetIcons(NULL,NULL,NULL,m_HyperLink.GetGetShowIconsOpt() | 0x0005)).
  • SetIcons sets the hyperlink icons to show on the left or on the right or on both sides of the link for all the different states: normal, hovered, clicked, disabled. Icons can be of any size and/or format. First of all, if you already set an icon, you can pass NULL for that icon to leave the icon unchanged. Now some explanation about the last parameter, that is uShowIcons. This is a WORD flag. The declaration below can help to understand what values it accepts:
    enum {
    
        SI_ICONUP_ON    = 0x0001, //Show icon when mouse Up (normal)
    
            SI_ICONUP_LEFT  = 0x0002, //Show icon when mouse up on the left
    
            SI_ICONUP_RIGHT = 0x0004, //Show icon when mouse up on the right
    
            //
    
            SI_ICONHOVER_ON    = 0x0010, //Show icon when mouse hover
    
            SI_ICONHOVER_LEFT  = 0x0020, //Show icon when mouse hover on the left
    
            SI_ICONHOVER_RIGHT = 0x0040, //Show icon when mouse hover on the right
    
            //
    
            SI_ICONDOWN_ON    = 0x0100, //Show icon when mouse down
    
            SI_ICONDOWN_LEFT  = 0x0200, //Show icon when mouse down on the left
    
            SI_ICONDOWN_RIGHT = 0x0400 //Show icon when mouse down on the right
    
            //
    
            SI_ICONDISABLED_ON    = 0x1000, //Show icon when hyperlink is disabled
    
            SI_ICONDISABLED_LEFT  = 0x2000, //Show icon when hyperlink is
    
                                            //disabled on the left
    
            SI_ICONDISABLED_RIGHT = 0x4000  //Show icon when hyperlink is
    
                                            //disabled on the right
    
        }; 

    Bits 0-7 represent the icon shown for the normal hyperlink state, bits 8-15 instead represent the icon shown for the hovered hyperlink state, 16-23 for the clicked hyperlink state and last 8 bits are for disabled state. Each 8 bits flag says how the icon is shown and whether it is shown on the left/right in that state. So a value of 3 shows only the left icon, while a value of 5 shows only the right icon, and a value of 7 shows both on the left and the right. So if you want to show icons on all states and both on left and right, simply call SetIcons with 0x7777 as the last parameter. It's simpler to do than to explain. For example, if you want a hyperlink with an icon on the left which disappears on the left and appears on the right when the hyperlink is hovered, and you want to show both when you click on the hyperlink and when it's disabled, you just have to call SetIcons with the flag 0x7753. Something like this:

    SetIcons(theApp.LoadIcon(IDI_ICON1),
    
        theApp.LoadIcon(IDI_ICON1),theApp.LoadIcon(IDI_ICON1),0x0753);

    In addition I added some specific functions to load icons from reasource ID. Those are SetIconUp, SetIconHover, SetIconDown, SetIconDisabled. Plus I put a method to set only how icons must be shown (that is, the uShowIcons flag).

  • GetAutoGenerateDisabled simply set whether to generate automatically or not the disabled state icon.
  • SetFont doesn't need explanations I think. The method requires the font face, font size, font weight and a BOOL indicating if the font will be italicised or an overloaded version which requires a LPLOGFONT.
  • Disable simply disables the hyperlink.
  • GetTooltip and SetTooltip respectively gets or sets a custom tooltip to use with the control.

Notes

I found those icons on my hard drive but sincerely don't know where do they come from, so if they're copyrighted material and the owner of the rights doesn't want to let me use them, just let me know and I will replace those with something else.

Conclusions

So, that's all, I think.

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.

你可能感兴趣的:(button)