|
Download demo project - 21 Kb
Download source - 7 Kb
This is a simple hyperlink control that can be plugged into any dialog. There are already
a few examples of this in circulation (PJ Naughter, and one from Paul DiLascia from MSJ)
but none had all the features that I wanted. I have used the GotoURL function by Stuart
Patterson in the Aug 97 WDJ. It is more robust than a simple ShellExecute, and I have
modified it to be a little more Unicode friendly.
The hyperlink is initially coloured blue, but changes colour when the cursor
is over it, and after the user has clicked on it. The cursor that appears when
the mouse pointer is over the link can easily be set using CHyperLink::SetLinkCursor,
as can the link colours and underlining. The default cursor is a small pointing
hand (cursor #106 from the winhlp32.exe file - Thanks to Paul DiLascia of MSJ for
that one). There is also a tooltip for the link that dispalys the underlying URL
of the control.
The link has three underline modes which are set using CHyperLink::SetUnderline:
CHyperLink::ulHover - Underline appears when cursor is over link
CHyperLink::ulNone - No underline
CHyperLinkulAlways - Link is always underlined
To use the hyperlink control, just create a static control (eg IDC_HYPERLINK)
and attach it to a member variable of type CHyperLink. The URL for the link is
taken as the caption of the static control unless otherwise specified. If no
caption for the control is specified then it is set as the URL. If neither exists,
then a debug assertion occurs.
The control can auto-size itself to fit the size of the caption (to preserve
a true hyperlink look and feel). The resizing will honour the SS_CENTERIMAGE,
SS_LEFT, SS_RIGHT and SS_CENTER flags.
The control is very simple. The button click and colour notifications are
handled by the control, and appropriate fonts and colours selected according
to the state of the link. The only tricky bit is handling the button click.
In order to do this the static control must have the SS_NOTIFY bit set and
a ON_CONTROL_REFLECT handler for the STN_CLICKED message is installed.
To actually follow the link, ShellExecute is called to open the URL,
but if this fails, then the registry is examined in order to find a likely
candidate for .htm files. If one is found then this it is launched with the
hope that it can handle the URL string supplied. In any case, an error message
is displayed on failure.
Operations for CHyperLink:
void SetURL(CString strURL);
CString GetURL();
void SetColours(COLORREF crLinkColour, COLORREF crVisitedColour
COLORREF crHoverColour = -1);
COLORREF GetLinkColour();
COLORREF GetVisitedColour();
COLORREF GetHoverColour();
void SetVisited(BOOL bVisited = TRUE);
BOOL GetVisited();
void SetLinkCursor(HCURSOR hCursor);
void SetUnderline(int nUnderline = CHyperLink::ulHover);
int GetUnderline();
void SetAutoSize(BOOL bAutoSize = TRUE);
BOOL GetAutoSize();
Note on SetColour: This allows
the unvisited/visited and hover colours to be specifed. If the Hover colour (the
colour the link appears when the cursor is over it) is -1, then the system
highlight colour is used.
History
29 Feb 2000 - P. Shaffer standard font modification
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 50 (Total in Forum: 50) (Refresh) | FirstPrevNext |
|
|
 |
|
|
In this example there is one dialog named "HyperLink Sample". On this dialog there is static text named "mail me" and Ok button is there.
I want same functionality but just want to destroy "HyperLink Sample" dialog when someone clicks on it as well as it should work as HyperLink.
Thanks, Hemang
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I'd like to thank for this wonderful MFC control. It really looks simple and professional. I was saved quite a lot of work. x10 ))
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
there are not completely understandable explanations in the article, which way can people use Hyperlink control ? If the usage of yours Hyperlink control is forbidden in the commercial products, please, let me know ASAP.
I have mentioned you and CHyperLink in the "Credits" section of the Help file.
Please, check this. If everything OK, or I should make credits some another way?
The product site is: http://www.timestimator.com/
If you found Timestimator useful for you, of course, you are always welcome for free license (when the program become completely commercial)
With the best regards, Vit
With the best regards, Vit
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hello
I am using CHyperLink in my application to open a page. This opens in IE. But how is it possible to make this open in Firefox?
thanks in advance Renjith
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
I just want to create a Hyperlink control with "Create()"Function like this : m_Hyperlink.Create(_T("Add Custom"),WS_VISIBLE,Rect(10,10,90,30), this, 99999); But I got this : Unhandled exception at 0x78337cba (mfc80ud.dll) in Manager.exe: 0xC0000005: Access violation reading location 0x00000004. Can you help me ?
|
| Sign In·View Thread·PermaLink | 1.80/5 (2 votes) |
|
|
|
 |
