728x90

사운드 관련 클래스 정의

using System.Runtime.InteropServices;
public class Win32
        {
            #region 사운드 관련
            [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);
 
            public static void SetSoundVolume(int volume)
            {
                try
                {
                    int newVolume = ((ushort.MaxValue / 10* volume);
                    uint newVolumeAllChannels = (((uint)newVolume & 0x0000ffff| ((uint)newVolume << 16));
                    waveOutSetVolume(IntPtr.Zero, newVolumeAllChannels);
                }
                catch (Exception) { }
            }
 
            public static int GetSoundVolume()
            {
                int value = 0;
                try
                {
                    uint CurrVol = 0;
                    waveOutGetVolume(IntPtr.Zero, out CurrVol);
                    ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
                    value = CalcVol / (ushort.MaxValue / 10);
                }
                catch (Exception) { }
                return value;
            }
            #endregion
        }



폼의 생성자

public Form1()
        {
            InitializeComponent();
            formInit();
 
            lobbyBgm.PlayLooping();
            Win32.SetSoundVolume(5);    // 초기 볼륨 설정 (0~10)
 
            trackBar1.Value = Win32.GetSoundVolume();   // 트랙바의 값을 현재 볼륨값으로 설정
        }


트랙바 스크롤 이벤트

private void trackBar1_Scroll(object sender, EventArgs e)
        {
            int volume;
            volume = trackBar1.Value;
            
            Win32.SetSoundVolume(volume);
        }


'Study > C#' 카테고리의 다른 글

c# docx 에서 텍스트 얻기  (0) 2020.10.28
IP 주소 정수로 변환  (0) 2017.05.16
C# Thread  (0) 2017.04.24
C# MS-SQL 연동  (0) 2017.04.14
C# 네트워크 기본  (1) 2017.04.12

+ Recent posts