C# window 窗体 保持最前显示

时间:2023-03-09 17:15:00
C# window 窗体 保持最前显示
两句话搞定
[DllImport("user32.dll", CharSet = CharSet.Auto)]
 private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);

SetWindowPos(this.Handle, -1, 0, 0, 0, 0, 1 | 2);

namespace test
{
public partial class Form1 : Form
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags); public Form1()
{
InitializeComponent();
} protected override void OnLoad(EventArgs e)
{
SetWindowPos(this.Handle, -, , , , , | ); //最后参数也有用1 | 4 p1.MainForm.Handle 我记得这个是MainForm吧 反正是主窗口
SetWindowPos(this.printPreviewDialog1.Handle, -, , , , , | ); //最后参数也有用1 | 4 p1.MainForm.Handle 我记得这个是MainForm吧 反正是主窗口 }
}
}