我的控件正确地以透明方式启动,但是一旦我输入文本,它就会进入常规的白色背景。如果我将鼠标移到控件上,它会再次触发OnPaint,并能够正确地绘制带有文本的控件。如何在输入文本时绘制透明背景?
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);
}
};
答案 0 :(得分:0)
仅供参考 - 找到解决方案:
1)将TextChanged事件绑定到每次输入文本时将触发的方法。
2)在新方法中,标记控件:control->Invalidate()
以触发重新绘制。