[C#] 프로그래밍 팁 #1

윈도우 사운드 볼륨 조절

프로그램에서 윈도우의 사운드 볼륨값을 지정하는 방법을 소개합니다.
알람을 발생하면서 소리를 내야 하는데 자꾸 볼륨이 줄어들어서
소리가 작게 들려서 강제로 볼륨을 지정할때 사용합니다.

using System.Media; // 맨위에 추가

[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) { }
}

사용법

SetSoundVolume(100); // 최고
SetSoundVolume(0); // 최소

중복실행 방지하기

C# 에서 프로그램 중복 실행 방지하기
Program.cs 에 다음과 같이 코딩합니다.
using System.Threading; 추가해주시구요. Main 함수에 이렇게 추가해주시면

bool CreateNew;
Mutex dup = new Mutex(true, "프로그램명", out CreateNew);

if (CreateNew)
{
  // 프로그램 실행부분
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Program());
  dup.ReleaseMutex(); // 이부분이 중요합니다.
}
else
{
  MessageBox.Show("이미 프로그램이 실행중입니다");
}

다른 프로그램으로 인식시키려면 당연히 "프로그램명" 부분을 변경해야 하는건
안가르쳐 드려도 아시겠죠!

댓글

이 블로그의 인기 게시물

2025년 7월 다이어리

1. 대학교 축제 전시 프로젝트