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, 0, 0, 0, 0, 1 | 2); //最后参数也有用1 | 4 p1.MainForm.Handle  我记得这个是MainForm吧  反正是主窗口
             SetWindowPos(this.printPreviewDialog1.Handle, -1, 0, 0, 0, 0, 1 | 2); //最后参数也有用1 | 4 p1.MainForm.Handle  我记得这个是MainForm吧  反正是主窗口
         
         }
    }
}

 

posted on 2016-01-11 17:51  苏上话  阅读(7131)  评论(0编辑  收藏  举报