资源描述:
《c#.net设置系统音量》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、C#设置Windows系统音量改变程序音:w:,但不改变系统音:w:设賈usingSystem;usingSystem.Runtime.InteropServices;usingSystem.Diagnostics;namespaceGavinTung{classSystemMultimediaController{/获得音s范围和获取/设賈当前音:w:**ypublicstaticintMaxValue{get{returnint.Parse(jMaxValue.ToStringO);}}publicstaticintMinValue{get{re
2、turnint.Parse(iMinValue.ToStringO);}}publicstaticintCurrentValue{get{GetVolume();returniCurrentValue;}set{SetValue(MaxValue,MinValue,value);*弹出系统音ft控制器**ypublicstaticvoidPopupController(){ProcessStartlnfoInfo=newProcessStartlnfo();Info.FileName="Sndvol32";Process.Start(lnfo);}#
3、regionPrivateStaticDataMembersprivateconstUlnt32iMaxValue=OxFFFF;privateconstUlnt32iMinValue=0x0000;privatestaticintiCurrentValue=0;#endregion#regionPrivateStaticMethod/修改音量伉**/privatestaticvoidSetValue(intaMaxValue,intaMinValue,intaValue){//先把trackbar的value值映射到0x0000〜OxFFFF范围U
4、lnt32Value=(Ulnt32)((double)0xffff*(double)aValue/(double)(aMaxValue-aMinValue));//限制value的取值范围if(Value<0)Value=0;if(Value>Oxffff)Value=Oxffff;Ulnt32left=(Ulnt32)Value;//左声道音量Ulnt32right=(Ulnt32)Value;//右waveOutSetVolume(0zleft«16
5、right};//"<<"左移,“
6、”逻辑或运算}#endregion/**在winmm.dl
7、l中*第一个参数可以为0,表示首选设备*第二个参数为音量:OxFFFF为最大,0x0000为最小,改变系统咅量设置,设置系统的咅量需要调用系统的AM接口函数[DllImport(“user32.dU”)]來控制减音量、加音量、静音;暂时没有找到直接将音量赋值的方法,但可以间接的来赋值(可以先把音量直接减到0,然后再加到您想要的音量)。publicclassSetsysVolume{publicSetsysVolume(){}privateconstbyteVK_VOLUME_MUTE=OxAD;privateconstbyteVK_VOLUME_DO
8、WN=OxAE;privateconstbyteVK_VOLUME_UP=OxAF;privateconstUint32KEYEVENTF_EXTENDEDKEY=0x0001;privateconstUInt32KEYEVENTF_KEYUP=0x0002;lDllImport(“user32.dir)]staticexternvoidkeybd_event(bytebVk,bytebScan,UInt32dwFlags,UInt32dwExtralnfo);lDllImport(“user32.dir)]staticexternByteMapVi
9、rtualKey(UInt32uCode,UInt32uMapType);//加音量,每次加2publicstaticvoidVolumeUpO*得到当前音量**/privatestaticvoidGetVolume(){Ulnt32d,v;d=0;longi=waveOutGetVolumefd,outv);Ulnt32vleft=v&OxFFFF;Ulnt32.Parse((MaxValueUlnt32vright=(v&OxFFFFOOOO)»16;Ulnt32all=vleft
10、vright;Ulnt32value=(allMinValue)
11、.ToString())/((l)lnt32)iMaxValue));iCurrentValue=int.P