Click here to Skip to main content
11,717,620 members (77,133 online)
Click here to Skip to main content

Win32 C Easy Hyper Link

, 9 Jun 2006 CPOL 56.3K 667 21
Rate this:
Please Sign up or sign in to vote.
A Win32 C based approach for an essential Hyper Link control for the dialog box (based on the Button control).

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)

Share

About the Author

Hao Hu
Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
SuggestionWindows XP and above Pin
Member 803644616-Oct-11 6:06
memberMember 803644616-Oct-11 6:06 
GeneralVery good but A BUG found Pin
win32sdk24-Jun-07 22:12
memberwin32sdk24-Jun-07 22:12 
QuestionThis is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
jaymacher5-Jun-06 20:04
memberjaymacher5-Jun-06 20:04 
AnswerRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
Hao Hu6-Jun-06 7:04
memberHao Hu6-Jun-06 7:04 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
jaymacher7-Jun-06 1:28
memberjaymacher7-Jun-06 1:28 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
jaymacher7-Jun-06 1:36
memberjaymacher7-Jun-06 1:36 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
Hao Hu7-Jun-06 8:50
memberHao Hu7-Jun-06 8:50 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
jaymacher7-Jun-06 17:43
memberjaymacher7-Jun-06 17:43 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
Hao Hu8-Jun-06 7:48
memberHao Hu8-Jun-06 7:48 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
jaymacher8-Jun-06 19:44
memberjaymacher8-Jun-06 19:44 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
Hao Hu9-Jun-06 8:06
memberHao Hu9-Jun-06 8:06 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
jaymacher12-Jun-06 17:38
memberjaymacher12-Jun-06 17:38 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
Hao Hu12-Jun-06 21:05
memberHao Hu12-Jun-06 21:05 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
jaymacher12-Jun-06 22:20
memberjaymacher12-Jun-06 22:20 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
Hao Hu13-Jun-06 10:30
memberHao Hu13-Jun-06 10:30 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
jaymacher13-Jun-06 17:41
memberjaymacher13-Jun-06 17:41 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
Hao Hu14-Jun-06 16:03
memberHao Hu14-Jun-06 16:03 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
jaymacher15-Jun-06 0:22
memberjaymacher15-Jun-06 0:22 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
Hao Hu15-Jun-06 12:36
memberHao Hu15-Jun-06 12:36 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
jaymacher4-Jul-06 19:39
memberjaymacher4-Jul-06 19:39 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
Hao Hu4-Jul-06 21:24
memberHao Hu4-Jul-06 21:24 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
jaymacher5-Jul-06 18:12
memberjaymacher5-Jul-06 18:12 
GeneralRe: This is regarding the A Win32 C based approach of an essential Hyper Link Control for dialog box application.. Pin
Hao Hu6-Jul-06 6:33
memberHao Hu6-Jul-06 6:33 
GeneralNice! Pin
Roland Pibinger31-Mar-06 21:35
memberRoland Pibinger31-Mar-06 21:35 

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.

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