ToolTips | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||
Download: |
|
|||||||||||
How to implement Tool-Tips |
Adding tool-tips for things like text boxes is easy once you know that it needs a couple of unusual things to be done first! Static Text Controls can have tool-tips too: right-click on the static text control in the Dialog Editor, select Properties, find Notify and set it to True). With a little extra code you can have an edit box tool-tip title get set from its label (the Static Text next to the Edit Box). I also offer a class to create a pop-up text box that looks like a tool-tip but you control when and where it gets drawn.
So the general idea with MFC Tool Tips is to start by creating a CToolTipCtrl in your dialog header file:CToolTipCtrl Tips;If you don't have an OnInitDialog(), use Class Wizard to add WM_INITDIALOG.
Then in the OnInitDialog() function add:Tips.Create(this); Tips.AddTool(GetDlgItem(IDC_THE_CONTROL),"Click me!"); Tips.Activate(TRUE);To make the Tip Control work use Class Wizard to add PreTranslateMessage.
Then in the PreTranslateMessage(MSG* pMsg) function add Tips.RelayEvent(pMsg); which should leave it looking like this:BOOL CMyDlg::PreTranslateMessage(MSG* pMsg) { Tips.RelayEvent(pMsg); return CDialog::PreTranslateMessage(pMsg); }
You can add more tips for more controls with further calls to AddTool and check out the other features in the help files or on MSDN.
Set a Tool Tip Title from another control
Just before a tool tip is shown, Windows gives an opportunity to alter it. If you capture the TTN_SHOW header code in the OnNotify event as follows, you can see which tip is about to be drawn and make changes to it. This example sets the Tool Tip Title of edit boxes to be the same as the Lable (the Static Text just before the Edit Box in the Tab Order). It won't matter if the Label Text is being changed (perhaps by an "inches/mm" Radio Button) - the title is always the latest Label Text:
Tips.Create(this, TTS_NOPREFIX | TTS_BALLOON | TTS_ALWAYSTIP); Tips.SetTitle(TTI_INFO,MSG(MSG_FEX_TITLE)); Tips.SetDelayTime(TTDT_AUTOPOP,0x7FFF); Tips.AddTool(GetDlgItem(IDC_HeightLabel), "Enter the height in mm"); Tips.AddTool(GetDlgItem(IDC_Height ), "Enter the height in mm"); Tips.Activate(TRUE);
BOOL CMyDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) { LPNMHDR Header=(LPNMHDR)lParam; if((Header==0) || (Header->code!=TTN_SHOW)) return CDialog::OnNotify(wParam, lParam, pResult); UINT nID=::GetDlgCtrlID((HWND)Header->idFrom); // idFrom is actually the HWND of the tool if(nID) { CString S; switch(nID) { case IDC_Height: GetDlgItemText(IDC_HeightLabel,S); break; ...Possibly many similar lines for other controls... default: GetDlgItemText(nID,S); break; } S.Remove('&'); Tips.SetTitle(0,S); return TRUE; } return CDialog::OnNotify(wParam, lParam, pResult); }Custom Tool-Tips anywhere, any time
PopupText.h and PopupText.cpp contain a class to draw a Tool-Tip style window, and a class to have the behaviour of a Tool-Tip (CTrackPopup). If this is a member of a CWnd derived class, add the follwing member:
CTrackPopup PopupText;call this in OnMouseMove:PopupText.SetTipTextAndPos("Hello World", x,y, this);
SetTipTextAndPos restarts a timer so it can only fire when the mouse stops moving for long enough. If the timer gets to fire, it will draw the Popup Text. To stop drawing pop-ups, call PopupText.Hide();
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.