正如其他人提到的,您需要将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