C# 为什么不是';";受保护覆盖无效WndProc(参考消息m)“;工作
我一直试图弄明白这一点已经很长时间了;我已经搜索了好几次,读了很多关于这方面的文章和问题,我都记不起来了,我似乎不知道到底出了什么问题。这是一个小程序,我一直试图编译它来测试生成一个应用程序使用的热键 我一直试图找出的测试来源如下:C# 为什么不是';";受保护覆盖无效WndProc(参考消息m)“;工作,c#,winforms,wndproc,registerhotkey,C#,Winforms,Wndproc,Registerhotkey,我一直试图弄明白这一点已经很长时间了;我已经搜索了好几次,读了很多关于这方面的文章和问题,我都记不起来了,我似乎不知道到底出了什么问题。这是一个小程序,我一直试图编译它来测试生成一个应用程序使用的热键 我一直试图找出的测试来源如下: using System; using System.Windows.Forms; using System.Drawing; using System.Runtime.InteropServices; namespace Prg { class Main
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注册热键的例子中都有“protectedoverridewndproc”方法,每个人都说它对他们很好,但我不明白为什么它在我的一生中都不起作用。如果有人能帮我解决这个问题,我将不胜感激。我使用的是64位Windows 7 Professional,csc.exe的路径是C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
谢谢:)
编辑
我现在已经把它编译好了,但是现在的问题是它似乎没有注册,没有热键,或者至少根本没有按键。我的代码中有错误吗
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(newmainclass());但即使窗体被聚焦,它仍然没有检测到按键
编辑
问题得以解决,这要感谢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();
}
}
}
2个错误:
- 在显示f1之前,必须先注册热键。交换最后两行
- 目前,您为MainClass重写WndProc,而不是为每个窗体重写WndProc。因此,表单f1继承的是基本form.WndProc,而不是覆盖的form.WndProc。所以只要将f1声明为MainClass,它就可以工作了
- 在显示f1之前,必须先注册热键。交换最后两行
- 目前,您为MainClass重写WndProc,而不是为每个窗体重写WndProc。因此,表单f1继承的是基本form.WndProc,而不是覆盖的form.WndProc。所以只要将f1声明为MainClass,它就可以工作了
对象
派生的。没有WndProc
,WndProc是以形式声明的,所以要重写它,您需要位于从它派生的类中。表单派生类也是您通常添加按钮等的地方。您的类不是从对象
派生的。没有WndProc
,WndProc是以形式声明的,所以要重写它,您需要位于从它派生的类中。表单派生类也是您通常添加按钮等的地方。嘿,谢谢您的总结。你不仅解决了这个问题,而且用两个简单的句子消除了不少困惑。谢谢:)嘿,谢谢你的总结。你不仅解决了这个问题,而且用两个简单的句子消除了不少困惑。谢谢:)