且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取所有正在运行的窗口表单应用程序的进程ID

更新时间:2022-11-06 17:34:01

您不需要进程ID-您需要窗口句柄:
You don''t need process IDs - you need window handles:
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(String sClassName, String sAppName);
[DllImport("user32")]
private const int SW_SHOWNORMAL    = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private static extern int ShowWindow(int hwnd, int nCmdShow);
private void butMakeNotePadFullScreen_Click(object sender, EventArgs e)
    {
    IntPtr hWnd = FindWindow(null, "Untitled - Notepad");
    ShowWindow((int) hWnd, SW_SHOWMAXIMIZED);
    }


尝试一下!


try this!


using System.Diagnostics;

Process[] processlist = Process.GetProcesses();

foreach(Process theprocess in processlist){
// u can access each process name // theprocess.ProcessName;
//process id// theprocess.Id;

//and an external window can maximized using

[DllImport("user32.dll")]
      private static extern bool IsIconic(IntPtr hWnd);
      [DllImport("user32.dll")]
      private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
      [DllImport("user32.dll")]
      private static extern int SetForegroundWindow (IntPtr hWnd);
      private const int SW_RESTORE = 9;
      private void ShowPID(int pId)///give your selected processid here
      {
          IntPtr hWnd = System.Diagnostics.Process.GetProcessById(pId).MainWindowHandle;
          if (!hWnd.Equals(IntPtr.Zero))
          {
              if (IsIconic(hWnd))
              {
                  ShowWindow(hWnd, SW_RESTORE);
              }
              SetForegroundWindow(hWnd);
          }
      }


这与ID无关.这是非常糟糕的设计.您有所有应用程序的源代码吗?如果是这样,您确实需要将它们重新处理到一个应用程序中,以便在一个进程中运行.

如果没有,那就不好了.您可以使用System.Diagnostics.Process.Start在项目中运行外部"或子"应用程序.该调用将返回子进程的Process实例.片刻之后(这已经是一个问题,您可能需要一些延迟),您可以通过Process.MainWindowHandle获取子进程主窗口的HWND.

之后,使用Windows API函数的P/Invoke与HWND一起操作:SetForegroundWindowSetActiceWindowSetWindowsPlacementSetWindowsPos.请参阅 http://msdn.microsoft.com/en-us/library/ms632595(v = vs.85).aspx [
This is not about IDs. This is just incredibly poor design. Do you have source code for all applications? If so you really need to re-work them into one application to be run in one process.

If not, it''s not good. You can run "foreign" or "child" applications in your project using System.Diagnostics.Process.Start. The call will return the Process instance of the child process. After a while (this is already a problem, you may need some delay) you can get the HWND of the main window of the child process via Process.MainWindowHandle.

After that, use P/Invoke of Windows API functions to operate with HWND: SetForegroundWindow, SetActiceWindow, SetWindowsPlacement, SetWindowsPos. See http://msdn.microsoft.com/en-us/library/ms632595(v=vs.85).aspx[^].

—SA