Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

I'm developing an application in c#.

I want give focus to process, like application, running on system tray.
I want to restore the application to open in the main form.

How can I achieve this?


thanks
Posted
Updated 25-May-11 21:50pm
v2
Comments
Dalek Dave 26-May-11 3:50am    
Edited for Grammar, Spelling and Readability.

1 solution

Your question is not very clear but what I understand is that, you wish to focus a running process from your application.

For that below is the code which take application name as input:
C#
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace WindowsApplication1 {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

    private void button1_Click(object sender, EventArgs e) {
      Process[] procs = Process.GetProcessesByName("notepad"); //app name here
      if (procs.Length > 0) {
        // Which one?  I'll just pick the first
        if (procs[0].MainWindowHandle != IntPtr.Zero)
          SetForegroundWindow(procs[0].MainWindowHandle);
      }
    }
  }
}


Source:
http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/6573bf2a-41a1-46d2-a1c5-797c8e0bd781/[^]
 
Share this answer
 
Comments
amitthakkar1987 26-May-11 3:26am    
i m using this code....but this is not working
Wild-Programmer 27-May-11 0:11am    
What error do you get?
Dalek Dave 26-May-11 3:51am    
Good Guess
Wild-Programmer 27-May-11 0:10am    
Thanks Dalek :)

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