winform顶部的记事本
本文关键字:记事本 顶部 winform | 更新日期: 2023-09-27 18:19:48
大家好,有人能帮我吗?我有一个设置TopMost = true
的winforms。我有一个按钮,当我点击它时,它会创建一个记事本。现在,我希望我的记事本显示在我的winforms的顶部,而不需要设置我的winform TopMost=false。也许我错过了什么。我愿意接受任何建议。顺便说一句,我把我的表单设置为TopMost=true
和BringToFront()
,因为我不希望任何用户在任务栏上选择任何程序,然后把它放在前面,最小化我的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;
}
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);
}