Author Topic: Hello Win in Freebasic not working  (Read 632 times)

0 Members and 1 Guest are viewing this topic.

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 257
Hello Win in Freebasic not working
« on: July 28, 2018, 06:41:38 PM »
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: [Select]
' 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



Offline Pierre Bellisle

  • Jr. Member
  • **
  • Posts: 86
Re: Hello Win in Freebasic not working
« Reply #1 on: July 28, 2018, 07:02:11 PM »
Chris,

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

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 257
Re: Hello Win in Freebasic not working
« Reply #2 on: July 28, 2018, 07:22:18 PM »
thanxx a lot Pierre


Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 257
Re: Hello Win in Freebasic not working
« Reply #3 on: July 28, 2018, 08:00:50 PM »
one more question ----  why there is a need to declare the WinMain function
                                     in this program ?

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 660
    • Charles Pegge
Re: Hello Win in Freebasic not working
« Reply #4 on: July 28, 2018, 08:39:54 PM »
Hi Chris,

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

Code: [Select]
End WinMain( GetModuleHandle( null ), null, Command$, SW_NORMAL )

in o2 we would do similar, or use #lookahead


Online Josť Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2491
    • Josť Roca Software
Re: Hello Win in Freebasic not working
« Reply #5 on: July 28, 2018, 09:09:36 PM »
> '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.

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 257
Re: Hello Win in Freebasic not working
« Reply #6 on: July 28, 2018, 09:45:30 PM »
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


Code: [Select]
' 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

Online Josť Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2491
    • Josť Roca Software
Re: Hello Win in Freebasic not working
« Reply #7 on: July 28, 2018, 09:49:49 PM »
No idea. I never use this sloppy way of programming.

Offline Pierre Bellisle

  • Jr. Member
  • **
  • Posts: 86
Re: Hello Win in Freebasic not working
« Reply #8 on: July 29, 2018, 12:05:44 AM »
Code: [Select]
#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

Offline Chris Chancellor

  • Sr. Member
  • ****
  • Posts: 257
Re: Hello Win in Freebasic not working
« Reply #9 on: July 29, 2018, 01:51:57 AM »
Thanxx a lot Pierre

a very concise tweak