简体   繁体   English

在面板上绘制矩形

[英]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.. 因此,我需要用变量替换(Brushes.Blue),并且需要在此代码中设置的坐标上的面板上绘制矩形。

Construct your Pen using the Pen(Color) constructor instead of the Pen(Brush) one. 构建你的Pen使用Pen(Color)构造函数,而不是Pen(Brush)之一。 Then you can define your color once you know it. 然后,一旦知道就可以定义颜色。

You should perform drawing in the Paint event of the panel. 您应该在面板的Paint事件中执行绘图。 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. 只要Windows决定是时候重新绘制面板,并且PaintEventArgs包含可以在其上绘制矩形的Graphics对象,就会发生此事件。

The Brush is an abstract class, but you can use the SolidBrush object to create a custom colored brush at runtime: Brush是一个抽象类,但是您可以在运行时使用SolidBrush对象创建自定义的彩色画笔:

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. 我认为更好的方法是扩展Panel类并添加一些自定义OnPaint事件逻辑。

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. PS这不是高级示例,但可能会对您有所帮助。 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). PSPS如果需要一个非填充矩形,只需使用Pen对象而不是Brush(您也可以将FillRectangle更改为更合适的对象)。

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 . 如果您想在其他地方绘制矩形(例如表格),则可以执行g = this.CreateGraphics

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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