MFC uses the slider control to control the system volume

Source: Internet
Author: User

I tried to implement this function and found a lot of information, which was not powerful. Finally, I found that what Microsoft gave was usable, in general, the mixer API is used to open the mixer device, obtain audio line information, and then set attributes. After this is done, it can be used in programs you write, such as players.

 

There are several functions in total:

Through these nine APIs, we can control the audio input and output devices. In fact, you can define these functions in C: /program files/Microsoft Visual Studio/vc98/include/mmsystem. in the H file. Below I will briefly introduce these functions. For more information, see msdn.

The mixeropen and mixerclose functions are used to enable and disable the mixer device.

Mixergetnumdevs can be used to determine the number of mixer devices in the system.

The mixergetdevcaps function determines the capabilities of the mixer device.

Mixergetlineinfo can be used to retrieve information of a specified audio line.

Mixergetlinecontrols is used to retrieve one or more general information about the control related to the audio line.

Mixergetcontroldetails is used to retrieve a control attribute related to an audio line.

Mixersetcontroldetails is used to set control attributes.

 

Dialog Box Project (Project-> Settings-> link to add winmm. Lib, header file contains mmsystem. h), add a slider control, and associate the control variable m_volume

I set the slider to a vertical style, like the small horn in the lower right corner :)

However, I found a problem. By default, the scale of the vertical style is large at the top and lower, while what I want to achieve is that the scale below is 0, and the above is the maximum value. There is no solution for Google.

Csdn is infinite wisdom, and I am despised... Kemee kids shoes say "...
I made another calculation myself... for example, 0 ~ 100. No matter what data is to be displayed on the control, perform an operation with 100 first and then set it. In this way, 0 is displayed when 100,100 is displayed ..", No need to explain this. I admit my head is rusty.

Add a member variable to csliderdlg:

 

Mixercontroldetails_signed volstruct;

Mixercontroldetails mxcd;

Mixerlinecontrols mxlc;

Mixerline mxl;

Mixercontrol mxc;

Hmixer m_hmx;

 

 

Add the code in oninitdialog

 

// Todo: add extra initialization here <br/> unsigned long err; <br/> cstring STR; <br/> err = mixeropen (& m_hmx, 0, 0, 0, 0 ); <br/> If (ERR) <br/> {<br/> MessageBox ("error: Can't open mixer device! "); <Br/>}< br/> // initialize the mixerline struct <br/> zeromemory (& mxl, sizeof (mxl); <br/> mxl. cbstruct = sizeof (mxl); <br/> // specifies the channel to be obtained. The speaker uses mixerline_componenttype_dst_speakers. <br/> mxl. dwcomponenttype = mixerline_componenttype_dst_speakers; <br/> If (mixergetlineinfo (hmixerobj) m_hmx, & mxl, <br/> mixer_getlineinfof_componenttype )) <br/> {<br/> MessageBox ("couldn't get the mixer line! "); <Br/>}< br/> // obtain the controller. <br/> zeromemory (& mxlc, sizeof (mxlc); <br/> mxlc. cbstruct = sizeof (mxlc); <br/> mxlc. dwlineid = mxl. dwlineid; <br/> mxlc. dwcontroltype = mixercontrol_controltype_volume; <br/> mxlc. ccontrols = 1; <br/> mxlc. cbmxctrl = sizeof (mxc); <br/> mxlc. pamxctrl = & mxc; <br/> zeromemory (& mxc, sizeof (mxc); <br/> mxc. cbstruct = sizeof (mxc); <br/> If (mixergetlinecontrols (hmixerobj) m_hmx, & mxlc, <B R/> mixer_getlinecontrolsf_onebytype) <br/> {<br/> MessageBox ("An error occurred while obtaining the controller! "); <Br/>}< br/> // gets the volume range of the value in the Controller: mxc. bounds. lminimum to mxc. bounds. lmaximum. </P> <p> // initialize the mixercontroldetails struct <br/> zeromemory (& mxcd, sizeof (mxcd); <br/> mxcd. cbstruct = sizeof (mxcd); <br/> mxcd. cbdetails = sizeof (volstruct); <br/> mxcd. dwcontrolid = mxc. dwcontrolid; <br/> mxcd. padetails = & volstruct; <br/> mxcd. cchannels = 1; </P> <p> // obtain the volume <br/> If (mixergetcontroldetails (hmixerobj) m_hmx, & mxcd, <br/> Mi Xer_getcontroldetailsf_value) <br/>{< br/> MessageBox ("unable to obtain volume"); <br/>}< br/>/Str. format ("% d", mxc. bounds. dwminimum, mxc. bounds. dwmaximum, volstruct. lvalue); <br/> // MessageBox (STR); <br/>/* unsigned long mixerid; <br/> err = mixergetid (struct hmixerobj __*) m_hmx, (unsigned int *) & mixerid, mixer_objectf_hmixer); <br/> If (ERR) <br/>{< br/> MessageBox ("error: can't get mixer device ID! "); <Br/>}< br/> else <br/>{< br/> Str. format ("mixer device ID = % d/N", mixerid); <br/> MessageBox (STR); <br/>}< br/> mixercaps mixcaps; <br/> unsigned long inumdevs, I; <br/> // obtain the number of devices in the system. <br/> inumdevs = mixergetnumdevs (); <br/> // traverse all the mixers and display their IDs and names <br/> for (I = 0; I <inumdevs; I ++) <br/>{< br/> // obtain the information of the next mixer device <br/> If (! Mixergetdevcaps (I, & mixcaps, sizeof (mixercaps) <br/>{< br/> // display ID and name <br/> Str. format ("device ID # % u: % S/R/N", I, mixcaps. szpname); <br/> MessageBox (STR); <br/>}< br/>}*/<br/> m_volume.setrange (mxc. bounds. dwminimum, mxc. bounds. dwmaximum); <br/> m_volume.setpos (mxc. bounds. dwmaximum-volstruct. lvalue ); 

 

Then in onvscroll (note that it is onvscroll, not onhscroll, because the control is vertical !)

Void csliderdlg: onvscroll (uint nsbcode, uint NPOs, cscrollbar * pscrollbar) <br/>{< br/> // todo: add your message handler code here and/or call default <br/> If (cwnd *) pscrollbar = (cwnd *) & m_volume) <br/>{< br/> int Pos = mxc. bounds. dwmaximum-m_volume.getpos (); <br/> cstring STR; <br/> Str. format ("% d", POS); <br/> // MessageBox (STR); <br/> volstruct. lvalue = Pos; // value to be set <br/> mixersetcontroldetails (hmixerobj) m_hmx, & mxcd, <br/> mixer_setcontroldetailsf_value ); <br/>}< br/> cdialog: onvscroll (nsbcode, NPOs, pscrollbar); <br/>} 

Succeeded. Run it!

 

 

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.