|
1 | | -// Hyperlinks.cpp |
2 | | -// |
3 | | -// Copyright 2002 Neal Stublen |
4 | | -// All rights reserved. |
5 | | -// |
6 | | -// http://www.awesoftware.com |
7 | | -// |
8 | | - |
9 | | -#include <windows.h> |
10 | | - |
11 | | -#include "Hyperlinks.h" |
12 | | - |
13 | | - |
14 | | -#define PROP_ORIGINAL_FONT TEXT("_Hyperlink_Original_Font_") |
15 | | -#define PROP_ORIGINAL_PROC TEXT("_Hyperlink_Original_Proc_") |
16 | | -#define PROP_STATIC_HYPERLINK TEXT("_Hyperlink_From_Static_") |
17 | | -#define PROP_UNDERLINE_FONT TEXT("_Hyperlink_Underline_Font_") |
18 | | - |
19 | | - |
20 | | -LRESULT CALLBACK _HyperlinkParentProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) |
21 | | -{ |
22 | | - WNDPROC pfnOrigProc = (WNDPROC) GetProp(hwnd, PROP_ORIGINAL_PROC); |
23 | | - |
24 | | - switch (message) |
25 | | - { |
26 | | - case WM_CTLCOLORSTATIC: |
27 | | - { |
28 | | - HDC hdc = (HDC) wParam; |
29 | | - HWND hwndCtl = (HWND) lParam; |
30 | | - |
31 | | - BOOL fHyperlink = (NULL != GetProp(hwndCtl, PROP_STATIC_HYPERLINK)); |
32 | | - if (fHyperlink) |
33 | | - { |
34 | | - LRESULT lr = CallWindowProc(pfnOrigProc, hwnd, message, wParam, lParam); |
35 | | - SetTextColor(hdc, RGB(0, 0, 192)); |
36 | | - return lr; |
37 | | - } |
38 | | - |
39 | | - break; |
40 | | - } |
41 | | - case WM_DESTROY: |
42 | | - { |
43 | | - SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(pfnOrigProc)); |
44 | | - RemoveProp(hwnd, PROP_ORIGINAL_PROC); |
45 | | - break; |
46 | | - } |
47 | | - } |
48 | | - return CallWindowProc(pfnOrigProc, hwnd, message, wParam, lParam); |
49 | | -} |
50 | | - |
51 | | -LRESULT CALLBACK _HyperlinkProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) |
52 | | -{ |
53 | | - WNDPROC pfnOrigProc = (WNDPROC) GetProp(hwnd, PROP_ORIGINAL_PROC); |
54 | | - |
55 | | - switch (message) |
56 | | - { |
57 | | - case WM_DESTROY: |
58 | | - { |
59 | | - SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(pfnOrigProc)); |
60 | | - RemoveProp(hwnd, PROP_ORIGINAL_PROC); |
61 | | - |
62 | | - HFONT hOrigFont = (HFONT) GetProp(hwnd, PROP_ORIGINAL_FONT); |
63 | | - SendMessage(hwnd, WM_SETFONT, (WPARAM) hOrigFont, 0); |
64 | | - RemoveProp(hwnd, PROP_ORIGINAL_FONT); |
65 | | - |
66 | | - HFONT hFont = (HFONT) GetProp(hwnd, PROP_UNDERLINE_FONT); |
67 | | - DeleteObject(hFont); |
68 | | - RemoveProp(hwnd, PROP_UNDERLINE_FONT); |
69 | | - |
70 | | - RemoveProp(hwnd, PROP_STATIC_HYPERLINK); |
71 | | - |
72 | | - break; |
73 | | - } |
74 | | - case WM_MOUSEMOVE: |
75 | | - { |
76 | | - if (GetCapture() != hwnd) |
77 | | - { |
78 | | - HFONT hFont = (HFONT) GetProp(hwnd, PROP_UNDERLINE_FONT); |
79 | | - SendMessage(hwnd, WM_SETFONT, (WPARAM) hFont, FALSE); |
80 | | - InvalidateRect(hwnd, NULL, FALSE); |
81 | | - SetCapture(hwnd); |
82 | | - } |
83 | | - else |
84 | | - { |
85 | | - RECT rect; |
86 | | - GetWindowRect(hwnd, &rect); |
87 | | - |
88 | | - POINT pt = { LOWORD(lParam), HIWORD(lParam) }; |
89 | | - ClientToScreen(hwnd, &pt); |
90 | | - |
91 | | - if (!PtInRect(&rect, pt)) |
92 | | - { |
93 | | - HFONT hFont = (HFONT) GetProp(hwnd, PROP_ORIGINAL_FONT); |
94 | | - SendMessage(hwnd, WM_SETFONT, (WPARAM) hFont, FALSE); |
95 | | - InvalidateRect(hwnd, NULL, FALSE); |
96 | | - ReleaseCapture(); |
97 | | - } |
98 | | - } |
99 | | - break; |
100 | | - } |
101 | | - case WM_SETCURSOR: |
102 | | - { |
103 | | - // Since IDC_HAND is not available on all operating systems, |
104 | | - // we will load the arrow cursor if IDC_HAND is not present. |
105 | | - HCURSOR hCursor = LoadCursor(NULL, IDC_HAND); |
106 | | - if (NULL == hCursor) |
107 | | - { |
108 | | - hCursor = LoadCursor(NULL, IDC_ARROW); |
109 | | - } |
110 | | - SetCursor(hCursor); |
111 | | - return TRUE; |
112 | | - } |
113 | | - } |
114 | | - |
115 | | - return CallWindowProc(pfnOrigProc, hwnd, message, wParam, lParam); |
116 | | -} |
117 | | - |
118 | | -BOOL ConvertStaticToHyperlink(HWND hwndCtl) |
119 | | -{ |
120 | | - // Subclass the parent so we can color the controls as we desire. |
121 | | - |
122 | | - HWND hwndParent = GetParent(hwndCtl); |
123 | | - if (NULL != hwndParent) |
124 | | - { |
125 | | - WNDPROC pfnOrigProc = (WNDPROC) GetWindowLongPtr(hwndParent, GWLP_WNDPROC); |
126 | | - if (pfnOrigProc != _HyperlinkParentProc) |
127 | | - { |
128 | | - SetProp(hwndParent, PROP_ORIGINAL_PROC, (HANDLE) pfnOrigProc); |
129 | | - SetWindowLongPtr(hwndParent, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(_HyperlinkParentProc)); |
130 | | - } |
131 | | - } |
132 | | - |
133 | | - // Make sure the control will send notifications. |
134 | | - |
135 | | - LONG_PTR dwStyle = GetWindowLongPtr(hwndCtl, GWL_STYLE); |
136 | | - SetWindowLongPtr(hwndCtl, GWL_STYLE, dwStyle | SS_NOTIFY); |
137 | | - |
138 | | - // Subclass the existing control. |
139 | | - |
140 | | - WNDPROC pfnOrigProc = (WNDPROC)GetWindowLongPtr(hwndCtl, GWLP_WNDPROC); |
141 | | - SetProp(hwndCtl, PROP_ORIGINAL_PROC, (HANDLE) pfnOrigProc); |
142 | | - SetWindowLongPtr(hwndCtl, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(_HyperlinkProc)); |
143 | | - |
144 | | - // Create an updated font by adding an underline. |
145 | | - |
146 | | - HFONT hOrigFont = (HFONT) SendMessage(hwndCtl, WM_GETFONT, 0, 0); |
147 | | - SetProp(hwndCtl, PROP_ORIGINAL_FONT, (HANDLE) hOrigFont); |
148 | | - |
149 | | - LOGFONT lf; |
150 | | - GetObject(hOrigFont, sizeof(lf), &lf); |
151 | | - lf.lfUnderline = TRUE; |
152 | | - |
153 | | - HFONT hFont = CreateFontIndirect(&lf); |
154 | | - SetProp(hwndCtl, PROP_UNDERLINE_FONT, (HANDLE) hFont); |
155 | | - |
156 | | - // Set a flag on the control so we know what color it should be. |
157 | | - |
158 | | - SetProp(hwndCtl, PROP_STATIC_HYPERLINK, (HANDLE) 1); |
159 | | - |
160 | | - return TRUE; |
161 | | -} |
162 | | - |
163 | | -BOOL ConvertStaticToHyperlink(HWND hwndParent, UINT uiCtlId) |
164 | | -{ |
165 | | - return ConvertStaticToHyperlink(GetDlgItem(hwndParent, uiCtlId)); |
166 | | -} |
| 1 | +// Hyperlinks.cpp |
| 2 | +// |
| 3 | +// Copyright 2002 Neal Stublen |
| 4 | +// All rights reserved. |
| 5 | +// |
| 6 | +// http://www.awesoftware.com |
| 7 | +// |
| 8 | + |
| 9 | +#include <windows.h> |
| 10 | + |
| 11 | +#include "Hyperlinks.h" |
| 12 | + |
| 13 | + |
| 14 | +#define PROP_ORIGINAL_FONT TEXT("_Hyperlink_Original_Font_") |
| 15 | +#define PROP_ORIGINAL_PROC TEXT("_Hyperlink_Original_Proc_") |
| 16 | +#define PROP_STATIC_HYPERLINK TEXT("_Hyperlink_From_Static_") |
| 17 | +#define PROP_UNDERLINE_FONT TEXT("_Hyperlink_Underline_Font_") |
| 18 | + |
| 19 | + |
| 20 | +LRESULT CALLBACK _HyperlinkParentProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) |
| 21 | +{ |
| 22 | + WNDPROC pfnOrigProc = (WNDPROC) GetProp(hwnd, PROP_ORIGINAL_PROC); |
| 23 | + |
| 24 | + switch (message) |
| 25 | + { |
| 26 | + case WM_CTLCOLORSTATIC: |
| 27 | + { |
| 28 | + HDC hdc = (HDC) wParam; |
| 29 | + HWND hwndCtl = (HWND) lParam; |
| 30 | + |
| 31 | + BOOL fHyperlink = (NULL != GetProp(hwndCtl, PROP_STATIC_HYPERLINK)); |
| 32 | + if (fHyperlink) |
| 33 | + { |
| 34 | + LRESULT lr = CallWindowProc(pfnOrigProc, hwnd, message, wParam, lParam); |
| 35 | + SetTextColor(hdc, RGB(0, 0, 192)); |
| 36 | + return lr; |
| 37 | + } |
| 38 | + |
| 39 | + break; |
| 40 | + } |
| 41 | + case WM_DESTROY: |
| 42 | + { |
| 43 | + SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(pfnOrigProc)); |
| 44 | + RemoveProp(hwnd, PROP_ORIGINAL_PROC); |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + return CallWindowProc(pfnOrigProc, hwnd, message, wParam, lParam); |
| 49 | +} |
| 50 | + |
| 51 | +LRESULT CALLBACK _HyperlinkProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) |
| 52 | +{ |
| 53 | + WNDPROC pfnOrigProc = (WNDPROC) GetProp(hwnd, PROP_ORIGINAL_PROC); |
| 54 | + |
| 55 | + switch (message) |
| 56 | + { |
| 57 | + case WM_DESTROY: |
| 58 | + { |
| 59 | + SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(pfnOrigProc)); |
| 60 | + RemoveProp(hwnd, PROP_ORIGINAL_PROC); |
| 61 | + |
| 62 | + HFONT hOrigFont = (HFONT) GetProp(hwnd, PROP_ORIGINAL_FONT); |
| 63 | + SendMessage(hwnd, WM_SETFONT, (WPARAM) hOrigFont, 0); |
| 64 | + RemoveProp(hwnd, PROP_ORIGINAL_FONT); |
| 65 | + |
| 66 | + HFONT hFont = (HFONT) GetProp(hwnd, PROP_UNDERLINE_FONT); |
| 67 | + DeleteObject(hFont); |
| 68 | + RemoveProp(hwnd, PROP_UNDERLINE_FONT); |
| 69 | + |
| 70 | + RemoveProp(hwnd, PROP_STATIC_HYPERLINK); |
| 71 | + |
| 72 | + break; |
| 73 | + } |
| 74 | + case WM_MOUSEMOVE: |
| 75 | + { |
| 76 | + if (GetCapture() != hwnd) |
| 77 | + { |
| 78 | + HFONT hFont = (HFONT) GetProp(hwnd, PROP_UNDERLINE_FONT); |
| 79 | + SendMessage(hwnd, WM_SETFONT, (WPARAM) hFont, FALSE); |
| 80 | + InvalidateRect(hwnd, NULL, FALSE); |
| 81 | + SetCapture(hwnd); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + RECT rect; |
| 86 | + GetWindowRect(hwnd, &rect); |
| 87 | + |
| 88 | + POINT pt = { LOWORD(lParam), HIWORD(lParam) }; |
| 89 | + ClientToScreen(hwnd, &pt); |
| 90 | + |
| 91 | + if (!PtInRect(&rect, pt)) |
| 92 | + { |
| 93 | + HFONT hFont = (HFONT) GetProp(hwnd, PROP_ORIGINAL_FONT); |
| 94 | + SendMessage(hwnd, WM_SETFONT, (WPARAM) hFont, FALSE); |
| 95 | + InvalidateRect(hwnd, NULL, FALSE); |
| 96 | + ReleaseCapture(); |
| 97 | + } |
| 98 | + } |
| 99 | + break; |
| 100 | + } |
| 101 | + case WM_SETCURSOR: |
| 102 | + { |
| 103 | + // Since IDC_HAND is not available on all operating systems, |
| 104 | + // we will load the arrow cursor if IDC_HAND is not present. |
| 105 | + HCURSOR hCursor = LoadCursor(NULL, IDC_HAND); |
| 106 | + if (NULL == hCursor) |
| 107 | + { |
| 108 | + hCursor = LoadCursor(NULL, IDC_ARROW); |
| 109 | + } |
| 110 | + SetCursor(hCursor); |
| 111 | + return TRUE; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return CallWindowProc(pfnOrigProc, hwnd, message, wParam, lParam); |
| 116 | +} |
| 117 | + |
| 118 | +BOOL ConvertStaticToHyperlink(HWND hwndCtl) |
| 119 | +{ |
| 120 | + // Subclass the parent so we can color the controls as we desire. |
| 121 | + |
| 122 | + HWND hwndParent = GetParent(hwndCtl); |
| 123 | + if (NULL != hwndParent) |
| 124 | + { |
| 125 | + WNDPROC pfnOrigProc = (WNDPROC) GetWindowLongPtr(hwndParent, GWLP_WNDPROC); |
| 126 | + if (pfnOrigProc != _HyperlinkParentProc) |
| 127 | + { |
| 128 | + SetProp(hwndParent, PROP_ORIGINAL_PROC, (HANDLE) pfnOrigProc); |
| 129 | + SetWindowLongPtr(hwndParent, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(_HyperlinkParentProc)); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Make sure the control will send notifications. |
| 134 | + |
| 135 | + LONG_PTR dwStyle = GetWindowLongPtr(hwndCtl, GWL_STYLE); |
| 136 | + SetWindowLongPtr(hwndCtl, GWL_STYLE, dwStyle | SS_NOTIFY); |
| 137 | + |
| 138 | + // Subclass the existing control. |
| 139 | + |
| 140 | + WNDPROC pfnOrigProc = (WNDPROC)GetWindowLongPtr(hwndCtl, GWLP_WNDPROC); |
| 141 | + SetProp(hwndCtl, PROP_ORIGINAL_PROC, (HANDLE) pfnOrigProc); |
| 142 | + SetWindowLongPtr(hwndCtl, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(_HyperlinkProc)); |
| 143 | + |
| 144 | + // Create an updated font by adding an underline. |
| 145 | + |
| 146 | + HFONT hOrigFont = (HFONT) SendMessage(hwndCtl, WM_GETFONT, 0, 0); |
| 147 | + SetProp(hwndCtl, PROP_ORIGINAL_FONT, (HANDLE) hOrigFont); |
| 148 | + |
| 149 | + LOGFONT lf; |
| 150 | + GetObject(hOrigFont, sizeof(lf), &lf); |
| 151 | + lf.lfUnderline = TRUE; |
| 152 | + |
| 153 | + HFONT hFont = CreateFontIndirect(&lf); |
| 154 | + SetProp(hwndCtl, PROP_UNDERLINE_FONT, (HANDLE) hFont); |
| 155 | + |
| 156 | + // Set a flag on the control so we know what color it should be. |
| 157 | + |
| 158 | + SetProp(hwndCtl, PROP_STATIC_HYPERLINK, (HANDLE) 1); |
| 159 | + |
| 160 | + return TRUE; |
| 161 | +} |
| 162 | + |
| 163 | +BOOL ConvertStaticToHyperlink(HWND hwndParent, UINT uiCtlId) |
| 164 | +{ |
| 165 | + return ConvertStaticToHyperlink(GetDlgItem(hwndParent, uiCtlId)); |
| 166 | +} |
0 commit comments