Click here to Skip to main content
Click here to Skip to main content

Win32 C Easy Hyper Link

By , 9 Jun 2006
 

Sample Image - Win32EasyHyperLink.png

Introduction

There are couple different approaches for a Hyper Link control. Two major ones are based on either the STATIC TEXT control or the BUTTON control. As James R. Twine mentioned in his great article: A Better(?) (Hyper)Link Control, a Hyper Link control should behavior correctly in that it can only be reached by keyboard (Tab navigation) and be triggered by Button-Up, not Button-Down (check out any URL link in the web page). When you search online, most Hyper Link control examples are C++ approaches. Here an essential and neat Hyper Link control based on Win32 C approach is demonstrated.

Using the code

The source code is fully commented so it should be easy to understand. Here are the steps to add the Hyper Link control to your dialog box:

  1. Add the [BUTTON] control to your dialog. Set [Owner draw] style to the [BUTTON].
  2. Add HyperLink.h and HyperLink.c to your project. Include HyperLink.h to the dialog proc source file.
  3. Edit LinkInfo_Table in HyperLink.c according to your requirements.
  4. In the Dialog Proc function, do the following steps:
    • Add InitLink() after WM_INITDIALOG.
    • Add DrawLink() after WM_DRAWITEM.
    • Add OpenLink() after WM_COMMAND.
    • Add CloseLink() after WM_DESTROY.
  5. Add comctl32.lib to the project's link library.
  6. Compile and enjoy the Hyper Link.

Points of interest

I actually spent some time on how to refine the implementation. Some points need to be mentioned here.

  • The button control is the best one for Hyper Link, since it can be navigated by keyboard, can get focus correctly, and be triggered by Mouse-Button-Up. But you need to set Owner Draw style to the button control so you can make it look like a Hyper Link by responding to the WM_DRAWITEM message.
  • One of the steps that must be done is adjusting the Hyper Link control's size based on its caption. By using GetTextExtentPoint32() on the corresponding DC and Font, the text size can be obtained, and then we can use SetWindowPos() to adjust the control's size.
  • The Hyper Link control's default WindowProc needs to be subclassed so that we can do SetCursor() correctly when the WM_SETCURSOR message reaches.
  • The neatest way to draw the Hyper Link is by using ExtTextOut(), then DrawFocusRect().
  • If you don't need a tooltip, just comment out CreateLinkTooltip().

History

Shouldn't have any update since it's a small demonstration program, and you can modify it for your own purposes. Any comments or suggestions will be very welcomed.

License

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

About the Author

Hao Hu
Software Developer
United States United States
Member
No Biography provided

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionWindows XP and abovememberMember 803644616 Oct '11 - 6:06 
On Windows XP and above you can use a SysLink Control instead.
GeneralVery good but A BUG foundmemberwin32sdk24 Jun '07 - 22:12 
i thank you for your codes, but i found a bug, so i correct it with my code
as below:
 
// Release the font that we are using for the URL
VOID CloseLink(VOID) {
if(g_hFontURL) DeleteObject(g_hFontURL);
 
if(g_hwndTT) DestroyWindow(g_hwndTT); // Add by cheungmine@gmail.com
}
QuestionThis is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application..memberjaymacher5 Jun '06 - 20:04 
I just tried to port the " Win32 C based approach of an essential Hyper Link Control for dialog box " onto the WInce ... it worked fine for me .. but i just cannot get the link to match the background ...
i mean to say
"SetBkMode(lpdis->hDC, TRANSPARENT);"
 
just dint work the way it did on Win32 platform..
what shud i do to get the buttons with the link match the background...

 
Thanks & Regards

AnswerRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application..memberHao Hu6 Jun '06 - 7:04 
I don't have WinCE on my hand right now.
But from MSDN library:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceui40/html/cerefsetbkmode.asp[^]
 
It says the function should work fine under WinCE. But you might need to link
to the Coredll.lib library.
 
And if you really can NOT make it work, I suggest you to get the background color and draw the same color by yourself.
 
Good luck!
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application..memberjaymacher7 Jun '06 - 1:28 
yeah thats right as far as all the documentation goes this feature should work fine in wince .. but its not working out for me ..
i dint get linking of the coredll part and also can u put some light on how exactly i can get the background coloConfused | :confused: r changed...
hope u really dont mind .... i am still very much an amateur and would appreciate any bit of help ..
thanks and Regards
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application..memberjaymacher7 Jun '06 - 1:36 
hello there ..
 
i just checked on the linking of the coredll.lib part... the linking has been done just right ..
so please ignore my query about the coredll.lib... i guess there is something else thats going wrong...
waiting for some inputs ...
Thanks and Regards....
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application..memberHao Hu7 Jun '06 - 8:50 
Try to replace that function to:
 
SetBkColor(lpdis->hDC, GetSysColor(COLOR_BTNFACE));
 

GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application..memberjaymacher7 Jun '06 - 17:43 
hello there ..
thanx a ton mate... i was just able to achieve the thing with the piece of tip u had given ..
i used
SetBkColor(lpdis->hDC, GetSysColor(COLOR_WINDOW));
 
instead of the
 
SetBkColor(lpdis->hDC, GetSysColor(COLOR_BTNFACE));
that u had suggested ...
it works just fine for me ..
i was actually working backwards on how the hyper link is implemented for a browser..
next i am on to the event capturing mechanism of the hyperlink and how it is inturn given to the network layer to fetch the page ..
If u have in inputs on the same i would appreciate that ..
i must say the code u had written for win32 was one of the easiest and best approach i had seen ..
thanx and please pass on ur valued inputs..
Wink | ;)
 

GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application..memberHao Hu8 Jun '06 - 7:48 
Thanks.
Let's see.
Good luck.
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application..memberjaymacher8 Jun '06 - 19:44 
hello there...
was just trying to achieve the transparency with the createwindow function ... the setbk doesnt work the same way as it did when i used it in combination with ExtTextOut()...
 
any specific reason for the same .... how do i the transparency with createwindow()?
 
Thanks and Regards...

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 9 Jun 2006
Article Copyright 2006 by Hao Hu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid