■ 볼륨을 무음/증가/감소시키는 방법을 보여준다.
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
using System; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 콘솔 윈도우 구하기 - GetConsoleWindow() /// <summary> /// 콘솔 윈도우 구하기 /// </summary> /// <returns>콘솔 윈도우 핸들</returns> [DllImport("Kernel32", CallingConvention = CallingConvention.StdCall, SetLastError = true)] private static extern IntPtr GetConsoleWindow(); #endregion #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter) /// <summary> /// 메시지 보내기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="message">메시지</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <returns>처리 결과</returns> [DllImport("user32")] private static extern IntPtr SendMessage(IntPtr windowHandle, int message, IntPtr wordParameter, IntPtr longParameter); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// WM_APPCOMMAND /// </summary> private const int WM_APPCOMMAND = 0x319; /// <summary> /// APPCOMMAND_VOLUME_MUTE /// </summary> private const int APPCOMMAND_VOLUME_MUTE = 0x80000; /// <summary> /// APPCOMMAND_VOLUME_DOWN /// </summary> private const int APPCOMMAND_VOLUME_DOWN = 0x90000; /// <summary> /// APPCOMMAND_VOLUME_UP /// </summary> private const int APPCOMMAND_VOLUME_UP = 0xA0000; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 무음 설정하기 - Mute(windowHandle) /// <summary> /// 무음 설정하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> private static void Mute(IntPtr windowHandle) { SendMessage(windowHandle, WM_APPCOMMAND, IntPtr.Zero, (IntPtr)APPCOMMAND_VOLUME_MUTE); } #endregion #region 볼륨 증가시키기 - IncreseVolumn(windowHandle) /// <summary> /// 볼륨 증가시키기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> private static void IncreseVolumn(IntPtr windowHandle) { SendMessage(windowHandle, WM_APPCOMMAND, IntPtr.Zero, (IntPtr)APPCOMMAND_VOLUME_UP); } #endregion #region 볼륨 감소시키기 - DecreseVolumn(windowHandle) /// <summary> /// 볼륨 감소시키기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> private static void DecreseVolumn(IntPtr windowHandle) { SendMessage(windowHandle, WM_APPCOMMAND, IntPtr.Zero, (IntPtr)APPCOMMAND_VOLUME_DOWN); } #endregion #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { IntPtr windowHandle = GetConsoleWindow(); Console.WriteLine("무음 설정을 위해 아무 키나 눌러 주시기 바랍니다."); Console.ReadKey(true); Mute(windowHandle); Console.WriteLine("3단계 볼륨 증가를 위해 아무 키나 눌러 주시기 바랍니다."); Console.ReadKey(true); IncreseVolumn(windowHandle); IncreseVolumn(windowHandle); IncreseVolumn(windowHandle); Console.WriteLine("3단계 볼륨 감소를 위해 아무 키나 눌러 주시기 바랍니다."); Console.ReadKey(true); DecreseVolumn(windowHandle); DecreseVolumn(windowHandle); DecreseVolumn(windowHandle); Console.WriteLine("프로그램을 종료하기 위해 아무 키나 눌러 주시기 바랍니다."); Console.ReadKey(true); } #endregion } } |