首页 > 代码库 > c#实现Form窗体始终在桌面最前端显示
c#实现Form窗体始终在桌面最前端显示
方法一:
//调用API[System.Runtime.InteropServices.DllImport("user32", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]public static extern IntPtr GetFocus(); //获得本窗体的句柄[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetForegroundWindow")]public static extern bool SetFocus(IntPtr hWnd);//设置此窗体为活动窗体// 定义变量,句柄类型public IntPtr han;private void Form1_Load(object sender, EventArgs e){//在窗体加载的时候给变量赋值,即将当前窗体的句柄赋给变量han = this.Handle;}private void timer1_Tick(object sender, EventArgs e){// 加载一个定时器控件,验证当前WINDOWS句柄是否和本窗体的句柄一样,如果不一样,则激活本窗体if (han != GetFocus()){SetFocus(han);}this.WindowState = FormWindowState.Normal;}
方法二:
[System.Runtime.InteropServices.DllImport("user32")]private static extern IntPtr GetActiveWindow();//获取当前窗体的活动状态// 判断当前窗口是否处于活动状态的方法private bool ThisIsActive(){ return (GetActiveWindow() == this.Handle);}private void timer1_Tick(object sender, EventArgs e){if (!ThisIsActive()){this.Activate();}this.WindowState = FormWindowState.Normal;}
方法三
[DllImport("user32")] private static extern IntPtr FindWindow(string lpClassName,string lpWindowName); [DllImport("user32")] private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); //在窗体On_Load事件中添加(Santos的代码): IntPtr hDeskTop=FindWindow("Progman", "Program Manager"); SetParent(this.Handle,hDeskTop);
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。