Click here to Skip to main content
15,881,089 members
Articles / Programming Languages / C#
Tip/Trick

Set window OnTop in running programs from C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
3 Jul 2014CPOL 31.2K   5   5   3
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.

C#
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

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student SLIIT
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionTopMost Pin
Emre Ataseven4-Jul-14 13:11
professionalEmre Ataseven4-Jul-14 13:11 
AnswerRe: TopMost Pin
Jhone Mitnick9-Jul-14 15:19
Jhone Mitnick9-Jul-14 15:19 
GeneralRe: TopMost Pin
Emre Ataseven9-Jul-14 20:23
professionalEmre Ataseven9-Jul-14 20:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.