Let the CButtonST class support the sound when the mouse is skimming

Source: Internet
Author: User

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();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.