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
655 views
in Technique[技术] by (71.8m points)

winforms - C# Drawing on Panels

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 the range they can see is shifted earlier or later. When an appointment runs off the end of the visible range, I want there to be a zig-zag indicating that the appointment extends beyond the visible bounds.

I've identified the case where this occurs, and I call a private function drawZigZag(Panel p, int direction); to draw it. The day is spread horizontally, and the direction -1 indicates a zigzag on the left and 1 indicates a zigzag on the right.

So far, I'm not up to the zigzag yet, I'm just experimenting with CreateGraphics() and FillPolygon(). So far I have:

    private void drawZigZag(Panel p, int direction) // 1 = right, -1 = left
    {
        Graphics g = p.CreateGraphics();

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

The first FillRectangle() I didn't originally have. I only added that when the FillPolygon() didn't work.

Basically, it's not working and I'm not sure why. The panel is the original colour - it hasn't been filled DarkGreen. I've used CreateGraphics() before for other things, and I'm not really sure why it's not working in this instance. Any ideas?

Edit: Sorry, I thought I should mention: There are several Label controls on my Panel which describe the appointment. These shouldn't be covered if possible.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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

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

...