C# WinForm如何实现全局快捷键?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# WinForm如何实现全局快捷键?相关的知识,希望对你有一定的参考价值。
如何实现让程序在后台运行的时候也能响应快捷键?就像千千静听一样。。
比如一个A窗体,在后台运行时,用ctrl+alt+s让它调出来,如何实现?
小弟初学,要实现的代码,最好通过运行,谢谢!
using System;
using System.Runtime.InteropServices;
namespace SystemHotKey
public delegate void HotkeyEventHandler(int HotKeyID);
public class Hotkey : System.Windows.Forms.IMessageFilter
System.Collections.Hashtable keyIDs = new System.Collections.Hashtable();
IntPtr hWnd;
public event HotkeyEventHandler OnHotkey;
public enum KeyFlags
MOD_ALT = 0x1,
MOD_CONTROL = 0x2,
MOD_SHIFT = 0x4,
MOD_WIN = 0x8
[DllImport("user32.dll")]
public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);
[DllImport("user32.dll")]
public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);
[DllImport("kernel32.dll")]
public static extern UInt32 GlobalAddAtom(String lpString);
[DllImport("kernel32.dll")]
public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);
public Hotkey(IntPtr hWnd)
this.hWnd = hWnd;
System.Windows.Forms.Application.AddMessageFilter(this);
public int RegisterHotkey(System.Windows.Forms.Keys Key, KeyFlags keyflags)
UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
keyIDs.Add(hotkeyid, hotkeyid);
return (int)hotkeyid;
public void UnregisterHotkeys()
System.Windows.Forms.Application.RemoveMessageFilter(this);
foreach (UInt32 key in keyIDs.Values)
UnregisterHotKey(hWnd, key);
GlobalDeleteAtom(key);
public bool PreFilterMessage(ref System.Windows.Forms.Message m)
if (m.Msg == 0x312)
if (OnHotkey != null)
foreach (UInt32 key in keyIDs.Values)
if ((UInt32)m.WParam == key)
OnHotkey((int)m.WParam);
return true;
return false;
参考技术A //以下代码我自己刚刚写的 测试通过
//有问题hi我
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;
namespace WindowsApplication3
public partial class Form1 : Form
public Form1()
InitializeComponent();
bool success = RegisterHotKey(Handle, 100, KeyModifiers.Control | KeyModifiers.Alt, Keys.S);
[Flags()]
public enum KeyModifiers
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, // handle to window
int id, // hot key identifier
KeyModifiers fsModifiers, // key-modifier options
Keys vk // virtual-key code
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, // handle to window
int id // hot key identifier
);
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
UnregisterHotKey(Handle, 100);
protected override void WndProc(ref Message m)
const int WM_HOTKEY = 0x0312;
switch (m.Msg)
case WM_HOTKEY:
this.WindowState = FormWindowState.Normal;
this.Show();
this.BringToFront();
//this.ShowInTaskbar = true;
//
//this.BringToFront();
break;
base.WndProc(ref m);
private void button1_Click(object sender, EventArgs e)
Hide();
//////////////////////////////////
//Program.cs改为
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsApplication3
static class Program
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
Application.Run(new AppContext(new Form1()));
internal class AppContext : ApplicationContext
private Form mainForm;
public AppContext(Form mainForm)
this.mainForm = mainForm;
this.mainForm.Closed += new EventHandler(mainForm_Closed);
// 主窗体被关闭时,退出应用程序
void mainForm_Closed(object sender, EventArgs e)
Application.Exit();
参考技术B 上楼所说的都是调用系统的自定义按键监视,还一种方法是自己另开一条线程,实施监控键盘按键也可以,你看着选择吧,用的系统你就看下他们用到的系统API,调用修改键反应就行,不然就自己做监视
C# winform 中如何实现如下的表格
目的不是展示数据,而是实现类似多选的功能;
当然,可以用 CheckBox实现,但太麻烦了,数目比较多,而且选项的个数需要动态生成。
自己用 DataGridView 实现了:
具体代码在这里:
http://blog.csdn.net/lj22377/article/details/47024325
/// <summary>
/// 自动生成checkbox
/// </summary>
/// <param name="line">行数</param>
/// <param name="col">列数</param>
public void SetCheckBox(int line,int col)
CheckBox ck = null;
for (int i = 0; i < line; i++)
for (int j = 0; j < col; j++)
ck = new CheckBox();
ck.Location = new Point(13 * (j), 13*(i));
ck.Size = new System.Drawing.Size(13, 13);
ck.Name = "ck" + i + j;
ck.Text = "";
this.Controls.Add(ck);
非常感谢,我想过这样实现。
但这样会显得界面很凌乱,图片中的表格效果会比较好。
怎么样实现多选的效果?
追答选择一个,改变一个单元格的背景色,同时在后台设置一个数组记录下已选择的单元格坐标,取消选择,则变回原来的颜色,同时在数组中将已记录的坐标删除。
或者,你看看有没有如下属性
设置datagridview的MuiltSelect=true;
以上是关于C# WinForm如何实现全局快捷键?的主要内容,如果未能解决你的问题,请参考以下文章