zoukankan      html  css  js  c++  java
  • C#中利用Handle的操作

    C#中利用Handle的操作

    1.我新建了个窗体,窗体中放个Label,这个Label用来显示窗体的句柄。

    2.拖个Timer控件到窗体中,设置属性Enable=true

    3.代码里添加名字空间引用

      using System.Runtime.InteropServices;
      //加入获得Handle的API
    
    [DllImport("user32.dll")]
    
    internal static extern IntPtr WindowFromPoint(Point Point);
    
    //加入获得鼠标焦点的API
    
    [DllImport("user32.dll")]
    
    internal static extern bool GetCursorPos(out Point lpPoint);

    4.写Timer的Tick事件,获取Handle

      private void timer1_Tick(object sender, EventArgs e)
    
      {
    
                Point p;
    
                if (GetCursorPos(out p))
    
                {
    
                    IntPtr hwndCurWindow = WindowFromPoint(p);
    
                    lblhandle.Text = hwndCurWindow.ToString();
    
                }
    
      }
    View Code

    5.执行它,晃动你的鼠标,当你的鼠标在各个窗体间切换的时候,label一直在变,这个就是获得的句柄。

    6.有了句柄又怎么样呢,我也不知道获得了句柄想干嘛。我的机器上有两个翻译软件,金山词霸和有道。有一个不能用的话也没有关系吧。我获得了有道窗体的句柄“B09FC”,我把它转成16进制的了,这样:hwndCurWindow.ToString("X");  于是我判断当句柄等于“B09FC”时,我就关掉窗体,我还要加入个关窗体的API:

      [DllImport("user32.dll")]
    
       public static extern bool CloseWindow(IntPtr hWnd);
    
    //然后我判断
    
      if (hwndCurWindow.ToString("X") == "B09FC")
    
      {
    
              CloseWindow(hwndCurWindow);
    
      }

    执行它,这时当我打开有道词典的时候,我的鼠标一放上去,窗体就关掉。

    我把它改成了一个Windows服务,让它在后台执行,好了,我再也不能使用有道了。

    这是winform的代码

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Data;
    
    using System.Drawing;
    
    using System.Text;
    
    using System.Windows.Forms;
    
     
    
    using System.Runtime.InteropServices;
    
     
    
    namespace HandleShow
    
    {
    
        public partial class Form1 : Form
    
        {
    
            [DllImport("user32.dll")]
    
            internal static extern IntPtr WindowFromPoint(Point Point);
    
            [DllImport("user32.dll")]
    
            internal static extern bool GetCursorPos(out Point lpPoint);
    
            [DllImport("user32.dll")]
    
            public static extern bool CloseWindow(IntPtr hWnd);
    
     
    
     
    
            public Form1()
    
            {
    
                InitializeComponent();
    
            }
    
     
    
            private void timer1_Tick(object sender, EventArgs e)
    
            {
    
                Point p;
    
                if (GetCursorPos(out p))
    
                {
    
                    IntPtr hwndCurWindow = WindowFromPoint(p);
    
                    lblhandle.Text = hwndCurWindow.ToString();
    
                    if (hwndCurWindow.ToString("") == "332106")
    
                    {
    
                        CloseWindow(hwndCurWindow);
    
                    }
    
     
    
                }
    
     
    
            }
    
        }
    
    }
    View Code

    闲来无事,复习下API。网上搜来的,我只能说太强大了

    C#.net. WPF.core 技术交流群 群号205082182,欢迎加入,也可以直接点击左侧和下方的"加入QQ群",直接加入
  • 相关阅读:
    2018-2019-2 20165316 《网络对抗技术》 Exp6 信息搜集与漏洞扫描
    2018-2019-2 网络对抗技术 20165316 Exp5 MSF基础应用
    2018-2019-2 网络对抗技术 20165316 Exp4 恶意代码分析
    2018-2019-2 20165316 『网络对抗技术』Exp3:免杀原理与实践
    2018-2019-2 《网络对抗技术》Exp2 后门原理与实践
    2018-2019-2 20165316 《网络对抗技术》Exp1 PC平台逆向破解
    2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165316
    最近决定要重新回到博客了
    清华大学OS操作系统实验lab1练习知识点汇总
    基本数据结构学习总结: 二叉树的遍历
  • 原文地址:https://www.cnblogs.com/aijiao/p/15162977.html
Copyright © 2011-2022 走看看