2015-10-06 97 views
-1

基本上,我創建了面板類的擴展,該類添加了在自身上繪製多個位圖以創建多個音樂曲譜。我試過在面板上添加一個垂直滾動條,但是沒有奏效。我的畫圖程序與此類似如何將滾動添加到面板

private void StavePanel_Paint(object sender, PaintEventArgs e) 
{ 
    for(int i = 0; i < linenumber; i++) 
    { 
     Bitmap bmp = new Bitmap(Width, 200); 
     //edit bmp to resemble stave 
     e.Graphics.DrawImage(bmp,new Point(0,200*i); 
    } 
} 
+0

你嘗試過'AutoScroll'屬性(或添加一個'VScrollBar' /'HScrollBar')嗎? – varocarbas

+0

是的,這似乎不起作用 – Rariolu

+0

你的意思是「似乎沒有工作」?也許你沒有正確使用它們。你能解釋一下你做了什麼嗎? – varocarbas

回答

0

AutoScroll property設置爲true。

你也可以考慮替代方案:

  • FlowLayoutPanel並添加PictureBoxes動態,而不是畫。
  • TableLayoutPanel並動態添加PictureBox而不是繪畫。
  • 延伸ListBox並設置DrawMode propertyOwnerDrawFixedOwnerDrawVariable然後覆蓋的方法和OnPaintOnMeasureItem(僅用於OwnerDrawVariable)。
0

如果你想繼續使用你現有的調用GDI代碼的模式來繪製你的控件,你應該添加一個滾動條控件並添加一個事件處理程序到它的change事件中。除了在面板上調用.Invalidate之外,更改處理程序不需要執行任何操作。 。無效是向控件發出的「髒」信號,需要重新繪製。您將需要修改繪畫代碼以與滾動條值相反的方向偏移繪圖。

所以,如果你的滾動條50位,你應該在Y處吸取一切 - 50

如果使用純GDI的繪製代碼沒有必要惹AutoScroll屬性都沒有。僅當您的面板託管比面板大的實際控制時纔會使用該功能。

1

剛剛成立AutoScrollMinSize屬性還:

panel1.AutoScrollMinSize = new Size(0, 1000); 

在油漆事件,您需要使用TranslateTransform方法繪圖的位置轉換。另外,你需要你畫後,他們處理您的位圖:

e.Graphics.TranslateTransform(panel1.AutoScrollPosition.X, panel1.AutoScrollPosition.Y); 

using (Bitmap bmp = new Bitmap(Width, 200)) { 
    //edit bmp to resemble stave 
    e.Graphics.DrawImage(bmp,new Point(0,200*i); 
} 

,或者創建並存儲的時間提前,以避免油漆事件過程中的成本。

0

正如其他人提到的,您需要將AutoScroll設置爲true。但是,如果您在添加或刪除位圖時(或者在開始時,如果它們是固定的),則可以使用公式bitmapCount * bitmapHeight來設置AutoScrollMinSize高度。同樣在你的油漆處理器中,你需要考慮AutoScrollPosition.Y屬性。

這裏是行動的概念的一個小例子:

using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace Tests 
{ 
    static class Program 
    { 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      var form = new Form(); 
      var panel = new Panel { Dock = DockStyle.Fill, Parent = form }; 
      // Setting the AutoScrollMinSize 
      int bitmapCount = 10; 
      int bitmapHeight = 200; 
      panel.AutoScrollMinSize = new Size(0, bitmapCount * bitmapHeight); 
      panel.Paint += (sender, e) => 
      { 
       // Considering the AutoScrollPosition.Y 
       int offsetY = panel.AutoScrollPosition.Y; 
       var state = offsetY != 0 ? e.Graphics.Save() : null; 
       if (offsetY != 0) e.Graphics.TranslateTransform(0, offsetY); 
       var rect = new Rectangle(0, 0, panel.ClientSize.Width, bitmapHeight); 
       var sf = new StringFormat(StringFormat.GenericTypographic) { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; 
       for (int i = 0; i < bitmapCount; i++) 
       { 
        // Your bitmap drawing goes here 
        e.Graphics.FillRectangle(Brushes.Yellow, rect); 
        e.Graphics.DrawRectangle(Pens.Red, rect); 
        e.Graphics.DrawString("Bitmap #" + (i + 1), panel.Font, Brushes.Blue, rect, sf); 
        rect.Y += bitmapHeight; 
       } 
       if (state != null) e.Graphics.Restore(state); 
      }; 
      Application.Run(form); 
     } 
    } 
} 

編輯:作爲LarsTech在評論中提到正確的,你並不真的需要設置AutoScroll屬性在這種情況下。所有其他的保持不變。

+0

AutoScroll實際上只有在面板內有控件時纔會使用。當您設置AutoScrollMinSize屬性時,您不需要AutoScroll爲true。 – LarsTech

+0

@LarsTech:謝謝,你當然是對的!我會糾正答案。現在,當我準備我的答案時,我發現您發佈的內容完全一樣,對於重複內容抱歉。 –

相關問題