C#实现QQ截图功能及相关问题

2019-12-30 18:17:23丽君

鼠标弹起 Mouseup


 /// <summary>
  /// 鼠标左键弹起事件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void ScreenBody_MouseUp(object sender, MouseEventArgs e)
  {
   if (e.Button == MouseButtons.Left)
   {
    //如果截图已经开始,鼠标左键弹起设置截图完成
    if (CatchStart)
    {
     CatchStart = false;
     CatchFinished = true;
    }
   }
  }

鼠标双击


 private void ScreenBody_MouseDoubleClick(object sender, MouseEventArgs e)
  {
   if (e.Button==MouseButtons.Left&&CatchFinished)
   {
    //新建一个矩形大小相同的空白图片
    Bitmap CatcheBmp = new Bitmap(CatchRectangle.Width, CatchRectangle.Height);
    Graphics g = Graphics.FromImage(CatcheBmp); ;
   
    //把basemap中指定的部分按照指定大小画到空白图片上
    //CatchRectangle指定的baseMap中指定的部分
    //第二个参数指定绘制到空白图片的位置和大小
    //画完后CatchedBmp不再是空白图片,而是具有与截取的图片一样的内容
    g.DrawImage(baseMap, new Rectangle(0, 0, CatchRectangle.Width, CatchRectangle.Height));

    //将图片保存到剪切板中
    Clipboard.SetImage(CatcheBmp);
    g.Dispose();

    CatchFinished = false;
    this.BackgroundImage = baseMap;
    CatcheBmp.Dispose();
    this.DialogResult = DialogResult.OK;
    this.Close();
   }
  }

鼠标右键 退出截图


/// <summary>
  /// 鼠标右键点击结束截图
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void ScreenBody_MouseClick(object sender, MouseEventArgs e)
  {
   if (e.Button == MouseButtons.Right)
   {
    this.DialogResult = DialogResult.OK;
    this.Close();
   }
  }

最复杂的热键注册  自己也是去网上看的  Main窗体中

声明枚举


[FlagsAttribute]
 public enum KeyModifiers
 {
  None = 0,
  Ctrl = 2,
  Shift = 4,
  WindowsKey = 8
 }

然后在类中编辑一下代码


 //在C#中引用命名空间System.Runtime.InteropServices;来加载非托管类user32.dll
  /*
  * RegisterHotKey函数原型及说明:
  * BOOL RegisterHotKey(
  * HWND hWnd,   // window to receive hot-key notification
  * int id,   // identifier of hot key
  * UINT fsModifiers, // key-modifier flags
  * UINT vk   // virtual-key code);
  * 参数 id为你自己定义的一个ID值
  * 对一个线程来讲其值必需在0x0000 - 0xBFFF范围之内,十进制为0~49151
  * 对DLL来讲其值必需在0xC000 - 0xFFFF 范围之内,十进制为49152~65535
  * 在同一进程内该值必须唯一参数 fsModifiers指明与热键联合使用按键
  * 可取值为:MOD_ALT MOD_CONTROL MOD_WIN MOD_SHIFT参数,或数字0为无,1为Alt,2为Control,4为Shift,8为Windows
  * vk指明热键的虚拟键码
  */
  [System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
  public static extern bool RegisterHotKey(
   IntPtr hWnd, // handle to window
   int id, // hot key identifier
   uint fsModifiers, // key-modifier options
   Keys vk // virtual-key code
  );

  [System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
  public static extern bool UnregisterHotKey(
   IntPtr hWnd, // handle to window
   int id // hot key identifier
  );