创建窗口句柄错误;定义后重写的WndProc方法
本文关键字:重写 WndProc 方法 定义 窗口句柄 错误 创建 | 更新日期: 2023-09-27 18:18:47
我创建了一个简单的WinForms项目,没有做任何事情,只是在Form1.cs中添加了以下代码:
protected override void WndProc(ref Message m)
{
}
就是这样。它编译,但抛出一个异常"创建窗口句柄错误"。就像在屏幕上一样:
http://oi62.tinypic.com/ivgww5.jpg另一件事是我有一个项目创建前几天,使用WndProc没有任何例外。谁能告诉我问题出在哪里?
谢谢你的帮助,
顺便说一句。堆栈跟踪:
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Form.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsFormsApplication1.Program.Main() in C:'Users'Johny'Documents'Visual Studio 2010'Projects'Temp'WindowsFormsApplication1'WindowsFormsApplication1'Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
您正在重写WndProc方法,而不是处理每个可能的WM_*消息,因此不能正确处理它们。
要解决这个问题,你必须将消息传递给base.WndProc(..)
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
}
覆盖WndProc是有用的,当你想做一些传入的WM_*消息,但是提供一个完整的WndProc实现在你自己是非常棘手的,最简单的方法是转发消息你不感兴趣的基类