0

フォーム ボタンが C# で作成されるときは常に、既定でそのオペレーティング システムのボタンのビジュアル スタイルが適用されます。私がやろうとしているのはこれです: デフォルトでは、ボタンのテキストまたはアイコンのみを表示します。テキストやアイコンにカーソルを合わせると、以前に非表示にした残りのボタンの視覚スタイルも表示されます。

それは意味がありますか?これがどのように達成されるか知っている人はいますか?以前にそれが行われたのを見たことがありますが、どのように行われたかはわかりません。

明確にする必要があります-私はWPFまたはXAMLを使用していません。

4

1 に答える 1

0

と を作成し、マウスが上にあるときに非表示にStyleすることができます。ControlTemplateControlTemplate

<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 に答える