0

每当在 C# 中创建表单按钮时,它们默认带有该操作系统按钮的视觉样式。我要做的是:默认情况下,只显示按钮的文本或图标。当文本和/或图标悬停时,还显示我们之前隐藏的按钮视觉样式的其余部分。

这有任何意义吗?有谁知道这可能是如何实现的?我以前看过它,但我不确定它是如何完成的。

我应该澄清一下 - 我没有使用 WPF 或 XAML。

4

1 回答 1

0

您可以制作 aStyle和 a并在鼠标悬停时ControlTemplate隐藏。ControlTemplate

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WpfApplication8" Height="200" Width="200" Name="mainWindow" >

    <Window.Resources>

        <ControlTemplate TargetType="Button" x:Key="dummyStyle" >
            <Border>
                <ContentPresenter Margin="0" VerticalAlignment="Center" HorizontalAlignment="Center" />
            </Border>
        </ControlTemplate>

        <Style TargetType="Button" x:Key="masterStyle">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="False">
                    <Setter Property="Template" Value="{StaticResource dummyStyle}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>

    <Grid>
        <Button Style="{StaticResource masterStyle}" Content="Button" Height="44" Width="129" />
    </Grid>

</Window>

如果您使用 winforms(不确定),您可以制作一个简单的自定义控件,如下所示:

public partial class HoverButton : UserControl
{
    private string _buttonText;

    public HoverButton()
    {
        InitializeComponent();
        ButtonText = "HoverButton1";
    }

    public string ButtonText
    {
        get { return _buttonText; }
        set
        { 
            _buttonText = value;
            button1.Text = _buttonText;
            label1.Text = _buttonText;
        }
    }


    private void label1_MouseEnter(object sender, EventArgs e)
    {
        label1.Visible = false;
    }

    private void button1_MouseLeave(object sender, EventArgs e)
    {
        label1.Visible = true;
    }
}

partial class HoverButton
{
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.button1.Location = new System.Drawing.Point(0, 0);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(150, 150);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.MouseLeave += new System.EventHandler(this.button1_MouseLeave);
        // 
        // label1
        // 
        this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.label1.Location = new System.Drawing.Point(0, 0);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(150, 150);
        this.label1.TabIndex = 1;
        this.label1.Text = "label1";
        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        this.label1.MouseEnter += new System.EventHandler(this.label1_MouseEnter);
        // 
        // HoverButton
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(this.label1);
        this.Controls.Add(this.button1);
        this.Name = "HoverButton";
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label label1;
}
于 2012-12-16T03:21:49.900 回答