C#如何判断WinForm是否有焦点? [英] C# How To Tell If A WinForm Has Focus Or Not?

查看:196
本文介绍了C#如何判断WinForm是否有焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想知道如何判断鼠标是否悬停在winform应用程序上。

i认为我必须挂钩到Gotfocus,LostFocus,MouseHover或者MouseEnter Events

这是我到目前为止所得到的:



Hello all, i would like to know how to tell whither or not the mouse is hovering over a winform app.
i think i have to hook in to either the Gotfocus, LostFocus, MouseHover or the MouseEnter Events
here is what i got so far:

this.MouseEnter += new System.EventHandler(this.Form1_MouseHover);

public void Form1_MouseHover(object sender, System.EventArgs e) 
{
    this.Opacity = 0.5;
}



我想要做的是确定鼠标是否悬停在表单上,​​如果是,则将表单Opacity设置为1.0但是如果然后它不会将不透明度设置为0.5

我该如何做?

我还能如何平滑地设置不透明度值1.0和0.5之间的过渡?

感谢你提前的所有帮助,

MasterCodeon


what i want to do is determine whither or not the mouse is hovering over the form and if it is then set the form Opacity to 1.0 but if it isn't then set the Opacity to 0.5
how would i do this?
also how would i am smoothly animate the transitions between the Opacity values of 1.0 and 0.5?
thanks for all of your help in advance,
MasterCodeon

推荐答案

嗨!



这很简单。要为转换设置动画,可以使用Timer组件。示例代码:



Hi!

This is very simple. To animate transitions you can use Timer component. Sample code:

public partial class Form1 : Form
{
    private const double OPACITY_MIN = 0.5;
    private const double OPACITY_MAX = 1.0;
    private const double OPACITY_STEP = 0.05;   // Step for changing opacity value
    private const int TIMER_INTERVAL = 100;     // Interval in miliseconds (1/1000 of second)

    private Timer _timer;                       // Timer control
    private bool _fading = false;               // Opacity mode: fading or showing

    public Form1()
    {
        InitializeComponent();
        
        // Configure time, but don't start yet
        _timer = new Timer();
        _timer.Interval = TIMER_INTERVAL;       // Set interval
        _timer.Tick += _timer_Tick;             // Set timer Tick event handler
    }

    // This method is executed every TIMER_INTERVAL miliseconds is Timer is enabled
    private void _timer_Tick(object sender, EventArgs e)
    {
        // Decrease or increase value of Form's opacity property by OPACITY_STEP in every timer Tick
        // and stop the timer if MIN or MAX opacity value was reached
        if (_fading)
        {
            if (this.Opacity > OPACITY_MIN)
            {
                this.Opacity -= OPACITY_STEP;
            }
            else
            {
                this.Opacity = OPACITY_MIN;
                _timer.Enabled = false;
            }
        }
        else
        {
            if (this.Opacity < OPACITY_MAX)
            {
                this.Opacity += OPACITY_STEP;
            }
            else
            {
                this.Opacity = OPACITY_MAX;
                _timer.Enabled = false;
            }
        }
    }

    private void Form1_MouseLeave(object sender, EventArgs e)
    {
        // When mouse is leaving form we are setting mode to Fading and enabling timer
        _fading = true;
        _timer.Enabled = true;
    }

    private void Form1_MouseEnter(object sender, EventArgs e)
    {
        // When mouse is entering form we are setting mode to Showing and enabling timer
        _fading = false;
        _timer.Enabled = true;
    }
}





您可以用同样的方式处理其他Form的事件。但是你应该记住,如果你放置了Panel控件并将Dock属性设置为Dock.Fill这段代码将不起作用。你将不得不处理Panel的事件(进入和离开)。



http://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.110%29 .aspx [ ^ ]



[Bill Woodruff的评论更新]

MouseHover事件不是鼠标进入表格时立即开枪。根据MSDN文档:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover%28v=vs.110%29.aspx [ ^ ]



