Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Simple "log" for PB/DLL

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple "log" for PB/DLL

    I like OLD DOS (and PB/CC) PRINT very much.
    To use Msgbox in PB/DLL sometimes not comfortable + sometimes impossible (during processing some messages in CALLBACK functions).
    I constructed two functions -
    LogPrint txt$ - To print a text
    LogStop - To view log

    This is a sample, how to use them.

    Code:
    #Compile Exe
    #Register None
    #Dim All
    #Include "win32api.INC"
    
    '------------------ Log (you can make inc-file) -----------------Global LogTxt As String
    CallBack Function LogPrint_DlgProc
       Select Case CbMsg
          Case %WM_INITDIALOG
             Local xScreen As Long, yScreen As Long
             SetWindowPos CbHndl, %HWND_TOPMOST, 0, 0, _
                GetSystemMetrics(%SM_CXSCREEN), GetSystemMetrics(%SM_CYSCREEN), 0
             Local tRect As Rect
             GetClientRect CbHndl, tRect
             Dialog Pixels CbHndl, tRect.nRight, tRect.nBottom To Units xScreen, yScreen
             Control Add TextBox, CbHndl, 501, LogTxt, _
                0.01 * xScreen, 0.01 * yScreen, 0.98 * xScreen, 0.90 * yScreen, _
                %ES_MULTILINE Or %ES_WANTRETURN Or %WS_TABSTOP Or %WS_BORDER
             Control Add Button, CbHndl, 1, "&Stop", _
                 0.35 * xScreen, 0.93 * yScreen, 0.10 * xScreen, 0.05 * yScreen, %BS_DEFAULT
             Control Add Button, CbHndl, 2, "&Continue", _
                 0.55 * xScreen, 0.93 * yScreen, 0.10 * xScreen, 0.05 * yScreen, %BS_DEFAULT
             Control Set Focus CbHndl, 1
          Case %WM_COMMAND
             If CbCtl = 1 Then LogTxt = "": Dialog End CbHndl
             If CbCtl = 2 Then Dialog End CbHndl
       End Select
    End Function
    
    Sub LogStop
       Static LastForeground As Long
       LastForeground = GetForegroundWindow
       Local hDlg As Long
       Dialog New %HWND_DESKTOP, " Log  " + Date$ + "  " + Time$, 0, 0, 0, 0, _
          %WS_POPUP Or %WS_BORDER Or %WS_CAPTION Or %WS_SYSMENU To hDlg
       Dialog Show Modal hDlg, Call LogPrint_DlgProc
       SetForegroundWindow LastForeground
    End Sub
    
    Sub LogPrint (Txt As String)
       LogTxt = LogTxt + Txt + $CRLF
    End Sub
    '-------------------------------------------------------------------------
    
    CallBack Function DlgProc
      Dim BrushLtBr As Static Long, BrushWhite As Static Long, BrushBlue As Static Long
      Select Case CbMsg
         Case %WM_INITDIALOG
             Local Lb As LOGBRUSH
             Lb.lbStyle = %BS_SOLID
             Lb.lbColor = &H80C0FF
             BrushLtBr = CreateBrushIndirect(Lb)
             LogPrint "Started at" + Time$
             LogStop
          Case %WM_DESTROY
             DeleteObject BrushLtBr
             DeleteObject BrushWhite
             DeleteObject BrushBlue
             LogPrint "Stopped at " + Time$
          Case %WM_CTLCOLORDLG
             Function = BrushLtBr
          Case %WM_COMMAND
             If CbCtl = %IDOK Then Dialog End CbHndl
          Case %WM_DESTROY
       End Select
    End Function
    
    
    Function PbMain
       Local i As Long, hDlg As Long
       For i = 1 To 100
          LogPrint "i = " + Str$(i) + "   100 + i = " + Str$(i + 100)
       Next
       LogStop
       Dialog New 0, "Test", , , 100, 200, %WS_BORDER Or %WS_SYSMENU To hDlg&
       Control Add Button, hDlg&, %IDOK, "&Quit", 30, 90, 40, 14
       Dialog Show Modal hDlg&, Call DlgProc
       LogStop
          
    End Function

  • #2
    I found that to print to console window also interesting.
    As you can see alone SUB CPrint includes some statements only
    Code:
    #Compile Exe
    #Register None
    #Dim All
    #Include "Win32Api.Inc"
    '================================
    Sub CPrint (SOut As String)
       Static hConsole As Long, cWritten As Long
       If hConsole = 0 Then AllocConsole: hConsole = GetStdHandle(-11&)
       WriteConsole hConsole, ByCopy sOut + $CrLf, Len(sOut) + 2, cWritten, ByVal 0&
    End Sub
    '===============================
    CallBack Function CB_Dlg
       If CbMsg = %WM_COMMAND Then CPrint "CbCtl = " + Str$(CbCtl) + " " + Time$
    End Function
    Function PbMain
       Local hDlg As Long
       CPrint "Started at " + Time$
       Dialog New 0, "CPrint", , , 80, 60, %WS_SYSMENU To hDlg
       Control Add Button, hdlg, 101, "Button 1", 10, 5, 60, 12
       Control Add Button, hdlg, 102, "Button 2", 10, 25, 60, 12
       Dialog Show Modal hDlg, Call CB_Dlg
    End Function
    ------------------

    Comment


    • #3
      The "console" logging is pretty cool, especially when you have two monitors and you can put the consolee screen on the second monitor.

      --Dave


      ------------------
      PowerBASIC Support
      mailto:[email protected][email protected]</A>
      Home of the BASIC Gurus
      www.basicguru.com

      Comment

      Working...
      X