C# Drawing on Panels

前端 未结 2 914
南旧
南旧 2020-12-02 02:13

I\'m drawing up a day schedule and representing timeslots with panels, and appointments are yet more panels on top.

The user is able to scroll up and down so that th

2条回答
  •  清歌不尽
    2020-12-02 02:27

    You need to call this method from the paint event handler, not just whenever you like. So in your constructor you might have:

    panel1.Paint += new PaintEventHandler(panel1_Paint);
    

    and then the implementation:

        private void panel1_Paint( object sender, PaintEventArgs e )
        {
            var p = sender as Panel;
            var g = e.Graphics;
    
            g.FillRectangle( new SolidBrush( Color.FromArgb( 0, Color.Black ) ), p.DisplayRectangle );
    
            Point[] points = new Point[4];
    
            points[0] = new Point( 0, 0 );
            points[1] = new Point( 0, p.Height );
            points[2] = new Point( p.Width, p.Height);
            points[3] = new Point( p.Width, 0 );
    
            Brush brush = new SolidBrush( Color.DarkGreen );
    
            g.FillPolygon( brush, points );
        }
    

提交回复
热议问题