ToolTip with Popup Sound

  Download Source Code and Example


Introduction

Can we do something to make a tooltip attracts the user attention on it a little more? If the tooltip plays a sound when it pops up then maybe the things should get better. Well, this is a small class called CWavTipCtrl that inherits from CToolTipCtrl and adds exactly this feature.

Implementation details

The key points in the control implementation are:

  • How to play a wave file resource?
  • How to understand when the tooltip window pops up?

The first argument is covered by the article "Play Wav Resource" by Anthony Petruso.
The answer to the second question isn't so trivial as you can think. The first idea that comes in mind is to handle the WM_SHOWWINDOW message: wrong! To find the solution, I browsed in the MFC source code. Reading the code, it was clear that the tooltip "pops up" thanks to a call to SetWindowPos() that moves its window at the top of the Z-order (the depth order of all the visible windows). The message to be handled is therefore WM_WINDOWPOSCHANGED. But this message is sent also when the tooltip window is dismissed. For this reason, in the OnWindowPosChanged() function, it is necessary to check if the lpwndpos->flag field contains the SWP_SHOWWINDOW flag. Here is the code:

void CWavTipCtrl::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos) 
{
	CToolTipCtrl::OnWindowPosChanged(lpwndpos);
	
	if (lpwndpos->flags & SWP_SHOWWINDOW)
		if (m_wWaveResourceID)
			PlayResource(m_wWaveResourceID);
}

How to use CWavTipCtrl

Using this class is very simple. Include the WavTipCtrl.cpp (and .h) in your project. Then add a tooltip to your window. For example, if you want to add CWavTipCtrl to a dialog, first add a tooltip component to it, then go in to the header file of the dialog class, #include "WavTipCtrl.h" and replace CToolTipCtrl with CWavTipCtrl. Include a custom wave resource in your project (e.g. IDR_TOOLTIP_SOUND). Now, just add a call to SetPopupSound(IDR_TOOLTIP_SOUND) in the OnInitDialog() body of your dialog. A last important thing: be aware that your program should be linked with the 'winmm.lib' static library, or a link error will occur.

Operations for CWawTipCtrl

void SetPopupSound(WORD wResourceID);
WORD GetPopupSound() const;

Note: If you don't want your CWavTipCtrl uses the popup sound, call SetPopupSound(NULL): NULL is considered as an invalid wave resource ID.

Author's note

I'm continuously working to improve this control. I'll be grateful to you if you mail me your comments, advice, or bug apparition reports!.

Updated: May, 21 1998



Comments

  • Try TTN_SHOW for detection

    Posted by Legacy on 04/22/2000 12:00am

    Originally posted by: Steven

    You might try using TTN_SHOW to make the code more direct and self documenting. Also, this precludes the need to check for hide/show.

    Reply
  • Using for all tooltips

    Posted by Legacy on 01/20/2000 12:00am

    Originally posted by: Drew

    Is it possible to use this for all tooltips? i.e. can you set this class as the default for all tooltips a application uses?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Today's "average" business in general is ever more reliant on technology and the Internet. Mobility is the most often cited business trend that has transformed the way many of us work and communicate. From an IT security perspective, this means that protection methods and tools from even a few years ago are rapidly becoming "unfit for purpose." This guide provides crucial facts to assist you in building a robust business case, meeting the demands of your business, and protecting against threats now and in the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds