开发者

Winform not scrolling when drawing objects on it

开发者 https://www.devze.com 2022-12-26 13:25 出处:网络
C#3.0,.net framework 3.5 I am drawing ( using the draw method in the graphics class) a lot of solid rectangles on a windows form vertically. The form starts at 500 x 500 px and the rectangles are only

C#3.0,.net framework 3.5 I am drawing ( using the draw method in the graphics class) a lot of solid rectangles on a windows form vertically. The form starts at 500 x 500 px and the rectangles are only drawn at runtime after data is downloaded from the net -and the number of rectangles depends on the download so I do not know it upfront.

So only a few rectangles are drawn as the size of the form is fixed. So I googled/Binged ( lest someone suggest I do that) and found a few tips but they don't work in this case -like setting the forms AutoScroll property to true or trying double buffering.I also tried to draw on a listbox control and set it's scroll property etc...but no dice.

I'm guessing there is no way to display , say 200 rectangles vertically on a windows form using draw. I 开发者_如何学JAVAneed some other solution... any ideas please.

Maybe a list of pictureboxes and then populate each picturebox with the solid color ?

Thanks


You are drawing GDI+ rectangles on a form during the paint event? The form would have no idea that you are creating objects outside of the clipping space and would therefore have no idea that you need to scroll.

You would need to add a scrollbar to the form and then calculate the value\position of the scrollbar and use that to determine what portion of your rectangles to draw upon the paint event. This would involve a bit of manual effort. You could draw them all to an in-memory bitmap of the appropriate size and then just copy the portions of that to the form upon draw.

Or:

If you wanted the form to do this for you, create a custom rectangle control and place 200 of those on the form. Since they are components and have a concrete height & width, the form would then know it needed to scroll, and would do so accordingly provided that autoscroll was set.

it can be as simple as this:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            this.AutoScroll = true;
            for (int i = 0; i < 100; i++)
                this.Controls.Add(new Rectangle() { Top = i * 120, Left = 10 });

        }
    }

    public class Rectangle : Control
    {
        public Rectangle()
        {
            this.Width = 100;
            this.Height = 100;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(new Pen(Color.Black, 5), 0, 0, 100, 100);
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消