简体   繁体   English

为什么我的自定义控件文本框的onpaint覆盖方法永远不会被调用?

[英]why my custom control textbox's onpaint overided method never gets called?

I have created a customized textbox which has border in red color. 我创建了一个带有红色边框的自定义文本框。 I then launch my application but this OnPaint never gets called. 然后,我启动我的应用程序,但是此OnPaint从未被调用。

My code is this: 我的代码是这样的:

public partial class UserControl1 : TextBox
    {
        public string disable, disableFlag;
        public string Disable 
        { 
            get 
            { 

                return disable;
            } 
            set 
            { 
                disable = value;
                disableFlag = disable;
                //OnPaint(PaintEventArgs.Empty);
            } 
        }

        public UserControl1()
        {
            InitializeComponent();
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.Text = "testing 1";
            if (disable == "true")
            {

                Pen redPen = new Pen(Color.Red);
                Graphics graphics = this.CreateGraphics();
                graphics.DrawRectangle(redPen, this.Location.X,
                              this.Location.Y, this.Width, this.Height);
                //  base.DrawFrame(e.Graphics);
            }
        }
    }

Please let me know what is the problem (Here is the snapshot of the winform http://prntscr.com/ceq7x5 )? 请让我知道问题出在哪里(这是winform的快照http://prntscr.com/ceq7x5 )?

You shouldn't create a new Graphics object. 您不应该创建一个新的Graphics对象。 Use e.Graphics.DrawRectangle to be able to draw on the control using the already existing Graphics object like this: 使用e.Graphics.DrawRectangle可以使用已经存在的Graphics对象在控件上进行绘制,如下所示:

Pen redPen = new Pen(Color.Red);
e.Graphics.DrawRectangle(redPen, this.Location.X,
                          this.Location.Y, this.Width, this.Height);

Also, to repeat my comment here about the Disabled flag. 另外,在这里重复我关于“禁用”标志的评论。 There's no point adding a custom Disable flag. 没有必要添加自定义“禁用”标志。 Use the Enabled property that is already provided by the Windows Forms TextBox control. 使用Windows窗体TextBox控件已经提供的Enabled属性。

Edit: Please notice that the above code doesn't work in TextBox's case, because it handles drawing differently. 编辑:请注意,以上代码在TextBox的情况下不起作用,因为它以不同的方式处理绘图。 TextBox is basically just a wrapper around the native Win32 TextBox so you need to listen to quite a few messages that tell it to repaint itself. 基本上,TextBox只是本机Win32 TextBox的包装,因此您需要聆听一些告诉其重新绘制自身的消息。 You also need to get the handle to the device context and transform it to a managed Graphics object to be able to draw. 您还需要获取设备上下文的句柄,并将其转换为托管Graphics对象才能绘制。

Take a look at this article for code and explanations on how to draw on top of TextBox. 查看本文,获取有关如何在TextBox上绘制的代码和说明。 Especially part 2. Drawing Onto the TextBox on the bottom of the page. 特别是第2部分。绘制到页面底部的TextBox上。

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

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