Who said WinAPI is so hard? It isn't! Here are some easy examples:
Window
Code: Select all
#Include "windows.bi"
Dim As MSG msg ' Message variable (stores massages)
Dim As HWND hWnd ' Window variable
' Create window
hWnd = CreateWindowEx( 0, "#32770", "Hello", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, 100, 100, 500, 300, 0, 0, 0, 0 )
While GetMessage( @msg, 0, 0, 0 ) ' Get message from window
TranslateMessage( @msg )
DispatchMessage( @msg )
Select Case msg.hwnd
Case hWnd ' If msg is window hwnd: get messages from window
Select Case msg.message
Case 273 ' Get message when 'X' was pressed
End
End Select
End Select
Wend
Code: Select all
#Include "windows.bi"
Dim As MSG msg ' Message variable (stores massages)
Dim As HWND hWnd, stc1 ' Window variable and object variables
' Create window
hWnd = CreateWindowEx( 0, "#32770", "Hello", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, 100, 100, 500, 300, 0, 0, 0, 0 )
' Create static box
stc1 = CreateWindowEx( 0, "STATIC", "Hello, World!", WS_VISIBLE Or WS_CHILD, 0, 0, 300, 30, hWnd, 0, 0, 0 )
While GetMessage( @msg, 0, 0, 0 ) ' Get message from window
TranslateMessage( @msg )
DispatchMessage( @msg )
Select Case msg.hwnd
Case hWnd ' If msg is window hwnd: get messages from window
Select Case msg.message
Case 273
End
End Select
End Select
Wend
Code: Select all
#Include "windows.bi"
Dim As MSG msg ' Message variable (stores massages)
Dim As HWND hWnd, btn1 ' Window variable and objects variables
' Create window
hWnd = CreateWindowEx( 0, "#32770", "Hello", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, 100, 100, 500, 300, 0, 0, 0, 0 )
' Create button
btn1 = CreateWindowEx( 0, "BUTTON", "Click Me!", WS_VISIBLE Or WS_CHILD, 0, 0, 0, 0, hWnd, 0, 0, 0 )
While GetMessage( @msg, 0, 0, 0 ) ' Get message from window
TranslateMessage( @msg )
DispatchMessage( @msg )
Select Case msg.hwnd
Case hWnd ' If msg is window hwnd: get messages from window
Select Case msg.message
Case 273
End
Case Else
Dim As RECT rct: GetClientRect( hWnd, @rct )
MoveWindow( btn1, 10, 10, rct.right-20, rct.bottom-20, TRUE )
End Select
Case btn1 ' If msg is button hwnd: get messages from button
Select Case msg.message
Case WM_LBUTTONDOWN ' If is left button pressed then show message box
MessageBox( hWnd, "Button is clicked!", "Message", MB_OK Or MB_ICONINFORMATION )
End Select
End Select
Wend
Code: Select all
#Include "windows.bi"
Dim As MSG msg
Dim As HWND hWnd, btn1, edt1
' Create window
hWnd = CreateWindowEx( 0, "#32770", "Hello", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, 100, 100, 500, 300, 0, 0, 0, 0 )
' Create button
btn1 = CreateWindowEx( 0, "BUTTON", "Button #1", WS_VISIBLE Or WS_CHILD, 20, 10, 100, 30, hWnd, 0, 0, 0 )
' Create edit box
edt1 = CreateWindowEx( 0, "EDIT", "Type text here...", ws_border Or WS_VISIBLE Or WS_CHILD Or WS_HSCROLL Or WS_VSCROLL Or ES_AUTOHSCROLL Or ES_AUTOVSCROLL Or ES_MULTILINE, 20, 50, 200, 100, hWnd, 0, 0, 0 )
While GetMessage( @msg, 0, 0, 0 )
TranslateMessage( @msg )
DispatchMessage( @msg )
Select Case msg.hwnd
Case hWnd
Select Case msg.message
Case 273
End
' If left mouse button was pressed in window area then
' check if is edit box text = "". If it is then set
' the edit box text to "Type text here" and set focus
' to the window
Case WM_LBUTTONDOWN
Dim As ZString*1024 txt
GetWindowText( edt1, txt, SizeOf( txt ) )
If txt = "" Then SetWindowText( edt1, "Type text here..." )
SetFocus( hWnd ) ' Set focus to the window
Case Else
' Create rect variable and store window size in it
Dim As RECT rct: GetClientRect( hWnd, @rct )
' Resize the edit box
MoveWindow( edt1, 20, 50, rct.right-40, rct.bottom-60, TRUE )
End Select
Case btn1
Select Case msg.message
' If left mouse button was pressed in button area then
' check if is edit box text = "". If it is then set
' the edit box text to "Type text here"
Case WM_LBUTTONDOWN
' When button is pressed set the text
' of button to "pressed"
SetWindowText( btn1, "Clicked!" )
Dim As ZString*1024 txt
GetWindowText( edt1, txt, SizeOf( txt ) )
If txt = "" Then SetWindowText( edt1, "Type text here..." )
' If left mouse button was released from the button area
' then set the button text to "Button #1" and show
' massage box with text from text box
Case WM_LBUTTONUP
SetWindowText( btn1, "Button #1" )
Dim As ZString*1024 txt
GetWindowText( edt1, txt, SizeOf( txt ) )
MessageBox( hWnd, txt, "Hello", MB_OK )
End Select
Case edt1
Select Case msg.message
' When textbox was pressed then clar the textbox text if
' text = "Type text here..."
Case WM_LBUTTONDOWN
Dim As ZString*1024 txt
GetWindowText( edt1, txt, SizeOf( txt ) )
If txt = "Type text here..." Then SetWindowText( edt1, "" )
End Select
End Select
Wend
Code: Select all
#Include "windows.bi"
' To use TrackBar include commctrl.bi and use InitCommonControls()
#Include "win/commctrl.bi"
InitCommonControls( )
Dim As MSG msg
Dim As HWND hWnd, tbr1, stc1
' Create window
hWnd = CreateWindowEx( 0, "#32770", "Hello", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, 100, 100, 500, 300, 0, 0, 0, 0 )
' Create track bar
tbr1 = CreateWindowEx( 0, TRACKBAR_CLASS, "", WS_VISIBLE Or WS_CHILD Or TBS_AUTOTICKS Or TBS_ENABLESELRANGE, 20, 10, 200, 30, hWnd, 0, 0, 0 )
' Create static box
stc1 = CreateWindowEx( 0, "STATIC", "0", WS_VISIBLE Or WS_CHILD, 225, 10, 50, 30, hWnd, 0, 0, 0 )
' Set track bar maximum value to 10
SendMessage( tbr1, TBM_SETRANGEMAX, 0, 10 )
While GetMessage( @msg, 0, 0, 0 )
TranslateMessage( @msg )
DispatchMessage( @msg )
Select Case msg.hwnd
Case hWnd
Select Case msg.message
Case 273
End
End Select
Case tbr1
Select Case msg.message
Case Else
' Get trackbar value and set that number to static box text
SetWindowText( stc1, Str( SendMessage( tbr1, TBM_GETPOS, 0, 0 ) ) )
End Select
End Select
Wend
Code: Select all
#Include "windows.bi"
' To use ProgressBar include commctrl.bi and use InitCommonControls()
#Include "win/commctrl.bi"
InitCommonControls( )
' Define pbm_setbkcolor message
#Define PBM_SETBKCOLOR 8193
Dim As MSG msg
Dim As HWND hWnd, pgb1, pgb2, pgb3, pgb4, pgb5 ' Define window and progress bars
' Create Window
hWnd = CreateWindowEx( 0, "#32770", "Hello", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, 100, 100, 500, 300, 0, 0, 0, 0 )
' Create 5 progress bars
pgb1 = CreateWindowEx( 0, PROGRESS_CLASS, "", WS_VISIBLE Or WS_CHILD, 20, 20, 460, 20, hWnd, 0, 0, 0 )
pgb2 = CreateWindowEx( 0, PROGRESS_CLASS, "", WS_VISIBLE Or WS_CHILD, 20, 50, 460, 20, hWnd, 0, 0, 0 )
pgb3 = CreateWindowEx( 0, PROGRESS_CLASS, "", WS_VISIBLE Or WS_CHILD, 20, 80, 460, 20, hWnd, 0, 0, 0 )
pgb4 = CreateWindowEx( 0, PROGRESS_CLASS, "", WS_VISIBLE Or WS_CHILD, 20, 110, 460, 20, hWnd, 0, 0, 0 )
pgb5 = CreateWindowEx( 0, PROGRESS_CLASS, "", WS_VISIBLE Or WS_CHILD, 20, 140, 460, 20, hWnd, 0, 0, 0 )
' Set progressbar ranges from 1 to 10
SendMessage( pgb1, PBM_SETRANGE, 0, MAKELPARAM( 1, 10 ) )
SendMessage( pgb2, PBM_SETRANGE, 0, MAKELPARAM( 1, 10 ) )
SendMessage( pgb3, PBM_SETRANGE, 0, MAKELPARAM( 1, 10 ) )
SendMessage( pgb4, PBM_SETRANGE, 0, MAKELPARAM( 1, 10 ) )
SendMessage( pgb5, PBM_SETRANGE, 0, MAKELPARAM( 1, 10 ) )
' Set progres bar foreground color
SendMessage( pgb1, PBM_SETBARCOLOR, 0, BGR( 255, 0, 0 ) )
SendMessage( pgb2, PBM_SETBARCOLOR, 0, BGR( 255, 255, 0 ) )
SendMessage( pgb3, PBM_SETBARCOLOR, 0, BGR( 255, 0, 255 ) )
SendMessage( pgb4, PBM_SETBARCOLOR, 0, BGR( 0, 255, 0 ) )
SendMessage( pgb5, PBM_SETBARCOLOR, 0, BGR( 0, 0, 255 ) )
' Set progress bar background color
SendMessage( pgb1, PBM_SETBKCOLOR, 0, BGR( 128, 0, 0 ) )
SendMessage( pgb2, PBM_SETBKCOLOR, 0, BGR( 128, 128, 0 ) )
SendMessage( pgb3, PBM_SETBKCOLOR, 0, BGR( 128, 0, 128 ) )
SendMessage( pgb4, PBM_SETBKCOLOR, 0, BGR( 0, 128, 0 ) )
SendMessage( pgb5, PBM_SETBKCOLOR, 0, BGR( 0, 0, 128 ) )
Dim As UInteger r1, r2, r3, r4, r5 ' Define variables to store progress values
SetTimer( hWnd, 0, 1, 0 ) ' Set timer to 1 milisecond
While GetMessage( @msg, 0, 0, 0 )
TranslateMessage( @msg )
DispatchMessage( @msg )
Select Case msg.hwnd
Case hWnd
Select Case msg.message
Case 273
End
Case WM_TIMER ' When timer was changed do some stuff
If r1 > 10 Then r1 = 0: r2 += 1
If r2 > 10 Then r2 = 0: r3 += 1
If r3 > 10 Then r3 = 0: r4 += 1
If r4 > 10 Then r4 = 0: r5 += 1
If r5 > 10 Then r5 = 0
r1 += 1
' Set progress bar values
SendMessage( pgb1, PBM_SETPOS, r1, 0 )
SendMessage( pgb2, PBM_SETPOS, r2, 0 )
SendMessage( pgb3, PBM_SETPOS, r3, 0 )
SendMessage( pgb4, PBM_SETPOS, r4, 0 )
SendMessage( pgb5, PBM_SETPOS, r5, 0 )
End Select
End Select
Wend
Code: Select all
#Include "windows.bi"
Dim As MSG msg
Dim As HWND hWnd, edt1
Dim As HMENU hMenu, hMessages, hEdit ' Menu variables
hWnd = CreateWindowEx( 0, "#32770", "Hello", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, 100, 100, 500, 300, 0, 0, 0, 0 )
edt1 = CreateWindowEx( WS_EX_CLIENTEDGE, "EDIT", "Hello", WS_VISIBLE Or WS_CHILD, 0, 0, 300, 30, hWnd, 0, 0, 0 )
' Create menus
hMenu = CreateMenu( )
hMessages = CreateMenu( )
hEdit = CreateMenu( )
' Create menus
InsertMenu( hMenu, 0, MF_POPUP, CInt( hMessages ), "&Messages" )
InsertMenu( hMenu, 0, MF_POPUP, CInt( hEdit ), "&Edit" )
' Create Messages submenus
AppendMenu( hMessages, 0, 1, "Message &Hello" )
AppendMenu( hMessages, 0, 2, "Message &Error" )
AppendMenu( hMessages, 0, 3, "Message &Information" )
AppendMenu( hMessages, 0, 4, "Message &Question" )
AppendMenu( hMessages, 0, 0, 0 )
AppendMenu( hMessages, 0, 5, "E&xit" )
' Create Edit submenus
AppendMenu( hEdit, 0, 6, "Set &Title" )
' Set hMenu to hWnd window
SetMenu( hWnd, hMenu )
' Draw menu bar
DrawMenuBar( hWnd )
While GetMessage( @msg, 0, 0, 0 )
TranslateMessage( @msg )
DispatchMessage( @msg )
Select Case msg.hwnd
Case hWnd
Select Case msg.message
Case 161
If msg.wParam = 20 Then
Dim As Integer res
' Confirm window close
res = MessageBox( hWnd, "Do you realy want to quit?", "Exit", MB_YESNO Or MB_ICONQUESTION )
If res = 6 Then End
EndIf
Case 273 ' Menu commands
Select Case msg.wParam
' Messages menu
Case 1 ' Message hello
MessageBox( hWnd, "Hello, World!", "Hello", MB_OK )
Case 2 ' Message error
MessageBox( hWnd, "This is error message!", "Error", MB_OK Or MB_ICONERROR )
Case 3 ' Message information
MessageBox( hWnd, "This is information message!", "Information", MB_OK Or MB_ICONINFORMATION )
Case 4 ' Message question
Dim As Integer res
showagain:
res = MessageBox( hWnd, "This is question message! Show again?", "Question", MB_YESNO Or MB_ICONQUESTION )
' if result = 6 (Yes) then show again the message
If res = 6 Then GoTo showagain
Case 5 ' Exit
Dim As Integer res
' Confirm exit
res = MessageBox( hWnd, "Do you realy want to quit?", "Exit", MB_YESNO Or MB_ICONQUESTION )
If res = 6 Then End
' Edit menu
Case 6 ' Set title
Dim As ZString*255 txt
GetWindowText( edt1, @txt, SizeOf( txt ) )
SetWindowText( hWnd, txt )
End Select
End Select
End Select
Wend