This shows you the differences between two versions of the page.
— |
dialog_with_link [2008/06/09 12:45] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Creating a link within a dialog ====== | ||
+ | |||
+ | <file>This code snippet is originally from the former WinAPI site www.winapi.net</file> | ||
+ | |||
+ | To create a link within a dialog box, you have to add a //Text// to your dialog, which should be used as link. Assign the //Notify//-Style to this text control. | ||
+ | |||
+ | {{:url1.png?300}} | ||
+ | |||
+ | We use the Name //IDC_URL// for this control. | ||
+ | |||
+ | Now we add the following code to your dialog procedure. After clicking the text you will be redirected to the website with the standard browser. | ||
+ | |||
+ | <code c>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); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | To get your link underlined, add the following code: | ||
+ | |||
+ | <code c>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; | ||
+ | </code> | ||
+ | |||
+ | Don't forget to add a ''HFONT hFont'' to your dialog procedure! | ||
+ | |||
+ | To change the cursor you have to create at first a gobal variable: | ||
+ | |||
+ | <code c>WNDPROC g_wndpStatic;</code> | ||
+ | |||
+ | Add the following code to your dialog procedure: | ||
+ | |||
+ | <code c> g_wndpStatic = (WNDPROC)SetWindowLongPtr(GetDlgItem(hWnd, IDC_URL), | ||
+ | GWLP_WNDPROC, (LONG_PTR)UrlProc); | ||
+ | </code> | ||
+ | |||
+ | Now we only need the subclass procedure to react on the **WM_SETCURSOR** message: | ||
+ | |||
+ | <code c>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)); | ||
+ | }</code> | ||
+ | |||
+ | Mostly, the URL links within a dialog are blue, so we have to change the text color: | ||
+ | |||
+ | <code c>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;</code> | ||
+ | |||
+ | That's it. Now you have a link within your dialog like this: | ||
+ | |||
+ | {{:url2.png}} | ||
+ | |||
+ | ====== Complete sourcecode ====== | ||
+ | |||
+ | <code c>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; | ||
+ | } | ||
+ | </code> | ||