欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用微信PC端的截图dll库实现微信截图功能

程序员文章站 2022-06-21 08:35:05
本文实例为大家分享了截图dll库实现微信截图功能 ,供大家参考,具体内容如下 screenform.cs代码: using system; using sys...

本文实例为大家分享了截图dll库实现微信截图功能 ,供大家参考,具体内容如下

screenform.cs代码:

using system;
using system.collections.generic;
using system.runtime.interopservices;
using system.windows.forms;

namespace screent
{
  public partial class screenform : form
  {
    public screenform()
    {
      initializecomponent();
    }


    private void screencapture()
    {
      dll.prscrn();
    }

    protected override void wndproc(ref message m)
    {
      base.wndproc(ref m);
      hotkey.processhotkey(m);
    }

    private void button1_click(object sender, eventargs e)
    {
      dll.prscrn();
    }

    private void form1_load(object sender, eventargs e)
    {
      //注册热键(窗体句柄,热键id,辅助键,实键)  
      try
      {
        hotkey.regist(handle, hotkeymodifiers.mod_alt, keys.f1, screencapture);
      }
      catch (exception te)
      {
        messagebox.show("alt + a 热键被占用");
      }
    }

    private void form1_formclosed(object sender, formclosedeventargs e)
    {
      //注消热键(句柄,热键id)  
      hotkey.unregist(handle, screencapture);
    }
  }

  public class dll
  {
    [dllimport("prscrn.dll", entrypoint = "prscrn")]
    public static extern int prscrn(); //与dll中一致  
  }


  public static class hotkey
  {
    #region 系统api

    [dllimport("user32.dll")]
    [return: marshalas(unmanagedtype.bool)]
    private static extern bool registerhotkey(intptr hwnd, int id, hotkeymodifiers fsmodifiers, keys vk);

    [dllimport("user32.dll")]
    private static extern bool unregisterhotkey(intptr hwnd, int id);

    #endregion

    public delegate void hotkeycallbackhanlder();

    private const int wm_hotkey = 0x312;
    private static int keyid = 10;

    private static readonly dictionary<int, hotkeycallbackhanlder> keymap =
      new dictionary<int, hotkeycallbackhanlder>();

    /// <summary>
    ///   注册快捷键
    /// </summary>
    /// <param name="hwnd">持有快捷键窗口的句柄</param>
    /// <param name="fsmodifiers">组合键</param>
    /// <param name="vk">快捷键的虚拟键码</param>
    /// <param name="callback">回调函数</param>
    public static void regist(intptr hwnd, hotkeymodifiers fsmodifiers, keys vk, hotkeycallbackhanlder callback)
    {
      int id = keyid++;
      if (!registerhotkey(hwnd, id, fsmodifiers, vk))
        throw new exception("regist hotkey fail.");
      keymap[id] = callback;
    }

    /// <summary>
    ///   注销快捷键
    /// </summary>
    /// <param name="hwnd">持有快捷键窗口的句柄</param>
    /// <param name="callback">回调函数</param>
    public static void unregist(intptr hwnd, hotkeycallbackhanlder callback)
    {
      foreach (var var in keymap)
      {
        if (var.value == callback)
          unregisterhotkey(hwnd, var.key);
      }
    }

    /// <summary>
    ///   快捷键消息处理
    /// </summary>
    public static void processhotkey(message m)
    {
      if (m.msg == wm_hotkey)
      {
        int id = m.wparam.toint32();
        hotkeycallbackhanlder callback;
        if (keymap.trygetvalue(id, out callback))
        {
          callback();
        }
      }
    }
  }

  public enum hotkeymodifiers
  {
    mod_alt = 0x1,
    mod_control = 0x2,
    mod_shift = 0x4,
    mod_win = 0x8
  }
}

使用微信PC端的截图dll库实现微信截图功能

运行结果如图:

使用微信PC端的截图dll库实现微信截图功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。