关于WinCE系统音量的设置

这里使用的方法是修改注册表的值,同时调用waveOutSetVolume来达到修改的目的。

1,调用封装好的 SysVolume.h,源码如下:

 1 //////////////////////////////////////////////////////////////////////
 2 // SysVolume.h: interface for the CSysVolume class.
 3 //
 4 
 5 #ifndef SYSVOLUME_H
 6 #define SYSVOLUME_H
 7 
 8 #include "Reg.h"
 9 
10 //-------------------------------------------------------------------------
11 //Macro define
12 #define MIN_VOLUME            0
13 #define MAX_VOLUME            0xFFFFFFFF
14 //-------------------------------------------------------------------------
15 //Enum value
16 enum VolumeModeType
17 {
18     VOL_SOFT,
19     VOL_LOUD,
20     VOL_MUTE
21 };
22 //------------------------------------------------------------------------
23 
24 class CSysVolume  
25 {
26 public:
27     BOOL SetVolumeScreenTap(VolumeModeType volMode);
28     BOOL SetVolumeKeyClick(VolumeModeType volMode);
29     BOOL EnableSoundNotification(BOOL bEnable);
30     BOOL EnableSoundApplication(BOOL bEnable);
31     BOOL EnableSoundEvent(BOOL bEnable);
32     BOOL SetVolume(DWORD dwVol);
33     CSysVolume();
34     virtual ~CSysVolume();
35 
36 protected:
37     BOOL Apply();
38     CReg m_Reg;
39 };
40 
41 #endif //#ifndef SYSVOLUME_H
  1 //////////////////////////////////////////////////////////////////////        
  2 // SysVolume.cpp: implementation of the CSysVolume class.
  3 //
  4 //////////////////////////////////////////////////////////////////////
  5 
  6 #include "stdafx.h"
  7 #include "SysVolume.h"
  8 
  9 
 10 //======================================================================
 11 //Macro define
 12 
 13 //Registry KEY
 14 #define BASE_KEY                     HKEY_CURRENT_USER
 15 #define SUB_KEY                        TEXT("ControlPanel\\Volume")
 16 #define VALUE_VOLUME                TEXT("Volume")
 17 #define VALUE_SCREEN                TEXT("Screen")
 18 #define VALUE_KEY                    TEXT("key")
 19 #define VALUE_MUTE                    TEXT("Mute")
 20 
 21 
 22 //For the screen tap and the key click
 23 #define VOL_VALUE_MUTE                0
 24 #define VOL_VALUE_LOUD                65538
 25 #define VOL_VALUE_SOFT                1
 26 
 27 
 28 //The bit for sound
 29 #define BIT_EVENT                    0x4
 30 #define BIT_APPLICATION                0x2
 31 #define BIT_NOTIFICATION            0x1
 32 //======================================================================
 33 //////////////////////////////////////////////////////////////////////
 34 // Construction/Destruction
 35 //////////////////////////////////////////////////////////////////////
 36 
 37 CSysVolume::CSysVolume()
 38 {
 39     m_Reg.Create(BASE_KEY, SUB_KEY);
 40     //m_Reg.
 41 }
 42 
 43 CSysVolume::~CSysVolume()
 44 {
 45 
 46 }
 47 
 48 
 49 //---------------------------------------------------------------------
 50 //Description:
 51 //    Apply the volume
 52 //---------------------------------------------------------------------
 53 BOOL CSysVolume::Apply()
 54 {
 55     typedef void (WINAPI *DLL_AUDIOUPDATEFROMREGISTRY)();
 56 
 57     DLL_AUDIOUPDATEFROMREGISTRY Dll_AudioUpdateFromRegistry = NULL; 
 58     HINSTANCE hCoreDll = LoadLibrary(TEXT("coredll.dll")); 
 59     if (hCoreDll) 
 60     { 
 61         Dll_AudioUpdateFromRegistry = (DLL_AUDIOUPDATEFROMREGISTRY)GetProcAddress(hCoreDll, _T("AudioUpdateFromRegistry")); 
 62         if (Dll_AudioUpdateFromRegistry) 
 63         { 
 64             (Dll_AudioUpdateFromRegistry)(); 
 65         } 
 66         else
 67         {
 68             return FALSE;
 69         }
 70         FreeLibrary(hCoreDll); 
 71     } 
 72     else
 73     {
 74         return FALSE;
 75     }
 76     return TRUE;
 77 }
 78 
 79 
 80 
 81 //---------------------------------------------------------------------
 82 //Description:
 83 //    Enable the sound for events
 84 //---------------------------------------------------------------------
 85 BOOL CSysVolume::EnableSoundEvent(BOOL bEnable)
 86 {
 87     if(m_Reg.IsOK() != TRUE)
 88     {
 89         return FALSE;
 90     }
 91     
 92     DWORD dwVal = m_Reg.GetValueDW(VALUE_MUTE);
 93 
 94     if(bEnable == TRUE)
 95     {
 96         dwVal |= BIT_EVENT;
 97     }
 98     else
 99     {
100         dwVal &= ~BIT_EVENT;
101     }
102 
103     m_Reg.SetDW(VALUE_MUTE,dwVal);
104 
105     return Apply();
106 }
107 
108 
109 //---------------------------------------------------------------------
110 //Description:
111 //    Enable the sound for application
112 //---------------------------------------------------------------------
113 BOOL CSysVolume::EnableSoundApplication(BOOL bEnable)
114 {
115     if(m_Reg.IsOK() != TRUE)
116     {
117         return FALSE;
118     }
119 
120     DWORD dwVal = m_Reg.GetValueDW(VALUE_MUTE);
121 
122     if(bEnable == TRUE)
123     {
124         dwVal |= BIT_APPLICATION;
125     }
126     else
127     {
128         dwVal &= ~BIT_APPLICATION;
129     }
130 
131     m_Reg.SetDW(VALUE_MUTE,dwVal);
132 
133     return Apply();
134 }
135 
136 
137 //---------------------------------------------------------------------
138 //Description:
139 //    Enable the sound for notifications. If the sound of application is
140 //mute, the sound of  notification is mute too.
141 //---------------------------------------------------------------------
142 BOOL CSysVolume::EnableSoundNotification(BOOL bEnable)
143 {
144     if(m_Reg.IsOK() != TRUE)
145     {
146         return FALSE;
147     }
148 
149     DWORD dwVal = m_Reg.GetValueDW(VALUE_MUTE);
150 
151     if(bEnable == TRUE)
152     {
153         dwVal |= BIT_NOTIFICATION;
154     }
155     else
156     {
157         dwVal &= ~BIT_NOTIFICATION;
158     }
159 
160     m_Reg.SetDW(VALUE_MUTE,dwVal);
161 
162     return Apply();
163 }
164 
165 
166 //---------------------------------------------------------------------
167 //Description:
168 //    Set the key click volume
169 //---------------------------------------------------------------------
170 BOOL CSysVolume::SetVolumeKeyClick(VolumeModeType volMode)
171 {
172 
173     DWORD dwVol = 0;
174 
175     switch(volMode)
176     {
177         case VOL_SOFT:
178             dwVol = VOL_VALUE_SOFT;
179             break;
180         case VOL_LOUD:
181             dwVol = VOL_VALUE_LOUD;
182             break;
183         case VOL_MUTE:
184             dwVol = VOL_VALUE_MUTE;
185             break;
186     }
187     
188     if(m_Reg.IsOK() != TRUE)
189     {
190         return FALSE;
191     }
192     m_Reg.SetDW(VALUE_KEY,dwVol);
193     
194     return Apply();
195 }
196 
197 
198 //---------------------------------------------------------------------
199 //Description:
200 //    Set the screen tap volume
201 //---------------------------------------------------------------------
202 BOOL CSysVolume::SetVolumeScreenTap(VolumeModeType volMode)
203 {    
204     DWORD dwVol = 0;
205 
206     switch(volMode)
207     {
208         case VOL_SOFT:
209             dwVol = VOL_VALUE_SOFT;
210             break;
211         case VOL_LOUD:
212             dwVol = VOL_VALUE_LOUD;
213             break;
214         case VOL_MUTE:
215             dwVol = VOL_VALUE_MUTE;
216             break;
217     }
218 
219     if(m_Reg.IsOK() != TRUE)
220     {
221         return FALSE;
222     }
223     m_Reg.SetDW(VALUE_SCREEN,dwVol);
224     return Apply();
225 }
226 
227 
228 //---------------------------------------------------------------------
229 //Description:
230 //    Set the volume. 
231 //
232 //Parameters:
233 //    dwVol: The volume to set. And the range is MIN_VOLUME ~  MAX_VOLUME
234 //---------------------------------------------------------------------
235 BOOL CSysVolume::SetVolume(DWORD dwVol)
236 {
237     if(dwVol < MIN_VOLUME || dwVol > MAX_VOLUME)
238     {
239         return FALSE;
240     }
241     m_Reg.SetDW(VALUE_VOLUME,dwVol);
242     return Apply();
243 }

 

2,创建对象:CSysVolume sysVol;

3,写音量大小:sysVol.SetVolume(dwVolume);

4,同步输出修改过的音量:waveOutSetVolume(0,dwVolume);

posted @ 2014-06-10 16:50  dh.wong  阅读(574)  评论(0编辑  收藏  举报