为什么CreateWindowEx返回0

时间:2010-02-16 16:14:34

标签: vb.net winapi user32

VB.NET 2008 Express

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?

2 个答案:

答案 0 :(得分:2)

调用CreateWindowEx后需要检查NULL,如果是,则可以立即调用Marshal.GetLastWin32Error

答案 1 :(得分:1)

可能的原因是您忘记使用RegisterClass / Ex()注册“MyWindow”窗口类,没有正确设置窗口过程或者没有正确处理WM_CREATE消息。此外,您的P / Invoke声明是错误的,它不适用于64位操作系统。

不要自己编写这种代码,Windows Forms是一个围绕CreateWindowEx()的非常好的包装。