C# Drawing on Panels

前端 未结 2 888
南旧
南旧 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 );
        }
    
    0 讨论(0)
  • 2020-12-02 02:36

    For example we have this drawing event which is drawing a text from textBox1:

    private void panel1_draw(object sender, PaintEventArgs e)
        {
            var g = e.Graphics;
            Pen myp = new Pen(System.Drawing.Color.Red, 4);
            Font fy = new Font("Helvetica", 10, FontStyle.Bold);
            Brush br = new SolidBrush(System.Drawing.Color.Red);
            g.DrawString(textBox1.Text, fy, br, 0,0);
        }
    

    In order to draw on your panel1, you need to write this code in your button event handler:

    private void button1_Click(object sender, EventArgs e)
        {
            panel1.Paint+=new PaintEventHandler(panel1_draw);
            panel1.Refresh();
        }
    

    The first line draws the text in your panel and if you want the text to appear you must refresh the panel. The main thing is in using the panel1.Pain += new PaintEventHandler(your void name); and panel1.Refresh();

    0 讨论(0)
提交回复
热议问题