简体   繁体   中英

Drawing a rectangle over panel

I need to draw a rectangle into a panel. I dont know the color in advance, I get the color during runtime, I dont know how to set the color not to a fixed value, and second - when i try to draw the rectangle, it does nothing at all. Here is my code that should draw the rectangle (infact it does in another project, but thats just in a plain form, not into a panel)

    Graphics g;  
    g = CreateGraphics();  
    Pen p;  
    Rectangle r;  
    p = new Pen(Brushes.Blue); 
    r = new Rectangle(1, 1, 578, 38);  
    g.DrawRectangle(p, r);`  

So I need to replace (Brushes.Blue) with a variable and I need to draw the rectangle in a panel on its coordinates set in this code..

Construct your Pen using the Pen(Color) constructor instead of the Pen(Brush) one. Then you can define your color once you know it.

You should perform drawing in the Paint event of the panel. This event occurs whenever windows decides it's time to repaint the panel, and the PaintEventArgs contain a Graphics object you can draw the rectangle on.

The Brush is an abstract class, but you can use the SolidBrush object to create a custom colored brush at runtime:

int red = 255;
int green = 0;
int blue = 0;
Brush myBrush = new SolidBrush(Color.FromArgb(red, green, blue));

Here you are:

private Color _color;                 // save the color somewhere
private bool iKnowDaColor = false;    // this will be set to true when we know the color
public Form1() {
    InitializeComponents();

    // on invalidate we want to be able to draw the rectangle
    panel1.Paint += new PaintEventHandler(panel_Paint);
}

void panel_Paint(object sender, PaintEventArgs e) {
    // if we know the color paint the rectangle
    if(iKnowDaColor) {
        e.Graphics.DrawRectangle(new Pen(_color),
            1, 1, 578, 38);
    }
}

And when you know the color:

_color = ...
iKnowDaColor = true;

// causes the panel to invalidate and our painting procedure to be called
panel.Invalidate();

I haven't tested this but should give you the basic idea.

I think that the better way of doing that is to extend the Panel class and add some custom OnPaint event logic.

public class PanelRect : Panel
{
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        using (Graphics g = e.Graphics)
        {
            Rectangle rect = ClientRectangle;
            rect.Location = new Point(20, 20);                  // specify rectangle relative position here (relative to parent container)
            rect.Size = new Size(30, 30);                       // specify rectangle size here

            using (Brush brush = new SolidBrush(Color.Aqua))    // specify color here and brush type here
            {
                g.FillRectangle(brush, rect);
            }
        }
    }
}

PS This is not an advanced example, but might help you. You can move size, location and color etc to properties so you can easily change them from designer.

PSPS If you need a non-filled rectangle just use Pen object instead of Brush (you might also change the FillRectangle to something more suitable).

put the following code in the appropriate place :

Graphics g = panel1.CreateGraphics();
int redInt=255, blueInt=255, greenInt=255; //255 is example, give it what u know
Pen p = new Pen(Color.FromArgb(redInt,blueInt,greenInt));
Rectangle r = new Rectangle(1, 1, 578, 38);
g.DrawRectangle(p, r);

and if you wanted to draw the rectangle somewhere else, say the form, you could do g = this.CreateGraphics .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM