调用C#实现系统功能类的主要方法

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace 实现系统热键
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(24, 128);
   this.button1.Name = "button1";
   this.button1.TabIndex = 0;
   this.button1.Text = "保存设置";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(16, 72);
   this.textBox1.Name = "textBox1";
   this.textBox1.TabIndex = 1;
   this.textBox1.Text = "";
   this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(16, 24);
   this.label1.Name = "label1";
   this.label1.TabIndex = 2;
   this.label1.Text = "请设置热键:";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(160, 273);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "系统热键测试";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  protected const String m_sFilename = "config.xml";
  protected Keys m_hotkey;
  protected bool m_ctrlhotkey, m_shifthotkey, m_althotkey, m_winhotkey;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.TextBox textBox1;

 
  //监视热键
  protected override void WndProc( ref Message m )
  {
   const int WM_HOTKEY = 0x0312; 
           
   switch(m.Msg)
   {
    case WM_HOTKEY: 
     MessageBox.Show("您点了热键!");
     break;
 
   } 
   base.WndProc(ref m );
  }
  //注册热键
  public void SetHotKey(Keys c, bool bCtrl, bool bShift, bool bAlt, bool bWindows)
  {
   //先赋到变量
   m_hotkey = c;
   m_ctrlhotkey = bCtrl;
   m_shifthotkey = bShift;
   m_althotkey = bAlt;
   m_winhotkey = bWindows;
 
   //注册系统热键
   NativeWIN32.KeyModifiers modifiers = NativeWIN32.KeyModifiers.None;
   if (m_ctrlhotkey)
    modifiers |= NativeWIN32.KeyModifiers.Control;
   if (m_shifthotkey)
    modifiers |= NativeWIN32.KeyModifiers.Shift;
   if (m_althotkey)
    modifiers |= NativeWIN32.KeyModifiers.Alt;
   if (m_winhotkey)
    modifiers |= NativeWIN32.KeyModifiers.Windows;  
   NativeWIN32.RegisterHotKey(Handle, 100, modifiers, c);
  }
 

  
  //显示热键到文本框
  public void ShowHotkey()
  {
   String sHotKey = "";
   if (m_ctrlhotkey)
    sHotKey += "Ctrl";
 
   if (m_shifthotkey)
   {
    if (sHotKey.Length!=0)
     sHotKey += " + ";
    sHotKey += "Shift";
   }
   if (m_althotkey)
   {
    if (sHotKey.Length!=0)
     sHotKey += " + ";
    sHotKey += "Alt";
   }
   if (m_winhotkey)
   {
    if (sHotKey.Length!=0)
     sHotKey += " + ";
    sHotKey += "Win";
   }
   if (sHotKey.Length!=0)
    sHotKey += " + ";
   sHotKey += (char)(int)m_hotkey;
   this.textBox1.Text=sHotKey;
   this.textBox1.Select(0,0);
  }
  //读配置文件 方法2 (读取热键到变量)
  public void LoadSetting()
  {
   System.Xml.XmlTextReader reader = null;  
   if (!System.IO.File.Exists(m_sFilename)) return;
   try
   {             
    reader = new System.Xml.XmlTextReader(m_sFilename);
    reader.WhitespaceHandling = System.Xml.WhitespaceHandling.None;
 
    bool bEnablePageContent = false; //是否有page节点
    bool bEnableHotkeyContent = false; //是否有hotkey节点
   
    while (reader.Read())
    {
     switch (reader.NodeType)
     {
      case System.Xml.XmlNodeType.Element:// are we parsing a <page> element       
       bEnablePageContent = (reader.Name=="page");//是否有page节点
       bEnableHotkeyContent = (reader.Name=="hotkey");//是否有hotkey节点
       if (bEnableHotkeyContent)
       {
        if (reader["ctrl"]!=null)
         m_ctrlhotkey = (reader["ctrl"]=="true")?true:false;
        if (reader["shift"]!=null)
         m_shifthotkey = (reader["shift"]=="true")?true:false;
        if (reader["alt"]!=null)
         m_althotkey = (reader["alt"]=="true")?true:false;
        if (reader["windows"]!=null)
         m_winhotkey = (reader["windows"]=="true")?true:false;       
       }
       break;
      case System.Xml.XmlNodeType.Text:      
       if (bEnableHotkeyContent)
       {     
 
        m_hotkey = (Keys) System.Int32.Parse(reader.Value);
        SetHotKey(m_hotkey, m_ctrlhotkey, m_shifthotkey, m_althotkey, m_winhotkey); // register hotkey
       }
       bEnablePageContent = false;
       bEnableHotkeyContent = false;
       break;
      case System.Xml.XmlNodeType.Attribute:
       if (bEnableHotkeyContent)
       {
        if (reader.Name=="ctrl")
         m_ctrlhotkey = (reader.Value=="true")?true:false;
        if (reader.Name=="shift")
         m_shifthotkey = (reader.Value=="true")?true:false;
        if (reader.Name=="alt")
         m_althotkey = (reader.Value=="true")?true:false;
        if (reader.Name=="windows")
         m_winhotkey = (reader.Value=="true")?true:false;
       }
       break;
 
     }      
    } // end while     
   }
   catch( System.Xml.XmlException e)
   {
    e.ToString();
   }
   finally
   {
    if (reader!=null) reader.Close();
   }
  }
 
 
  // 存储热键到配置文件
  public void SaveSetting()
  {
   System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(m_sFilename, new System.Text.UTF8Encoding());
   w.Formatting = System.Xml.Formatting.Indented;
   w.WriteStartDocument();
   w.WriteStartElement("setting");
 
 
   w.WriteStartElement("hotkey"); // <hotkey>
   w.WriteAttributeString("ctrl",m_ctrlhotkey?"true":"false");
   w.WriteAttributeString("shift",m_shifthotkey?"true":"false");
   w.WriteAttributeString("alt",m_althotkey?"true":"false");
   w.WriteAttributeString("windows",m_winhotkey?"true":"false");
   int n = (int) m_hotkey;
   w.WriteString( n.ToString() );
   w.WriteEndElement(); // </hotkey>
 
   w.WriteEndElement(); // </popupkiller>
   w.Close();
  }
 
  private void button1_Click(object sender, System.EventArgs e)
  { 
   SetHotKey(m_hotkey, m_ctrlhotkey, m_shifthotkey, m_althotkey, m_winhotkey);
   SaveSetting();
   //   Close();
  }
 
  //设置热键
  private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
   //   SetHotKey(e.KeyCode,
   //    (ModifierKeys&Keys.Control)>0?true:false,
   //    (ModifierKeys&Keys.Shift)>0?true:false,
   //    (ModifierKeys&Keys.Alt)>0?true:false,
   //    ((ModifierKeys&Keys.LWin)>0||(ModifierKeys&Keys.RWin)>0)?true:false  );
 
   m_hotkey = e.KeyCode;
   m_ctrlhotkey = (ModifierKeys&Keys.Control)>0?true:false;
   m_shifthotkey = (ModifierKeys&Keys.Shift)>0?true:false;
   m_althotkey = (ModifierKeys&Keys.Alt)>0?true:false;
   m_winhotkey = ((ModifierKeys&Keys.LWin)>0||(ModifierKeys&Keys.RWin)>0)?true:false;
 
   ShowHotkey();//显示热键到文本框
 
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   //设置初始热键// default hotkey is Ctrl+Shift+J
   //   m_hotkey = Keys.Home;
   //   m_hotkey = Keys.J;
   //   m_ctrlhotkey = true;
   //   m_shifthotkey = true;
   //   m_althotkey = false;
   //   m_winhotkey = false;
   //   SetHotKey(m_hotkey, m_ctrlhotkey, m_shifthotkey, m_althotkey, m_winhotkey);
 

   //读取配置热键
   LoadSetting();读配置文件(读取热键到变量)
   ShowHotkey();//显示热键到文本框
  }
  
 }
}

转载于:https://www.cnblogs.com/zhouxiancai0128/archive/2006/05/30/412555.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值