-
Notifications
You must be signed in to change notification settings - Fork 9
/
MyHyperLink.cpp
294 lines (232 loc) · 6.2 KB
/
MyHyperLink.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// MyHyperLink.cpp : implementation file
//
// Written By : Renjith.R
// Email : [email protected]
// Details :Derived from MFC CStatic
// Date :Nov 25 2002
#include "stdafx.h"
#include "MyHyperLink.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyHyperLink
CMyHyperLink::CMyHyperLink()
{
m_sLinkColor = RGB(0, 0 ,255);
m_sHoverColor = RGB(255, 0, 0);
m_sVisitedColor = RGB(5, 34, 143);
m_bFireChild = false;
m_bMouseOver = false;
m_bEnableToolTip = false;
m_bVisited = false;
//Create Tooltip
}
CMyHyperLink::~CMyHyperLink()
{
}
BEGIN_MESSAGE_MAP(CMyHyperLink, CStatic)
//{{AFX_MSG_MAP(CMyHyperLink)
ON_WM_MOUSEMOVE()
ON_WM_SETCURSOR()
ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
ON_WM_CTLCOLOR_REFLECT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyHyperLink message handlers
//Sets the Link Color
void CMyHyperLink::SetLinkColor(COLORREF sLinkColor)
{
m_sLinkColor = sLinkColor;
}
//open the URL by Windows ShellExecute()
bool CMyHyperLink::GoToLinkUrl(CString csLink)
{
HINSTANCE hInstance = (HINSTANCE)ShellExecute(NULL, _T("open"), csLink.operator LPCTSTR(), NULL, NULL, 2);
if ((UINT)hInstance < HINSTANCE_ERROR){
return false;
}else
return true;
}
//User can Active/Inactive the Tooltip already they set
void CMyHyperLink::ActiveToolTip(int nFlag)
{
if (nFlag)
m_bEnableToolTip = true;
else
m_bEnableToolTip = false;
}
//change The Tooltip text
void CMyHyperLink::SetTootTipText(LPCTSTR szToolTip)
{
if (m_bEnableToolTip )
{
m_ToolTip.UpdateTipText( szToolTip,this,1001);
}
}
//The Mouse Move Message
void CMyHyperLink::OnMouseMove(UINT nFlags, CPoint point)
{
CStatic::OnMouseMove(nFlags, point);
if (m_bMouseOver)
{
CRect oRect;
GetClientRect(&oRect);
//check if the mouse is in the rect
if (oRect.PtInRect(point) == false)
{
m_bMouseOver = false;
//Release the Mouse capture previously take
ReleaseCapture();
RedrawWindow();
return;
}
}else
{
m_bMouseOver = true;
RedrawWindow();
//capture the mouse
SetCapture();
}
}
//before Subclassing
void CMyHyperLink::PreSubclassWindow()
{
//Enable the Static to send the Window Messages To its parent
DWORD dwStyle = GetStyle();
SetWindowLong(GetSafeHwnd() ,GWL_STYLE ,dwStyle | SS_NOTIFY);
TCHAR szCurretText[MAX_PATH];
GetWindowText(szCurretText, MAX_PATH);
if ((szCurretText) == NULL){
SetWindowText(m_csLinkText.operator LPCTSTR());
}
LOGFONT sLogFont;
GetFont()->GetLogFont(&sLogFont);
//Set the Link UnderLined
sLogFont.lfUnderline = true;
//Set the Font to the Control
m_oTextFont.CreateFontIndirect(&sLogFont);
this->SetFont(&m_oTextFont, true);
//Adjust the window
//IsValidURL();
//Set the Cursor Hand
//WinHlp32.exe in windows folder ResourceID 106
//is a standard window HAND cursor
//courtesy www.codeguru.com
//you can use a custom Hand cursor resourse also
// i added that as a resourse in this project with
// ID - IDC_CURSOR_HAND
TCHAR szWindowsDir[MAX_PATH*2];
GetWindowsDirectory( szWindowsDir ,MAX_PATH*2);
_tcscat_s( szWindowsDir, MAX_PATH*2, _T( "\\Winhlp32.exe"));
HMODULE hModule = LoadLibrary(szWindowsDir);
if (hModule){
m_hHyperCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
}
this->SetCursor(m_hHyperCursor);
//free the module
if (hModule)
FreeLibrary(hModule);
CStatic::PreSubclassWindow();
this->SetCursor(m_hHyperCursor);
m_ToolTip.Create(this,TTS_ALWAYSTIP);
CRect oRect;
GetClientRect(&oRect);
m_ToolTip.AddTool(this, _T(""),oRect,1001);
m_ToolTip.ShowWindow(SW_HIDE);
}
void CMyHyperLink::SetLinkText(CString csLinkText)
{
m_csLinkText = csLinkText;
this->SetWindowText(csLinkText.operator LPCTSTR());
}
BOOL CMyHyperLink::PreTranslateMessage(MSG* pMsg)
{
m_ToolTip.RelayEvent(pMsg);
return CStatic::PreTranslateMessage(pMsg);
}
BOOL CMyHyperLink::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
::SetCursor(m_hHyperCursor);
return true;
//return CStatic::OnSetCursor(pWnd, nHitTest, message);
}
//////////////////EVENT WILL GET HERE //////////////////////
void CMyHyperLink::OnClicked()
{
if (m_bFireChild){
//Fire the Event to Parent Window
CWnd *pParent;
pParent = GetParent();
int nCtrlID = GetDlgCtrlID();
::SendMessage(pParent->m_hWnd, _HYPERLINK_EVENT, (WPARAM)nCtrlID, 0);
//::PostMessage(pParent->m_hWnd, __EVENT_ID_, (WPARAM)nCtrlID, 0);
}else
{
GoToLinkUrl(m_csUrl);
}
m_bVisited = true;
//reddraw the control
this->Invalidate(true);
}
HBRUSH CMyHyperLink::CtlColor(CDC* pDC, UINT nCtlColor)
{
if (m_bMouseOver){
if (m_bVisited)
pDC->SetTextColor(m_sVisitedColor);
else
pDC->SetTextColor(m_sHoverColor);
}else {
if (m_bVisited)
pDC->SetTextColor(m_sVisitedColor);
else
pDC->SetTextColor(m_sLinkColor);
}
pDC->SetBkMode(TRANSPARENT);
return((HBRUSH)GetStockObject(NULL_BRUSH));
}
void CMyHyperLink::SetToolTipTextColor(COLORREF sToolTipText) {
m_ToolTip.SetTipTextColor(sToolTipText);
}
void CMyHyperLink::SetToolTipBgColor(COLORREF sToolTipBgColor)
{
m_ToolTip.SetTipBkColor(sToolTipBgColor);
}
CString CMyHyperLink::GetLinkText() {
if (m_csLinkText.IsEmpty())
return CString("");
return m_csLinkText;
}
void CMyHyperLink::SetLinkUrl(CString csUrl) {
m_csUrl= csUrl;
}
CString CMyHyperLink::GetLinkUrl() {
return m_csUrl;
}
void CMyHyperLink::SetVisitedColor(COLORREF sVisitedColor) {
m_sVisitedColor = sVisitedColor ;
}
void CMyHyperLink::SetHoverColor(COLORREF cHoverColor) {
m_sHoverColor = cHoverColor;
}
void CMyHyperLink::SetFireChild(int nFlag) {
if (nFlag)
m_bFireChild = true;
else
m_bFireChild = false;
}
BOOL CMyHyperLink::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
NMHDR* pMsgHdr;
pMsgHdr = (NMHDR*) lParam;
switch (pMsgHdr->code){
case NM_RCLICK:
break;
default:
;
}
return CStatic::OnNotify(wParam, lParam, pResult);
}