How do put a clickable URL in a dialog?

It's getting quite common these days to place a URL to a web site in about boxes. But how do you this in a typical MFC app? Well, first you need a static which will respond to being clicked. Let's assume you have a static named IDC_URL in your dialog, which has as its text the URL for your website. You need to apply the SS_NOTIFY style to this static (i.e. check the Notify checkbox in the static's properties dialog). Then you need to override the WM_COMMAND handler for the dialog which hosts the static, and place code something like that below in it:

BOOL CTestbed2Dlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
   CString csURL;

   if (HIWORD(wParam) == STN_CLICKED)
   {
      if (LOWORD(wParam) == IDC_URL)
      {
         FromHandle ((HWND)lParam)->GetWindowText (csURL);
         ShellExecute (GetSafeHwnd(),
                       "open",
                       csURL,
                       NULL,
                       NULL,
                       SW_SHOWNORMAL);
         return TRUE;
      }
   }
   return (CDialog::OnCommand(wParam,lParam));
}

If you want the URL to appear in a different colour, see tip 37 for instructions on changing the colour of a static.

Or, if you want big gobs of functionality, including cursor changing, auto-colour handling etc, then you could go to www.codeproject.com/miscctrl/hyperlink.asp and download Chris Maunder's excellent alternative.

Download