Cet extrait de code est originaire de l'ancien site WinAPI www.winapi.net
Pour créer un lien dans une boîte de dialogue, vous devez ajouter un Texte à votre boîte de dialogue, qui seras utilisé en tant que lien. Attribuer le style Notification à ce contrôle de texte.
Nous utilisons le nom IDC_URL pour ce contrôle.
Maintenant, nous ajoutons le code suivant à votre procédure de dialogue. Après avoir cliqué sur le texte,vous allez être redirigé vers le site Web avec votre navigateur standard.
case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_URL: if(HIWORD(wParam) == STN_CLICKED) { ShellExecute(hWnd, NULL, TEXT("http://www.pellesc.de/"), NULL, NULL, SW_SHOW); } return(TRUE); }
Pour obtenir votre lien souligné, ajoutez le code suivant:
case WM_INITDIALOG: { LOGFONT lf; if(NULL == (hFont = (HFONT)SendDlgItemMessage(hWnd, IDC_URL, WM_GETFONT, 0, 0))) hFont = (HFONT)GetStockObject(SYSTEM_FONT); GetObject((HGDIOBJ)hFont, sizeof(lf), &lf); lf.lfUnderline = TRUE; hFont = CreateFontIndirect(&lf); SendDlgItemMessage(hWnd, IDC_URL, WM_SETFONT, (WPARAM)hFont, (LPARAM)TRUE); } return(TRUE); case WM_NCDESTROY: if(hFont) { DeleteObject((HGDIOBJ)hFont); } break;
N'oubliez pas d'ajouter un HFONT HFont
à votre procédure de dialogue!
Pour modifier le curseur, vous devez créer d'abord une variable global:
WNDPROC g_wndpStatic;
Ajoutez le code suivant à votre procédure de dialogue:
g_wndpStatic = (WNDPROC)SetWindowLongPtr(GetDlgItem(hWnd, IDC_URL), GWLP_WNDPROC, (LONG_PTR)UrlProc);
Maintenant nous n'avons besoin que de la sous classe de procédure pour réagir au message WM_SETCURSOR :
LRESULT CALLBACK UrlProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_SETCURSOR: SetCursor(LoadCursor(NULL, IDC_HAND)); return(TRUE); default: break; } return(CallWindowProc(g_wndpStatic, hWnd, uMsg, wParam, lParam)); }
La plupart du temps, les liens URL dans une boîte de dialogue sont de couleur bleue, pour modifier cette couleur:
case WM_CTLCOLORSTATIC: if(GetDlgItem(hWnd, IDC_URL) == (HWND)lParam) { SetTextColor((HDC)wParam, GetSysColor(COLOR_HIGHLIGHT)); SetBkMode((HDC)wParam, TRANSPARENT); return((BOOL)GetStockObject(NULL_BRUSH)); } break;
C'est tout. Maintenant vous avez un lien dans votre boîte de dialogue comme celui-ci:
LRESULT CALLBACK UrlProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_SETCURSOR: SetCursor(LoadCursor(NULL, IDC_HAND)); return(TRUE); default: break; } return(CallWindowProc(g_wndpStatic, hWnd, uMsg, wParam, lParam)); } LRESULT CALLBACK AboutDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { HFONT hFont; switch (uMsg) { case WM_INITDIALOG: { LOGFONT lf; if(NULL == (hFont = (HFONT)SendDlgItemMessage(hDlg, IDC_URL, WM_GETFONT, 0, 0))) hFont = (HFONT)GetStockObject(SYSTEM_FONT); GetObject((HGDIOBJ)hFont, sizeof(lf), &lf); lf.lfUnderline = TRUE; hFont = CreateFontIndirect(&lf); SendDlgItemMessage(hDlg, IDC_URL, WM_SETFONT, (WPARAM)hFont, (LPARAM)TRUE); if(NULL == (hFont = (HFONT)SendDlgItemMessage(hDlg, IDC_APPNAME, WM_GETFONT, 0, 0))) hFont = (HFONT)GetStockObject(SYSTEM_FONT); GetObject((HGDIOBJ)hFont, sizeof(lf), &lf); lf.lfHeight = 16; lf.lfWeight = FW_BOLD; hFont = CreateFontIndirect(&lf); SendDlgItemMessage(hDlg, IDC_APPNAME, WM_SETFONT, (WPARAM)hFont, (LPARAM)TRUE); g_wndpStatic = (WNDPROC)SetWindowLongPtr(GetDlgItem(hDlg, IDC_URL), GWLP_WNDPROC, (LONG_PTR)UrlProc); } return TRUE; case WM_COMMAND: switch (wParam) { case IDC_URL: if(HIWORD(wParam) == STN_CLICKED) { ShellExecute(hDlg, NULL, TEXT("http://www.pellesc.de/"), NULL, NULL, SW_SHOW); } return(TRUE); case IDOK: EndDialog(hDlg, TRUE); return TRUE; } break; case WM_CTLCOLORSTATIC: if(GetDlgItem(hDlg, IDC_URL) == (HWND)lParam) { SetTextColor((HDC)wParam, GetSysColor(COLOR_HOTLIGHT)); SetBkMode((HDC)wParam, TRANSPARENT); return((BOOL)GetStockObject(NULL_BRUSH)); } break; case WM_NCDESTROY: if(hFont) { DeleteObject((HGDIOBJ)hFont); } break; } return FALSE; }