调整大小后,不会在自定义控件的整个区域上绘制一条线,C# [英] doesn't draw a line on full area of a custom control after resize it, C#

查看:105
本文介绍了调整大小后,不会在自定义控件的整个区域上绘制一条线,C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义标签控件,我尝试在其上绘制线条。

我在表单上绘制后,它在重新调整后不会重新绘制区域...



我的代码 - :



I created a custom label control, I try to draw lines on it.
after I draw it on form, it doesn''t repaint the area after resize it...

My Code -:

using System.Drawing;
using System.Windows.Forms;

namespace SevenSegmentDigit
{
    public partial class DigitDisplay : Label
    {
        Graphics grx;
        Pen roundPen;
        public DigitDisplay()
        {
            InitializeComponent();            
            SetAutoSizeMode(AutoSizeMode.GrowAndShrink);
            this.ResizeRedraw = true;
            AutoSize = false;
            grx = this.CreateGraphics();
            roundPen = new Pen(Brushes.Black, 2);
            roundPen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            roundPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;                       
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            showDigit();
            base.OnPaint(pe);            
        }
        protected override void OnSizeChanged(System.EventArgs e)
        {                       
            showDigit();
            Invalidate(); 
        }
        protected override void OnResize(System.EventArgs e)
        {                       
            showDigit();
            Invalidate();
        }                
        
        private void showDigit()
        {
            grx.Clear(this.BackColor);
            switch (digitValue)
            {
                case 1:
                    drawOne();
                    break;
                case 2:
                    drawTwo();
                    break;
            };
        }
        private int digitValue = 0;
        public int DisplayDigit
        {
            get { return digitValue; }
            set { digitValue = value; showDigit(); }
        }
        private void drawOne()
        {
            grx.DrawLine(roundPen, 30, 10, 30, 24);
            grx.DrawLine(roundPen, 30, 26, 30, 40);
        }


        private void drawTwo()
        {
            grx.DrawLine(roundPen, 14, 10, 29, 10);
            grx.DrawLine(roundPen, 30, 10, 30, 24);
            grx.DrawLine(roundPen, 14, 25, 29, 25);
            grx.DrawLine(roundPen, 14, 26, 14, 40);
            grx.DrawLine(roundPen, 14, 41, 29, 41);
        }
}
}





但它显示相同的结果...... :-(

提前感谢..



but it show the same result... :-(
thanks in advance..

推荐答案

调整无效大小已更改。将解决你的问题。



每当你需要重新渲染并最终调用你的绘制处理程序时,你会调用 Invalidate 方法。请注意,其中两个方法只允许您使场景的一部分无效( Rectangle Region ),如果需要多个无效,可以提高性能。另外,多个invalidate不会每次都重新呈现;而是,它们OR在一起(使理论集 union )无效不是说这个机制是开发任何交互式和/或动画行为的主要方式;在这种情况下你通常还需要使用双缓冲。



请另见我过去的答案:

在mdi子表单之间绘制行 [ ^ ],

在面板上捕获绘图 [ ^ ],

< a href =http://www.codeproject.com/Answers/176000/What-kind-of-playful-method-is-Paint havenGridViewI#answer2>什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

如何加速我的vb.net应用程序? [ ^ ]。



-SA
Call Invalidate on size changed. That will solve your problem.

Whenever you need re-rendering and ultimately calling you paint handler, you call one of the Invalidate methods. Pay attention that two of those method allows your to invalidate only a part of the scene (Rectangle or Region), which can improve performance if multiple invalidates is needed. Also, multiple invalidate do not cause re-rendering every time; rather, they "OR" together (make theory-set union) of invalidated point set. Not that this mechanism is the major way to develop any interactive and/or animated behavior; in such cases you usually also need to use double buffering.

Please also see my past answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^].

—SA


如果您在调整窗口大小时尝试缩放在窗口中绘制的内容,则绘图代码的大小都不会考虑绘图表面的大小。您每次都以完全相同的比例绘制完全相同的图像。设置AutoSizeMode不会为您重新缩放绘图。您必须提供将根据绘图表面的大小计算比例的代码,并提供将根据新的比例值适当更改绘图坐标的代码。
If you''re trying to scale what you''re drawing in the window when the window is resized, none of your drawing code takes into consideration the size of the drawing surface. You''re drawing the exact same image at the exact same scale every time. Setting the AutoSizeMode will NOT rescale your drawing for you. You have to provide the code that will calculate the scale based on the size of the drawing surface and provide the code that will change the drawing coordinates appropriately based on the new scale values.


尝试放置
Invalidate();
after the 

之后的Invalidate();

showDigit();





Hopefull这将解决您的问题。

.

Hopefull this will solve your problem.


这篇关于调整大小后,不会在自定义控件的整个区域上绘制一条线,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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