本文介绍了使用 GDI+ 绘制内容时如何使用 AutoScrollbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 e.graphics.DrawLine() 等在 OnPaint 事件内的表单上绘制我的内容....到目前为止,我是根据表单 size(调整我的元素大小)进行绘制的,但现在我想绘制尽可能大的尺寸,并且如果我在可见区域(将绘制对象的位置)之外绘制是在运行时动态决定的),我希望用户使用滚动条来查看我绘制的整个内容的一部分.

I draw my contents on a form inside OnPaint event with e.graphics.DrawLine(), etc... . So far I was drawing according to form size (resizing my elements) but now I'd like draw as big as I want, and if I draw outside the visible area (the place where object will be drawn is decided at runtime dynamically), I want user to use scroll bars in order to see parts of whole content which I draw.

我已启用 AutoScrolling 但我不知道当我在该表单上没有任何控件时它会如何帮助我.

I have enabled AutoScrolling but I don't know how it may help me when I don't have any controls on that form.

我该怎么做?

推荐答案

只需将 AutoScrollMinSize 属性设置为您想要的大小.当表单的 ClientSize 小于此值时,滚动条会自动出现.您还需要根据滚动位置偏移您绘制的内容,如下所示:

Simply set the AutoScrollMinSize property to the size you want. The scrollbar(s) automatically appear when the form's ClientSize is smaller than this value. You'll also need to offset what you draw according to the scroll position, like this:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        this.AutoScroll = true;
        this.AutoScrollMinSize = new Size(3000, 1000);
        this.ResizeRedraw = true;
    }
    protected override void OnPaint(PaintEventArgs e) {
        e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
        e.Graphics.DrawLine(Pens.Black, 0, 0, 3000, 1000);
        base.OnPaint(e);
    }
}

这篇关于使用 GDI+ 绘制内容时如何使用 AutoScrollbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:32