用于绘制图形和滚动的面板 [英] Panel for drawing graphics and scrolling
问题描述
我希望能够使用Panel
或类似方式将图形绘制到Winform上.如果图形变得比控件大,我似乎看不到任何有关添加滚动条的信息?
I want to be able to use a Panel
or similar to draw graphics onto a Winform. I cannot seem to see anything regarding adding scrollbars if the graphics become larger than the control?
是否可以通过Panel
来执行此操作,或者是否有类似的控件允许这样做?
Is it possible to do this with a Panel
or is there a similar control that will allow it?
推荐答案
将AutoScroll属性设置为true,将AutoScrollMinSize属性设置为图像的大小.现在,当图像太大时,滚动条将自动出现.
Set the AutoScroll property to true and the AutoScrollMinSize property to the size of the image. The scrollbars will now automatically appear when the image is too large.
您将要从Panel继承您自己的类,以便可以在构造函数中将DoubleBuffered属性设置为true.否则,闪烁会很明显.一些示例代码:
You'll want to inherit your own class from Panel so that you can set the DoubleBuffered property to true in the constructor. Flicker would be noticeable otherwise. Some sample code:
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);
}
}
这篇关于用于绘制图形和滚动的面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!