Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 620
Ozzah Avatar asked Mar 31 '11 23:03

Ozzah


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


2 Answers

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 );
    }
like image 144
Daniel Kinsman Avatar answered Oct 17 '22 13:10

Daniel Kinsman


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

like image 25
Alin Leon Avatar answered Oct 17 '22 11:10

Alin Leon