IT-Berater: Theo Gottwald (IT-Consultant) > Freebasic

Hello Win in Freebasic not working

(1/2) > >>

Chris Chancellor:
Hello all

i tried compiling a Hello Win in Freebasic in order to learn its GUI but not sucessful
i'm a beginner and just wanted to learn the ropes of programming in FB

what's wrong with this code, it can compile but nothing showing on the screen?
appreciate any help


--- Code: ---' https://www.freebasic.net/wiki/wikka.php?wakka=TutMessageIntro
' Hello Win.bas
#include once "windows.bi"

'Declare Function        WinMain     ( ByVal hInstance As HINSTANCE, _
 '                                     ByVal hPrevInstance As HINSTANCE, _
 '                                     szCmdLine As String, _
 '                                     ByVal iCmdShow As Integer ) As Integer
                                 
                                 
    ''
    '' Entry point   
    ''
 '   End WinMain( GetModuleHandle( null ), null, Command$, SW_NORMAL )

'' ::::::::
'' name: WndProc
'' desc: Processes windows messages
''
'' ::::::::
Function WndProc ( ByVal hWnd As HWND, _
                   ByVal message As UINT, _
                   ByVal wParam As WPARAM, _
                   ByVal lParam As LPARAM ) As LRESULT
   
    Function = 0
   
    ''
    '' Process messages
    ''
    Select Case( message )
        ''
        '' Window was created
        ''       
        Case WM_CREATE           
            Exit Function
       
        '' User clicked the form
        Case WM_LBUTTONUP
            MessageBox NULL, "Hello world from FreeBasic", "FB Win", MB_OK
        ''
        '' Windows is being repainted
        ''
        Case WM_PAINT
            Dim rct As RECT
            Dim pnt As PAINTSTRUCT
            Dim hDC As HDC
         
            hDC = BeginPaint( hWnd, @pnt )
            GetClientRect( hWnd, @rct )
           
            DrawText( hDC, _
                      "Hello Windows from FreeBasic!", _
                      -1, _
                      @rct, _
                      DT_SINGLELINE Or DT_CENTER Or DT_VCENTER )
           
            EndPaint( hWnd, @pnt )
           
            Exit Function           
       
        ''
        '' Key pressed
        ''
        Case WM_KEYDOWN
            'Close if esc key pressed
            If( LoByte( wParam ) = 27 ) Then
                PostMessage( hWnd, WM_CLOSE, 0, 0 )
            End If

        ''
        '' Window was closed
        ''
        Case WM_DESTROY
            PostQuitMessage( 0 )
            Exit Function
    End Select
   
    ''
    '' Message doesn't concern us, send it to the default handler
    '' and get result
    ''
    Function = DefWindowProc( hWnd, message, wParam, lParam )   
   
End Function

'' ::::::::
'' name: WinMain
'' desc: A win2 gui program entry point
''
'' ::::::::
Function WinMain ( ByVal hInstance As HINSTANCE, _
                   ByVal hPrevInstance As HINSTANCE, _
                   szCmdLine As String, _
                   ByVal iCmdShow As Integer ) As Integer   
     
    Dim wMsg As MSG
    Dim wcls As WNDCLASS   
    Dim szAppName As String
    Dim hWnd As HWND
     
    Function = 0
     
    ''
    '' Setup window class
    ''
    szAppName = "HelloWin"
     
    With wcls
        .style         = CS_HREDRAW Or CS_VREDRAW
        .lpfnWndProc   = @WndProc
        .cbClsExtra    = 0
        .cbWndExtra    = 0
        .hInstance     = hInstance
        .hIcon         = LoadIcon( NULL, IDI_APPLICATION )
        .hCursor       = LoadCursor( NULL, IDC_ARROW )
        .hbrBackground = GetStockObject( WHITE_BRUSH )
        .lpszMenuName  = NULL
        .lpszClassName = StrPtr( szAppName )
    End With
         
    ''
    '' Register the window class   
    ''   
    If( RegisterClass( @wcls ) = FALSE ) Then
       MessageBox( null, "Failed to register wcls!", szAppName, MB_ICONERROR )
       Exit Function
    End If
   
    ''
    '' Create the window and show it
    ''
    hWnd = CreateWindowEx( 0, _
                           szAppName, _
                           "The Hello Program", _
                           WS_OVERLAPPEDWINDOW, _
                           CW_USEDEFAULT, _
                           CW_USEDEFAULT, _
                           CW_USEDEFAULT, _
                           CW_USEDEFAULT, _
                           NULL, _
                           NULL, _
                           hInstance, _
                           NULL )
                         

    ShowWindow( hWnd, iCmdShow )
    UpdateWindow( hWnd )
     
    ''
    '' Process windows messages
    ''
    While( GetMessage( @wMsg, NULL, 0, 0 ) <> FALSE )   
       TranslateMessage( @wMsg )
        DispatchMessage( @wMsg )
    Wend
   
  '  dim bRet as Long
  '
  ' while bRet = GetMessage (@wMsg, 0, 0, 0)
  '  if bRet = -1 then
      'show an error message
  '    print "error"
  '  else
  '    TranslateMessage @wMsg
  '    DispatchMessage @wMsg
  '  end if
 ' wend

   
    ''
    '' Program has ended
    ''
    Function = wMsg.wParam

End Function

--- End code ---

got this code from
https://www.freebasic.net/wiki/wikka.php?wakka=TutMessageIntro


Pierre Bellisle:
Chris,

unRem> Declare Function WinMain(...
unRem> End WinMain(...

Chris Chancellor:
thanxx a lot Pierre

Chris Chancellor:
one more question ----  why there is a need to declare the WinMain function
                                     in this program ?

Charles Pegge:
Hi Chris,

This line calls WinMain before WinMain is defined. ('End...' is a bit strange')


--- Code: ---End WinMain( GetModuleHandle( null ), null, Command$, SW_NORMAL )

--- End code ---

in o2 we would do similar, or use #lookahead

Navigation

[0] Message Index

[#] Next page

Go to full version