为啥 CreateWindowEx 返回 0
Posted
技术标签:
【中文标题】为啥 CreateWindowEx 返回 0【英文标题】:Why does CreateWindowEx return 0为什么 CreateWindowEx 返回 0 【发布时间】:2011-01-17 11:23:39 【问题描述】:VB.NET 2008 速成版
Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Integer, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hwndParent As Integer, ByVal hMenu As Integer, ByVal hInstance As Integer, ByRef lpParam As Object) As Integer
Private Const WS_EX_APPWINDOW = &H40000
Private Const WS_EX_WINDOWEDGE = &H100&
Private Const WS_SYSMENU = &H80000
Private Const WS_CAPTION = &HC00000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_THICKFRAME = &H40000
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX)
Dim AppHandle As Int32 = System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32()
Dim WindowHandle as integer = CreateWindowEx(WS_EX_APPWINDOW Or WS_EX_WINDOWEDGE, "MyWindow", "MyTitle", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, 0, 0, AppHandle, Nothing)
CreateWindowEx 返回 0?
【问题讨论】:
【参考方案1】:调用CreateWindowEx后需要检查NULL,如果是,则可以立即调用Marshal.GetLastWin32Error了解原因。
【讨论】:
GetLastWin32Error 返回 1407 ERROR_CANNOT_FIND_WND_CLASS 1407 (0x57F) 找不到窗口类。【参考方案2】:可能的原因是您忘记使用 RegisterClass/Ex() 注册“MyWindow”窗口类,未正确设置窗口过程或未正确处理 WM_CREATE 消息。此外,您的 P/Invoke 声明是错误的,它不适用于 64 位操作系统。
不要自己编写这种代码,Windows 窗体是 CreateWindowEx() 的一个非常好的包装器。
【讨论】:
以上是关于为啥 CreateWindowEx 返回 0的主要内容,如果未能解决你的问题,请参考以下文章