从右向左收缩Form时不会调用面板的Paint事件吗? [英] Paint event of panel not called when shrinking Form from right to left?

查看:246
本文介绍了从右向左收缩Form时不会调用面板的Paint事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗体,该窗体有一个父面板,有一个子面板,我在其中使用绘图机制绘制项目时,它按预期运行良好,但是当我将窗体从右向左缩小时,它不会调用子面板绘画事件,而如果我从左向右缩小一点并再次展开,则称为绘画事件,我该如何解决? 下面是我的代码.

I have a Form which has got a parent panel and it had got a child panel where I am drawing items using the drawing mechanism it works good as expected, but when I shrink my form from right to left it doesn't call child panels paint event while if I shrink a little from left to right and again spread it then it calls the paint event, how should I fix it? Below is my code.

  private void canvas_Paint(object sender, PaintEventArgs e)
        {
            drawString(e);
            this.Invalidate();
            //this.Refresh();
            //this.Update();


        }

        private void drawString(PaintEventArgs e)
        {

            System.Drawing.Drawing2D.LinearGradientBrush myBrush = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, Color.Red, Color.Yellow, System.Drawing.Drawing2D.LinearGradientMode.Horizontal);
            cBasketItemHelper objHelper = new cBasketItemHelper() { CanvasWidth = this.canvas.Width, CanvasHeight = this.canvas.Height, X = 3, Y = 3 };
            objHelper.myBrush = myBrush;
            objHelper.currOrder = Program.currOrder;
            objHelper.g = e.Graphics;//this.canvas.();//this.canvas.Graphics;
            objHelper.DrawBasketItems();
            e.Dispose();
        }

推荐答案

Panel类被设计为只是其他控件的容器,除了绘制背景之外,它不希望自己做任何绘制.它在某种程度上用力地优化了绘画,调整大小仅绘画显示的部分,而不是整个客户区域.

The Panel class was designed to be just a container for other controls, it is not expected to do any painting of its own beyond drawing the background. Somewhat heavy-handedly it optimizes the painting, a resize only paints the parts that were revealed, not the entire client area.

但是,您希望OnPaint在大小更改时始终运行,即使您减小它的大小也是如此.从Panel派生您自己的类,然后在构造函数中将ResizeRedraw属性设置为true:

You however want OnPaint to always run when the size changes, even when you make it smaller. Derive your own class from Panel and set the ResizeRedraw property to true in the constructor:

class Canvas {
    public Canvas() {
        this.ResizeRedraw = true;
        this.DoubleBuffered = true;   // extra goodie
    }
}

构建.从工具箱顶部放下新的Canvas控件,替换现有的面板控件.如果您不需要Panel提供的滚动支持,则使用PictureBox可以使您两个都不需要派生.

Build. Drop the new Canvas control from the top of the toolbox, replacing your existing panel control. If you don't need the scrolling support that Panel provides then using a PictureBox gets you both without needing to derive.

这篇关于从右向左收缩Form时不会调用面板的Paint事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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