Exemplo n.º 1
0
        private void menuProcess_Popup(object sender, EventArgs e)
        {
            virtualizationProcessMenuItem.Checked = false;

            // Menu item fixup...
            if (treeProcesses.SelectedTreeNodes.Count == 0)
            {
                // If nothing is selected, disable everything.
                // The Select All menu item will be enabled later if 
                // we have at least one process in the tree.
                menuProcess.DisableAll();
            }
            else if (treeProcesses.SelectedTreeNodes.Count == 1)
            {
                // All actions should work with one process selected.
                menuProcess.EnableAll();

                // Singular nouns.
                priorityMenuItem.Text = "&Priority";
                terminateMenuItem.Text = "&Terminate Process";
                suspendMenuItem.Text = "&Suspend Process";
                resumeMenuItem.Text = "&Resume Process";

                // Check the appropriate priority level menu item.
                realTimeMenuItem.Checked = false;
                highMenuItem.Checked = false;
                aboveNormalMenuItem.Checked = false;
                normalMenuItem.Checked = false;
                belowNormalMenuItem.Checked = false;
                idleMenuItem.Checked = false;

                try
                {
                    using (var phandle = new ProcessHandle(processSelectedPid, Program.MinProcessQueryRights))
                    {
                        switch (phandle.GetPriorityClass())
                        {
                            case ProcessPriorityClass.RealTime:
                                realTimeMenuItem.Checked = true;
                                break;

                            case ProcessPriorityClass.High:
                                highMenuItem.Checked = true;
                                break;

                            case ProcessPriorityClass.AboveNormal:
                                aboveNormalMenuItem.Checked = true;
                                break;

                            case ProcessPriorityClass.Normal:
                                normalMenuItem.Checked = true;
                                break;

                            case ProcessPriorityClass.BelowNormal:
                                belowNormalMenuItem.Checked = true;
                                break;

                            case ProcessPriorityClass.Idle:
                                idleMenuItem.Checked = true;
                                break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    priorityMenuItem.Text = "(" + ex.Message + ")";
                    priorityMenuItem.Enabled = false;
                }

                // Check the virtualization menu item.
                try
                {
                    using (var phandle = new ProcessHandle(processSelectedPid, Program.MinProcessQueryRights))
                    {
                        try
                        {
                            using (var thandle = phandle.GetToken(TokenAccess.Query))
                            {
                                if (virtualizationProcessMenuItem.Enabled = thandle.IsVirtualizationAllowed())
                                    virtualizationProcessMenuItem.Checked = thandle.IsVirtualizationEnabled();
                            }
                        }
                        catch
                        { }
                    }
                }
                catch
                {
                    virtualizationProcessMenuItem.Enabled = false;
                }

                // Enable/disable DLL injection based on the process' session ID. This only applies 
                // on XP and above.
                try
                {
                    if (
                        OSVersion.IsBelowOrEqual(WindowsVersion.XP) &&
                        processP.Dictionary[processSelectedPid].SessionId != Program.CurrentSessionId
                        )
                        injectDllProcessMenuItem.Enabled = false;
                    else
                        injectDllProcessMenuItem.Enabled = true;
                }
                catch (Exception ex)
                {
                    Logging.Log(ex);
                }

                // Disable Terminate Process Tree if the selected process doesn't 
                // have any children. Note that this may also happen if the user 
                // is sorting the list (!).
                try
                {
                    if (treeProcesses.SelectedTreeNodes[0].IsLeaf &&
                        (treeProcesses.Tree.Model as ProcessTreeModel).GetSortColumn() == "")
                        terminateProcessTreeMenuItem.Visible = false;
                    else
                        terminateProcessTreeMenuItem.Visible = true;
                }
                catch (Exception ex)
                {
                    Logging.Log(ex);
                }

                // Find the process' window (if any).
                windowHandle = WindowHandle.Zero;
                WindowHandle.Enumerate(
                    (handle) =>
                    {
                        // GetWindowLong
                        // Shell_TrayWnd
                        if (handle.IsWindow() && handle.IsVisible() && handle.IsParent())
                        {
                            int pid;
                            Win32.GetWindowThreadProcessId(handle, out pid);

                            if (pid == processSelectedPid)
                            {
                                windowHandle = handle;
                                return false;
                            }
                        }
                        return true;
                    });

                // Enable the Window submenu if we found window owned 
                // by the process. Otherwise, disable the submenu.
                if (windowHandle.IsInvalid)
                {
                    windowProcessMenuItem.Enabled = false;
                }
                else
                {
                    windowProcessMenuItem.Enabled = true;
                    windowProcessMenuItem.EnableAll();

                    switch (windowHandle.GetPlacement().ShowState)
                    {
                        case ShowWindowType.ShowMinimized:
                            minimizeProcessMenuItem.Enabled = false;                          
                            break;

                        case ShowWindowType.ShowMaximized:
                            maximizeProcessMenuItem.Enabled = false;                       
                            break;

                        case ShowWindowType.ShowNormal:
                            restoreProcessMenuItem.Enabled = false; 
                            break;                       
                    }
                }
            }
            else
            {
                // Assume most process actions will not work with more than one process.
                menuProcess.DisableAll();

                // Use plural nouns.
                terminateMenuItem.Text = "&Terminate Processes";
                suspendMenuItem.Text = "&Suspend Processes";
                resumeMenuItem.Text = "&Resume Processes";

                // Enable a specific set of actions.
                terminateMenuItem.Enabled = true;
                suspendMenuItem.Enabled = true;
                resumeMenuItem.Enabled = true;
                reduceWorkingSetProcessMenuItem.Enabled = true;
                copyProcessMenuItem.Enabled = true;
            }

            // Special case for invalid PIDs.
            if (processSelectedPid <= 0 && treeProcesses.SelectedNodes.Count == 1)
            {
                priorityMenuItem.Text = "&Priority";
                menuProcess.DisableAll();
                propertiesProcessMenuItem.Enabled = true;
            }

            // Enable/disable the Select All menu item.
            if (treeProcesses.Model.Nodes.Count == 0)
            {
                selectAllProcessMenuItem.Enabled = false;
            }
            else
            {
                selectAllProcessMenuItem.Enabled = true;
            }
        }
Exemplo n.º 2
0
 private void menuProcess_Popup(object sender, EventArgs e)
 {
     virtualizationProcessMenuItem.Checked = false;
     if (treeProcesses.SelectedTreeNodes.Count == 0)
     {
         menuProcess.DisableAll();
     }
     else if (treeProcesses.SelectedTreeNodes.Count == 1)
     {
         menuProcess.EnableAll();
         priorityMenuItem.Text = "&Priority";
         terminateMenuItem.Text = "&Terminate Process";
         suspendMenuItem.Text = "&Suspend Process";
         resumeMenuItem.Text = "&Resume Process";
         realTimeMenuItem.Checked = false;
         highMenuItem.Checked = false;
         aboveNormalMenuItem.Checked = false;
         normalMenuItem.Checked = false;
         belowNormalMenuItem.Checked = false;
         idleMenuItem.Checked = false;
         try
         {
             using (var phandle = new ProcessHandle(processSelectedPid, Program.MinProcessQueryRights))
             {
                 switch (phandle.GetPriorityClass())
                 {
                     case ProcessPriorityClass.RealTime:
                         realTimeMenuItem.Checked = true;
                         break;
                     case ProcessPriorityClass.High:
                         highMenuItem.Checked = true;
                         break;
                     case ProcessPriorityClass.AboveNormal:
                         aboveNormalMenuItem.Checked = true;
                         break;
                     case ProcessPriorityClass.Normal:
                         normalMenuItem.Checked = true;
                         break;
                     case ProcessPriorityClass.BelowNormal:
                         belowNormalMenuItem.Checked = true;
                         break;
                     case ProcessPriorityClass.Idle:
                         idleMenuItem.Checked = true;
                         break;
                 }
             }
         }
         catch (Exception ex)
         {
             priorityMenuItem.Text = "(" + ex.Message + ")";
             priorityMenuItem.Enabled = false;
         }
         try
         {
             using (var phandle = new ProcessHandle(processSelectedPid, Program.MinProcessQueryRights))
             {
                 try
                 {
                     using (var thandle = phandle.GetToken(TokenAccess.Query))
                     {
                         if (virtualizationProcessMenuItem.Enabled = thandle.IsVirtualizationAllowed())
                             virtualizationProcessMenuItem.Checked = thandle.IsVirtualizationEnabled();
                     }
                 }
                 catch
                 { }
             }
         }
         catch
         {
             virtualizationProcessMenuItem.Enabled = false;
         }
         try
         {
             if (
                 OSVersion.IsBelowOrEqual(WindowsVersion.XP) &&
                 processP.Dictionary[processSelectedPid].SessionId != Program.CurrentSessionId
                 )
                 injectDllProcessMenuItem.Enabled = false;
             else
                 injectDllProcessMenuItem.Enabled = true;
         }
         catch (Exception ex)
         {
             Logging.Log(ex);
         }
         try
         {
             if (treeProcesses.SelectedTreeNodes[0].IsLeaf &&
                 (treeProcesses.Tree.Model as ProcessTreeModel).GetSortColumn() == "")
                 terminateProcessTreeMenuItem.Visible = false;
             else
                 terminateProcessTreeMenuItem.Visible = true;
         }
         catch (Exception ex)
         {
             Logging.Log(ex);
         }
         windowHandle = WindowHandle.Zero;
         WindowHandle.Enumerate(
             (handle) =>
             {
                 if (handle.IsWindow() && handle.IsVisible() && handle.IsParent())
                 {
                     int pid;
                     Win32.GetWindowThreadProcessId(handle, out pid);
                     if (pid == processSelectedPid)
                     {
                         windowHandle = handle;
                         return false;
                     }
                 }
                 return true;
             });
         if (windowHandle.IsInvalid)
         {
             windowProcessMenuItem.Enabled = false;
         }
         else
         {
             windowProcessMenuItem.Enabled = true;
             windowProcessMenuItem.EnableAll();
             switch (windowHandle.GetPlacement().ShowState)
             {
                 case ShowWindowType.ShowMinimized:
                     minimizeProcessMenuItem.Enabled = false;
                     break;
                 case ShowWindowType.ShowMaximized:
                     maximizeProcessMenuItem.Enabled = false;
                     break;
                 case ShowWindowType.ShowNormal:
                     restoreProcessMenuItem.Enabled = false;
                     break;
             }
         }
     }
     else
     {
         menuProcess.DisableAll();
         terminateMenuItem.Text = "&Terminate Processes";
         suspendMenuItem.Text = "&Suspend Processes";
         resumeMenuItem.Text = "&Resume Processes";
         terminateMenuItem.Enabled = true;
         suspendMenuItem.Enabled = true;
         resumeMenuItem.Enabled = true;
         reduceWorkingSetProcessMenuItem.Enabled = true;
         copyProcessMenuItem.Enabled = true;
     }
     if (processSelectedPid <= 0 && treeProcesses.SelectedNodes.Count == 1)
     {
         priorityMenuItem.Text = "&Priority";
         menuProcess.DisableAll();
         propertiesProcessMenuItem.Enabled = true;
     }
     if (treeProcesses.Model.Nodes.Count == 0)
     {
         selectAllProcessMenuItem.Enabled = false;
     }
     else
     {
         selectAllProcessMenuItem.Enabled = true;
     }
 }