Create New Window with CreateWindowEx with API's

Hello,

I am attempting to make a new window with the CreateWindowEx call. After working on this for a few weeks I have created much spaghetti code ( :frowning: ) and am stuck on the RegisterClassEx(wc) command.

Here is the code that has been put together. If anyone has a nicer example, then than would be good.

There is no returned value from RegisterClassEx and Xojo crashes before MyGetLastError is called.

[code] Declare Function GetModuleHandle Lib “kernel32” Alias “GetModuleHandleA” (ByVal lpModuleName As Integer) As Integer
Declare Function LoadIcon Lib “user32” Alias “LoadIconA” (ByVal hInstance As Integer, ByVal lpIconName As Integer) As Integer
Declare Function LoadCursor Lib “user32” Alias “LoadCursorA” (ByVal hInstance As Integer, ByVal lpCursorName As Integer) As Integer
Declare Function GetStockObject Lib “gdi32” (ByVal fnObject As Integer) As Integer
Declare Function RegisterClassEx Lib “user32” Alias “RegisterClassExA” (lpwcx As WNDCLASSEX) As Integer
Declare Function CreateWindowEx Lib “user32” Alias “CreateWindowExA” (ByVal dwExStyle As Integer, ByVal lpClassName As CString, ByVal lpWindowName As CString, 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, lpParam As WNDCLASSEX) As Integer
Declare Function ShowWindow Lib “user32” (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
Declare Function UpdateWindow Lib “user32” (ByVal lhwnd As Integer) As Integer
Declare Sub PostQuitMessage Lib “user32” (ByVal nExitCode As Integer)
Declare Function DefWindowProc Lib “user32” Alias “DefWindowProcA” (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Declare Function TextOut Lib “gdi32” Alias “TextOutA” (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer, ByVal lpString As CString, ByVal nCount As Integer) As Integer
Declare Function RegisterWindowMessage Lib “user32” Alias “RegisterWindowMessageA” (lpString As CString) As Integer
Declare Function RegisterClassExA Lib “user32” (ByRef pcWndClassEx As WNDCLASSEX) As Integer
Declare Function SendMessage Lib “user32” Alias “SendMessageA” (hwnd As Integer, wMsg As Integer, wParam As Integer, lParam As Integer) As Integer
Declare Function GetLastError lib “kernel32” () as Integer
Declare Function DestroyWindow Lib “user32” (ByVal hwnd As Integer) As Integer

Const WS_CHILD = &H40000000
Const CW_USEDEFAULT = &H80000000
Const SW_NORMAL = 1
Const WS_EX_STATICEDGE = &H20000
Const WS_EX_TRANSPARENT = &H20
Const IDI_APPLICATION = 32512
Const IDC_ARROW = 32512
Const WS_SYSMENU = &H80000
Const WS_OVERLAPPED = &H0
Const WC_SYSTRAY As String = “Shell_TrayWnd”

Dim mWnd As Integer
Dim MyClassName as CString
MyClassName = “MyWindowsClass”
Dim MyWndProc as Ptr

Dim wc as WNDCLASSEX
wc.cbSize = 48
wc.style = 0
wc.lpfnWndProc = MyWndProc
wc.cbClsExtra = 0
wc.cbWndExtra = 0
wc.hInstance = InstanceHandle
wc.hIcon = LoadIcon(0, IDI_APPLICATION)
wc.hCursor = LoadCursor(0, IDC_ARROW)
wc.hbrBackground = 0
wc.lpszMenuName = “test”
wc.lpszClassName = WC_SYSTRAY
wc.hIconSm = LoadIcon(0, IDI_APPLICATION)

Dim AValue as integer
AValue = RegisterClassEx(wc)//Error occurs here
MsgBox "AValue = " + CStr(AValue)
Call MyGetLastError

Dim MyCString as CString
MyCString = “test”

'Create a new label
mWnd = CreateWindowEx(0 , MyCString, MyCString, WS_OVERLAPPED, CW_USEDEFAULT, CW_USEDEFAULT, 240,120,0,0,InstanceHandle, wc)
[/code]

Here is the download link to the example project: New Window.xojo_binary_project

Thanks for your help. :slight_smile:

Thanks for the help from an anonymous person.

Try passing the structure ByRef:

Declare Function RegisterClassEx Lib "user32" Alias "RegisterClassExA" (ByRef lpwcx As WNDCLASSEX) As Integer

I hope you plan on sharing the resulting code. Or will it remain anonymous as well ?

It takes a while to make it easy to read.

Here is a simple window (although it is not simple :slight_smile: ):

[code] //Example 6-1
//I Wish I Knew How To… Implement Win32 Declares with Xojo on Windows
//Create variables for the position and size of the window
Dim XPos, YPos, WindowWidth, WindowHeight as integer
XPos = 100
YPos = 100
WindowWidth = 200
WindowHeight = 300

//Get the desktop window handle
Dim mControlHandle as Integer
Declare Function GetDesktopWindow Lib “User32” () as Integer
mControlHandle = GetDesktopWindow

//Retrieve the application instance
Dim hInstance as Integer
Declare Function GetModuleHandleA Lib “Kernel32” ( name as Integer ) as Integer
hInstance = GetModuleHandleA( 0 )

//Create the functions from C++ code
Declare Function RegisterClassExA Lib “user32” (ByRef lpwcx As WNDCLASSEX) As Integer

Declare Function CreateWindowExA Lib “user32” (_
dwExStyle As Integer, lpClassName As CString, lpWindowName As CString, _
dwStyle As Integer, X As Integer, Y As Integer, width As Integer, height As Integer, _
hWndParent As Integer, hMenu As Integer, hInstance As Integer, lpParam As Integer) As Integer

//Register the class
Dim wcx As WNDCLASSEX
wcx.cbSize = wcx.Size
wcx.Style = 0
wcx.lpfnWndProc = AddressOf WindowProc
wcx.hInstance = hInstance
Dim className as String = “test”
wcx.lpszClassName = className

//Was registration successful
Dim RegClassAnswer As Integer = RegisterClassExA(wcx)
If RegClassAnswer = 0 Then
MsgBox “Error with RegisterClassExA”
End If

//Create window constants
Const WS_EX_TOOLWINDOW = &H00000080
Const WS_CLIPSIBLINGS = &H4000000

//Capture the handle of thew new window
Dim windowHandle as integer
//Create a title for the new window
Dim MyTitle as Cstring = “A New Window”

//Call the function
windowHandle = CreateWindowExA(WS_EX_TOOLWINDOW, className, MyTitle, WS_CLIPSIBLINGS, Xpos, Ypos, windowWidth, windowHeight, mControlHandle, 0, hInstance, 0)

//Create the C++ Show window function
Declare Function ShowWindow Lib “user32.dll” (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
Const SW_SHOWNOACTIVATE = 4

//Actually show the window
Call ShowWindow(WindowHandle, SW_SHOWNOACTIVATE)[/code]

Thank you for sharing, Eugene.