用微信PC端dll庫實現截圖的執行個體代碼

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了使用PC端的dll庫實現功能,具有一定的參考價值,感興趣的小夥伴們可以參考一下

本文執行個體為大家分享了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  }}

運行結果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.