滚动图片框 [英] Scrolling PictureBox

查看:85
本文介绍了滚动图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个绘制树状图的应用程序,例如

I am trying to make an application that is drawing Dendrograms like this one.

因此我在Winform中添加了PictureBox,首先,我想使用以下代码编写所有标签,如图片中所示:

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));
}

但是问题是我有很多标签,所以只在800x600 px上写了一些标签.我想添加滚动条,但这根本不起作用.仅当我将图像设置为PictureBox时,它才起作用.

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.

有没有其他方法,有没有PictureBox?

Is there any other way, with or without PictureBox?

推荐答案

PictureBox是一个非常简单的控件,仅显示图片是很好的.您不需要的一项功能是滚动内容的功能.所以不要使用它.

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.

在Winforms中创建自己的控件非常简单.一个基本的起点是从Panel(一个支持滚动的控件)开始,并为其派生自己的类,以便您自定义它以适合任务.在您的项目中添加一个新类,并粘贴以下代码.编译.将新控件从工具箱的顶部拖放到窗体上.请注意如何使用设计器或代码设置Lines属性.使用Paint事件绘制树状图.或在类中扩展OnPaint()方法,即可根据需要将其设置为精美.

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);
    }
}

这篇关于滚动图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