zoukankan      html  css  js  c++  java
  • C# WINFORM 强制让窗体获得焦点

    //调用API
     [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
     public static extern IntPtr GetForegroundWindow(); //获得本窗体的句柄
     [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
     public static extern bool SetForegroundWindow(IntPtr hWnd);//设置此窗体为活动窗体
     
     //定义变量,句柄类型
     public IntPtr han;
     
    在窗体加载的时候给变量赋值,即将当前窗体的句柄赋给变量
    void Form1_Load(object sender, EventArgs e)
     {
     han = this.Handle;
     timer2.Enabled = true;
     }
     

    //加载一个定时器控件,验证当前WINDOWS句柄是否和本窗体的句柄一样,如果不一样,则激活本窗体

    private void timer2_Tick(object sender, EventArgs e)
     {
     if (han != GetForegroundWindow())
     {
     SetForegroundWindow(han);
     }
     }
     
  • 相关阅读:
    使用watch定时执行命令并显示结果
    LVS配置
    myeclipse通过查询源码查询方法
    导入struts2框架项目无法运行tomcat
    解决eclipse/myeclipse导入项目时出现红色叹号的方法
    my/eclipse中的debug调试
    导入项目后servlet报错
    转到Servlet出现500型错误
    关于快捷键导包失败
    关于jsp找不到servlet
  • 原文地址:https://www.cnblogs.com/z_lb/p/2321539.html
Copyright © 2011-2022 走看看