c#在表单内创建进程并监视活动

时间:2015-10-09 15:06:30

标签: c# process

我需要运行一个程序,如果程序超过20分钟未使用,请将其关闭。 我的主要想法是在c#窗口应用程序表单中创建一个进程,并使用该表单(de)激活,以告知应用程序何时不在焦点。 不幸的是,当内部进程被聚焦时(例如:在Windows窗体内运行notepad.exe),该表单被认为是不活动的。

我的代码在这里:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace testing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool GetWindowRect(IntPtr hWnd, ref RECT Rect);
        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        private static System.Timers.Timer aTimer;
        private void Form1_Load(object sender, EventArgs e)
        {
            Process p = new Process();
        p.StartInfo.FileName = "notepad.exe";
            p.Exited += new EventHandler(myProcess_Exited);
            p.EnableRaisingEvents = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
            p.Start();
            IntPtr handle = p.MainWindowHandle;
            RECT pRect = d ;
            Size cSize = new Size();
            GetWindowRect(handle, ref pRect);
            cSize.Width = pRect.right - pRect.left;
            cSize.Height = pRect.bottom - pRect.top;

            p.WaitForInputIdle();
            SetParent(p.MainWindowHandle, this.Handle);
            aTimer = new System.Timers.Timer();
            aTimer.Interval = 7000;
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = false;

            MessageBox.Show("You are in the Form.Deactivate event.");
            this.Activated += Form1_Activated;
            this.Deactivate += Form1_Deactivate;
        }
        private void myProcess_Exited(object sender, System.EventArgs e)
        {
            Application.Exit();
        }
        private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
        {
            MessageBox.Show("You are in the Form.Deactivate event.");
        }
        private void Form1_Deactivate(Object sender, EventArgs e)
        {
            aTimer.Enabled = true;
            //MessageBox.Show("You are in the Form.Deactivate event.");

        }
        private void Form1_Activated(object sender, System.EventArgs e)
        {
            aTimer.Enabled = false;
            // MessageBox.Show("hiya .");
        }
    }

}

有人可以指出如何做到这一点(例如:将流程封闭在面板中,并使用面板激活或停用?)

0 个答案:

没有答案