为什么painteventhandler的后续方法有时不起作用? [英] Why do subsequent methods for the painteventhandler not work sometimes?

查看:82
本文介绍了为什么painteventhandler的后续方法有时不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始在表格上处理一些图形并复制了一些示例并且能够使它们正常工作。



PaintEventHandler工作正常。我有从PaintEventHandler调用的方法,它将绘制两个图形中的一个。这些工作正常。它们在以下代码中被注释掉。



我在质疑为什么基于复选框状态的这些相同方法调用不起作用。我希望我可以选中一个复选框并调整窗口大小,如果选中该复选框,将绘制相应的图形。如果未选中该复选框,则不会绘制图形。



我猜这里有一个概念错误。那些图形会不会停滞不前?



I'm just getting into working on some graphics on forms and have copied a few examples and have been able to make them work.

The PaintEventHandler works fine. I have methods called from the PaintEventHandler that will paint one of two graphics. These work fine. They are commented out in the following code.

I am questioning why these same method calls based upon the status of a checkbox do not work. I am expecting that I can check a checkbox and resize the window and if the checkbox is checked, the appropriate graphic will be drawn. If the checkbox is not checked, the graphic would not be drawn.

I'm guessing I have a conceptual error here. Would the graphics just be left there stagnant maybe?

public Form1()
{
    InitializeComponent();
   // chkBxOutlinedSquare.CheckState = CheckState.Checked;
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}

private void Form1_Paint(object sender,
    System.Windows.Forms.PaintEventArgs pe)
{
    // Declares the Graphics object and sets it to the Graphics object
    // supplied in the PaintEventArgs.
    Graphics g = pe.Graphics;

    //outlinedSquare(g);
    //someGraphic(g);

    // Insert code to paint the form here.
    if (chkBxOutlinedSquare.CheckState == CheckState.Checked)
    {
        outlinedSquare(g);
    }

    if (chkBxSomeGraphics.Checked)
    {
        someGraphic(g);
    }
}

private void someGraphic(Graphics g)
{
    LinearGradientBrush linGrBrush = new LinearGradientBrush(
    new Point(0, 10),
    new Point(200, 10),
    Color.FromArgb(255, 255, 0, 0),   // Opaque red
    Color.FromArgb(255, 0, 0, 255));  // Opaque blue

    Pen pen = new Pen(linGrBrush);

    g.DrawLine(pen, 0, 10, 200, 10);
    g.FillEllipse(linGrBrush, 0, 30, 200, 100);
    g.FillRectangle(linGrBrush, 0, 155, 500, 30);

}

private void outlinedSquare(Graphics g)
{
    // Create a new pen.
    Pen skyBluePen = new Pen(Brushes.DeepSkyBlue);

    // Set the pen's width.
    skyBluePen.Width = 8.0F;

    // Set the LineJoin property.
    skyBluePen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;

    // Draw a rectangle.
    g.DrawRectangle(skyBluePen,
        new Rectangle(40, 40, 150, 200));

    //Dispose of the pen.
    skyBluePen.Dispose();

}





我的尝试:



我在两种方法中都设置了断点,代码肯定在正确的时间运行。清除复选框时,只是不清除图形。



What I have tried:

I have put breakpoints in both methods and the code is definitely running at the correct times. It's just not clearing the graphics when the checkboxes are cleared.

推荐答案

首先,你没有处理你在 someGraphic中创建的笔



其次,你没有在 outlineSquare()。那个区域,也不是整个表格,都被清除,等待你画它。如果您必须删除填充区域以绘制空白轮廓,则必须自己清除该区域。它不会为你完成。
First, you're not disposing the Pen you created in someGraphic.

Second, you're not clearing out the area you're drawing to in outlinedSquare(). That area, nor then entire form, is cleared waiting for you to paint it. If you have to remove the area that is filled just to draw an empty outline, you have to clear that area yourself. It's not going to be done for you.


你的问题对我来说不是很安静。

一般来说,如果你想要一个控件(或表格)重新绘制有时你必须告诉它。这可以通过myControl.Invalidate完成(例如)。这个方法强制从Control(或Form)调用Paint-method。

这个答案是Dave提到的解决方案的补充......
Your question is not quiet clear for me.
Generally if you want that a Control (or a Form) repaints itself sometimes you must tell that to it. This could be done (for example) by the myControl.Invalidate. This method forces the call of the Paint-method from the Control (or Form).
This answer is additional to the Solution, mentioned by Dave ...


这篇关于为什么painteventhandler的后续方法有时不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