记事本在winform之上

时间:2015-10-29 09:38:44

标签: c# winforms

好日子的家伙,任何人都可以帮助我。我有一个设置TopMost = true的winforms。我有一个按钮,当我点击它时,它会创建一个记事本。现在,我希望我的记事本显示在我的winforms 的顶部,而设置我的winforms TopMost = false。也许我错过了什么。我愿意接受任何建议。顺便说一下,我将表单设置为TopMost=trueBringToFront()因为我不希望任何用户在任务栏中选择任何程序并将其置于最前面并最小化我的winforms。提前致谢

public Form1()
{
    InitializeComponent();
    this.BringToFront();
    this.TopMost = true;
}

// bunch of codes here...

private void button1_Click(object sender, EventArgs e)
{
    Process process = new Process();
    process.StartInfo.FileName = "notepad.exe";
    process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
    process.Start();

}

// some codes here

private void Form1_Load(object sender, EventArgs e)
{
    FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    Left = Top = 0;
    Width = Screen.PrimaryScreen.WorkingArea.Width;
    Height = Screen.PrimaryScreen.WorkingArea.Height;  
}

1 个答案:

答案 0 :(得分:2)

PInvoke 解决方案:

using System.Runtime.InteropServices;
...

// Even if it is "user32.dll" it will do on IA64 as well
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, 
                                int X, int Y, int cx, int cy, int uFlags);

 ...

// Since Process is IDisposable, put it into "using"
using (Process process = new Process()) {
  process.StartInfo.FileName = "notepad.exe";
  process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
  process.Start();

  // wait for main window be created
  process.WaitForInputIdle();

  // Insert (change Z-order) as the topmost - (IntPtr) (-1); 
  // NoMove, NoSize - 0x0002 | 0x0001
  SetWindowPos(process.MainWindowHandle, (IntPtr) (-1), 0, 0, 0, 0, 0x0002 | 0x0001);
}