C#怎么判断系统的焦点现在在哪个程序上

解决方案 »

  1.   

    The GetForegroundWindow function returns a handle to the foreground window (the window with which the user is currently working). The system assigns a slightly higher priority to the thread that creates the foreground window than it does to other threads. SyntaxHWND GetForegroundWindow(VOID);The GetActiveWindow function retrieves the window handle to the active window attached to the calling thread's message queue. SyntaxHWND GetActiveWindow(VOID);
      

  2.   

      private void timer1_Tick(object sender, EventArgs e)
            {
                IntPtr _Foreground=GetForegroundWindow();
                int _ProcessID=0;
                GetWindowThreadProcessId(_Foreground, ref _ProcessID);
                System.Diagnostics.Process _Process = System.Diagnostics.Process.GetProcessById(_ProcessID);
                if (_Process == null) return;
                this.Text = _Process.ProcessName +"{"+_Process.MainWindowTitle+"}";
            }        /// <summary>
            /// 获得当前活动窗体句柄
            /// </summary>
            /// <returns></returns>
            [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
            public static extern IntPtr GetForegroundWindow();
            /// <summary>
            /// 根据句柄获取进程PID
            /// </summary>
            /// <param name="hwnd">句饼</param>
            /// <param name="lpdwProcessId">引用 返回进程PID</param>
            /// <returns>未知</returns>
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindowThreadProcessId(IntPtr hwnd, ref int lpdwProcessId);
    看看行不行~~放个时间控件