• Welcome to Jose's Read Only Forum 2023.
 

Hello Win in Freebasic not working

Started by Chris Chancellor, July 28, 2018, 06:41:38 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

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


' 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


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


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')


End WinMain( GetModuleHandle( null ), null, Command$, SW_NORMAL )


in o2 we would do similar, or use #lookahead


José Roca

> 'End...' is a bit strange'

Because FreeBasic does not call an entry point procedure such Main or WinMain (PBMain in PowerBasic), the way it is being used in the example is a clever way to simulate it. It calls WinMain (the name doesn't matter, you can use wathever you wish), exists the application and returns the value returned by the called function to the operating system. Other uses of End, such simply mark the end of a program, are discouraged. In the example, you can remove Function WinMain/End Function, but then all the variables declared in it will become global.

Chris Chancellor

Thanxx everyone

but i have removed the FUNCTION WINMAIN  and no declaration

unfortunately the compiled program cannot run, what's wrong with this program?

the code is as below



' 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

   dim hInstance As HINSTANCE
   dim hPrevInstance As HINSTANCE
   dim szCmdLine As String
   dim iCmdShow 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 )
       goto gitout
      ' 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
   
 
     gitout :
   
    ''
    '' Program has ended
    ''
   ' Function = wMsg.wParam

'End Function

José Roca

No idea. I never use this sloppy way of programming.

Pierre Bellisle


#include once "windows.bi"

'Declare Function        WinMain     ( ByVal hInstance As HINSTANCE, _
'                                      ByVal hPrevInstance As HINSTANCE, _
'                                      ByRef szCmdLine As String, _
'                                      ByVal iCmdShow As Integer ) As Integer
'
'
'    ''
'    '' Entry point
'    ''
'    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, _
'                   ByREf 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     = GetModuleHandle(null) '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, _
                           GetModuleHandle(null), _ 'hInstance hInstance, _
                           NULL )


    ShowWindow( hWnd, SW_NORMAL) 'iCmdShow )
    UpdateWindow( hWnd )

    ''
    '' Process windows messages
    ''
    While( GetMessage( @wMsg, NULL, 0, 0 ) <> FALSE )
        TranslateMessage( @wMsg )
        DispatchMessage( @wMsg )
    Wend


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

'End Function

Chris Chancellor

Thanxx a lot Pierre

a very concise tweak