简体   繁体   English

是否可以将命令按钮设置为在Visual Studio 2008中具有禁用的3D图形?

[英]Is it possible to set a command button to have a 3D disabled graphic in Visual Studio 2008?

I've recently started a WinForms project in Visual Studio 2008 and I notice that when I disable a command button, it flattens out the button and leave the text black. 我最近在Visual Studio 2008中启动了一个WinForms项目,我注意到当我禁用命令按钮时,它会使按钮变平并使文本保持黑色。 This is quite an unexpected change from my experiences in VB6 and VS2005 where the button simply grays itself out. 与我在VB6和VS2005中的体验相比,这是一个意想不到的变化,在这些体验中,按钮只是显示为灰色。

Is there a setting that would allow me to make the disabled state of the command button 3D like the enabled state? 是否有可以让我将命令按钮的3D禁用状态设为启用状态的设置? I have the FlatStyle set to Standard, though System results in the same behavior. 我将FlatStyle设置为Standard,尽管系统导致了相同的行为。 I also noticed that UseVisualStyleBackColor needs to be set to True for the button to appear correctly in its Enabled state. 我还注意到,必须将UseVisualStyleBackColor设置为True,按钮才能正确显示为Enabled状态。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

The painting code for a button is locked up tight in internal classes, you can't override it nor re-use it. 按钮的绘画代码在内部类中被严格锁定,您无法覆盖它或重新使用它。 The public ButtonRenderer class doesn't handle disabled buttons. 公共ButtonRenderer类不处理禁用的按钮。

One thing that could work is playing games with the BackgroundImage property. 可能有用的一件事是使用BackgroundImage属性玩游戏。 Add a new class to your project and paste the code shown below. 将新类添加到您的项目中,然后粘贴以下代码。 Drop the new control from the top of the toolbar onto your form. 将新控件从工具栏顶部拖放到窗体上。

using System;
using System.Drawing;
using System.Windows.Forms;

public class MyButton : Button {
  protected override void OnSizeChanged(EventArgs e) {
    base.OnSizeChanged(e);
    Bitmap bmp = new Bitmap(this.Width, this.Height);
    using (Graphics gr = Graphics.FromImage(bmp)) {
      ButtonRenderer.DrawButton(gr,
        new Rectangle(0, 0, bmp.Width, bmp.Height),
        System.Windows.Forms.VisualStyles.PushButtonState.Normal);
    }
    if (this.BackgroundImage != null) this.BackgroundImage.Dispose();
    this.BackgroundImage = bmp;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM