正如其他人提到的,您需要將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
屬性在這種情況下。所有其他的保持不變。
你嘗試過'AutoScroll'屬性(或添加一個'VScrollBar' /'HScrollBar')嗎? – varocarbas
是的,這似乎不起作用 – Rariolu
你的意思是「似乎沒有工作」?也許你沒有正確使用它們。你能解釋一下你做了什麼嗎? – varocarbas