打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用微信PC端的截图dll库实现微信截图功能
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
  }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C# 如何实现热键注册 RegisterHotKey
C#使用全局热键ASDF空格
Win32 API对文本框发送消息(多个文本Edit,动态 控件 ID) - Soma -...
[原创]C#应用WindowsApi实现查找\枚举(FindWindow、EnumChildWindows)窗体控件,并发送消息。
[C#/C ]C#调用非托管DLL的APIs - .NET人字拖 - 博客园
MSDN中回调函数的讲解及其C#例子:用委托实现回调函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服