Dar*_*ung 21 c# panel winforms
我希望能够使用面板或类似工具将图形绘制到Winform上.如果图形变得比控件大,我似乎无法看到有关添加滚动条的任何内容?
是否可以使用面板执行此操作,或者是否存在允许它的类似控件?
谢谢.
Han*_*ant 20
将AutoScroll属性设置为true,将AutoScrollMinSize属性设置为图像的大小.现在,当图像太大时,滚动条会自动出现.
您将希望从Panel继承自己的类,以便可以在构造函数中将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);
}
}
Run Code Online (Sandbox Code Playgroud)