英文:
How to make the button and textbox in orginal look when Enabled is false
问题
当我创建C# WinForms按钮和文本框时,当enabled = false
时,颜色和设计变得灰色,所以我需要任何技巧或代码,使文本框看起来像已启用,即使Enabled = false
。
英文:
When I make C# WinForms button and textbox enabled = false color and design get gray so I need any trick or code that make textbox looks Enabled true while its Enabled = false.
答案1
得分: 2
对于 TextBox
,实现这个目标的一种方法是拦截 WM_PAINT
消息并检查 Enabled
属性。如果它为假,可以使用 TextBoxRenderer 类来绘制文本框,就好像它是启用的。
尝试用这个自定义版本替换设计器文件中的 TextBox
实例:
class TextBoxEx : TextBox
{
const int WM_PAINT = 0x000F;
protected override void WndProc(ref Message m)
{
if (m.Msg.Equals(WM_PAINT) && !Enabled)
{
paintDisabled();
return;
}
base.WndProc(ref m);
}
private void paintDisabled()
{
using (Graphics graphics = CreateGraphics())
{
TextBoxRenderer.DrawTextBox(
graphics,
Bounds,
System.Windows.Forms.VisualStyles.TextBoxState.Normal
);
}
}
}
Button
在这种情况下,使用 ButtonRendererClass
代替。
class ButtonEx : Button
{
const int WM_PAINT = 0x000F;
protected override void WndProc(ref Message m)
{
if (m.Msg.Equals(WM_PAINT) && !Enabled)
{
paintDisabled();
return;
}
base.WndProc(ref m);
}
private void paintDisabled()
{
using (Graphics graphics = CreateGraphics())
{
ButtonRenderer.DrawButton(
graphics,
Bounds,
System.Windows.Forms.VisualStyles.PushButtonState.Normal
);
}
}
}
希望这有所帮助。
英文:
For a TextBox
, one way to achieve this objective is to intercept the WM_PAINT
message and inspect the Enabled
property. If it's false, use TextBoxRenderer class to draw the text box as if it were enabled.
Try swapping out the instances of TextBox
in your designer file with this custom version:
class TextBoxEx : TextBox
{
const int WM_PAINT = 0x000F;
protected override void WndProc(ref Message m)
{
if (m.Msg.Equals(WM_PAINT) && !Enabled)
{
paintDisabled();
return;
}
base.WndProc(ref m);
}
private void paintDisabled()
{
using (Graphics graphics = CreateGraphics())
{
TextBoxRenderer.DrawTextBox(
graphics,
Bounds,
System.Windows.Forms.VisualStyles.TextBoxState.Normal
);
}
}
}
Button
In this case, the ButtonRendererClass
is used instead.
class ButtonEx : Button
{
const int WM_PAINT = 0x000F;
protected override void WndProc(ref Message m)
{
if (m.Msg.Equals(WM_PAINT) && !Enabled)
{
paintDisabled();
return;
}
base.WndProc(ref m);
}
private void paintDisabled()
{
using (Graphics graphics = CreateGraphics())
{
ButtonRenderer.DrawButton(
graphics,
Bounds,
System.Windows.Forms.VisualStyles.PushButtonState.Normal
);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论