2013-03-30 58 views
0

所以,我試圖做一個簡單的圖形用戶界面來使音樂與C#上的嘟嘟聲。我一直在嘗試,但我不確定是否可以製作Console.Beep播放,例如按住按鈕。How to Console.Beep while(during)mouse click?

這裏有一個正常的Beep()方法,它只是播放一箇中等頻率的短蜂鳴聲,並且有一個過載蜂鳴聲(int frequency,int duration)。我想要做的事情實際上是在我按住按鈕的整個過程中播放它,但顯然我無法事先說明持續時間。

我在想這是不可能的,但也許有辦法嗎?

這也是我在網站上的第一個問題,所以,嘿。

+0

表單或WPF?你應該可以使用鼠標按下eventhandeler –

+0

表單,問題是我無法設置持續時間,因爲我將按住一個未知的時間段。 –

+0

btw在我的答案上的複選標記謝謝 –

回答

3

你可以這樣做,我只是測試它,它的工作原理,並沒有在運行時鎖定窗體。

private void Window_MouseDown_1(object sender, MouseButtonEventArgs e) 
    { 
     // Starts beep on background thread 
     Thread beepThread = new Thread(new ThreadStart(PlayBeep)); 
     beepThread.IsBackground = true; 
     beepThread.Start(); 
    } 
    private void PlayBeep() 
    { 
     // Play 1000 Hz for max amount of time possible 
     // So as long as you dont hold the mouse down for 2,147,483,647 milliseconds it should work. 
     Console.Beep(1000, int.MaxValue); 
    } 

    private void Window_MouseUp_1(object sender, MouseButtonEventArgs e) 
    { 
     //Aborts the beep with a new 1ms beep on mouse up which finishes the task. 
     Console.Beep(1000, 1); 
    } 
+0

哇,很酷,沒想到會中止長長的嗶嗶聲。這對我有用,你的意思是什麼? –

+0

另一個答案也很好,但嗶嗶聲停止而不是一個固定的聲音,這正是我需要的。非常感謝你們。 –