简体   繁体   English

C#Windows窗体中的图形按钮状态

[英]Graphic Button States in C# Windows Form

I'm building a windows form in C# with VS2008.I have about 32 buttons, I've placed an image over the default buttons, but I want to have a Hover/Up/Down effect for each of these buttons, and I can't seem to find this information anywhere. 我正在使用VS2008用C#构建Windows窗体。我有大约32个按钮,已将图像放置在默认按钮上,但是我想为每个按钮都具有悬浮/上/下效果,我可以似乎在任何地方都找不到此信息。

Perhaps I'm searching for the wrong terms, any direction is appreciated. 也许我在搜索错误的术语,任何方向都值得赞赏。

Inherit from the System.Windows.Forms.Button class, and override the following methods: 从System.Windows.Forms.Button类继承,并重写以下方法:

  • OnMouseDown, this handles your "Pressed Image" OnMouseDown,这将处理您的“已按下图像”
  • OnMouseEnter, this handles your "Hovering Image" OnMouseEnter,它将处理您的“悬停图像”
  • OnMouseLeave, and this handles your original image OnMouseLeave,并处理您的原始图像

The code would look something like this: 代码看起来像这样:

public class MyButton : System.Windows.Forms.Button {
    public MyButton()
        : base() {
        // No default implementation
    }

    protected override void OnMouseDown(Object sender, MouseEventArgs e) {
        this.Image = Properties.Resources.PressedImage;
    }

    protected override void OnMouseEnter(Object sender, MouseEventArgs e) {
        this.Image = Properties.Resources.HoveringImage;
    }

    protected override void OnMouseLeave(Object sender, MouseEventArgs e) {
        this.Image = Properties.Resources.DefaultImage;
    }
}

Now since you have multiple buttons, you could of course turn the resource acquisitions into properties. 现在,由于有多个按钮,您当然可以将资源获取转换为属性。

public Image DefaultImage {
    get;
    set;
}

public Image PressedImage {
    get;
    set;
}

public Image HoveringImage {
    get;
    set;
}

And use those in the overridden, virtual methods. 并在覆盖的虚拟方法中使用它们。 This is probably your best approach. 这可能是您最好的方法。 I would avoid the answer below (no offense Phoexo) as it is a very unusual and messy solution. 我会避免下面的答案(不要冒犯Phoexo),因为这是一个非常不寻常且混乱的解决方案。

Once you write and compile the new button, you can do a simple find and replace to change the existing button's types. 编写并编译新按钮后,即可进行简单查找和替换以更改现有按钮的类型。 You will have to do a little cleanup and set the three properties for each button to their corresponding images, and cleanup some designer code, but this will make your life easier in the long run if you have to add more buttons. 您将需要进行一些清理,并将每个按钮的三个属性设置为它们对应的图像,并清理一些设计器代码,但是从长远来看,如果必须添加更多按钮,这将使您的工作变得更轻松。

Sounds like a job for inheritance to me. 听起来像是对我的继承工作。 I'd subclass a button class and handle the hover, press, and leave by overriding the OnHover, OnPress, OnLeave, and OnPaint methods. 我将通过覆盖OnHover,OnPress,OnLeave和OnPaint方法来继承按钮类并处理悬停,按下和离开。 Do your button painting in OnPaint. 在OnPaint中绘画按钮。

I have made a program which needed this with some labels, the result I came up with (add the AddHandles()-void to Form_Load): 我已经制作了一个需要带有一些标签的程序,我想出了结果(将AddHandles()-void添加到Form_Load):

    private void AddHandles()
    {
        foreach (Control c in this.Controls)
        {
            if (c.GetType() == typeof(Button))
            {
                c.MouseEnter += new EventHandler(ButtonEnter);
                c.MouseLeave += new EventHandler(ButtonLeave);
                c.MouseDown += new MouseEventHandler(ButtonDown);
                c.MouseUp += new MouseEventHandler(ButtonUp);
            }
        }
    }
    private void ButtonEnter(object sender, EventArgs e)
    {
        ((Button)sender).BackColor = Color.DarkGray;
        ((Button)sender).Cursor = System.Windows.Forms.Cursors.Hand;
    }
    private void ButtonLeave(object sender, EventArgs e)
    {
        ((Button)sender).BackColor = Color.Black;
        ((Button)sender).Cursor = System.Windows.Forms.Cursors.Default;
    }
    private void ButtonDown(object sender, MouseEventArgs e)
    {
        ((Button)sender).BackColor = Color.Gray;
    }
    private void ButtonUp(object sender, MouseEventArgs e)
    {
        ((Button)sender).BackColor = Color.Black;
        ((Button)sender).Cursor = System.Windows.Forms.Cursors.Default;

        //Code to be executed 
        //Use the name/text/etc of the Button to figure what to do...
    }

Note that I originally used this with Labels, so there may be need for some changes. 请注意,我最初将此用于Labels,因此可能需要进行一些更改。

I implemented it much similarly to David Anderson's solution once, but I kept record of the actual state and overrided the OnPaint method to paint the proper image. 我曾经与David Anderson的解决方案非常相似地实施它,但是我保留了实际状态的记录,并重写了OnPaint方法以绘制适当的图像。 I don't know if it's better in any way, it's just how I did this. 我不知道这是否更好,这就是我做到的方式。

Use a Check Box or a Radio Button and then change the Appearance to Button. 使用复选框或单选按钮,然后将外观更改为Button。 You can add an image to the Check Box or Radio Button just like a regular Button. 您可以像常规按钮一样将图像添加到复选框或单选按钮。

The setting can be set in either the Designer or via code: 可以在设计器中或通过代码设置该设置:

CheckBox checkBox = new CheckBox(); CheckBox checkBox = new CheckBox(); checkBox.Appearance = Appearance.Button; checkBox.Appearance = Appearance.Button;

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

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