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

问题描述

我开发了一个简单的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

推荐答案


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.");
            }
        }
    }
}



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

09-12 22:50