简体   繁体   English

从另一个窗体触发按钮单击事件时,在窗体的面板中绘制一个矩形

[英]Draw a rectangle in a panel of a form when a button click event is fired from another form

I'm currently working on windows form application where I have two forms; 我目前正在使用Windows窗体应用程序,其中有两个窗体。 form1 and form2. form1和form2。 There is a button inside form1 which opens form2 when clicked and what I want is to create a rectangle inside a panel of form1 when a button inside Form2 is clicked. 在form1内有一个按钮,单击该按钮时会打开form2,而我要在单击Form2内的按钮时在form1的面板内创建一个矩形。 I put some code to create rectangle inside the button click event of form2, but it showed nothing when clicked. 我在form2的按钮单击事件中放入了一些代码来创建矩形,但是单击时它什么也没显示。 However, whenever I put the draw.rectangle method inside the same form where button is clicked, it work, but differently it doesn't 但是,每当我将draw.rectangle方法放入单击按钮的相同窗体内时,它都可以工作,但是不同的是,它不会

Here's the code inside form1 这是form1中的代码

  private void btnSave_Click(object sender, EventArgs e)
    {
        Layoutsetting a = new Layoutsetting();
        a.ShowDialog();
    }
public void DrawObject()
    {

            Graphics g = panel1.CreateGraphics();
            Rectangle rect = new Rectangle(10, 10, 80, 90);
            rect.Inflate(-10, -10);
            g.DrawRectangle(black, rect);
            g.FillRectangle(Brushes.BlueViolet, rect);
            StringFormat f = new StringFormat();
            f.LineAlignment = StringAlignment.Center;
            f.Alignment = StringAlignment.Center;
            g.DrawString("Hello", this.Font, Brushes.GhostWhite, rect, f);
            panel1.Refresh();
 }

This is the code inside form2 这是form2中的代码

  private void btnConfirm_Click(object sender, EventArgs e)
    {
        Form1.Default.DrawObject();
        this.Close();
    }

You have to add a method to Paint: 您必须向Paint添加一个方法:

panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.draw);

private void draw(object sender, PaintEventArgs e)
{
    if(buttonClicked) {
        Graphics g = e.Graphics;
        //...
    }
}

The problem is not with drawing rectangle, the panel paint event fires whenever even a slightest part of the panel were hidden (for example a part of it was behind another form) and redraws the panel, so the rectangle disappears (but when it is the active form which paint event doesn't fire, the rectangle will be drawn and will not be cleared unless you do something that panel needs to be redrawn.). 问题不在于绘制矩形,每当面板的一小部分被隐藏时(例如,它的一部分位于另一个表单后面),面板油漆事件就会触发并重新绘制面板,因此矩形消失(但是当矩形是活动状态,不会触发绘制事件,除非您需要重新绘制面板,否则矩形将被绘制并且不会被清除。)

Easy Solution: 简易解决方案:

Create an image of recangle and use it as background image when needed, instead of drawing it. 创建矩形图像,并在需要时用作背景图像,而不是绘制它。

Another Solution: 另一个解决方案:

add a property to your form (or your panel): 在表单(或面板)中添加属性:

public bool NeedsToBeDrawn {get; set;}

instead of this line of code: 而不是这行代码:

Form1.Default.DrawObject();

just set the property to true: 只需将属性设置为true即可:

   Form1.NeedsToBeDrawn  = true;

and move your code to your panel's paint event: 并将代码移至面板的绘画事件:

private void panel1_Paint(object sender, PaintEventArgs e)
{
     if(NeedsToBeDrawn)
     {
            Rectangle rect = new Rectangle(10, 10, 80, 90);
            rect.Inflate(-10, -10);
            e.Graphics.DrawRectangle(black, rect);
            e.Graphics.FillRectangle(Brushes.BlueViolet, rect);
            StringFormat f = new StringFormat();
            f.LineAlignment = StringAlignment.Center;
            f.Alignment = StringAlignment.Center;
            e.Graphics.DrawString("Hello", this.Font, Brushes.GhostWhite, rect, f);
     }
}

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

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