问题
I am trying to make an application that is drawing Dendrograms like this one.
So I added a PictureBox
to the winform, and for start, I wanted to wrote all labels like in the picture with this code:
foreach (var line1 in lines)
{
i++;
gpx.DrawString(line1, myFont, Brushes.Green, new PointF(2, 10 * i));
}
But the problem is that I have a lot of labels so it writes only a few of them on 800x600 px. I wanted to add scrolling bars, but it doesn't work at all. It works only when I am setting an Image to PictureBox
.
Is there any other way, with or without PictureBox
?
回答1:
PictureBox is a very simple control, it is only good to display a picture. The one capability it doesn't have that you need is the ability to scroll the content. So don't use it.
Creating your own control is very simple in Winforms. A basic starting point is to begin with Panel, a control that supports scrolling, and derive your own class for it so you customize it to be suitable for the task. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto a form. Note how you can set the Lines
property, either with the designer or your code. Use the Paint event to draw the dendrogram. Or extend the OnPaint() method in the class, you can make it as fancy as you want.
using System;
using System.Drawing;
using System.Windows.Forms;
class DendrogramViewer : Panel {
public DendrogramViewer() {
this.DoubleBuffered = this.ResizeRedraw = true;
this.BackColor = Color.FromKnownColor(KnownColor.Window);
}
public override System.Drawing.Font Font {
get { return base.Font; }
set { base.Font = value; setSize(); }
}
private int lines;
public int Lines {
get { return lines; }
set { lines = value; setSize(); }
}
private void setSize() {
var minheight = this.Font.Height * lines;
this.AutoScrollMinSize = new Size(0, minheight);
}
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
base.OnPaint(e);
}
}
来源:https://stackoverflow.com/questions/27817333/scrolling-picturebox