【发布时间】:2016-01-06 21:34:47
【问题描述】:
我们如何让 Panel 控件滚动其中的任何内容?我不是在谈论控件、用户控件或自定义控件。我说的只是像素;用 GDI+ 绘制:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GDITEST
{
public partial class Form1 : Form
{
public class Item
{
public int Height { get; set; }
public int Width { get; set; }
public int Top { get; set; }
}
internal List<Item> items = new List<Item>();
public Form1()
{
InitializeComponent();
}
private void panel_Paint(object sender, PaintEventArgs e)
{
if (items != null)
{
if (items.Count >= 1)
{
foreach (Item item in items)
{
using (Pen pen = new Pen(Color.Blue, 1))
{
int count;
count = items.Count;
count = count >= 1 ? count : 1;
e.Graphics.DrawRectangle(pen, 0, item.Top, (item.Width - SystemInformation.VerticalScrollBarWidth), item.Height);
}
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
items.Add(new Item() { Width = this.Width, Height = 25, Top = (items.Count * 25) });
panel.Refresh();
}
}
}
上面的代码绘制了一个蓝色矩形(有点像垂直列表)。当矩形的数量扩展面板的高度时,我希望面板滚动。
我无法找到如何执行此操作,因为大多数结果只返回与滚动自定义控件相关的内容。
我确实在某个地方(我再也找不到)读到您可以使用一些 translateX 或 translateY 方法......但是我很难找到更多关于这些方法的信息。
【问题讨论】: