註冊系統熱鍵(c#)

引用命名空间

using System.Runtime.InteropServices;

在类中引用方法


         [DllImport("user32.dll",SetLastError=true)]  

        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control, Keys vk);

private void Form2_Load1(object sender, System.EventArgs e)
{      
       RegisterHotKey(this.Handle,888,2,Keys.S);
 //handle:這個窗體的handle   888:這個熱鍵的標誌id    2:crtl鍵   S: s鍵 

   }

private void Form2_Load1(object sender, System.EventArgs e)
{
             UnregisterHotKey(this.Handle,888);    
            //handle:這個窗體的handle   888:上面那個熱鍵的標誌id  
}


protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x0312:    //這個是window消息定義的   註冊的熱鍵消息
                    if(m.WParam.ToString().Equals("888"))  //如果是我們註冊的那個熱鍵
                        MessageBox.Show("你按了ctrl+a");
                    break;
            }            
            base.WndProc (ref m);
        }
這是熱鍵的定義  alt+crtl是3  直接相加就可以了




如果用sendmessagea這個api  就是

SendMessage(this.Handle,WM_SETHOTKEY,0x45a, 0)

//WM_SETHOTKEY 是 0x32
//0x45a    高字節是contrl鍵  低字節是a b c d   所以5a是z的ascii   4是alt  也就是alt+z

Demo
https://files.cnblogs.com/5tao/工作日志.rar



参考:

