Hello, I need to keep the exe's window (for example Notepad) always on the foreground of the form, it means that even if we click on the form , the exe's window will always be ahead of the form.In other words,I have a button which when we click on it, it launches the exe and the exe's window will appear. If we click on the form the exe's window must remain at it's place.
This is what I did:

Process myProcess = new Process();

           try
           {
               this.TopMost = false;
               string winpath = Environment.GetEnvironmentVariable("windir");
               myProcess.StartInfo.UseShellExecute = true;
               myProcess.StartInfo.FileName = winpath+@"\system32\notepad.exe";

               myProcess.Start();

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

Thank you.

Recommended Answers

All 3 Replies

That's been batted about for a long time. https://www.google.com/search?q=c%23+keep+app+on+top finds priors and I won't tell which is the right answer but you need to decide if you want to use a timer or not but let me tell you why I'd use a timer.

Since it's a lot of work to tap into the OS's global events to figure out focus was lost, many just opt to run a timer and say, once a second call the window position to where you demand it.

This is a bit roundabout but it should work. I've used AutoIt for years to automate tasks. There is a scriptable (usable from vbScript, vb.net and C#) component that exposes the WinSetOnTop method.

WinSetOnTop - Change a window's "Always On Top" attribute.

WinSetOnTop "title", "text", flag

Parameters

title - The title of the window to affect. 
text  - The text of the window to affect. 
flag  - Determines whether the window should have the "TOPMOST" flag set.
    1=set on top flag, 0=remove on top flag

Another way would to show the external app in a dialog form. This way it can't be set to the background. The following will open Notepad in a dialog window:

Dialog form with a panel that's dock filled

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

namespace ExternalModal___CS
{
    public partial class Form2 : Form
    {
        [DllImport("user32.dll")]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        const int SW_MAXIMIZE = 3;
        public Form2(string processName)
        {
            InitializeComponent();
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = processName;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
            p.Start();
            Thread.Sleep(500); // Allow the process to open it's window
            SetParent(p.MainWindowHandle, panel1.Handle);
            ShowWindow(p.MainWindowHandle, SW_MAXIMIZE);
        }
    }
}

Main form with a button

using System;
using System.Windows.Forms;

namespace ExternalModal___CS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
        //Change this parameter to the path of the app you want to run.
            Form2 newForm2 = new Form2("notepad.exe");
            newForm2.ShowDialog();
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.