Button控件

1、通过设置AutoSize(bool)属性来控制按钮的大小以适应文本的长度

btn_One.AutoSize = true;//设置按钮基于内容自动调整大小

2、当按钮得到焦点是自动放大,失去焦点时自动缩小

       private void button1_MouseEnter(object sender, EventArgs e)
        {
            button1.Location = new Point(80, 10);//设置按钮位置
            button1.Font = new Font("隶书", 18);//设置按钮字体样式
            button1.Width = 200;//设置按钮宽度
            button1.Height = 80;//设置按钮高度
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            button1.Location = new Point(130, 30);//设置按钮位置
            button1.Font = new Font("宋体",9);//设置按钮字体样式
            button1.Width = 100;//设置按钮宽度
            button1.Height = 40;//设置按钮高度
        }

3、制作跑动的按钮,使用Trimer组件间隔性的更改按钮的位置

4、根据需要生成Button按钮

            Random G_Random=new Random();
Button bt = new Button()//创建按钮对象 { Text = "动态生成按钮",//设置按钮的文本信息 ForeColor = Color.FromArgb(//设置按钮的前景颜色 G_Random.Next(0, 255), G_Random.Next(0, 255), G_Random.Next(0, 255)), AutoSize = true,//设置按钮自动调整大小 Location = e.Location//设置按钮位置 }; Controls.Add(bt);//将按钮加入控件集合

所有窗体控件都继承于Control类,所以所有窗体控件都可以使用Control集合的Add方法动态地将控件添加到窗体上

 Button In WPF

普通

<Button Name="btnTest" Width="120" Height="36" Margin="10,10" HorizontalAlignment="Left">
            你好
</Button>

文本为一个形状

<Button Name="btnTest" Width="120" Height="36" Margin="10,10" HorizontalAlignment="Left">         
            <Button.Content>
                <Rectangle Fill="LightBlue" Width="90" Height="28">
                </Rectangle>
            </Button.Content>
</Button>

文本为一张图片

<Button Name="btnTest" Width="120" Height="36" Margin="10,10" HorizontalAlignment="Left">         
            <Button.Content>
                <Image Source="image/img-mytest.jpg"></Image>
            </Button.Content>
</Button>

图片和文字都有

<Button Name="btnTest" Width="360" Height="360" Margin="10,10" HorizontalAlignment="Left">    
            <StackPanel>
                <Image VerticalAlignment="Top" Source="image/img-mytest.jpg"/>
                <TextBlock Text="点击试试" HorizontalAlignment="Center"/>
            </StackPanel>
</Button>

 两个有趣的属性

IsDefault,按钮会成为默认按钮

IsCancel,按钮会成为窗体的取消按钮

<Button Name="BtnText" IsCancel="True" Width="120" Height="36" Click="BtnText_OnClick">IsCancel</Button>
<Button Name="BtnText2" IsDefault="True" Width="120" Height="36" Margin="5" Click="BtnText2_OnClick" >IsDefault</Button>
        private void BtnText_OnClick ( object sender, RoutedEventArgs e )
        {
            MessageBox.Show("你触发了IsCanel按钮");
        }

        private void BtnText2_OnClick ( object sender, RoutedEventArgs e )
        {
            MessageBox.Show("你触发了IsDefault按钮");
        }


ToggleButton控件和RepeatButton控件

ToggleButton类:该类表示具有两个状态的按钮(按下状态和未按下状态)。当单击按钮时,他会保持按下状态,知道再次单击该按钮以释放它为止,有人称其为“粘贴单击”行为,这个控件一般情况下不单独使用,用于构建更加强大的CheckBox和RaddioButton类。

RepeatButton类:只要按钮保持按下状态,该类就不断触发Click事件,对于普通的按钮,用户每次单击只触发一个Click事件

 ToolTip

<Button Name="BtnText2" ToolTip="你好啊" Click="BtnText2_OnClick" >IsDefault</Button>
ToolTipService.SetInitialShowDelay ( BtnText2, 3000 );
<Button ToolTip="fafdsafa" ToolTipService.InitialShowDelay="1000"></Button>
原文地址:https://www.cnblogs.com/chenyongblog/p/3233841.html