========================================================================
We are writing volume control tool in Windows XP. We google some information that volume control in Windows XP is different with Windows Vista, Windows 7...
We have an sample code in below to show how to control volume in Windows XP.
In the form, we use track bar to control the volume and use tool tip to show the current volume.
In the code, we use Windows API to control the volume.
[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);
[DllImport("winmm.dll")]
public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
private void trackWave_Scroll(object sender, EventArgs e)
{
// Calculate the volume that's being set
int NewVolume = ((ushort.MaxValue / 10) * trackWave.Value);
// Set the same volume for both the left and the right channels
uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
// Set volume
waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
toolTip1.SetToolTip(trackWave, trackWave.Value.ToString());
}
When the user scroll the bar. The Wave volume will up or down.
中文
========================================================================
最近在撰寫音量控制的程式,上網查詢了一下,發現 XP 中音量控制與 Vista, Windows 7...之後系統控制的方式不相同,透過以下的範例,讓大家知道如何控制音量。
在程式介面的部分,我們使用 Track bar來進行音量大小聲的操控,並使用 Tool tip 來顯示現在音量的數值
在主程式的部分,使用 Windows 內建的 API 來進行音量的操控,
[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);
[DllImport("winmm.dll")]
public static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
當使用者在拉動 Track bar 時,音量控制的部分如下:
private void trackWave_Scroll(object sender, EventArgs e)
{
// Calculate the volume that's being set
int NewVolume = ((ushort.MaxValue / 10) * trackWave.Value);
// Set the same volume for both the left and the right channels
uint NewVolumeAllChannels = (((uint)NewVolume & 0x0000ffff) | ((uint)NewVolume << 16));
// Set volume
waveOutSetVolume(IntPtr.Zero, NewVolumeAllChannels);
toolTip1.SetToolTip(trackWave, trackWave.Value.ToString());
}
使用者調整拉霸,就可以對音量做控制,如下圖:
P.S 使用者調整的拉霸,對應的拉霸為 "Wave" 這條
沒有留言:
張貼留言