使用GDI +绘制内容时如何使用自动滚动条 [英] How to make use of AutoScrollbar when drawing contents with GDI+

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

问题描述

我用 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 +绘制内容时如何使用自动滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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