2010-10-27 74 views
2

我想提出一個音樂播放器中的WinForms。我有一個進度條,當我沿着進度條的位置單擊,我想該位置上的INT(從1到100,即當我想在我的歌的某一點)。我怎樣才能做到這一點 ?進度條點擊=>改變當前曲目播放位置

問候, 亞歷Badescu

+1

你應該使用一個滑塊,而不是一個進度條。 – Aren 2010-10-27 19:13:29

回答

0

那麼你可以使用一個圖片來模擬一個進度條...有一個方法基於目前的進展,以局部填充,及導線上MouseDown事件(這會爲您提供與鼠標的位置,你可以相應縮放)。

2

使用TrackBar控制和this是可以提前一個,我希望它可以幫助你。

祝你好運。

+0

謝謝!我會在早上第一件事嘗試! – Alex 2010-10-27 21:49:17

+0

:@Badescu亞歷山:歡迎,但不要忘了給我的答案的接受,如果它工作):d,祝你好運! – Homam 2010-10-27 21:56:23

1

您可以使用必須被放置在進度條--like小子彈上的控件。 現在如果你使用的代碼

progressbar1.value=control.location.x/y 

發生在控制x/y的位置控制的名字決定的座標。

讓你有移動的進度條上的控制簡化使用圖片框,並使用keydown事件並排將它移到一邊記

1

試試這個:

'Using The Click or MouseDown or any Mouse Event 

Dim Value As Integer= Me.PointToClient(MousePosition).X-Progressbar.Bounds.X 

Progressbar.value= Value 
+0

這個應該被標記爲這是根據問題最接近的解決方案。但是這段代碼爲鼠標位置提供了一個絕對值。更好的辦法是將其轉換爲範圍爲0到100的相對值。檢查我的傳入答案。 Upvoted爲正確的方向。 – C4u 2014-09-18 11:29:33

1

這其中一個顯示與進度條上的點擊位置相關的值。這裏的值範圍是從0到100%計算的。它也與進度條的寬度有關,所以範圍將保持固定爲百分比邏輯。

private void progressBar1_Click(object sender, EventArgs e) 
     { 
      // Get mouse position(x) minus the width of the progressbar (so beginning of the progressbar is mousepos = 0 // 
      float absoluteMouse = (PointToClient(MousePosition).X - progressBar1.Bounds.X); 
      // Calculate the factor for converting the position (progbarWidth/100) // 
      float calcFactor = progressBar1.Width/(float)100; 
      // In the end convert the absolute mouse value to a relative mouse value by dividing the absolute mouse by the calcfactor // 
      float relativeMouse = absoluteMouse/calcFactor; 

      // Set the calculated relative value to the progressbar // 
      progressBar1.Value = Convert.ToInt32(relativeMouse); 
     } 

我知道這個問題被問得很久以前,但它是出合適的解決方案。這是現在。