示例#1
0
        // 托盘菜单 新监控项
        private void notifyNewmonitor_Click(object sender, EventArgs e)
        {
            newMonitor newWindow = new newMonitor();

            newWindow.ShowDialog();
            MonitorNode monitorNode = new MonitorNode();

            if (newWindow.monitoredProcessName == null)
            {
                return;
            }

            //System.Threading.ParameterizedThreadStart ts = new System.Threading.ParameterizedThreadStart(monitorThread);        // 主窗口启动后台扫描进程
            //System.Threading.Thread thread = new System.Threading.Thread(ts);
            //thread.Start(newWindow.monitoredProcessName);                       // 启动监视线程

            // 加入combox选项
            monitorNode.ProcessName   = newWindow.monitoredProcessName;
            monitorNode.ProcessIdList = new List <int>();
            monitorNode.ProcessIdList.Clear();

            ComboBoxItem item = new ComboBoxItem();

            item.Content = newWindow.monitoredProcessName;
            notifyShowBalloomTip("start monitor", "target:" + item.Content.ToString() + ".exe", 0);
            this.detailProcessName.Items.Add(item);

            // 加入monitorlist
            lock (monitorlist)
            {
                monitorlist.Add(monitorNode);
            }

            return;
        }
示例#2
0
        // 窗口启动监视线程 知道监听到 我们才启动UI线程 来修改界面元素
        public MainWindow()
        {
            this.Visibility = Visibility.Hidden;      // 初始化不可见
            InitializeComponent();

            ulong         monitorProcessNumber = 0;
            StringBuilder monitorProcess       = new StringBuilder(255);
            MonitorNode   monitorNode          = new MonitorNode();

            // 初始化INI文件目录
            inifilePath = System.Environment.CurrentDirectory + "\\config.ini";

            // 记录文件夹
            recordDirectory  = System.Environment.CurrentDirectory;
            recordDirectory += "\\record";      // 注意最后没有\\

            resourceDirectory  = System.Environment.CurrentDirectory;
            resourceDirectory += "\\resource";

            if (!IsDirectoryExists(recordDirectory))        // 记录文件夹不存在 创建文件夹
            {
                Directory.CreateDirectory(recordDirectory);
            }

            // 读取要监视的进程名 --- 读取ini文件 monitor节
            if (IsFileExists(inifilePath))  // 判断监视文件存在吗
            {
                // 读取 number节的值
                monitorProcessNumber = GetPrivateProfileInt("monitor", "number", 0, inifilePath);

                // 遍历 数字节 得到各个监控进程名 不包含extension
                for (ulong i = 1; i <= monitorProcessNumber; i++)
                {
                    GetPrivateProfileString("monitor", i.ToString(), null, monitorProcess, 255, inifilePath);
                    monitorNode.ProcessName   = monitorProcess.ToString();
                    monitorNode.ProcessIdList = new List <int>();
                    monitorNode.ProcessIdList.Clear();

                    lock (monitorlist)                // 锁上monitor
                    {
                        monitorlist.Add(monitorNode); // 添加到monitorlist
                    }

                    ThreadPool.QueueUserWorkItem(monitorManagerThread);
                }
            }

            // 设置托盘
            notifyIcon             = new NotifyIcon();
            notifyIcon.Text        = "monitor"; // 停留显示
            notifyIcon.Icon        = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
            notifyIcon.Visible     = true;
            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notify_MouseClick);

            // 右键菜单构造和处理函数添加
            System.Windows.Forms.MenuItem menuItem1 = new System.Windows.Forms.MenuItem("new monitor");
            menuItem1.Click += new EventHandler(notifyNewmonitor_Click);
            System.Windows.Forms.MenuItem menuItem2 = new System.Windows.Forms.MenuItem("detail dynamics");
            menuItem2.Click += new EventHandler(notifyDynamics_Click);
            System.Windows.Forms.MenuItem menuItem3 = new System.Windows.Forms.MenuItem("exit");
            menuItem3.Click += new EventHandler(notifyExit_Click);

            // 菜单和托盘绑定
            System.Windows.Forms.MenuItem[] notifyMenu = new System.Windows.Forms.MenuItem[] { menuItem1, menuItem2, menuItem3 };
            notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(notifyMenu);

            // 读取ini文件 config 设置 setting 下各个设置的状态

            // 读取开启启动项的状态
            ulong bStartInBoot = GetPrivateProfileInt("config", "StartInBoot", 0, inifilePath);

            if (bStartInBoot == 0)
            {
                this.IsStartInBoot.IsChecked = false;
            }
            else
            {
                this.IsStartInBoot.IsChecked = true;
            }
        }