C#:将一个exe文件放在表单的前台 [英] C#: put an exe file on the foreground of the form

查看:86
本文介绍了C#:将一个exe文件放在表单的前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要将exe的窗口(例如记事本)始终保持在窗体的前台,这意味着即使我们单击窗体,exe的窗口也将始终位于窗体的前面。换句话说,我有一个按钮,当我们点击它时,它会启动exe并显示exe的窗口。如果我们点击表单,exe的窗口必须保留在它的位置。

这就是我所做的:



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 = false;
               myProcess.StartInfo.FileName = winpath+@"\system32\notepad.exe";


               myProcess.Start();


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





我尝试过:



我可以启动exe但不能保持在前台。

谢谢。



What I have tried:

I can launch the exe but can't keep it on the foreground.
Thank you.

推荐答案

您可以使用互操作服务来解决您的请求。

You can use interop service fot solve your request.
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);
        }
    }
    // ...
}





注1:

如果你的应用程序是32位且赢了,你可以在这里结束问题是64位。



我希望它有所帮助。



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

I hope it helps.


这篇关于C#:将一个exe文件放在表单的前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