C# 允许自定义控件在视图外正确滚动
我有一个自定义控件,它由一个充满文本的圆角矩形组成。(实际控件更复杂,但此处显示的代码具有相同的症状。)我将控件的实例附加到面板,并使该面板成为另一个面板的子面板,AutoScroll=true。我认为这对于正确的滚动行为来说已经足够了,但是如果滚动使控件的左侧离开面板的左侧,它会粘住,控件会收缩。与滚动相同,控件应该在顶部关闭。(底部和右侧似乎没有问题。)下面是示例代码(需要引用System.Windows.Forms和System.Drawing。)我正在Windows上使用Visual Studio 2010和.NET 4客户端C# 允许自定义控件在视图外正确滚动,c#,winforms,C#,Winforms,我有一个自定义控件,它由一个充满文本的圆角矩形组成。(实际控件更复杂,但此处显示的代码具有相同的症状。)我将控件的实例附加到面板,并使该面板成为另一个面板的子面板,AutoScroll=true。我认为这对于正确的滚动行为来说已经足够了,但是如果滚动使控件的左侧离开面板的左侧,它会粘住,控件会收缩。与滚动相同,控件应该在顶部关闭。(底部和右侧似乎没有问题。)下面是示例代码(需要引用System.Windows.Forms和System.Drawing。)我正在Windows上使用Visual S
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace CustomControlScrollTest
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
// Form with a Dock = Fill panel with autoscroll turned on, and a nested panel
// with few custom roundRectControls.
public class Form1 : Form
{
Panel autoScrollPanel;
Panel rectPanel;
public Form1()
{
Size = new Size(300, 200);
autoScrollPanel = new Panel();
autoScrollPanel.Dock = DockStyle.Fill;
autoScrollPanel.AutoScroll = true;
autoScrollPanel.AutoScrollMinSize = new Size(600, 450);
autoScrollPanel.Resize += autoScrollPanel_Resize;
autoScrollPanel.Scroll += autoScrollPanel_Scroll;
Controls.Add(autoScrollPanel);
rectPanel = new Panel();
rectPanel.Size = autoScrollPanel.AutoScrollMinSize;
rectPanel.Controls.AddRange(new RoundRectControl[] {
new RoundRectControl(),
new RoundRectControl(),
new RoundRectControl(),
new RoundRectControl(),
new RoundRectControl()
});
foreach (Control c in rectPanel.Controls)
{
c.Click += c_Click;
}
autoScrollPanel.Controls.Add(rectPanel);
placeBoxes();
}
// we want to be able to recalculate the boxes position at any time
// in the real program this occurs due to model changes
void c_Click(object sender, EventArgs e)
{
placeBoxes();
}
void autoScrollPanel_Scroll(object sender, ScrollEventArgs e)
{
Refresh();
}
void autoScrollPanel_Resize(object sender, EventArgs e)
{
Refresh();
}
private void placeBoxes()
{
for (int i = 0; i < rectPanel.Controls.Count; ++i)
{
int j = i + 1;
var node = rectPanel.Controls[i] as RoundRectControl;
if (node != null)
{
node.Title = "Hello (" + j + ")";
node.Location = new Point(i * 100, j * 75);
node.Visible = true;
}
}
}
}
// A rounded rectangle filled blue with a black border and white text
// the size is determined by the text
public class RoundRectControl : Control
{
public RoundRectControl()
{
var f = SystemFonts.MessageBoxFont;
titleFont = new Font(f.Name, f.SizeInPoints + 2, FontStyle.Bold, GraphicsUnit.Point);
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
var left = e.ClipRectangle.X;
var right = left + titleWidth + 2 * radius;
var top = e.ClipRectangle.Y;
var bottom = top + nodeHeight + 2 * radius;
var r2 = 2 * radius;
using (var path = new GraphicsPath())
{
path.AddArc(left, bottom - r2, r2, r2, 90, 90);
path.AddArc(left, top, r2, r2, 180, 90);
path.AddArc(right - r2, top, r2, r2, 270, 90);
path.AddArc(right - r2, bottom - r2, r2, r2, 0, 90);
path.CloseFigure();
g.FillPath(titleBrush, path);
g.DrawPath(borderPen, path);
}
g.DrawString(title, titleFont, titleTextBrush, left + radius, top + radius);
}
private string title;
public string Title
{
get { return title; }
set
{
title = value;
Size = getSize();
Invalidate();
}
}
private Brush titleBrush = Brushes.Blue;
private Brush titleTextBrush = Brushes.White;
private Pen borderPen = Pens.Black;
private Size getSize()
{
var g = CreateGraphics();
var titleSize = g.MeasureString(title, titleFont);
titleWidth = (int)titleSize.Width;
nodeHeight = (int)titleSize.Height;
return new Size(titleWidth + 2 * radius + 1, nodeHeight + 2 * radius + 1);
}
public override Size GetPreferredSize(Size proposedSize)
{
return getSize();
}
private int titleWidth;
private int nodeHeight;
private Font titleFont;
private int radius = 5;
}
}
使用系统;
使用系统图;
使用System.Drawing.Drawing2D;
使用System.Windows.Forms;
命名空间CustomControlScrollTest
{
静态类程序
{
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
//打开自动滚动的Dock=Fill面板和嵌套面板的窗体
//只有很少的自定义控件。
公开课表格1:表格
{
面板自动旋转面板;
面板矩形面板;
公共表格1()
{
尺寸=新尺寸(300200);
autoScrollPanel=新面板();
autoScrollPanel.Dock=DockStyle.Fill;
autoScrollPanel.AutoScroll=true;
autoScrollPanel.AutoScrollMinSize=新尺寸(600450);
autoScrollPanel.Resize+=autoScrollPanel_Resize;
autoScrollPanel.Scroll+=autoScrollPanel\u Scroll;
控件。添加(autoScrollPanel);
rectPanel=新面板();
rectPanel.Size=autoScrollPanel.AutoScrollMinSize;
rectPanel.Controls.AddRange(新的RoundRectControl[]{
新的RoundRectControl(),
新的RoundRectControl(),
新的RoundRectControl(),
新的RoundRectControl(),
新的RoundRectControl()
});
foreach(rectPanel.Controls中的控件c)
{
c、 点击+=点击;
}
autoScrollPanel.Controls.Add(rectPanel);
Placebox();
}
//我们希望能够随时重新计算箱子的位置
//在实际程序中,这是由于模型更改而发生的
无效c_单击(对象发送者,事件参数e)
{
Placebox();
}
void autoScrollPanel_滚动(对象发送器,ScrollEventArgs e)
{
刷新();
}
void autoScrollPanel_Resize(对象发送器,事件参数e)
{
刷新();
}
专用空置箱()
{
对于(int i=0;i
试试这个
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
//total width and height of rounded rectangle
var width = titleWidth + 2 * radius + 1;
var height = nodeHeight + 2 * radius + 1;
var left = e.ClipRectangle.X;
var top = e.ClipRectangle.Y;
//check if clipping occurs. If yes, set to 0
if (width > e.ClipRectangle.Width)
{
left = 0; // *= -1;
}
//check if clipping occurs.If yes, set to 0
if (height > e.ClipRectangle.Height)
{
top = 0; // *= -1
}
var right = left + titleWidth + 2 * radius;
var bottom = top + nodeHeight + 2 * radius;
var r2 = 2 * radius;
using (var path = new GraphicsPath())
{
path.AddArc(left, bottom - r2, r2, r2, 90, 90);
path.AddArc(left, top, r2, r2, 180, 90);
path.AddArc(right - r2, top, r2, r2, 270, 90);
path.AddArc(right - r2, bottom - r2, r2, r2, 0, 90);
path.CloseFigure();
g.FillPath(titleBrush, path);
g.DrawPath(borderPen, path);
}
g.DrawString(title, titleFont, titleTextBrush, left + radius, top + radius);
}
问题是当您向右滚动(矩形向左移动,e.ClipRectangle.Width变小)并且矩形超出区域时,e.ClipRectangle.X为正值!在这种情况下,我们把它设为零。e、 ClipRectangle.xc
var left = 0;
var right = left + titleWidth + 2 * radius;
var top = 0;
var bottom = top + nodeHeight + 2 * radius;
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);