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吧  反正是主窗口
         
         }
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM