将按钮放在以C#Form运行的进程顶部

时间:2014-07-22 18:08:18

标签: c# process always-on-top stayontop

我在表单中运行一个进程,需要用一个按钮覆盖部分工具栏。无论我做什么,如果我点击进程,按钮就会落在后面。有人可以查看我的代码并提供任何建议吗?谢谢!

namespace Example
{
public partial class Form1 : Form
{
    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
    public const int SWP_ASYNCWINDOWPOS = 0x4000;
    public const int SWP_DEFERERASE = 0x2000;
    public const int SWP_DRAWFRAME = 0x0020;
    public const int SWP_FRAMECHANGED = 0x0020;
    public const int SWP_HIDEWINDOW = 0x0080;
    public const int SWP_NOACTIVATE = 0x0010;
    public const int SWP_NOCOPYBITS = 0x0100;
    public const int SWP_NOMOVE = 0x0002;
    public const int SWP_NOOWNERZORDER = 0x0200;
    public const int SWP_NOREDRAW = 0x0008;
    public const int SWP_NOREPOSITION = 0x0200;
    public const int SWP_NOSENDCHANGING = 0x0400;
    public const int SWP_NOSIZE = 0x0001;
    public const int SWP_NOZORDER = 0x0004;
    public const int SWP_SHOWWINDOW = 0x0040;

    public const int HWND_TOP = 0;
    public const int HWND_BOTTOM = 1;
    public const int HWND_TOPMOST = -1;
    public const int HWND_NOTOPMOST = -2;

    public Form1() {
        InitializeComponent();
        openApp();
        InitTimer();
    }

    [DllImport("user32.dll")]
    static extern IntPtr SetParent(IntPtr hwc, IntPtr hwp);

    Panel myPanel = new Panel();

    private void openApp() {

        FormBorderStyle = FormBorderStyle.FixedToolWindow;

        Thread.Sleep(500);

        string fullPath = @"Notepad.exe";
        ProcessStartInfo stinfo = new ProcessStartInfo();
        stinfo.FileName = fullPath;
        stinfo.CreateNoWindow = false;
        stinfo.UseShellExecute = false;
        stinfo.WindowStyle = ProcessWindowStyle.Hidden;

        Console.WriteLine("Process started... ");
        Process p = Process.Start(stinfo);
        Thread.Sleep(500);

        SetWindowPos(this.Handle, (IntPtr)HWND_TOPMOST, 0, 0, 275, 488, SWP_NOREPOSITION);
        SetWindowPos(p.MainWindowHandle, (IntPtr)HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE);

        Thread.Sleep(500);

        SetParent(p.MainWindowHandle, this.Handle);

        myPanel.Controls.Add(button1);
        myPanel.Width = 275;
        myPanel.Height = 30;
        myPanel.Top = 0;
        this.Controls.Add(myPanel);
        myPanel.BringToFront();

        SetParent(this.Handle, myPanel.Handle);

        SetWindowPos(myPanel.Handle, (IntPtr)HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE);

    }

    private void button1_Click(object sender, EventArgs e)
    {

    }
    private void Form1_Layout(object sender, LayoutEventArgs e)
    {
        myPanel.BringToFront();
    }

    private System.Timers.Timer timer1;

    public void InitTimer()
    {
        timer1 = new System.Timers.Timer(2000);
        timer1.Elapsed += new ElapsedEventHandler(timer1_Tick);
        timer1.Enabled = true;
    }

    private void timer1_Tick(object sender, EventArgs e) {
        button1.BringToFront();
    }
}
}

0 个答案:

没有答案