在该事件被解雇之前有一段时间:

http://msdn.microsoft.com/en-us/library/system。 windows.forms.systeminformation.mousehovertime%28v = vs.110%29.aspx [ ^ ]

以及鼠标指针必须留在的指定区域在事件被触发之前:
http ://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.mousehoversize%28v=vs.110%29.aspx [ ^ ]



因此,使用MouseEnter和MouseLeave是首选。第二个选择应该是Form的Activated和Deactivated事件,当您切换到另一个窗口/应用程序时会被触发。

- 结束更新 -



[更新 - 对于MasterCodeon的评论]

我承认没有测试此解决方案的控件放在窗体上。在那种情况下,即使鼠标光标将进入/离开窗体上的控件,Form的MouseEnter和MouseLeave也会被触发。

所以,我已经完全基于Timer找到了解决这个问题的方法。基本上我摆脱了MouseEnter和MouseLeave事件处理程序,并添加了检查(有些间隔)光标的位置在Form的边界内。

另外我添加了一个选项(SMOOTH_SHOWING)来设置表单的行为方式光标进入表单时出现。如果设置为yes,则表单将平滑显示(如淡入淡出),如果没有则表单将立即显示。以下是我的代码:



You can handle other Form's events in the same way. But you should remember that if you place i.e. Panel control and set Dock property to Dock.Fill this code will not work. You will have to handle Panel's events (Enter and Leave).

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer%28v=vs.110%29.aspx[^]

[Update for Bill Woodruff's comment]
MouseHover event isn't fired immediatelly when mouse enters the form. According to MSDN documentation:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover%28v=vs.110%29.aspx[^]

there is some amount of time before that event is fired:
http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.mousehovertime%28v=vs.110%29.aspx[^]
and specified area within which the mouse pointer has to stay before event is fired:
http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.mousehoversize%28v=vs.110%29.aspx[^]

So, using MouseEnter and MouseLeave is first choice. The second choice should be Form's Activated and Deactivated events which are fired when you i.e. switch to/from another window/app.
-- End update --

[Update - For MasterCodeon's comment]
I admit that didn't test this solution with controls placed on form. In that situation Form's MouseEnter and MouseLeave will be fired even when mouse cursor will enter/leave controls located on form.
So, I have found workaround to this problem entirely based on Timer. Basically I got rid of MouseEnter and MouseLeave event handlers and added checking (for some interval) that cursor's position is within Form's bounds.
Additionally I've added an option (SMOOTH_SHOWING) for setting behaviour how form will appear when cursor enters the form. If set to yes, then form will be shown smoothly (like in fading), if no then form will appear immediatelly. Below is my code:

public partial class Form1 : Form
{
    private const double OPACITY_MIN = 0.5;
    private const double OPACITY_MAX = 1.00;
    private const double OPACITY_STEP = 0.05;
    private const bool SMOOTH_SHOWING = true;   // If True then form will appear smoothly otherwise it will be shown immediatelly
    private const int TIMER_INTERVAL = 100;

    private Timer _timer;

    public Form1()
    {
        InitializeComponent();

        _timer = new Timer();
        _timer.Interval = TIMER_INTERVAL;
        _timer.Tick += _timer_Tick;
        _timer.Enabled = true;
    }

    void _timer_Tick(object sender, EventArgs e)
    {
        var inside = this.Bounds.Contains(Cursor.Position);
        if (inside) // Showing
        {
            if (this.Opacity < OPACITY_MAX)
            {
                if (SMOOTH_SHOWING)
                {
                    this.Opacity += OPACITY_STEP;
                }
                else
                {
                    this.Opacity = OPACITY_MAX;
                }
            }

        }
        else        // Fading
        {
            if (this.Opacity > OPACITY_MIN)
            {
                this.Opacity -= OPACITY_STEP;
            }
        }
    }
}



- 结束更新---



我希望它可以帮到你。


-- End update ---

I hope it help you.


这篇关于C#如何判断WinForm是否有焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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