c# - OnPaint 文本框问题 -
问题描述
在 TextBox 上使用 OnPaint 事件时遇到问题。每当我用鼠标单击文本区域或开始键入时,文本框的背景都会变回白色。知道是什么原因造成的吗?
这是我的代码:
protected override void OnCreateControl() {
base.OnCreateControl();
SetStyle(ControlStyles.OptimizedDoubleBuffer
| ControlStyles.DoubleBuffer
| ControlStyles.AllPaintingInWmPaint
| ControlStyles.ResizeRedraw
| ControlStyles.UserPaint, true);
}
protected override void OnPaintBackground(PaintEventArgs e) {
SolidBrush bBrush = new SolidBrush(Enabled ? BackColor : Color.LightGray);
e.Graphics.FillRectangle(bBrush, ClientRectangle);
bBrush.Dispose();
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
using (Brush aBrush = new SolidBrush(ForeColor)) {
StringFormat sf = new StringFormat();
switch (TextAlign) {
case HorizontalAlignment.Center:
sf.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Left:
sf.Alignment = StringAlignment.Near;
break;
case HorizontalAlignment.Right:
sf.Alignment = StringAlignment.Far;
break;
}
string text = UseSystemPasswordChar && !Enabled
? new StringBuilder(Text.Length).Insert(0, PasswordChar.ToString(), Text.Length).ToString()
: Text;
e.Graphics.DrawString(text, (Font)Font.Clone(), aBrush, ClientRectangle, sf);
aBrush.Dispose();
}
}
解决方案
推荐阅读
- react-native - ReactNative - 从 redux-form 获取值
- python - 获取 AttributeError:“系列”对象没有属性“to_html”?
- ios - 如何检查字符串是否已包含 Swift 中的字符
- vue.js - 基于三元运算符应用内联样式背景图像
- hyperledger-fabric - 如何使用 Hyperledger Composer 在 Hyperledger Fabric 中存储大文件
- python - 使用python进行3D曲线拟合
- mysql - 您在 PDO 中的 SQL 语法有错误
- date - 取消内联编辑后使用自定义格式化程序免费 jqGrid 恢复日期
- c++ - 如何创建可以重新创建的自定义需求
- python - Pygame 的 colliderect 没有注意到碰撞