按钮的FlatStyle在悬停时是系统样式,但点击时是标准样式

本文关键字:样式 标准 悬停 FlatStyle 按钮 系统 | 更新日期: 2023-09-27 17:49:39

我见过很多窗体,比如Windows的设置/选项窗体,当鼠标悬停在按钮上时,按钮的FlatStyle是System。但是当您单击该按钮时,FlatStyle将更改为Standard。这可以在Windows的Skype选项表单的"保存"answers"取消"按钮上找到。

顺便说一下,我希望代码是用c#写的,因为我在我的项目中使用c#。

我尝试了一些方法来实现这个目标,但从来没有成功过。

如果你使用的是Windows,但没有Skype,那么你可以在很多Windows的设置表单中找到我所说的,比如Windows的"播放设备"表单。

你还可以注意到,如果选择的选项卡是按钮本身,则不会发生这种情况。

这将是伟大的代码/例子来实现这一点。

按钮的FlatStyle在悬停时是系统样式,但点击时是标准样式

您需要像下面的代码那样为按钮click和MouseHover事件连接一个事件处理程序。然后使用FlatStyle类来改变外观:

  private void button1_MouseHover(object sender, EventArgs e)
        {
            button1.FlatStyle = FlatStyle.System;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            button1.FlatStyle = FlatStyle.Standard;
        }

希望这对你有帮助!

你需要的是MouseEnter而不是MouseHover,所以使用这个:

 private void button1_MouseEnter(object sender, EventArgs e)
 {
    button1.FlatStyle = FlatStyle.System;
 }
 private void button1_Down(object sender, EventArgs e)
 {
    button1.FlatStyle = FlatStyle.Standard;
 }
调试:

 int i = 0;
 private void button1_MouseEnter(object sender, EventArgs e)
 {
    button1.FlatStyle = FlatStyle.System;
    i++;
    Console.WriteLine(i.ToString());
 }
 private void button1_Down(object sender, EventArgs e)
 {
    button1.FlatStyle = FlatStyle.Standard;
 }