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:
- Add the [BUTTON] control to your dialog. Set [Owner draw] style to the [BUTTON].
- Add HyperLink.h and HyperLink.c to your project. Include HyperLink.h to the dialog proc source file.
- Edit L
inkInfo_Table in HyperLink.c according to your requirements.
- In the Dialog Proc function, do the following steps:
Add comctl32.lib to the project's link library.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
- 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
- If you don't need a tooltip, just comment out
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.