Click here to Skip to main content
11,437,713 members (51,697 online)
Click here to Skip to main content

Tagged as

Set window OnTop in running programs from C#

, 4 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jhone Mitnick
Student SLIIT
United States United States
No Biography provided

Comments and Discussions

 
QuestionTopMost Pin
Emre Ataseven4-Jul-14 14:11
professionalEmre Ataseven4-Jul-14 14:11 
AnswerRe: TopMost Pin
Elshan EMK9-Jul-14 16:19
memberElshan EMK9-Jul-14 16:19 
GeneralRe: TopMost Pin
Emre Ataseven9-Jul-14 21:23
professionalEmre Ataseven9-Jul-14 21:23 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150506.1 | Last Updated 4 Jul 2014
Article Copyright 2014 by Jhone Mitnick
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid