첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
0.00MB

▶ Program.cs

using System;
using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region WAVE OUT 볼륨 구하기 - waveOutGetVolume(waveOutHandle, volume)

        /// <summary>
        /// WAVE OUT 볼륨 구하기
        /// </summary>
        /// <param name="waveOutHandle">WAVE OUT 핸들</param>
        /// <param name="volume">볼륨</param>
        /// <returns>처리 결과</returns>
        [DllImport("winmm")]
        private static extern int waveOutGetVolume(IntPtr waveOutHandle, out uint volume);
 
        #endregion
        #region WAVE OUT 볼률 설정하기 - waveOutSetVolume(waveOutHandle, volume)

        /// <summary>
        /// WAVE OUT 볼률 설정하기
        /// </summary>
        /// <param name="waveOutHandle">WAVE OUT 핸들</param>
        /// <param name="volume">볼륨</param>
        /// <returns>처리 결과</returns>
        [DllImport("winmm")]
        private static extern int waveOutSetVolume(IntPtr waveOutHandle, uint volume);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            SetSoundVolume(50);

            int volume = GetSoundVolume();

            Console.WriteLine("설정 볼륨 : {0}", volume);
        }

        #endregion

        #region 사운드 볼륨 설정하기 - SetSoundVolume(volume)

        /// <summary>
        /// 사운드 볼륨 설정하기
        /// </summary>
        /// <param name="volume">볼륨</param>
        /// <remarks>volume는 0 ~ 100 사이의 값을 설정한다.</remarks>
        private static void SetSoundVolume(ushort volume)
        {
            try
            {
                int value = (int)((double)volume * ushort.MaxValue / 100);

                uint leftChannelValue  = ((uint)value & 0x0000ffff);
                uint rightChannelValue = ((uint)value << 16);

                waveOutSetVolume(IntPtr.Zero, leftChannelValue | rightChannelValue);
            }
            catch
            {
            }
        }

        #endregion
        #region 사운드 볼륨 구하기 - GetSoundVolume()

        /// <summary>
        /// 사운드 볼륨 구하기
        /// </summary>
        /// <returns>볼륨</returns>
        private static int GetSoundVolume()
        {
            uint value;

            waveOutGetVolume(IntPtr.Zero, out value);

            ushort leftChannelValue  = (ushort)(value & 0x0000ffff);
            ushort rightChannelValue = (ushort)(value >> 16);

            int volume = (int)(leftChannelValue * 100.0 / ushort.MaxValue + 0.5);

            return volume;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요