2011-10-16 46 views
7

Tôi muốn thêm hộp thoại Giới thiệu vào ứng dụng Win32 của mình (được phát triển bằng C++). Làm cách nào để thêm siêu liên kết vào hộp thoại? Tôi đang tải hộp thoại từ tệp tài nguyên (.rc). Có thể định nghĩa chức năng này từ tệp .rc không?C++ win32 thêm siêu liên kết vào hộp thoại

tập tin rc của tôi bây giờ trông như thế này:

IDD_ABOUTBOX DIALOGEX 0, 0, 218, 118 
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU | DS_CENTER 
CAPTION "About My App" 
FONT 8, "MS Shell Dlg" 
BEGIN 
    ICON   IDI_APP_ICON,IDC_STATIC,13,88,15,15 
    LTEXT   "MY url http://www.myurl.com",IDC_STATIC,15,6,194,24,SS_NOPREFIX 
    DEFPUSHBUTTON "OK",IDOK,95,98,50,14,WS_GROUP 
END  

Trả lời

10

Bạn có thể sử dụng một SysLink Control trên Windows XP hoặc cao hơn.

Bạn có thể xác định nó từ file rc như thế này:

Trong resource.rc:

CONTROL   "<a>Link</a>",IDC_SYSLINK1,"SysLink",WS_TABSTOP,7,7,53,12 

Trong resource.h:

#define IDC_SYSLINK1     1001 
+0

SYSLink và MFCLink kiểm soát cả làm cho toàn bộ cửa sổ hộp thoại trống của bất kỳ điều khiển nào khác, giống như nó làm cho tất cả các điều khiển vô hình .. không có ý tưởng tại sao .. – SSpoke

+0

@SSpoke: Chỉ cần tò mò nếu các hộp thoại được tô sáng được tạo bằng [CreateDialogIndirect] (http s: //msdn.microsoft.com/en-us/library/windows/desktop/ms645436 (v = vs.85) .aspx) function –

+0

@LaurieStearn Không, tôi đã sử dụng 'tab1_hwnd = CreateDialog (hDLLModule, MAKEINTRESOURCE (IDD_TAB_1), hDlg, DialogPage); 'nó bị ẩn theo mặc định, tôi kích hoạt nó bằng' ShowWindow' 'ShowWindow (tab1_hwnd, (Current_Visible_Selected_Tab == 0)? SW_SHOW: SW_HIDE); 'Tìm thấy một sửa chữa tốt cho điều này nếu có ai cần tôi có thể đăng nó .. nó đòi hỏi Subclassing nhưng nó vẫn còn khá nhỏ và di động. Chỉ cần thay đổi màu sắc khi bạn đặt chuột lên nhãn và nếu bạn nhấp chuột vào nhãn đó là 'ShellExecute (NULL," mở "," http://blah.com ", NULL, NULL, SW_SHOWNORMAL); ' – SSpoke

2

Cách tốt nhất để làm nổi bật mà không có bất kỳ thư viện bên ngoài nào, vẫn trông và cảm thấy giống như bất kỳ điều khiển nào sẽ làm điều đó, thậm chí làm cho con trỏ chuột vào biểu tượng trỏ ngón tay.

/* Start of HyperLink URL */ 
#define PROP_ORIGINAL_FONT  TEXT("_Hyperlink_Original_Font_") 
#define PROP_ORIGINAL_PROC  TEXT("_Hyperlink_Original_Proc_") 
#define PROP_STATIC_HYPERLINK TEXT("_Hyperlink_From_Static_") 
#define PROP_UNDERLINE_FONT  TEXT("_Hyperlink_Underline_Font_") 
LRESULT CALLBACK _HyperlinkParentProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 
LRESULT CALLBACK _HyperlinkProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 
static void CreateHyperLink(HWND hwndControl); 
/* End of HyperLink URL */ 



static void CreateHyperLink(HWND hwndControl) 
{ 
    // Subclass the parent so we can color the controls as we desire. 
    HWND hwndParent = GetParent(hwndControl); 
    if (NULL != hwndParent) 
    { 
     WNDPROC pfnOrigProc = (WNDPROC)GetWindowLong(hwndParent, GWL_WNDPROC); 
     if (pfnOrigProc != _HyperlinkParentProc) 
     { 
      SetProp(hwndParent, PROP_ORIGINAL_PROC, (HANDLE)pfnOrigProc); 
      SetWindowLong(hwndParent, GWL_WNDPROC, (LONG)(WNDPROC)_HyperlinkParentProc); 
     } 
    } 

    // Make sure the control will send notifications. 
    DWORD dwStyle = GetWindowLong(hwndControl, GWL_STYLE); 
    SetWindowLong(hwndControl, GWL_STYLE, dwStyle | SS_NOTIFY); 

    // Subclass the existing control. 
    WNDPROC pfnOrigProc = (WNDPROC)GetWindowLong(hwndControl, GWL_WNDPROC); 
    SetProp(hwndControl, PROP_ORIGINAL_PROC, (HANDLE)pfnOrigProc); 
    SetWindowLong(hwndControl, GWL_WNDPROC, (LONG)(WNDPROC)_HyperlinkProc); 

    // Create an updated font by adding an underline. 
    HFONT hOrigFont = (HFONT)SendMessage(hwndControl, WM_GETFONT, 0, 0); 
    SetProp(hwndControl, PROP_ORIGINAL_FONT, (HANDLE)hOrigFont); 

    LOGFONT lf; 
    GetObject(hOrigFont, sizeof(lf), &lf); 
    lf.lfUnderline = TRUE; 

    HFONT hFont = CreateFontIndirect(&lf); 
    SetProp(hwndControl, PROP_UNDERLINE_FONT, (HANDLE)hFont); 

    // Set a flag on the control so we know what color it should be. 
    SetProp(hwndControl, PROP_STATIC_HYPERLINK, (HANDLE)1); 
} 

LRESULT CALLBACK _HyperlinkParentProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    WNDPROC pfnOrigProc = (WNDPROC)GetProp(hwnd, PROP_ORIGINAL_PROC); 

    switch (message) 
    { 
    case WM_CTLCOLORSTATIC: 
    { 
     HDC hdc = (HDC)wParam; 
     HWND hwndCtl = (HWND)lParam; 

     BOOL fHyperlink = (NULL != GetProp(hwndCtl, PROP_STATIC_HYPERLINK)); 
     if (fHyperlink) 
     { 
      LRESULT lr = CallWindowProc(pfnOrigProc, hwnd, message, wParam, lParam); 
      SetTextColor(hdc, RGB(0, 0, 192)); 
      return lr; 
     } 

     break; 
    } 
    case WM_DESTROY: 
    { 
     SetWindowLong(hwnd, GWL_WNDPROC, (LONG)pfnOrigProc); 
     RemoveProp(hwnd, PROP_ORIGINAL_PROC); 
     break; 
    } 
    } 
    return CallWindowProc(pfnOrigProc, hwnd, message, wParam, lParam); 
} 

