Panel Background not refreshing during scrolling (C#/VB.net)

自闭症网瘾萝莉.ら 提交于 2021-02-11 12:41:51

问题


I have this weird scrolling/background bug in C# and VB.net. When I create a panel and I use Autoscroll the background isnt updating during scrolling. In the end it looks really weird (video: https://youtu.be/0vaO-zmWFmk) I tried the same with a TabControl and the background scrolled like it should. I tried external scrollbars and the same happened. And I tried VB.net too. I think this is a bug from Visual Studios and I would appreciate if someone could help me Thanks, LG!


回答1:


If you want the image to scroll with the scrollbar, you can do this easily by simply extending the Panel and overriding OnPaintBackground.

Keep in mind, if you do this, you must make the control DoubleBuffered (this is done for you in the code below).

In this example, I added in a "Tiled" option. So you could use one big image, or use a seamless tile and tile it (set via BackgroundImageLayout property. All other options will paint the same).

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

How to use this

  1. Right-click on your project and in the menu select Add then Class. Name the class PanelEx.cs
  2. Copy and paste the code above in to that class file.
  3. In the form that has the panel you want to modify, go in to the designer file (Look at image below)
  4. Change all instances of System.Windows.Forms.Panel(); to PanelEx();
  5. Save, do a full rebuild and run.


VB.NET

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

How to use this

  1. Right-click on your project and in the menu select Add then Class. Name the class PanelEx.vb
  2. Copy and paste the code above in to that class file.
  3. In the form that has the panel you want to modify, go in to the designer file (Look at image below)
  4. Change all instances of Panel to PanelEx
  5. Save, do a full rebuild and run.



来源:https://stackoverflow.com/questions/63439473/panel-background-not-refreshing-during-scrolling-c-vb-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!