gpt4 book ai didi

c# - "Error creating window handle"定义覆盖的 WndProc 方法后

转载 作者:太空宇宙 更新时间:2023-11-03 18:29:36 26 4
gpt4 key购买 nike

我创建了一个简单的 WinForms 项目,什么也没做,只是在 Form1.cs 中添加了这段代码:

    protected override void WndProc(ref Message m)
{
}

就是这样。它编译,但抛出异常“创建窗口句柄时出错”。就像在屏幕上:

http://oi62.tinypic.com/ivgww5.jpg

另一件事是我几天前创建了一个项目,它毫无异常(exception)地使用 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);
}

当您想对传入的 WM_* 消息执行某些操作时,重写 WndProc 很有用,但是自己提供完整的 WndProc 实现确实很棘手,最简单的方法是将您不感兴趣的消息转发到基类.

关于c# - "Error creating window handle"定义覆盖的 WndProc 方法后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25389897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com