滚动时面板背景不刷新 (C#/VB.net)

维维因

我在 C# 和 VB.net 中有这个奇怪的滚动/背景错误。当我创建一个面板并使用 Autoscroll 时,滚动过程中背景不会更新。最后,它看起来真的很奇怪(视频:https : //youtu.be/0vaO-zmWFmk)我用 TabControl 尝试了同样的方法,并且背景像它应该的那样滚动。我尝试了外部滚动条,也发生了同样的情况。我也试过 VB.net。我认为这是来自 Visual Studios 的错误,如果有人能帮助我,我将不胜感激,谢谢,LG!

安迪

如果您希望图像随滚动条一起滚动,您可以通过简单地扩展Panel和覆盖OnPaintBackground.

请记住,如果您这样做,您必须进行控制DoubleBuffered(这在下面的代码中为您完成)。

在这个例子中,我添加了一个“平铺”选项。所以你可以使用一个大图像,或者使用一个无缝平铺并平铺它(通过BackgroundImageLayout属性设置。所有其他选项将绘制相同)。

C#

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

public class PanelEx : Panel
{
    public PanelEx()
    {
        DoubleBuffered = true;
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        if (BackgroundImage != null)
        {
            if (ImageLayout.Tile == BackgroundImageLayout)
            {
                for (int x = 0; x <= DisplayRectangle.Width;
                    x += BackgroundImage.Width)
                {
                    for (int y = 0; y <= DisplayRectangle.Height;
                        y += BackgroundImage.Height)
                    {
                        e.Graphics.DrawImage(BackgroundImage,
                            new Point(x - HorizontalScroll.Value,
                                y - VerticalScroll.Value));
                    }
                }
            }
            else
            {
                e.Graphics.DrawImage(BackgroundImage,
                    new Point(-HorizontalScroll.Value, -VerticalScroll.Value));
            }
        }
        else
        {
            base.OnPaintBackground(e);
        }
    }
}

如何使用这个

  1. 右键单击您的项目并在菜单中选择Addthen Class为班级命名PanelEx.cs
  2. 将上面的代码复制并粘贴到该类文件中。
  3. 在包含要修改的面板的表单中,进入设计器文件(看下图)
  4. 将所有实例更改System.Windows.Forms.Panel();PanelEx();
  5. 保存,进行完全重建并运行。

面板安装


网络

Public Class PanelEx
    Inherits Panel

    Public Sub New()
        DoubleBuffered = True
    End Sub

    Protected Overrides Sub OnPaintBackground(e As PaintEventArgs)
        If Not BackgroundImage Is Nothing Then
            If BackgroundImageLayout = ImageLayout.Tile Then

                Dim x As Integer, y As Integer
                While x <= DisplayRectangle.Width
                    y = 0
                    While y <= DisplayRectangle.Height
                        e.Graphics.DrawImage(
                            BackgroundImage,
                            New Point(x - HorizontalScroll.Value,
                                      y - VerticalScroll.Value))
                        y += BackgroundImage.Height
                    End While
                    x += BackgroundImage.Width
                End While
            Else
                e.Graphics.DrawImage(BackgroundImage,
                                     New Point(-HorizontalScroll.Value,
                                               -VerticalScroll.Value))
            End If
        Else
            MyBase.OnPaintBackground(e)
        End If
    End Sub
End Class

如何使用这个

  1. 右键单击您的项目并在菜单中选择Addthen Class为班级命名PanelEx.vb
  2. 将上面的代码复制并粘贴到该类文件中。
  3. 在包含要修改的面板的表单中,进入设计器文件(看下图)
  4. 将所有实例更改PanelPanelEx
  5. 保存,进行完全重建并运行。

vb.net 设计师

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章