码迷,mamicode.com
首页 > Windows程序 > 详细

一种比较少见的C#代码段

时间:2015-02-16 10:08:47      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

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.Runtime.InteropServices;

namespace CSharp_Message
{
    [Flags()]
    public enum KeyModifiers
    {
        None=0,
        Alt=1,
        Ctrl=2,
        Shift=4,
        WindowKey=5
    }
    public partial class Form3 : Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegistryHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        public Form3()
        {
            InitializeComponent();
            RegistryHotKey(this.Handle, 100, KeyModifiers.None, Keys.Escape);
            RegistryHotKey(this.Handle, 101, KeyModifiers.Ctrl, Keys.F);
            RegistryHotKey(this.Handle, 102, KeyModifiers.Ctrl, Keys.A);
        }

        protected override void WndProc(ref Message m)
        {
            try
            {
                const int WM_HOTKEY = 0x0312;
                switch (m.Msg)
                {
                    case WM_HOTKEY:
                        switch(m.WParam.ToInt32())
                        {
                            case 101:
                                //...
                                break;
                            case 100:
                                //...
                                break;
                        }
                        break;
                    case 104:
                        break;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            base.WndProc(ref m);
        }

        private void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            UnregisterHotKey(this.Handle, 100);
            UnregisterHotKey(this.Handle, 101);
            UnregisterHotKey(this.Handle, 102);
        }
    }
}

一种比较少见的C#代码段

标签:

原文地址:http://www.cnblogs.com/QQ122252656/p/4293706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!