加载中…
个人资料
懒得伪装
懒得伪装
  • 博客等级:
  • 博客积分:0
  • 博客访问:654
  • 关注人气:0
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

c#全局热键+获取窗口句柄

(2011-02-07 22:51:39)
标签:

杂谈

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Xml;
using System.IO;

namespace WinAPI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static string path = Application.StartupPath + "\\game.xml";

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        //获取窗口句柄
        public static extern int FindWindow(string ClassName,string FormName);
        //获取窗口类名
        //public static extern void GetClassName(IntPtr hwnd, StringBuilder s, int nMaxCount);

        static string classname;
        static string winname;
        private void getIDBtn_Click(object sender, EventArgs e)
        {
            int ID = FindWindow(classname , winname );
            IDText.Text = ID.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists(path))
            {
                DataSet ds_xml = new DataSet();
                ds_xml.ReadXml(path );
                classname = ds_xml.Tables[0].Rows[0]["ClassName"].ToString();
                winname = ds_xml.Tables[0].Rows[0]["WinName"].ToString();
                if (classname == "" || winname == "")
                {
                    MessageBox.Show("配置文件损坏","提示",
                        MessageBoxButtons .OK ,
                        MessageBoxIcon.Warning );
                    getIDBtn.Enabled = false;
                }
                //注册热键
                RegisterHotKey(this.Handle, 300, (int)Mykeys.Alt, (int)Keys.A);//注册热键Alt+A
                //RegisterHotKey(this.Handle, 300,(int)MyKeys.Ctrl|(int)MyKeys.Alt, (int)Keys.A); //注册热键Ctrl+Alt+A     
                //RegisterHotKey(this.Handle, 300, (int)MyKeys.None, (int)Keys.F2); //注册热键F2
            }
            else
            {
                XmlTextWriter xml = new XmlTextWriter(path, Encoding.Unicode);
                xml.Formatting = Formatting.Indented;
                xml.WriteStartDocument();
                xml.WriteStartElement("config");
                xml.WriteElementString("ClassName", "WindowsForms10.Window.8.app.0.b7ab7b");
                xml.WriteElementString("WinName", "WinAPI");
                xml.WriteEndElement();
                xml.WriteEndDocument();
                xml.Close();
            }
        }

        private enum Mykeys
        {
            None=0,
            Alt=1,
            Ctrl=2,
            Shift=4,
            Win=8
        }
        private const int WM_HOTKEY = 0x312; //窗口消息-热键
        //private const int WM_CREATE = 0x1; //窗口消息-创建
        //private const int WM_DESTROY = 0x2; //窗口消息-销毁
        //private const int MOD_ALT = 0x1; //ALT
        //private const int MOD_CONTROL = 0x2; //CTRL
        //private const int MOD_SHIFT = 0x4; //SHIFT
        //private const int VK_SPACE = 0x20; //SPACE

        [DllImport("User32.dll", EntryPoint = "RegisterHotKey")]
        //注册热键
        public static extern bool RegisterHotKey
        (IntPtr jubing, //窗口句柄
            int id, //热键编号
            int special,    //特殊键
            int key);    //一般键

        [DllImport("User32.dll", EntryPoint = "UnregisterHotKey")]
        //卸载热键
        public static extern bool UnregisterHotKey
        (IntPtr jubing, //窗口句柄
            int id  //注册热键中所对应的热键编号
            );

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            UnregisterHotKey(this.Handle, 300); //注销热键
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_HOTKEY)
                {
                    switch (m.WParam.ToInt32())
                    {
                        case 300:
                            if (this.Visible == true)
                            {
                                this.Hide();
                            }
                            else { this.Show(); }
                            break;
                    }
                }
            base.WndProc(ref m );
        }
    }
}

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有