为什么CreateWindowEx返回0
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?
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 returns 0?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调用 CreateWindowEx 后需要检查是否为 NULL,如果是,则可以立即调用 Marshal.GetLastWin32Error 了解原因。
You need to check for NULL after calling CreateWindowEx, and if it is, you can then immediately call Marshal.GetLastWin32Error for the reason why.
可能的原因是您忘记使用RegisterClass/Ex()注册“MyWindow”窗口类,没有正确设置窗口过程或没有正确处理WM_CREATE消息。另外,您的 P/Invoke 声明是错误的,它无法在 64 位操作系统上运行。
不要自己编写此类代码,Windows 窗体是 CreateWindowEx() 的一个非常好的包装器。
Possible reasons are that you forgot to register the "MyWindow" window class with RegisterClass/Ex(), didn't set the window procedure correctly or don't properly handle the WM_CREATE message. Also, your P/Invoke declaration is wrong, it won't work on 64-bit operating systems.
Don't write this kind of code yourself, Windows Forms is a very nice wrapper around CreateWindowEx().