Share via


Get programs currently present in the taskbar...

Question

Thursday, December 23, 2010 11:37 AM

Hi,

In my application i want to know the list of items present in the taskbar.

How i can get the list of items and show them in the listbox using there names...?

After the items are shown... if user selects any item in the listbox then i want that application to be shown(bring to front).

How i can do this using C#...? I think its similar to what windows 'ALT+TAB' keys are doing...?

 

Thanks in advance,

IamHuM

All replies (9)

Friday, December 31, 2010 8:51 AM âś…Answered

Hi Hum,

 

I modified code as below(get process by PID),

        private void ListTask()

        {

            Process[] processes = Process.GetProcesses();

            foreach (Process process in processes)

            {

                    listBox1.Items.Add(process.ProcessName + " " + process.Id.ToString());

            }

        }

 

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            Process p = new Process();

            string name = listBox1.SelectedItem.ToString();

            string IDs =  name.Substring(name.IndexOf(" "), name.Length - name.IndexOf(" "));

            Int32 ID = Convert.ToInt32(IDs);

            p = Process.GetProcessById(ID);

            const uint SWP_SHOWWINDOW = 1;

            ShowWindow(p.MainWindowHandle, SWP_SHOWWINDOW);

            SetForegroundWindow(p.MainWindowHandle);

        }

As we know, there are some processes when you open several IEs. But actually for IE 7 or more advanced version, there is only one process of IE which has the MainWindow. So if you are using IE 7, you could find only one IE process which has window. Please test it.

 

Hope this can resolve your problem.

Good luck

Paul Zhou [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Thursday, December 23, 2010 11:52 AM

Hi,

 

Hope the following code will help you.

  Process[] processes = Process.GetProcesses();
      foreach (var item in processes)
      {
        if(item.MainWindowTitle.Length > 0)
          Console.WriteLine(item.MainWindowTitle);
      }

Reference: How to get the process names of applications in taskbar using c# ???

Also refer the following links.

C# - Get list of open tasks

Which windows appear in the Alt+Tab list?

C# - Get number of apps in TaskBar

Microsoft MVP - ASP/ASP.NET


Thursday, December 23, 2010 12:02 PM

Hello IamHum

 

You can get process list very easily just by using this

 

           ** Process[] processes = Process.GetProcesses();**

 

then to add these process to list box simply use this method

 

    foreach (Process process in processes)

**    lstProcess.add(process.ProcessName + ":" + process.Id.ToString());**

 

for displaying process which is selected use this code

**[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_SHOWWINDOW = 0x0040;

void ActivateProcess(int PID)
{
    Process proc = Process.GetProcessById(PID);
    IntPtr mainWindow = proc.MainWindowHandle;

    IntPtr newPos = new IntPtr(0);  // 0 puts it on top of Z order.   You can do new IntPtr(-1) to force it to a topmost window, instead.
    SetWindowPos(mainWindow, new IntPtr(0), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW );
 }**


Thursday, December 23, 2010 5:41 PM

hi,

Thanks all of you for immediate reply... Let me try this...!!!

 

IamHu/m


Friday, December 24, 2010 10:36 AM

Hi All,

Thanks for your replies.

I got the Windows/Processes which are visible in the taskbar.

But I am not able show the process using 'ShowWindow' api... is there any other way to show or bring the window in front  on screen?

Thanks in advance,

IamHuM


Friday, December 24, 2010 10:53 AM

Try the BringWindowToTop function.


Tuesday, December 28, 2010 2:12 AM

Hi HuM,

 

Welcome to MSDN Forum!

 

I wrote a sample for you, using ShowWindow and SetForegroundWindow to show and set focus the window.

        [DllImport("User32.dll")]

        static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);

        [DllImport("User32.dll")]

        private static extern bool SetForegroundWindow(IntPtr hWnd);

 

        private void ListTask()

        {

            Process[] processes = Process.GetProcesses();

            foreach (Process process in processes)

                listBox1.Items.Add(process.ProcessName);

        }

 

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

            Process p = new Process();

            string name = listBox1.SelectedItem.ToString();

            p = Process.GetProcessesByName(name)[0];

            const uint SWP_SHOWWINDOW = 0x0001;

            ShowWindow(p.MainWindowHandle, SWP_SHOWWINDOW);

            SetForegroundWindow(p.MainWindowHandle);

            p.Dispose();

        }

 

Hope this can help resolve your problem.

Have a nice day!

 

Best regards,

Paul Zhou

Paul Zhou [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Wednesday, December 29, 2010 5:36 AM

Hi Hum,

 

I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 
 
If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

 

Have a nice day!

 

Best regards,

Paul Zhou

Paul Zhou [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Wednesday, December 29, 2010 7:05 PM

Hi Paul,

 

Actually Process.GetProcesses() return list of processes.

I want my application should have all the list of windows which are currently running.

That mean for each IE window it should be displayed in the listbox... which is not happening currently.

 

How i can do it...?

 

Thanks,

IamHuM