C#中怎样设置Button控件的按下和弹起事件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#中怎样设置Button控件的按下和弹起事件相关的知识,希望对你有一定的参考价值。

参考技术A 你选中按钮的时候 看属性栏,里面有事件响应的, 直接双击就会给你生成一个空函数,你把要实现的 功能写在里面就行 参考技术B 按钮的按下和弹起事件,其实就是按钮鼠标和键盘的按下和弹起事件。你把这四个事件处理好就行了本回答被提问者采纳

实现按钮的按下和鼠标停留的颜色自动生成

1.为button模板增加触发器:

<ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" TargetName="border">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource mouseOverColorConverter}">
                            <Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="MouseOverBackground" ></Binding>
                            <Binding RelativeSource="{RelativeSource  Mode=TemplatedParent}" Path="Background" ></Binding>
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Background" TargetName="border" >
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource pressColorConverter}">
                            <Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path="PressedBackground" ></Binding>
                            <Binding RelativeSource="{RelativeSource  Mode=TemplatedParent}" Path="Background" ></Binding>
                        </MultiBinding>
                    </Setter.Value>
                </Setter> 
            </Trigger>
</ControlTemplate.Triggers>

2.增加多值转换器:

public class PressColorConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if ((values[0] as SolidColorBrush).Color == Colors.Blue)//默认蓝色则自动生成
            {
                var c = System.Drawing.ColorTranslator.FromHtml(values[1].ToString());
                var c1 = StaticFunc.ChangeColor(c, -0.2f);
                return new SolidColorBrush(Color.FromArgb(c1.A, c1.R, c1.G, c1.B));
            }
            else
            {
                return values[0];
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class MouseOverColorConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if ((values[0] as SolidColorBrush).Color == Colors.Blue)
            {
                var c = System.Drawing.ColorTranslator.FromHtml(values[1].ToString());
                var c1 = StaticFunc.ChangeColor(c, -0.1f);
                return new SolidColorBrush(Color.FromArgb(c1.A, c1.R, c1.G, c1.B));
            }
            else
            {
                return values[0];
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

3.颜色转换函数:

 public class StaticFunc
    {
        public static Color ChangeColor(Color color, float correctionFactor)
        {
            float red = (float)color.R;
            float green = (float)color.G;
            float blue = (float)color.B;
            if (correctionFactor < 0)
            {
                correctionFactor = 1 + correctionFactor;
                red *= correctionFactor;
                green *= correctionFactor;
                blue *= correctionFactor;
            }
            else
            {
                red = (255 - red) * correctionFactor + red;
                green = (255 - green) * correctionFactor + green;
                blue = (255 - blue) * correctionFactor + blue;
            }
            if (red < 0) red = 0;
            if (red > 255) red = 255;
            if (green < 0) green = 0;
            if (green > 255) green = 255;
            if (blue < 0) blue = 0;
            if (blue > 255) blue = 255;
            return Color.FromArgb(color.A, (int)red, (int)green, (int)blue);
        }
    }

 

以上是关于C#中怎样设置Button控件的按下和弹起事件的主要内容,如果未能解决你的问题,请参考以下文章

如何用Arduino检测长按按键的按下和弹起,包括去抖动

如何编写button按下和弹起两种状态的函数

菜鸟刚刚学android。怎么让android分别响应按下和弹起的事件?跪求高手~~~~

[Python自学] PyQT5-QPushButtonQRadioButtonQCheckBoxQComboBox控件

MFC按钮控件按下和弹起的消息函数分别是啥啊

delphi button如何实现按键按下暂停 再按下启动