获取所有正在运行的窗口表单应用程序的进程ID [英] get process ids of all running window form applications

查看:101
本文介绍了获取所有正在运行的窗口表单应用程序的进程ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分配,其中有3个窗口窗体应用程序,并且所有应用程序都处于运行模式.屏幕上的第一个应用程序处于活动状态,其他两个应用程序被最小化.我在first(active)应用程序中有一个包含列表框的表单.此列表框包含其他两个应用程序的名称.
任务是:如果我双击列表框中的任何名称(表示第二个或第三个窗口表单应用程序的名称),则该特定的窗口表单应用程序应将其状态从最小化状态切换为活动状态.意味着窗口窗体应用程序现在应该出现在屏幕上.
我想为此,我需要其他两个(第二个和第三个窗口窗体应用程序)的进程ID.如果是这样,那我该怎么做.如果没有,请给我建议其他解决方案.
请帮助紧急处理.

I have an assignment in which I have 3 window form applications and all in running mode. First application is active on screen and other two are minimized. i have a form in first(active) application which contains a listbox. This listbox contains names of other two applications.
The task is : if I double click on any name(means name of second or third window form application) in the listbox, that particular window form application should switch its state from minimized state to active. Means that window form application should now appear on the screen.
i think for this I need to have process ids of the other two(second and third window form applications).if it is so then how do i do that. if not then please suggest me some other solution.
Please help its urgent.

推荐答案

您不需要进程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


这篇关于获取所有正在运行的窗口表单应用程序的进程ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