Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Set window OnTop in running programs from C#

0.00/5 (No votes)
4 Jul 2014 1  
You can set running programs to ontop of window stack (application float above other windows).

Introduction

You can set running programs to ontop of window stack (application float above other windows).


Using the code

I used Platform Invoke method call SetWindowPos to set selected window to ontop of others and System.Runtime.InteropServices to get Running Applications.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace OnTop
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetWindowPos(IntPtr hWnd,
            int hWndInsertAfter, int x, int y, int cx, int cy, int uFlags);

        private const int HWND_TOPMOST = -1;
        private const int HWND_NOTOPMOST = -2;
        private const int SWP_NOMOVE = 0x0002;
        private const int SWP_NOSIZE = 0x0001;
        public Form1()
        {
            InitializeComponent();
            LoadProcesses();   
        }
        private void LoadProcesses()
        {
            //Getting all the process
            listBox1.Items.Clear();
            Process[] processes = Process.GetProcesses();
            this.listBox1.DisplayMember = "MainWindowTitle";
            this.listBox1.ValueMember = "MainWindowHandle";
          
            foreach (Process process in processes)
            {
                //Adding only process which has got Window Title.
                if (process.MainWindowTitle.Length >= 1)
                {
                    this.listBox1.Items.Add(process);
                }
            }
        }
        private void btnOntopSet_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex != -1)
            {
                Process process = this.listBox1.SelectedItem as Process;
                SetWindowPos(process.MainWindowHandle,
                    HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
            }
        }

       private void btnRefresh_click(object sender, EventArgs e)
        {
            LoadProcesses(); 
        }

       private void btnOntopSetDefault_click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex != -1)
            {
                Process process = this.listBox1.SelectedItem as Process;
                SetWindowPos(process.MainWindowHandle,
                    HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
            }
        }
    }
}

 

Points of Interest

 private static extern bool SetWindowPos(IntPtr hWnd,
            int hWndInsertAfter, int x, int y, int cx, int cy, int uFlags);

Changes the size, position, and Z order of a child, pop-up, or top-level window. These windows are ordered according to their appearance on the screen. The topmost window receives the highest rank and is the first window in the Z order.

more info about SetWindowPos()

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545%28v=vs.85%29.aspx

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here