透明文本框-更改文本时不触发OnPaint [英] Transparent TextBox - OnPaint not firing when changin text

查看:111
本文介绍了透明文本框-更改文本时不触发OnPaint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控件开始时应该是透明的,但是一旦输入文本,它就会变成常规的白色背景.如果将鼠标移到控件上,它将再次触发OnPaint,并能够正确绘制带有文本的控件.输入文本时如何获得透明背景上的颜色?

My control properly starts as transparent, but once I enter text, it's going to a regular white background. If I move my mouse over the control, it fires the OnPaint again, and is able to draw the control with text correctly. How can I get it to paint the transparent background when text is being entered?

默认值:

直接输入文字后:

将鼠标移到上方后,迫使OnPaint触发:

After moving mouse over, to force OnPaint to fire:

ref class CustomTextBox : System::Windows::Forms::TextBox
{
public:

    CustomTextBox()
    {
        SetStyle(ControlStyles::SupportsTransparentBackColor, true);
        SetStyle(ControlStyles::OptimizedDoubleBuffer, true); //Tried this both ways
        SetStyle(ControlStyles::AllPaintingInWmPaint, true);
        SetStyle(ControlStyles::ResizeRedraw, true);
        SetStyle(ControlStyles::UserPaint, true);

        BackColor = Color::Transparent;
        ForeColor = Color::Red;
    }

    virtual void OnPrint(PaintEventArgs^ e) override
    {
        System::Windows::Forms::TextBox::OnPrint(e);
Debug::WriteLine("OnPrint: " + this->Text);
    }

    virtual void OnPaintBackground(PaintEventArgs^ e) override
    {
        System::Windows::Forms::TextBox::OnPaintBackground(e);
Debug::WriteLine("OnPaintBackground: " + this->Text);

    }

    virtual void OnPaint(PaintEventArgs^ e) override
    {
        //System::Windows::Forms::TextBox::OnPaint(e);

        //Paint Background
        Graphics^ g = e->Graphics;
        RectangleF^ bounds = gcnew RectangleF(0, 0, Convert::ToSingle(this->Width - 1), Convert::ToSingle(this->Height - 1));
        e->Graphics->FillRectangle(gcnew SolidBrush(this->BackColor), *bounds);

        //Paint text
        g->DrawString(this->Text,this->Font,gcnew SolidBrush(Color::Red),1,1);

Debug::WriteLine("OnPaint: " + this->Text);

    }

};

推荐答案

仅供参考-找到了解决方案:

FYI - Found the solution:

1)将TextChanged事件绑定到一个在每次输入文本时都会触发的方法. 2)在新方法中,标记控件:control->Invalidate()以触发重新绘制.

1) Tie the TextChanged event to a method that will fire every time text is entered. 2) In the new method, mark the control: control->Invalidate() to trigger the re-painting.

这篇关于透明文本框-更改文本时不触发OnPaint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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