你在程序中需要自己注册全局热键,  
  用到API  
  RegisterHotKey  
  UnregisterHotKey  
   
   
  首先,创建一个WinHotKey类,如下  
  public   class   WinHotKey  
  {  
          [DllImport("user32.dll",SetLastError=true)]  
          public   static   extern   bool   RegisterHotKey(  
  IntPtr   hWnd,   //窗口句柄  
  int   id,  
  KeyModifiers   fsModifiers,  
  Keys   vk  
          );  
   
          [DllImport("user32.dll",SetLastError=true)]  
          public   static   extern   bool   UnregisterHotKey(  
  IntPtr   hWnd,  
  int   id  
          );  
   
          [Flags()]  
          public   enum   KeyModifiers  
          {  
  None   =   0,  
  Alt   =   1,  
  Control   =2,  
  Shift   =   4,  
  Windows   =   8  
          }  
   
          public   WinHotKey()  
          {  
  //  
  //   TODO:   在此处添加构造函数逻辑  
  //  
          }  
  }  
   
  然后,在程序中这样调用  
  //快捷键定义  
  private   bool   key_Ctrl   =   false;  
  private   bool   key_Shift   =   false;  
  private   bool   key_Alt   =   false;  
  private   bool   key_Windows   =   false;  
  private   Keys     key_other;  
   
  public   void   SetHotKey(bool   bCtrl,bool   bShift,bool   bAlt,bool    
  bWindows,Keys   nowKey)  
  {  
  try  
  {  
  this.key_Alt   =   bAlt;  
  this.key_Ctrl   =   bCtrl;  
  this.key_Shift   =   bShift;  
  this.key_Windows   =   bWindows;  
  this.key_other   =   nowKey;  
   
  WinHotKey.KeyModifiers   modifier   =   WinHotKey.KeyModifiers.None;  
   
  if(   this.key_Ctrl   )  
  modifier   |=   WinHotKey.KeyModifiers.Control;  
  if(this.key_Alt   )  
  modifier   |=   WinHotKey.KeyModifiers.Alt;  
  if(this.key_Shift)  
  modifier   |=   WinHotKey.KeyModifiers.Shift;  
  if(this.key_Windows)  
  modifier   |=   WinHotKey.KeyModifiers.Windows;  
   
  WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey);  
  }  
  catch  
  {  
  //login.ShowMessage("快捷键定义错误!");  
  }  
  }  
   
  //激活热键  
  protected   override   void   WndProc(ref   Message   m   )  
  {  
  const   int   WM_HOTKEY   =   0x0312;    
   
  switch(m.Msg)  
  {  
  case   WM_HOTKEY:  
  {  
  //如果有新消息,弹出消息  
  if(   ReceiveNewMessage   ==   true)  
  {  
  for(int   i=0;i  {  
  ManInfor   searchMan   =   (ManInfor)this.manInforList[i];  
  if(   searchMan.manInforID.Equals(   getFriendID))  
  {  
  searchMan.Clicked   =   true;  
  searchMan.P2PShow();  
  break;  
  }  
  }  
  }  
  else  
  {  
  this.Show();  
  this.TopMost   =   true;  
  this.panel_Main.Refresh();  
  this.WindowState   =   System.Windows.Forms.FormWindowState.Normal;  
  }  
  }  
  break;  
  }    
  base.WndProc(ref   m   );  
  }  
   
  自己研究吧
void regallkey(HWND winhand)
{
 RegisterHotKey(winhand, 96, MOD_ALT, 96);//0
 RegisterHotKey(winhand, 97, MOD_ALT, 97);//1
 RegisterHotKey(winhand, 98, MOD_ALT, 98);//2
 RegisterHotKey(winhand, 99, MOD_ALT, 99);//3
 RegisterHotKey(winhand, 100, MOD_ALT, 100);//4
 RegisterHotKey(winhand, 101, MOD_ALT, 101);//5
 RegisterHotKey(winhand, 102, MOD_ALT, 102);//6
 RegisterHotKey(winhand, 103, MOD_ALT, 103);//7
 RegisterHotKey(winhand, 104, MOD_ALT, 104);//8
 RegisterHotKey(winhand, 105, MOD_ALT, 105);//9
 RegisterHotKey(winhand, 106, MOD_ALT, 107);//密码
 RegisterHotKey(winhand, 107, MOD_ALT, 109);//查询
 RegisterHotKey(winhand, 108, MOD_ALT, 106);//返回
 RegisterHotKey(winhand, 109, MOD_ALT, 111);//确认
 RegisterHotKey(winhand, 110, MOD_SHIFT, 111);//左
 RegisterHotKey(winhand, 111, MOD_SHIFT, 106);//右

 RegisterHotKey(winhand, 960,    MOD_ALT, 45);//0
 RegisterHotKey(winhand, 970, MOD_ALT, 35);//1
 RegisterHotKey(winhand, 980, MOD_ALT, 40);//2
 RegisterHotKey(winhand, 990, MOD_ALT, 34);//3
 RegisterHotKey(winhand, 1000, MOD_ALT, 37);//4
 RegisterHotKey(winhand, 1010, MOD_ALT, 12);//5
 RegisterHotKey(winhand, 1020, MOD_ALT, 39);//6
 RegisterHotKey(winhand, 1030, MOD_ALT, 36);//7
 RegisterHotKey(winhand, 1040, MOD_ALT, 38);//8
 RegisterHotKey(winhand, 1050, MOD_ALT, 33);//9
}

void unregallkey(HWND winhand)
{
 for(int i=96;i<122;i++)
  UnregisterHotKey(winhand, i);//0
 for(i=96;i<106;i++)
  UnregisterHotKey(winhand, i*10);//0
}

数字键是  alt+NUM0~NUM9的组合 
密码键是  alt+小键盘的加号
查询键是  alt+小键
返回键是  alt+小键盘星号 
确认键是  alt+小键盘/号 
左方向键是 shift+小键盘/号
右方向键是 shift+小键盘星号

窗口移动:

using System.Runtime.InteropServices;
[DllImport("user32.dll")]

public static extern int MoveWindow(IntPtr hWnd,int x,int y,int m,int n,bool repaint);
+MoveWindow(myfrmShowLogin.Handle,1024,0,392,240,true);


原文地址:https://www.cnblogs.com/5tao/p/743113.html