C#Winforms:以编程方式显示Button的悬停状态 [英] C# Winforms: programatically display Button's Hover State

查看:193
本文介绍了C#Winforms:以编程方式显示Button的悬停状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Winform上显示数字小键盘以输入代码.我正在显示带有按钮的nummpad ...用户将仅使用键盘numpad来输入代码\密码\?但是偏航时,您可以使用鼠标...

如果我们使用鼠标单击按钮,则会显示蓝色效果,以显示悬停&跌落状态..

我在想我是否可以根据用户在数字键盘上按下的键以某种方式以编程方式显示按钮的按下状态 ... 操作方法

解决方案

不可能通过使用按钮类的默认实现来模拟此行为.但是,您可以对按钮进行子类化以添加此行为:

   public class KeyboardButton : Button
    {
        public void SimulateButtonDown()
        {
            this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 0, 1, 1, 0));
        }

        public void SimulateButtonUp()
        {
            this.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 0, 1, 1, 0));
        }
    }

当您调用SimulateButtonDown时,该按钮将进入(可视)状态,仿佛将在该按钮上单击(并按住)鼠标.您也可以为悬停事件实现此类方法.

i am displaying a numeral keypad in on a winform to enter code. i am displaying nummpad with buttons... The users will be using only keyboards numpad to enter the code\password\? but off-course you can use mouse...

If we use mouse to click button we get a blue-ish effect to display hover & down states..

i was thinking if i can somehow programatically display the down-state of a button according to he key that user pressed on the numpad... HOW TO

解决方案

It is not possible to simulate this behavior by using the default implementation of the button class. However, you can sub class the button to add this behavior:

   public class KeyboardButton : Button
    {
        public void SimulateButtonDown()
        {
            this.OnMouseDown(new MouseEventArgs(MouseButtons.Left, 0, 1, 1, 0));
        }

        public void SimulateButtonUp()
        {
            this.OnMouseUp(new MouseEventArgs(MouseButtons.Left, 0, 1, 1, 0));
        }
    }

When you call SimulateButtonDown the button will go into a (visual) state imitating that mouse would have been clicked (and held) on the button. You can implement methods like these for hover events as well.

这篇关于C#Winforms:以编程方式显示Button的悬停状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