110,533
社区成员
发帖
与我相关
我的任务
分享
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 测试热键
{
public partial class Form1 : Form
{
[DllImport("user32")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control, Keys vk);
//注册热键的api
[DllImport("user32")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Clipboard.Clear();//先清空剪贴板防止剪贴板里面先复制了其他内容
//注册热键(窗体句柄,热键ID,辅助键,实键)
RegisterHotKey(Handle, 100, 2, Keys.F10);// 热键:Control +F10
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//注消热键(句柄,热键ID)
UnregisterHotKey(Handle, 100);
}
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey(m);//调用主处理程序
break;
}
base.WndProc(ref m);
}
void ProcessHotkey(Message m)
{
//这里是处理快捷键的地方,为空就好了
IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
string sid = id.ToString();
switch (sid)
{
case "100": DecreseVolumnb();
break;
}
}
void DecreseVolumnb()
{
MessageBox.Show("test");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
Clipboard.Clear();//先清空剪贴板防止剪贴板里面先复制了其他内容
//注册热键(窗体句柄,热键ID,辅助键,实键)
RegisterHotKey(Handle, 100, 2, Keys.F10);// 热键:Control +F10
MessageBox.Show("handle = " + this.Handle.ToInt32() ); //<---
if (this.Visible == true)
{
this.Hide();
this.ShowInTaskbar = false; //<---
MessageBox.Show("handle = " + this.Handle.ToInt32() ); //<---
}
else
{
this.Visible = true;
this.ShowInTaskbar = true; //<---
MessageBox.Show("handle = " + this.Handle.ToInt32() ); //<---
this.WindowState = FormWindowState.Normal;
this.BringToFront();
}
}