|
|
Introduction
For many years I have used Chris Maunder's excellent Hyperlink control. Recently I needed a hyperlink that would send a notification to the parent dialog. So I started by adding this feature, and then I found that I had added other features too. Here is a list of the new features:
- Parent notification - the parent window is optionally sent a notification message.
- New browser window - you can optionally tell the browser to open a new window (same as if you had held down SHIFT key).
- Background color - you can override default background color and set your own.
- Disable tooltip - you can optionally disable tooltips for the hyperlink. This makes sense where the hyperlink only sends a notification to the parent window, but does not navigate.
- Disable URL - optionally disable the URL. Again, this makes sense where the hyperlink only sends a notification to the parent window.
- Dynamic resizing - you can set new window text at any time and now the control will be resized.
- Use of
IDC_HAND - the MFC hand cursor is used when it is available, before defaulting to the cursor from Winhelp.
GoToURL() is now static - now you can say CXHyperLink::GoToURL("http://www.somedomain.com").
XHYPERLINK_REPORT_ERROR define - by default this symbol is not defined, which will remove all the strings and message box from ReportError().
- Resource leak fixed - added
DestroyCursor(m_hLinkCursor) that was suggested by Geert Delmeiren.
How to use
To integrate XHyperLink() into your app, you first need to add following files to your project:
- XHyperLink.cpp
- XHyperLink.h
Then use the resource editor to add a static control to your dialog, and use Class Wizard to attach a member variable to that control. Note that when adding the static control, you must name it something other than IDC_STATIC.
Next, include the header file XHyperLink.h in the dialog's header file. Then replace the CStatic definition with CXHyperLink. Now you are ready to start using XHyperLink.
Demo app
The XHyperLinkTest.exe demo shows how to use CXHyperLink. The first hyperlink sends an email:

The next hyperlink sends a notification to the parent dialog:

and the dialog displays a message box:

The last hyperlink shows how the text and URL may be dynamically changed:

Acknowledgments
Revision history
- Version 1.0 - 2003 October 17
Usage
This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.
| You must Sign In to use this message board. |
|
| | Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh) | FirstPrevNext |
|
|
 |
|
|
 |
