如何知道我的表格失去焦点 [英] How to know my form lost focus

查看:145
本文介绍了如何知道我的表格失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个简单的C#Windows窗体应用程序,当用户切换到其他应用程序(例如IE)时,我想更改窗体背景色提示失去焦点,我使用了form_leave事件,它不起作用,怎么办?谢谢

I develop a simple C# windows form application, When user switch to other application, Example IE, I want to change my form backcolor prompt lost focus, I use form_leave event, It does not work, How to do it? Thanks

推荐答案

This[^] event might help you.


以下是使用Process.GetProcessesByName()和GetForegroundWindow()API的一种方法:

Here is an approach using Process.GetProcessesByName() and the GetForegroundWindow() API:

using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        private void button1_Click(object sender, EventArgs e)
        {
            Boolean focused = false;
            Process[] p = Process.GetProcessesByName("karafun");
            if (p.Length > 0)
            {
                if (p[0].MainWindowHandle.Equals(GetForegroundWindow()))
                {
                    focused = true;
                }
            }
            if (focused)
            {
                MessageBox.Show("KaraFun IS in focus.");
            }
            else
            {
                MessageBox.Show("KaraFun is NOT in focus.");
            }
        }
    }
}


感谢您的答复,但以上两种方法都无法解决我的问题! :(
Thanks for reply, but it does not solve my issue for the above two method! :(


这篇关于如何知道我的表格失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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