我已经尝试了很长时间了。我已经搜索了好几次,并且阅读了更多关于此的文章和问题,但我似乎无法弄清楚到底出了什么问题。这是一个小程序,我一直在尝试编译以测试生成应用程序使用的热键。
我一直试图弄清楚的测试来源如下:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
namespace Prg
{
class MainClass
{
[DllImport("User32.dll")]
private static extern int RegisterHotKey(IntPtr hWnd, int id, int
fsModifiers, int vk);
[DllImport("User32.dll")]
private static extern int UnregisterHotKey(IntPtr hWnd, int id);
public static Form f1 = new Form();
public static int Register(Form f)
{
IntPtr ip = f.Handle;
return RegisterHotKey(ip, 1, 0, (int)Keys.Escape);
}
public static void b1_click(object sender, EventArgs e)
{
//Blah Blah stuff
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312)
{
MessageBox.Show("wow");
}
base.WndProc(ref m);
}
public static void Main()
{
Button b1 = new Button();
b1.Location = new Point(10, 10);
b1.Text = "wow";
b1.Click += new EventHandler(b1_click);
f1.Width = 200;
f1.Height = 200;
f1.Controls.Add(b1);
f1.ShowDialog();
Register(f1);
}
}
}
我一直在使用 C# 4.0 编译 csc.exe。每次我尝试编译这个或类似的代码时,我都会收到这个错误:
Main.csx(37,27): 错误 CS0115: 'Prg.MainClass.WndProc(System.Windows.Forms.Message)': 找不到合适的方法来覆盖
使用 User32.dll 注册热键的每个示例都在其中包含“受保护的覆盖 WndProc”方法,每个人都说它对他们来说工作得很好,但我不明白为什么它不能在生活中工作我的。如果有人可以帮助我解决这个问题,将不胜感激。我使用的是 Windows 7 Professional 64 位,csc.exe 的路径是 C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
谢谢 :)
编辑
我现在已经编译好了,但是现在的问题是它似乎没有注册和热键,或者至少根本没有拿起 KeyPress。我的代码有错误吗?
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
namespace Prg
{
class MainClass : Form
{
[DllImport("User32.dll")]
private static extern int RegisterHotKey(IntPtr hWnd, int id, int
fsModifiers, int vk);
[DllImport("User32.dll")]
private static extern int UnregisterHotKey(IntPtr hWnd, int id);
public static Form f1 = new Form();
public static int Register(Form f)
{
IntPtr ip = f.Handle;
return RegisterHotKey(ip, 1, 0, (int)Keys.Escape);
}
public static void b1_click(object sender, EventArgs e)
{
//Blah Blah stuff
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312)
{
MessageBox.Show("wow");
}
base.WndProc(ref m);
}
public static void Main()
{
Button b1 = new Button();
b1.Location = new Point(10, 10);
b1.Text = "wow";
b1.Click += new EventHandler(b1_click);
f1.Width = 200;
f1.Height = 200;
f1.Controls.Add(b1);
f1.ShowDialog();
Register(f1);
}
}
}
我已经尝试了几种不同的解决方案,但它们都没有让热键工作。我什至尝试重写源代码以使用 Application.Run(new MainClass()); 但即使表单获得焦点,它仍然没有检测到 Keypress。
编辑
多亏了 zzxyz 帮助编译它,Antoine 帮助我修复了代码中的错误,问题得以解决。多谢你们。这是编译和工作的代码,适用于任何可能遇到相同问题或只是喜欢通过示例学习的人。再次感谢。
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
namespace Prg
{
class MainClass : Form
{
[DllImport("User32.dll")]
private static extern int RegisterHotKey(IntPtr hWnd, int id, int
fsModifiers, int vk);
[DllImport("User32.dll")]
private static extern int UnregisterHotKey(IntPtr hWnd, int id);
public static MainClass f1 = new MainClass();
public static int Register(Form f)
{
IntPtr ip = f.Handle;
return RegisterHotKey(ip, 1, 0, (int)Keys.Escape);
}
public static void b1_click(object sender, EventArgs e)
{
//Blah Blah stuff
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312)
{
MessageBox.Show("wow");
}
base.WndProc(ref m);
}
public static void Main()
{
Button b1 = new Button();
b1.Location = new Point(10, 10);
b1.Text = "wow";
b1.Click += new EventHandler(b1_click);
f1.Width = 200;
f1.Height = 200;
f1.Controls.Add(b1);
Register(f1);
f1.ShowDialog();
}
}
}