' ====================================================== ' Sample Windows API program ' by MystikShadows ' ====================================================== Option explicit Option private ' ------------------------ ' This include is a must ' ------------------------ #include once "windows.bi" 'Const CB_ADDSTRING = &H143 'Const CB_SETITEMDATA = &H151 DECLARE FUNCTION WinMain ( BYVAL hInstance AS HINSTANCE, _ BYVAL hPrevInstance AS HINSTANCE, _ szCmdLine AS STRING, _ BYVAL iCmdShow AS INTEGER ) AS INTEGER ' ------------------------------ ' Shared Variable Declarations ' ------------------------------ DIM SHARED StaticLabel AS UINTEGER DIM SHARED EditControl AS UINTEGER DIM SHARED OKButtonHandle AS UINTEGER ' ------------------------------------ ' So far, only one line of main code ' ------------------------------------ END WinMain( GetModuleHandle( null ), null, COMMAND$, SW_NORMAL ) ' ========================================================== ' NAME........: WindowsCallbackProcedure ' PARAMETERS..: BYVAL hWnd AS HWND Window HAndle ' BYVAL message AS UINT Message ' BYVAL wParam AS WPARAM Message ' BYVAL lParam AS UINT Message ' RETURNS.....: 0 or the error level if any. ' ---------------------------------------------------------- ' DESCRIPTION.: This function is the windows callback ' procedure. It will be attached to the ' created window when that window is ' registered, created and shown. ' ========================================================== FUNCTION WindowsCallbackProcedure ( BYVAL hWnd AS HWND, _ BYVAL message AS UINT, _ BYVAL wParam AS WPARAM, _ BYVAL lParam AS LPARAM ) AS LRESULT ' ---------------------------- ' We'll need a few variables ' ---------------------------- DIM WindowRectangle AS RECT DIM WindowPaint AS PAINTSTRUCT DIM DeviceContextHandle AS HDC FUNCTION = 0 ' ----------------------------- ' Message Process Select Case ' ----------------------------- SELECT CASE message CASE WM_CREATE EXIT FUNCTION CASE WM_LBUTTONUP ' MessageBox NULL, "Hello world from FreeBasic", "FB Win", MB_OK CASE WM_PAINT DeviceContextHandle = BeginPaint( hWnd, @WindowPaint ) GetClientRect( hWnd, @WindowRectangle ) DrawText( DeviceContextHandle, _ "", _ -1, _ @WindowRectangle, _ DT_SINGLELINE Or DT_CENTER Or DT_VCENTER ) EndPaint( hWnd, @WindowPaint ) EXIT FUNCTION CASE WM_KEYDOWN ' -------------------------- ' Close if esc key pressed ' -------------------------- IF lobyte( wParam ) = 27 Then PostMessage( hWnd, WM_CLOSE, 0, 0 ) END IF CASE WM_DESTROY PostQuitMessage( 0 ) EXIT FUNCTION CASE WM_COMMAND SELECT CASE lParam ' ----------------------- ' Ok Button was clicked ' ----------------------- CASE OKButtonHandle MessageBox hWnd, "You have clicked the OK button!", "INFORMATION", 0 END SELECT END SELECT ' ---------------------------------------------------------------- ' If the message isn't for our program it getts sent to Window's ' default message handler for standard processing. ' ---------------------------------------------------------------- FUNCTION = DefWindowProc( hWnd, message, wParam, lParam ) END FUNCTION ' ========================================================= ' NAME........: WinMain ' PARAMETERS..: BYVAL hInstance AS HINSTANCE ' BYVAL hPrevInstance AS HINSTANCE ' szCmdLine AS STRING ' BYVAL iCmdShow AS INTEGER ' RETURNS.....: 0 or the error level if any. ' --------------------------------------------------------- ' DESCRIPTION.: This function is the windows callback ' procedure. It will be attached to the ' created window when that window is ' registered, created and shown. ' ========================================================= FUNCTION WinMain ( Byval hInstance As HINSTANCE, _ Byval hPrevInstance As HINSTANCE, _ szCmdLine As String, _ Byval iCmdShow As Integer ) As Integer ' --------------------- ' Some work variables ' --------------------- DIM WindowMessage AS MSG DIM WindowClass AS WNDCLASS DIM ApplicationClassName AS STRING DIM hWnd AS HWND DIM BackgroundColor AS INTEGER ' --------------------------------------------- ' First we populate the WindowClass structure ' --------------------------------------------- FUNCTION = 0 BackgroundColor = &HC0C0C0 ApplicationClassName = "Your First Window" WITH WindowClass .style = CS_HREDRAW Or CS_VREDRAW .lpfnWndProc = @WindowsCallbackProcedure .cbClsExtra = 0 .cbWndExtra = 0 .hInstance = hInstance .hIcon = LoadIcon( NULL, IDI_APPLICATION ) .hCursor = LoadCursor( NULL, IDC_ARROW ) .hbrBackground = GetStockObject(LTGRAY_BRUSH) .lpszMenuName = NULL .lpszClassName = STRPTR( ApplicationClassName ) END WITH ' ----------------------------------- ' Next we register the window class ' ----------------------------------- IF RegisterClass( @WindowClass ) = FALSE THEN MessageBox( null, "Failed to register WindowClass!", ApplicationClassName, MB_ICONERROR ) EXIT FUNCTION END IF ' ------------------------------------ ' We then Create and show the window ' ------------------------------------ hWnd = CreateWindowEx( 0, _ ' Extended Window Style ApplicationClassName, _ ' Registered Class Name "The Hello Program", _ ' Windows Title Caption WS_OVERLAPPEDWINDOW, _ ' Window Style Bit CW_USEDEFAULT, _ ' X Position CW_USEDEFAULT, _ ' Y Position CW_USEDEFAULT, _ ' Width of the window CW_USEDEFAULT, _ ' Height Of the window NULL, _ ' Handle to owner window NULL, _ ' handle to menu hInstance, _ ' Handle to Application Instance NULL ) ' Handle to Window Creation Data ' ----------------------------------------------------- ' Right here is where we add our controls to the form ' ----------------------------------------------------- StaticLabel = CreateWindowEx(0, "STATIC", "Enter Your Name:", WS_VISIBLE Or WS_CHILD Or WS_TABSTOP, _ 30, 30, 200, 30, hWnd, null, hInstance, null ) EditControl = CreateWindowEx(0, "EDIT", "", WS_VISIBLE Or WS_CHILD Or WS_TABSTOP Or WS_BORDER, _ 300,30, 200, 30, hWnd, null, hInstance, null) OKButtonHandle = CreateWindowEx(0, "BUTTON", "&Ok", WS_VISIBLE Or WS_CHILD Or WS_TABSTOP, _ 100,300, 80, 30, hWnd, null, hInstance, null) ' ---------------------------------- ' We're now ready to show the form ' ---------------------------------- ShowWindow( hWnd, iCmdShow ) UpdateWindow( hWnd ) ' ------------------------------------------ ' This loop processes the windows messages ' ------------------------------------------ WHILE GetMessage( @WindowMessage, NULL, 0, 0 ) <> FALSE TranslateMessage( @WindowMessage ) DispatchMessage( @WindowMessage ) WEND ' ------------------------------------------------------------- ' Program has ended, we return wParam to the operating system ' ------------------------------------------------------------- FUNCTION = WindowMessage.wParam END FUNCTION