Click here to Skip to main content
15,939,736 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 1-May-11 22:29pm
v2

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);
    }
 
Share this answer
 
Comments
Olivier Levrey 2-May-11 8:06am    
Best answer here. Have my 5.
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);
          }
      }
 
Share this answer
 
v2
Comments
LaxmikantYadav 2-May-11 4:56am    
My +5, Exactaly what OP is required.
Olivier Levrey 2-May-11 8:08am    
This works but is not easy to use since you need the process ID to show the form. OriginalGriff's answer is the best approach.
Varshney Manish 3-May-11 1:12am    
Thanks a lot , its working fine.
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900