Click here to Skip to main content
16,004,647 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
Process myProcess = new Process();

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


               myProcess.Start();


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


What I have tried:

I can launch the exe but can't keep it on the foreground.
Thank you.
Posted
Updated 29-Jul-16 6:25am
v2

1 solution

You can use interop service fot solve your request.
C#
using System.Runtime.InteropServices;

public partial class FormMain : Form
{
    //
    // WINAPI definitions, copeied from windows.h
    enum SetWinPos_ZOrderOpt{HWND_TOP        = 0,
                                HWND_BOTTOM     = 1,
                                HWND_TOPMOST    = -1,
                                HWND_NOTOPMOST  = -2}
    enum SetWinPosFlags
    {
        SWP_NOSIZE = 0x0001,
        SWP_NOMOVE = 0x0002,
        SWP_NOZORDER = 0x0004,
        SWP_NOREDRAW = 0x0008,
        SWP_NOACTIVATE = 0x0010,
        SWP_FRAMECHANGED = 0x0020, /* The frame changed: send WM_NCCALCSIZE */
        SWP_SHOWWINDOW = 0x0040,
        SWP_HIDEWINDOW = 0x0080,
        SWP_NOCOPYBITS = 0x0100,
        SWP_NOOWNERZORDER = 0x0200, /* Don't do owner Z ordering */
        SWP_NOSENDCHANGING = 0x0400, /* Don't send WM_WINDOWPOSCHANGING */
        SWP_DRAWFRAME = SWP_FRAMECHANGED,
        SWP_NOREPOSITION = SWP_NOOWNERZORDER
    }

    //
    // Import SetWindowPos
    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    public static extern IntPtr SetWindowPos(int hWnd, int hWndInsertAfter,
                                             int X, int Y, int cx, int cy,
                                             uint flages);

    // Start Notepad and set TOOPMOST
    private void buttonTestTopLevel_Click(object sender, EventArgs e)
    {
        Process myProcess = new Process();
        try
        {
            this.TopMost = false;
            string winpath = Environment.GetEnvironmentVariable("windir");
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.FileName = winpath + @"\system32\notepad.exe";	// See Note 1
            myProcess.Start();
            myProcess.WaitForInputIdle();
            {
                // Here HWND_TOPMOST does it's Job
                SetWindowPos(myProcess.MainWindowHandle.ToInt32(), 
                                (int)SetWinPos_ZOrderOpt.HWND_TOPMOST,
                                0, 0, 0, 0,
                                (int)(SetWinPosFlags.SWP_NOSIZE |
                                      SetWinPosFlags.SWP_NOMOVE | 
                                      SetWinPosFlags.SWP_SHOWWINDOW));
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    // ...
}


Note 1:
Here you can end in problems in case your app is 32bit and Win is 64bit.

I hope it helps.
 
Share this answer
 
v2
Comments
TatsuSheva 29-Jul-16 12:34pm    
Thank you , you saved my day.
[no name] 29-Jul-16 12:36pm    
You are welcome.
TatsuSheva 29-Jul-16 12:43pm    
In fact, my Win is 64bit but it works perfectly.
[no name] 29-Jul-16 13:04pm    
Yep it works if everything fits together. I assume in your Project Options you unchecked "32-bit preferred",so everything ok. In case you check this Option, than your app runs as 32 bit and will give Problems with the above example. The Workaround then is, to start Notepad.exe from C:\Windows\SysWOW64 where the 32 bot Version of Notepad is located.
TatsuSheva 29-Jul-16 13:15pm    
Okay thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900