BOOKS i'm reading | Small C++ class to transform any static control into a hyperlink control for WindowsContentsThe demo programThe demo program is just a simple MFC AppWizard generated application where the About dialog class has been changed to demonstrate how to use my First, in the Dialog editor, add some static controls. Make sure to select the TABSTOP style and to give the control a unique ID. Then I derive a new class from
class CDemoLink : public CHyperLink { protected: virtual void OnSelect(void) { ((CFrameWnd *)AfxGetMainWnd())-> SetMessageText(m_strURL); } virtual void OnDeselect(void) { ((CFrameWnd *)AfxGetMainWnd())-> SetMessageText(AFX_IDS_IDLEMESSAGE); } }; Then add member variables of type void CAboutDlg::setURL(CHyperLink &ctr, int id) { TCHAR buffer[URLMAXLENGTH]; int nLen = ::LoadString(AfxGetResourceHandle(), id, buffer, URLMAXLENGTH); if( !nLen ) { lstrcpy( buffer, __TEXT("")); } ctr.ConvertStaticToHyperlink(GetSafeHwnd(),id,buffer); } BOOL CAboutDlg::OnInitDialog() { CAboutDlg::OnInitDialog(); // TODO: Add extra initialization here setURL(m_DemoLink,IDC_HOMEPAGE); setURL(m_DemoMail,IDC_EMAIL); return TRUE; // return TRUE unless you // set the focus to a control // EXCEPTION: OCX Property // Pages should return FALSE } The demo program also demonstrates how to use BOOL CAboutDlgWithToolTipURL::OnInitDialog() { CAboutDlg::OnInitDialog(); // TODO: Add extra initialization here m_ctlTT.Create(this); setURL(m_DemoLink,IDC_HOMEPAGE); setURL(m_DemoMail,IDC_EMAIL); /* * It is OK to add a Window tool * to the tool tip control with * the CHyperLink dynamically * allocated URL string because * the windows are destroyed with * WM_DESTROY before the CHyperLink * destructor call where the URL string is freed. */ m_ctlTT.AddWindowTool(GetDlgItem(IDC_HOMEPAGE)->GetSafeHwnd(), (LPTSTR)m_DemoLink.getURL()); m_ctlTT.AddWindowTool(GetDlgItem(IDC_EMAIL)->GetSafeHwnd(), (LPTSTR)m_DemoMail.getURL()); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } Note that /* * class CSubclassToolTipCtrl */ class CSubclassToolTipCtrl : public CToolTipCtrl { // Operations public: /****************************************************************************** * * Name : AddWindowTool * * Purpose : Add a window tool by using the Tooltip subclass feature * * Parameters: * hWin (HWND) Tool window * pszText (LPTSTR) Tip text (can also be a string resource ID). * * Return value : Returns TRUE if successful, or FALSE otherwise. * ****************************************************************************/ BOOL AddWindowTool( HWND hWin, LPTSTR pszText ); /****************************************************************************** * * Name : AddRectTool * * Purpose : Add a rect tool by using the Tooltip subclass feature * * Parameters: * hWin (HWND) Tool window parent * pszText (LPTSTR) Tip text (can also be a string resource ID). * lpRect (LPCRECT) Tool rect * nIDTool (UINT) User defined Tool ID * * Return value : Returns TRUE if successful, or FALSE otherwise. * ****************************************************************************/ BOOL AddRectTool( HWND hWin, LPTSTR pszText, LPCRECT lpRect, UINT nIDTool ); // Implementation void FillInToolInfo(TOOLINFO& ti, HWND hWnd, UINT nIDTool) const; }; /* * Function CSubclassToolTipCtrl::AddWindowTool */ BOOL CSubclassToolTipCtrl::AddWindowTool( HWND hWin, LPTSTR pszText ) { TOOLINFO ti; FillInToolInfo(ti,hWin,0); ti.uFlags |= TTF_SUBCLASS; ti.hinst = AfxGetInstanceHandle(); ti.lpszText = pszText; return (BOOL)SendMessage(TTM_ADDTOOL,0,(LPARAM)&ti); } /* * Function CSubclassToolTipCtrl::AddRectTool */ BOOL CSubclassToolTipCtrl::AddRectTool( HWND hWin, LPTSTR pszText, LPCRECT lpRect, UINT nIDTool ) { TOOLINFO ti; FillInToolInfo(ti,hWin,nIDTool); ti.uFlags |= TTF_SUBCLASS; ti.hinst = AfxGetInstanceHandle(); ti.lpszText = pszText; ::CopyRect(&ti.rect,lpRect); return (BOOL)SendMessage(TTM_ADDTOOL,0,(LPARAM)&ti); } // Implementation /* * Function CSubclassToolTipCtrl::FillInToolInfo */ void CSubclassToolTipCtrl::FillInToolInfo(TOOLINFO& ti, HWND hWnd, UINT nIDTool) const { ::ZeroMemory(&ti, sizeof(TOOLINFO)); ti.cbSize = sizeof(TOOLINFO); if (nIDTool == 0) { ti.hwnd = ::GetParent(hWnd); ti.uFlags = TTF_IDISHWND; ti.uId = (UINT)hWnd; } else { ti.hwnd = hWnd; ti.uFlags = 0; ti.uId = nIDTool; } } I added a minor improvement to the solution found in the Programming Windows With MFC book. I initialized the ConclusionThat is it! I hope you enjoyed this C++ Windows programming tutorial on my hyperlink control C++ class and I hope that the source code will be helpful to you in your projects. In the next section, you will find the books that I have consulted to build this C++ Windows programming tutorial. Those books are great and filled with Windows programming gems that you should know. It is strongly recommended that you get yourself a copy of these books especially since from time to time, you can find these books at a bargain price. Take the time to check the prices. This is maybe your lucky day today! Bibliography
Page 1 2 |