如何在“Enabled”为false时使按钮和文本框看起来与原始一样。

huangapple go评论76阅读模式
英文:

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
            );
        }
    }
}

如何在“Enabled”为false时使按钮和文本框看起来与原始一样。

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
            );
        }
    }
}

如何在“Enabled”为false时使按钮和文本框看起来与原始一样。


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
           );
        }
    }
}

huangapple
  • 本文由 发表于 2023年2月6日 18:56:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定