Mar*_*all 1 c# winapi wndproc visual-studio-2010
我创建了一个简单的WinForms项目,没有做任何事情,只是在Form1.cs中添加了这段代码:
protected override void WndProc(ref Message m)
{
}
Run Code Online (Sandbox Code Playgroud)
就是这样.它编译,但抛出异常"创建窗口句柄时出错".就像屏幕上一样:
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()
Run Code Online (Sandbox Code Playgroud)
您正在重写WndProc方法而不处理每个可能的WM_*消息,因此无法正确处理它们.
要解决此问题,您必须将消息传递给base.WndProc(..)
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
}
Run Code Online (Sandbox Code Playgroud)
当您想要对某些传入的WM_*消息执行某些操作时,覆盖WndProc很有用,但是在您自己中提供完整的WndProc实现非常棘手,最简单的方法是将您不感兴趣的消息转发给基类.