|
|
I'm not sure if these fixes are 100% correct, but I had to make these changes in order for things to work...
1) In SetURL
m_strURL = strURL; SetWindowText(m_strURL); <-- added this
The url wouldn't change using this method, so I added the SetWindowText call. A check to see if it's an empty string might be useful too.
2) In PositionWindow
My center aligned url was being cut off on the left side.
I changed... Extent.cx += WndRect.Width() - ClientRect.Width(); to Extent.cx += WndRect.Width() - ClientRect.Width() + 1;
Now the text is showing correctly.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Hi,
You have it wrong with the fix #1. This is not a bug. It allows you to have the hyperlink caption set to something with SetWindowText() and associate a URL with SetURL.
Go on any web page and you'll see that it is quite rare that a hyperlink text is the underlying URL associated to the link.
If you want both the caption to be the URL text, the correct way of using it would be to both call SetWindowText and SetURL().
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, Thanks for this nice Hyperlink control. I am encountering one problem. I have 10-15 static controls on my form, and I am using Hyperlink class for 3 static controls. But the CtlColor event of Hyperlink class is not firing. I am firing OnCtlColor event in my View class. Please tell me how to fire the CtlColor event of Hyperlink class.
Thanks, Sanat
Sanat
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
I tried to build the demo project in VC6, and I got compile error:
Cannot open include file: 'atlconv.h'
I suspect I need to add some compile option. Can anyone advice?
Thanks a lot,
Don Chen
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
Hello Sir, I want create hyperlink control as u show but i don't like to use mfc so how can i create hyperlink control using win32(api)
plz ,help me
|
| Sign In·View Thread·PermaLink | 3.00/5 (2 votes) |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
First of all, thanks for this very good control. 
I have only one suggestion about the default hand cursor : on Windows 98/Me/2000/XP, you can use IDC_HAND to load the cursor which gives you also support for the drop shadow. To keep compatibility for Win 95, you can modify the SetDefaultCursor() in the following way :
void CHyperLink::SetDefaultCursor() { if (m_hLinkCursor == NULL) // No cursor handle - load our own { #ifdef IDC_HAND m_hLinkCursor = ::LoadCursor(NULL, IDC_HAND); // Load Windows' hand cursor if (m_hLinkCursor == NULL) // if not available, load it from winhlp32.exe #endif { // Get the windows directory CString strWndDir; GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH); strWndDir.ReleaseBuffer();
strWndDir += _T("\\winhlp32.exe"); // This retrieves cursor #106 from winhlp32.exe, which is a hand pointer HMODULE hModule = LoadLibrary(strWndDir); if (hModule) { HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106)); if (hHandCursor) m_hLinkCursor = CopyCursor(hHandCursor); } FreeLibrary(hModule); } } }
|
| Sign In·View Thread·PermaLink | 5.00/5 (3 votes) |
|
|
|
 |
|
|
Hi Chris !
First, nice work, this control is really cool. But I have the following problem :
If I update the window text of the CHyperLink on runtime, it would not erase the control background (and the old text) before overwriting the new text ... I've tried Invalidate(), InvalidateRect() and RedrawWindow() for both only the control and the main dialog, the only thing that will function is RedrawWindow for the main dialog, but then it goes to flickering  Can you help me with that ? (It can be that I do not know how to RedrawWindow() properly only the control, too )
Thanks a lot.
~RaGE();
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
i have a win32-project (without mfc) building upon the scrnsave.lib by microsoft. i have a dialog for the screensaver and want to use a text or image as a hyperlink there. how can i get a click-message from a static text in my wndproc?
greets,
morgler
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
I wonder if anybody knows how hyperlink control can be plugged into a dialog bar. If I am using Chris Maunder's dialog with hyperlink control on it as my dialog bar, the dialog bar would be created in MainFrm.cpp :
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1;
if (!m_wndDlgBar.Create(this, IDD_HLSAMPLE_DIALOG, CBRS_ALIGN_RIGHT, AFX_IDW_DIALOGBAR)) { TRACE0("Failed to create dialogbar\n"); return -1; // fail to create }
return 0; }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
hi, i want to show hyperlink on a particular column. this column is in listcontrol. So how can i do this? i want to show hand cursor if i am on that column or row and after clicking on it i get some dialog. I am ablre to get dialog after clicking. My requirement is to display hyperlink or hand cursor for that column or row. this is highpriority for my project. Please tell me how can i achieve this? thank you, kalpana
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
In your code you are using a timer to determine whether the user is moving the mouse inside or outside the control. This is a good way to do it, but you get WM_TIMER msg every 100 ms. And even you have left the control, you will see a sort of delay after the underline is removed. You see it especially when you have more than one hyperlink. A better way to detect that the mouse is outside the control is to capture all OnMouseMove messages. If the point retrieved is not inside the rectangular of the control, then you know that the mouse is not on the control anymore. This is the code to replace the OnMouseMove function:
void CHyperLink::OnMouseMove(UINT nFlags, CPoint point) { if (m_bOverControl != FALSE) { CRect rect; GetClientRect(&rect);
if (rect.PtInRect(point) == FALSE) { m_bOverControl = FALSE; SetFont(&m_StdFont); Invalidate(); ReleaseCapture(); } } else { m_bOverControl = TRUE; SetFont(&m_UnderlineFont); Invalidate(); SetCapture(); }
CStatic::OnMouseMove(nFlags, point); }
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
Good work, but you forgot something. Function SetFont should only be called in hover mode. If you unconditionally call SetFont, the always and none mode will have no effect. Your calls to SetFont should look like this.
if (m_nUnderline == ulHover) SetFont(&m_StdFont); ... if (m_nUnderline == ulHover) SetFont(&m_UnderlineFont);
I think you also forgot to point out that the Timer handler is no longer required. Your code removed the call to SetTimer. If SetTimer isn't called, anything associated with the Timer handle can be deleted.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
There is a bug with the suggested code. The control can lose the mouse capture in others ways than calling ReleaseCapture. For instance, click on a link and keep the cursor over the link. When the Webbrowser window will pop up, it will break the mouse capture. Because the capture is broken, the control finds itself in a inconsistent state. It will stay underlined forever because m_bOverControl is TRUE so SetCapture() will never be called again and as a consequence, the code that sets back m_stdFont will never be exectuted because the control won't receive mouse messages when the cursor leaves the link control. To fix that change the code:
if (rect.PtInRect(point) == FALSE) { m_bOverControl = FALSE; SetFont(&m_StdFont); Invalidate(); ReleaseCapture(); }
to
if (rect.PtInRect(point) == FALSE) { ReleaseCapture(); }
and add a WM_CAPTURECHANGE handler:
void CHyperLink::OnCaptureChanged(CWnd *pWnd) { m_bOverControl = FALSE; SetFont(&m_StdFont); Invalidate(); }
Once fixed, it will really be a more efficient method than the timer one and it will cover all possible cases!
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|