|
|
When this control is used on a property page that is added and removed several times, some errors occur because the font and the tooltip objects are not destroyed.
Here's the fix for the tooltip:
// Create the tooltip if (m_bToolTip) { CRect rect; GetClientRect(rect); if (m_ToolTip) { m_ToolTip.DestroyWindow(); m_ToolTip.Detach(); } m_ToolTip.Create(this); m_ToolTip.AddTool(this, m_strURL, rect, TOOLTIP_ID); }
For the font objects you need to do:
if (m_Font.m_hObject) { m_Font.DeleteObject(); m_Font.Detach(); }
All these fixes need to be done in the PreSubclassWindow procedure.
Best regards,
Matthias
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
In the downloaded code the call to SetUnderline() has no effect because the underline state has been hard coded in the function declaration. Here are my changes to give you full control over your hyperlink appearance.
1) in the XHyperLink.h file comment out the [= ulHover] in the function declaration.
2) in the XHyperLink.cpp file set the m_nUnderline variable to your desired default state in the constructor.
3) in the PreClassSubwindow override function pass the m_nUnderline variable in the SetUnderline() function call -- eg. SetUnderline(m_nUnderline);
4) in the SetUnderline() function implementation change the first if statement to the following:
if (m_nUnderline == nUnderline && nUnderline != 1) return;
This forces an "Always Underline" call to set the underline font. Otherwise, an "Always" call will not underline if it follows a previous "hover" call.
After making these changes now you can call m_mycontrol.SetUnderline(state) and set unique text states for each hyperlink. Of course, if you omit these calls, then the default state you set in the constructor will determine the state for all controls.
That's all. Hope this helps.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
This is a fabulous class. I would only like to suggest one improvement.
There may be times where you want to show a tooltip, but hide the URL -- for example the URL is very long.
I modified this class to enable customized tooltips. I simply modified the class declaration for the SetURL function in the XHyperLink.h file as follows:
void SetURL(CString strURL, CString strTip); Then in the "Protected Attributes" section I added a new member variable:
CString m_strTip; // ToolTip text Then in the XHyperLink.cpp file I made two modifications. First in the PreSubclassWindow() function, at the end of the function I modified the following statement:
// Create the tooltip if (m_bToolTip) { CRect rect; GetClientRect(rect); m_ToolTip.Create(this); m_ToolTip.AddTool(this, m_strTip, rect, TOOLTIP_ID); } and finally, I modified the SetURL() function as follows:
void CXHyperLink::SetURL(CString strURL, CString strTip) { m_strURL = strURL; m_strTip = strTip;
if (::IsWindow(GetSafeHwnd())) { PositionWindow(); m_ToolTip.UpdateTipText(strTip, this, TOOLTIP_ID); } } Now from the OnInitDialog() function of your dialog class you simply call SetURL passing two variables, the first is the actual URL and the second your customized Tooltip string -- for example:
CString szURL = "http://wwwp.musicmatch.com/skins/search.cgi? NOHEADER=1&REQUEST=THEMES&Font=Arial,9,FW_BOLD&TextColor=1e323c&ForeColor=0000 00&TextBackColor=b2c8d2&HighLightTextBackColor=91b4c3&HighLightTextColor=00000 0&MMJB_KEY=J425L-WA299-L24KK-H27UK&KEY_STATE=1&CERT_STATE=1&MMUID=C1554A68- 4D2D-4A8D-9683-8DF167402ECC&Grant=0&VERSION=7.50.0089MMD&ThemeVersion=2.1 &OEM=MMD&OOEM=MMD&LANG=ENU";
CString szTip = "Get MMJB Skins Here";
m_Download.SetURL(szURL, szTip);
_____________________________________________________________________________
That's All Folks!!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I agree, but I think it will be more useful a function like this:
void SetURL(CString szURL, LPCTSTR szTip=NULL);
So, if invoked with just 1 argument, it must use szURL for both URL and tooltip.
void CXHyperLink::SetURL(CString szURL, LPCTSTR szTip /*=NULL*/) { m_strURL = szURL; m_strTip = szTip ? szTip : szURL; if (::IsWindow(GetSafeHwnd())) { PositionWindow(); m_ToolTip.UpdateTipText(m_strTip, this, TOOLTIP_ID); } }
Best regards, Jaime.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, how can I use this control in a SDI application? I need to have this XHyperLink in the midle of a CView... Thanke you in advance!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, Hans.
It occurs to me that if the length of the url to be set is large than a certain number not only the hyperlink will cease to function but also the dialog in which the hyperlink control is embedded will disappear.
Any idea?
Thanks,
Don Chen
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
The URL is passed in a CString variable, which as far as the class is concerned could contain all the data of a text file or HTML page if you so needed. The limit would only be imposed if you set a limit at the time you created the variable (in ClassWizard for example).
As far as the static control is concerned, the idea is to NOT try to place the URL in the static control, but rather a meaningful abbreviated version -- such as www.microsoft.com
Use SetWindowText to set the text in your control, and SetURL to set the actual URL (they can--and probably should--be two separate strings).
Also see my posting in this forum on customized tooltips for setting and displaying custom tooltips rather than the URL (this modification was made due to long URLS).
_____________________________________________________________________________
There is nothing left to say!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi Sir, I like yr hyperlink control but i want to create without using mfc how can i create a hyperlink using win32(api)
plz help me, if u free!!
thank a lot
|
| Sign In·View Thread·PermaLink | 1.67/5 (2 votes) |
|
|
|
 |
|
|
Background colors do not take effect:
// transparent text. pDC->SetBkMode(TRANSPARENT);
if (m_Brush.GetSafeHandle()) { pDC->SetBkColor(m_crBackground); return (HBRUSH) m_Brush; } else { return (HBRUSH)GetStockObject(NULL_BRUSH); }
Corrected code:
if (m_Brush.GetSafeHandle()) { pDC->SetBkColor(m_crBackground); return (HBRUSH) m_Brush; } else { // transparent text. pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)GetStockObject(NULL_BRUSH); }
Not so much a bug as a performance issue, but SetTimer and OnTimer are unnecessary:
void CXHyperLink::OnMouseMove(UINT nFlags, CPoint point) { if (!m_bOverControl) // Cursor has just moved over control { m_bOverControl = TRUE; if (m_nUnderline == ulHover) SetFont(&m_UnderlineFont); Invalidate();
SetTimer(m_nTimerID, 100, NULL); } CStatic::OnMouseMove(nFlags, point); }
Modified code:
void CXHyperLink::OnMouseMove(UINT nFlags, CPoint point) { if (m_bOverControl != FALSE) { CRect rect; GetClientRect(&rect);
if (rect.PtInRect(point) == FALSE) { m_bOverControl = FALSE; if (m_nUnderline == ulHover) SetFont(&m_StdFont); Invalidate(); ReleaseCapture(); } } else { m_bOverControl = TRUE; if (m_nUnderline == ulHover) SetFont(&m_UnderlineFont); Invalidate(); SetCapture(); }
CStatic::OnMouseMove(nFlags, point); }
Don't forget to delete the OnTimer() code to save a little time compiling and help reduce final EXE size (in debug mode anyway).
|
| Sign In·View Thread·PermaLink | 3.50/5 (2 votes) |
|
|
|
 |
|
|
The timer "bugfix" is wrong because it leaves the hyperlink in active state after clicking. The original code (with timer) doesn't have this problem.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|