Introduced a powerful CButton derived class CButtonST, but I think this class has a little function "flaw" when used. I think we all have this experience, some software when the mouse across the button, will make a sound. I made a bit of a makeover on cbuttonnst to make it this feature.
OK, now follow Me,step by step doing it.
First, create a project test based on the dialog box, and then add the original BCMenu.cpp, BCMenu.h, BtnST.cpp, BtnST.h four files to the project.
Next we start to transform the CButtonST class.
1. Open the header file for the CButtonST class BtnST.h add a reference to the multimedia header file and library file at its beginning:#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")
2. Add two member variables and two member functions to the CButtonST class:
private:
CString SoundID;
BOOL m_bPlaySound;
public:
void PlaySound();
void SetPlaySound(BOOL bPlaySound,LPCTSTR sID=NULL);
3. Initialization of variables:
Add code to the constructor of the CButtonST class:
m_bPlaySound=false;
SoundID="";
4. PlaySound (), the code for the Setplaysound function is as follows:
void CButtonST::PlaySound()
{
if(!m_bPlaySound)
return;
if(SoundID=="")
{
MessageBeep(-1);
return;
}
else
{
CString sID="IDR_WAVE1";
HINSTANCE h=AfxGetInstanceHandle();
HRSRC hr=FindResource(h,sID,"WAVE");
HGLOBAL hg=LoadResource(h,hr);
LPSTR lp=(LPSTR)LockResource(hg);
////sndPlaySound(lp,SND_MEMORY|SND_SYNC);
sndPlaySound(lp,SND_MEMORY|SND_ASYNC);
FreeResource(hg);
}
}
void CButtonST::SetPlaySound(BOOL bPlaySound, LPCTSTR sID)
{
m_bPlaySound=bPlaySound;
SoundID=sID;
}
5. In the nesting of the last if statement of the CButtonST onmousemove function, add a sentence playsound ()
if (wndUnderMouse && wndUnderMouse->m_hWnd == m_hWnd && wndActive)
{
if (!m_bMouseOnButton)
{
m_bMouseOnButton = TRUE;
Invalidate();
csTME.cbSize = sizeof(csTME);
csTME.dwFlags = TME_LEAVE;
csTME.hwndTrack = m_hWnd;
::_TrackMouseEvent(&csTME);
PlaySound(); //此句为我们添加的
} // if
}
else
CancelHover();