程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#編程完成自界說熱鍵的辦法

C#編程完成自界說熱鍵的辦法

編輯:C#入門知識

C#編程完成自界說熱鍵的辦法。本站提示廣大學習愛好者:(C#編程完成自界說熱鍵的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#編程完成自界說熱鍵的辦法正文


本文實例講述了C#編程完成自界說熱鍵的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Net;
using System.Drawing.Drawing2D;
namespace App
{
 public partial class Form2 : Form
 {
  public Form2()
  {
   InitializeComponent();
   //法式加載加載熱鍵
   this.Load += delegate
   {
    // 熱鍵一:Control +光標左箭頭
    CustomSystemKeyBoard.RegisterHotKey(Handle, 100, 8, Keys.Left);
    // 熱鍵一:Control +光標右箭頭
    CustomSystemKeyBoard.RegisterHotKey(Handle, 200, 2, Keys.Right);
    // 熱鍵一:Control +光標上箭頭
    CustomSystemKeyBoard.RegisterHotKey(Handle, 300, 2, Keys.Up);
    // 熱鍵一:Control +光標下箭頭
    CustomSystemKeyBoard.RegisterHotKey(Handle, 400, 2, Keys.Down);
   };
   //在法式加入時撤消熱鍵的注冊
   this.FormClosing += delegate
   {
    CustomSystemKeyBoard.UnregisterHotKey(Handle, 100);
    CustomSystemKeyBoard.UnregisterHotKey(Handle, 200);
    CustomSystemKeyBoard.UnregisterHotKey(Handle, 300);
    CustomSystemKeyBoard.UnregisterHotKey(Handle, 400);
   };
  }
  protected override void WndProc(ref Message m)
  {
   //假如m.Msg的值為0x0312那末表現用戶按下了熱鍵
   const int WM_HOTKEY = 0x0312;
   switch (m.Msg)
   {
    case WM_HOTKEY:
     ProcessKey(m);
     break;
   }
   base.WndProc(ref m);
  }
  /// <summary>
  /// 按下熱鍵的操作
  /// </summary>
  /// <param name="m"></param>
  protected void ProcessKey(Message m)
  {
   //IntPtr用於表現指針或句柄的平台特定類型 
   string id = m.WParam.ToString();
   MessageBox.Show(id);
   switch (id)
   {
    case "100":
     //
     break;
    case "200":
     break;
    case "300":
     break;
    case "400":
     break;
   }
  }
 }
 public class CustomSystemKeyBoard
 {
  /// <summary>
  /// 聲明API函數
  /// </summary>
  /// <param name="hWnd">Handle</param>
  /// <param name="id">熱鍵id</param>
  /// <param name="fsModifiers">熱鍵類型(KeyModifiers)</param>
  /// <param name="vk">鍵盤按鍵</param>
  /// <returns></returns>
  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern bool RegisterHotKey(
   IntPtr hWnd,
   int id,
   uint fsModifiers,
   Keys vk
   );
  /// <summary>
  /// 聲明API函數
  /// </summary>
  /// <param name="hWnd"></param>
  /// <param name="id"></param>
  /// <returns></returns>
  [System.Runtime.InteropServices.DllImport("user32.dll")]
  public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
  /// <summary>
  /// 界說一個KeyModifiers的列舉,以便湧現組合鍵
  /// </summary>
  public enum KeyModifiers
  {
   None = 0,
   Alt = 1,
   Control = 2,
   Shift = 3,
   Windows = 8
  }
 }
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved