Hope you are keeping well.
I've just been looking at your WinAPI template and one thing puzzles me. You don't appear to actually register the window class. If you insert "RegisterClass @wcls", then your error checking code brings up the Messagebox "Failed to register wcls". Is the window class automatically registered, or should the statement "RegisterClass @wcls" be included?
I've looked online and I've noticed that a few WinAPI code submissions do include RegisterClass wcls (or whatever thy call the window class), but don't include any error checking that it has been successfully registered. When the error check is included, the Messagebox with the error pops up.
Code: Select all
'===============================================================================
' WinAPI_GUI.bas
' Windows API GUI
' Vorlage mit Menue, Textbox, Editor, Buttons, Listbox
' Erstellt am 20.02.2021
' Letzte Bearbeitung am 22.02.2021
'===============================================================================
#include once "windows.bi"
Dim Shared As HMENU hMenu, hDatei, hHilfe
Dim Shared As HWND Edit1, Edit2, List1, Button1, Button2
Function WndProc(ByVal hWnd As HWND, ByVal Msg As UINT, ByVal wParam As WPARAM, _
ByVal lParam As LPARAM ) As LRESULT
Dim As HFONT Font
Dim As Integer i
Dim As ZString*1024 text
Function = 0
Select Case Msg
Case WM_CREATE
'Menü:
hMenu = CreateMenu()
hDatei = CreateMenu()
hHilfe = CreateMenu()
InsertMenu(hMenu, 0, MF_POPUP, CInt(hDatei), "Datei")
InsertMenu(hMenu, 0, MF_POPUP, CInt(hHilfe), "Hilfe")
AppendMenu(hDatei, 0, 1, "Neu" )
AppendMenu(hDatei, 0, 2, "Oeffnen" )
AppendMenu(hDatei, 0, 3, "Speichern" )
AppendMenu(hDatei, 0, 4, "Beenden" )
AppendMenu(hHilfe, 0, 5, "?")
SetMenu(hwnd, hMenu)
'Controls:
Var hStatic1 = CreateWindowEx(0, "STATIC", "Geben Sie hier einen Text ein:", _
WS_VISIBLE Or WS_CHILD, _
20, 20, 200, 20, hWnd, 0, 0, 0)
Edit1 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Text ...", WS_BORDER Or WS_VISIBLE Or WS_CHILD Or ES_AUTOHSCROLL, _
20, 50, 200, 20, hWnd, 0, 0, 0 )
Button1 = CreateWindowEx(0, "BUTTON", "Kopieren", WS_VISIBLE Or WS_CHILD, _
60, 80, 100, 20, hWnd, 0, 0, 0 )
Edit2 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Bitte Text eingeben!", _
WS_BORDER Or WS_VISIBLE Or WS_CHILD Or WS_HSCROLL Or WS_VSCROLL Or ES_MULTILINE Or ES_WANTRETURN, _
20, 120, 300, 200, hWnd, 0, 0, 0 )
Button2 = CreateWindowEx(0, "BUTTON", "Kopieren", WS_VISIBLE Or WS_CHILD, _
340, 200, 100, 20, hWnd, 0, 0, 0 )
List1 = CreateWindowEx(WS_EX_CLIENTEDGE, "LISTBOX", "", _
WS_BORDER Or WS_VISIBLE Or WS_CHILD Or WS_VSCROLL Or LBS_NOTIFY, _
20, 350, 200, 200, hWnd, 0, 0, 0 )
'Schriftart fuer den Editor:
Font = CreateFont(0, 0, 0, 0, 0, 0, 0, 0, ANSI_CHARSET, FALSE, FALSE, _
DEFAULT_QUALITY, DEFAULT_PITCH or FF_ROMAN, "Courier New")
SendMessage(Edit2, WM_SETFONT, Cast(WPARAM, Font), True)
SetWindowText(Edit2, "Bitte hier einen Text schreiben!")
'Listbox befuellen:
For i = 0 To 20
text = "Eintrag Nr. " + Str(i)
SendMessage(List1, LB_ADDSTRING, 0, Cast(LPARAM, @text))
Next
Case WM_COMMAND
Select Case LoWord(wParam)
'Menü:
Case 1
MessageBox(0, "Neue Datei ...", "Datei", 0)
Case 2
MessageBox(0, "Oeffnen ...", "Datei", 0)
Case 3
MessageBox(0, "Speichern ...", "Datei", 0)
Case 4
SendMessage(hWnd, WM_CLOSE, 0, 0)
Case 5
MessageBox(0, "Ich kann Ihnen leider nicht helfen!", "Hilfe", 0)
End Select
Select Case HiWord(wParam)
Case BN_CLICKED
Select Case lParam
Case Button1
'Text aus Edit1 auf die Konsole kopieren:
GetWindowText(Edit1, text, SizeOf(text))
text = RTrim(text)
Print text
Case Button2
'Text aus Edit2 auf die Konsole kopieren:
GetWindowText(Edit2, text, SizeOf(text))
text = RTrim(text)
Print text
End Select
Case LBN_SELCHANGE
If lParam = List1 Then
'Gewählten Index mit Text auf Konsole ausgeben
i = SendMessage(List1, LB_GETCURSEL, 0, 0)
SendMessage(List1, LB_GETTEXT, i, Cast(LPARAM, @text))
text = RTrim(text)
Print i; Space(1); text
End If
End Select
Case WM_PAINT
Case WM_SIZE
Case WM_KEYDOWN
'Beenden mit ESC-Taste: Funktioniert nicht zuverlässig
If(LoByte(wParam) = 27) Then PostMessage(hWnd, WM_CLOSE, 0, 0)
Case WM_DESTROY
PostQuitMessage(0)
Exit Function
End Select
Return DefWindowProc(hWnd, Msg, wParam, lParam)
End Function
Function WinMain(ByVal hInstance As HINSTANCE, ByVal hPrevInstance As HINSTANCE, _
ByVal szCmdLine As zstring ptr, ByVal iCmdShow As integer) As integer
Dim Msg As MSG
Dim wcls As WNDCLASS
Dim hWnd As HWND
Function = 0
with wcls
.style = CS_HREDRAW or CS_VREDRAW
.lpfnWndProc = @WndProc
.cbClsExtra = 0
.cbWndExtra = 0
.hInstance = hInstance
.hIcon = LoadIcon(0, IDI_APPLICATION )
.hCursor = LoadCursor(0, IDC_ARROW )
.hbrBackground = Cast(HBRUSH,COLOR_WINDOW)
.lpszMenuName = 0
.lpszClassName = @"MainWindow"
End With
RegisterClass @wcls
If(RegisterClass( @wcls) = FALSE ) Then
MessageBox(0, "Failed to register wcls", "Error", MB_ICONERROR )
Exit Function
End If
'Fenster:
hWnd = CreateWindowEx(0, @"MainWindow", "Windows GUI", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, _
CW_USEDEFAULT, CW_USEDEFAULT, 500, 650, 0, 0, hInstance, 0 )
ShowWindow(hWnd, iCmdShow)
UpdateWindow(hWnd)
While( GetMessage(@Msg, 0, 0, 0 ) <> FALSE )
TranslateMessage(@Msg )
DispatchMessage(@Msg )
Wend
Return Msg.wParam
End Function
WinMain(GetModuleHandle(0), 0, Command(), SW_NORMAL)
End
Run the code and you'll see what I mean.