我希望能够使用Panel
或类似的工具在Winform上绘制图形。如果图形变得比控件大,我似乎看不到任何关于添加滚动条的内容?
有没有可能使用Panel
来实现这一点,或者是否有类似的控件允许这样做?
发布于 2010-11-30 01:29:47
将AutoScroll属性设置为true,并将AutoScrollMinSize属性设置为图像的大小。现在,当图像太大时,滚动条将自动出现。
您将希望从面板继承您自己的类,以便可以在构造函数中将DoubleBuffered属性设置为true。否则,闪烁将是显而易见的。一些示例代码:
using System;
using System.Drawing;
using System.Windows.Forms;
class ImageBox : Panel {
public ImageBox() {
this.AutoScroll = true;
this.DoubleBuffered = true;
}
private Image mImage;
public Image Image {
get { return mImage; }
set {
mImage = value;
if (value == null) this.AutoScrollMinSize = new Size(0, 0);
else {
var size = value.Size;
using (var gr = this.CreateGraphics()) {
size.Width = (int)(size.Width * gr.DpiX / value.HorizontalResolution);
size.Height = (int)(size.Height * gr.DpiY / value.VerticalResolution);
}
this.AutoScrollMinSize = size;
}
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
if (mImage != null) e.Graphics.DrawImage(mImage, 0, 0);
base.OnPaint(e);
}
}
发布于 2010-11-30 00:37:12
我不能100%确定你想要实现什么,但这里有一个类似的SO question,它可能会对你有所帮助。
您也可以尝试使用PictureBox,您可以在图形变大时手动更改其大小。然后将表单AutoScroll设置为true。
https://stackoverflow.com/questions/4305011
复制相似问题