LRESULT CALLBACK _HyperlinkProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    WNDPROC pfnOrigProc = (WNDPROC)GetProp(hwnd, PROP_ORIGINAL_PROC); 

    switch (message) 
    { 
    case WM_DESTROY: 
    { 
     SetWindowLong(hwnd, GWL_WNDPROC, (LONG)pfnOrigProc); 
     RemoveProp(hwnd, PROP_ORIGINAL_PROC); 

     HFONT hOrigFont = (HFONT)GetProp(hwnd, PROP_ORIGINAL_FONT); 
     SendMessage(hwnd, WM_SETFONT, (WPARAM)hOrigFont, 0); 
     RemoveProp(hwnd, PROP_ORIGINAL_FONT); 

     HFONT hFont = (HFONT)GetProp(hwnd, PROP_UNDERLINE_FONT); 
     DeleteObject(hFont); 
     RemoveProp(hwnd, PROP_UNDERLINE_FONT); 

     RemoveProp(hwnd, PROP_STATIC_HYPERLINK); 

     break; 
    } 
    case WM_MOUSEMOVE: 
    { 
     if (GetCapture() != hwnd) 
     { 
      HFONT hFont = (HFONT)GetProp(hwnd, PROP_UNDERLINE_FONT); 
      SendMessage(hwnd, WM_SETFONT, (WPARAM)hFont, FALSE); 
      InvalidateRect(hwnd, NULL, FALSE); 
      SetCapture(hwnd); 
     } 
     else 
     { 
      RECT rect; 
      GetWindowRect(hwnd, &rect); 

      POINT pt = { LOWORD(lParam), HIWORD(lParam) }; 
      ClientToScreen(hwnd, &pt); 

      if (!PtInRect(&rect, pt)) 
      { 
       HFONT hFont = (HFONT)GetProp(hwnd, PROP_ORIGINAL_FONT); 
       SendMessage(hwnd, WM_SETFONT, (WPARAM)hFont, FALSE); 
       InvalidateRect(hwnd, NULL, FALSE); 
       ReleaseCapture(); 
      } 
     } 
     break; 
    } 
    case WM_SETCURSOR: 
    { 
     // Since IDC_HAND is not available on all operating systems, 
     // we will load the arrow cursor if IDC_HAND is not present. 
     HCURSOR hCursor = LoadCursor(NULL, IDC_HAND); 
     if (NULL == hCursor) 
      hCursor = LoadCursor(NULL, IDC_ARROW); 
     SetCursor(hCursor); 
     return TRUE; 
    } 
    } 

    return CallWindowProc(pfnOrigProc, hwnd, message, wParam, lParam); 
} 

Dưới đây là làm thế nào để sử dụng nó:

CreateHyperLink(GetDlgItem(Dialog_HWND_GOES_HERE, STATIC_TEXT_IDENIFIER_GOES_HERE)); 

Trường hợp nhãn tĩnh có thể được nhấp trong các hộp thoại chính lớp con làm điều gì đó như thế này ..

 if (HIWORD(wParam) == BN_CLICKED) { //Buttons, checkboxs, labels, static labels clicked 
      switch (LOWORD(wParam)) 
      { 
       case STATIC_TEXT_IDENIFIER_GOES_HERE: 
        ShellExecute(NULL, "open", "http://www.google.com", NULL, NULL, SW_SHOWNORMAL); 
        break; 
      } 
     } 
+0

Đẹp! (winhlp32.exe tại đây). Có một lời giải thích khác [ở đây] (http://www.codeguru.com/cpp/controls/staticctrl/article.php/c5803/Transforming-Static-Text-Controls-into-Active-Hyperlinks-Using-the-Win32- API.htm) và các mã lỗi từ [this] (http://www.codeproject.com/Articles/34/Hyperlink-control) bò lên một chút. –

Các vấn đề liên quan