绘制背景图像到表单溢出客户区

时间:2014-11-19 19:44:33

标签: c# winforms gdi+

我在表单背景上绘制图片时遇到问题。我有一个表格,其中插入了两个滚动条(H和V)。因为我需要能够以原始尺寸显示图像,所以我使用它们来滚动它,但是当我滚动到两侧最大的右边或底部时,缺少7个隐藏在滚动条下的像素。有示例代码:

private int PosX, PosY;
this.Map = new Bitmap(TestLines.Properties.Resources.mapa);

protected override void OnPaintBackground(PaintEventArgs e)
{
    base.OnPaintBackground(e);
    if (this.Map != null)
    {
        e.Graphics.DrawImageUnscaled(Map, new Point(this.PosX, this.PosY));

        int MapResX = (int)((float)this.Map.Width / this.Map.HorizontalResolution * e.Graphics.DpiX);
        int MapResY = (int)((float)this.Map.Height / this.Map.VerticalResolution * e.Graphics.DpiY);

        if (MapResX > this.ClientSize.Width && MapResY > this.ClientSize.Height - this.toolStrip1.Height)
        {
            hScrollBar1.Minimum = 0;
            hScrollBar1.Maximum = MapResX - this.ClientSize.Width + vScrollBar1.Width;
            hScrollBar1.Visible = true;
            vScrollBar1.Minimum = 0;
            vScrollBar1.Maximum = MapResY - this.ClientSize.Height + toolStrip1.Height + hScrollBar1.Height;
            vScrollBar1.Visible = true;
        }
        else if (MapResX > this.ClientSize.Width)
        {
            hScrollBar1.Minimum = 0;
            hScrollBar1.Maximum = MapResX - this.ClientSize.Width;
            hScrollBar1.Visible = true;
            vScrollBar1.Visible = false;
        }
        else if (MapResY > this.ClientSize.Height - this.toolStrip1.Height)
        {
            vScrollBar1.Minimum = 0;
            vScrollBar1.Maximum = MapResY - this.ClientSize.Height + toolStrip1.Height;
            vScrollBar1.Visible = true;
            hScrollBar1.Visible = false;
        }
        else
        {
            hScrollBar1.Visible = false;
            vScrollBar1.Visible = false;
        }
    }            
}

请注意,还有一个我不画画的工具条。然后是简单的滚动条动作:

private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
    this.PosX = -e.NewValue;
    this.Invalidate(false);
    this.Update();
}

private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
    this.PosY = toolStrip1.Height -e.NewValue;
    this.Invalidate(false);
    this.Update();
}

你能描述一下为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

这不是正确的方法。使用Panel作为基类,创建自己的控件,以便免费滚动。在项目中添加一个新类并粘贴下面显示的代码。编译。将它从工具箱顶部拖放到表单上,您可能希望将其Dock属性设置为Fill。使用设计器或代码分配Map属性。

using System;
using System.Drawing;
using System.Windows.Forms;

class MapPanel : Panel {
    public MapPanel() {
        this.DoubleBuffered = true;
        this.ResizeRedraw = true;
    }

    private Image map;
    public Image Map {
        get { return map; }
        set {
            map = value;
            this.AutoScrollMinSize = value == null ? Size.Empty : value.Size;
            this.Invalidate();
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e) {
        base.OnPaintBackground(e);
        if (map != null) {
            e.Graphics.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);
            e.Graphics.DrawImage(map, 0, 0);
        }
    }
}