Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

c# - Create vertical scrollbar with multiple pictures

I am creating a project that needs to have a vertical scroll bar with multiple pictures like the server explorer in discord: For example: enter image description here how can I mimic in WinForms in C# (not only having pictures scrolling but also the pictures can have events attached to them?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First you need add a Parent Panel, i'm used the pnServers.

enter image description here

Set property value: AutoScroll = True;

On Code Behind you can create a List of Rounded Pictures.

private void DiscordServerBarExample_Load(object sender, System.EventArgs e)
{
    // Example, in your case this looping is based on return (Database, Api, ...).
    for (int i = 1; i <= 10; i++)
    {
        Panel pnServer = new Panel()
        {
            Dock = DockStyle.Top,
            Height = pnServers.Width,
            Padding = new Padding(10)
        };

        RoundedPictureBox serverImage = new RoundedPictureBox()
        {
            SizeMode = PictureBoxSizeMode.CenterImage,
            Dock = DockStyle.Fill
        };

        serverImage.Image = Properties.Resources._255352;
        pnServer.Controls.Add(serverImage);

        pnServers.Controls.Add(pnServer);
    }
}

Rounded Picture Box Code:

public class RoundedPictureBox : PictureBox
{
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        using (GraphicsPath gp = new GraphicsPath())
        {
            gp.AddEllipse(0, 0, Width - 1, Height - 1);
            Region rg = new Region(gp);
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.DrawEllipse(new Pen(new SolidBrush(this.BackColor), 1), 0, 0, this.Width - 1, this.Height - 1);

            Region = rg;
        }
    }
}

And this is final result.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...