[英]How to add scrolling to Panel
基本上,我創建了面板類的擴展,它添加了在自身上繪制多個位圖以創建多個音樂五線譜。 我試過在面板上添加一個垂直滾動條,但沒有奏效。 我的油漆程序與此類似
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);
}
}
只需設置 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);
}
或者提前創建和存儲它們以避免在油漆事件期間產生該成本。
將AutoScroll
屬性設置為 true。
您還可以考慮替代方案:
FlowLayoutPanel
並動態添加 PictureBoxes 而不是繪畫。TableLayoutPanel
並動態添加 PictureBoxes 而不是繪畫。ListBox
並將DrawMode
屬性設置為OwnerDrawFixed
或OwnerDrawVariable
,然后覆蓋方法OnPaint
和OnMeasureItem
(僅適用於OwnerDrawVariable
)。如果您想繼續使用現有的調用 GDI 代碼的模式來繪制您的控件,您應該添加一個滾動條控件並向其更改事件添加一個事件處理程序。 除了在面板上調用.Invalidate
之外,更改處理程序不需要做任何事情。 .Invalidate 是一個信號給控件,表明它是“臟的”,需要重新繪制。 您將需要修改您的繪畫代碼以在與滾動條值相反的方向上偏移繪圖。
因此,如果您的滾動條在位置 50,您應該在 Y - 50 處繪制所有內容。
如果您使用的是純 GDI 繪圖代碼,則根本不需要弄亂 AutoScroll 屬性。 僅當您的面板承載比面板大的實際控件時才使用。
正如其他人提到的,您需要將 任何時候添加或刪除位圖(或者在開始時,如果它們是固定的),您需要使用公式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
屬性。 所有其他保持不變。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.