No announcement yet.

Graphics gurus... simple graph with PBDLL?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Graphics gurus... simple graph with PBDLL?

    Ive created a very simple yet effective graph with VB6 that Id like to use in my PB apps, but graphics in PBDLL seem to be so different than in VB6 so I'm getting nowhere
    My graph is very easy to build, just start a new VB project and add just two controls - one picturebox (Autoredraw=True, Forecolor=Green, ScaleMode=Pixels, ScaleWidth=110, ScaleWidth=80), and a Timer.
    Add this code to the project, and press F5 to start. It will show randomly created values, I think it looks kinda neat considering there's not much behind it
    Dim GraphValue(100) As Integer  'Can display up to 100 different values
    Private Sub Form_Load()
    On Error Resume Next
    For I = 1 To 100
     GraphValue(I) = 0
    Next I
    Timer1.Interval = 200
    End Sub
    Private Sub Timer1_Timer()
    Dim NextVal As Integer
    'Move all values down 1 (visually all pixels move left 1)
    For I = 2 To 100
     GraphValue(I - 1) = GraphValue(I)
    Next I
    GraphValue(100) = CInt(Rnd * 70)  'Picture1 is only 70 pixels high
    For I = 2 To 100
     Picture1.Line (I - 1, 70 - GraphValue(I - 1))-(I, 70 - GraphValue(I))
    Next I
    End Sub
    I want to do this with pure PB code only, no 3rd party DLLs


  • #2
    the output looks like my last ecg print out. i'm no graphics guru, but i can point you to this
    article that should start you in the right direction...

    'message [url=""][/url] 
    'forum:  source code
    'topic:  primitive chart recorder
    'name:   iain johnstone, member
    'date:   february 27, 2001 11:53 am
    '   chart.bas for pb/dll
    '   iain johnstone.
    '   an experiment to create a chart recorder
    '   sine wave data generated as demo (plus square wave, random noise and fundamental plus octave sine
    '   select wave form in drawtrace
    '   all variations and adaptations are up to you!
    '   on switches, click on black dots by numbers
    '   on pen zero, drag black circle around.
    '   let me know if you remove the flicker in drawtrace
    '   it would be interesting to see how big this would be in c or vb.
    '   though i am not suggesting anybody tries!


    • #3
      Simple but nice example... (translation of VB a VB example)
      #Compile Exe
      #Include "
      %IMAGEBOX    = %WM_USER + 1024
      %TIMERID1    = %WM_USER + 1025
      %PWIDTH      = 250
      %PHEIGHT     = 150
      %PGRID       = 25
      %TINTERVAL   = 100
      Global Counter As Long, OldY As Long, hDCh As Long, hDC As Long, hPenB As Long, hPenC As Long
      Global hDlg As Long, hTimer As Long, hImg As Long, NewY As Long, hPenBg As Long
      CallBack Function CbMain () As Long
        Local lRet As Long, lCnt As Long
        Local ps As PAINTSTRUCT
        Static hMapBmp As Long, hMapBmpOld As Long, hMemDC As Long, _
               hPenOld As Long, rectA As RECT, rectD As RECT
        Select Case CbMsg
          Case %WM_INITDIALOG
            hPenB  = CreatePen(%PS_SOLID, 0, Rgb(250,250,  0))
            hPenC  = CreatePen(%PS_SOLID, 0, Rgb(200,  0,  0))
            hPenBg = CreatePen(%PS_SOLID, 0, Rgb(  0,  0,  0))
            rectA.nTop = 0 : rectA.nLeft = 0 : rectA.nRight = %PWIDTH : rectA.nBottom = %PHEIGHT
            hDC = GetDC(%HWND_DESKTOP)
            hMemDC = CreateCompatibleDC (hDC)
            hMapBmp = CreateCompatibleBitmap (hDC, %PWIDTH, %PHEIGHT )
            hMapBmpOld = SelectObject( hMemDC, hMapBmp )
            Call ReleaseDC(%HWND_DESKTOP, hDC)
            hPenOld = SelectObject( hMemDC, hPenB )
            PatBlt hMemDC, 0, 0, %PWIDTH, %PHEIGHT, %WHITENESS
            rectD.nTop = 5 : rectD.nLeft = 5
            MapDialogRect hdlg, rectD
            rectD.nTop  = rectD.nTop  + 3
            rectD.nLeft = rectD.nLeft + 3
          Case %WM_TIMER
            ScrollDC hMemDC, -1, 0, rectA, rectA, ByVal %NULL, ByVal %NULL
            OldY = Sin(0.15*Counter)*(%PHEIGHT\2 -1)+%PHEIGHT\2
            Incr Counter
            NewY = Sin(0.15*Counter)*(%PHEIGHT\2 -1)+%PHEIGHT\2
            If Counter Mod %PGRID = 0 Then
              SelectObject hMemDC, hPenC
              SelectObject hMemDC, hPenBg
            End If
            MoveToex hMemDC, %PWIDTH-3, 2, ByVal 0
            LineTo   hMemDC, %PWIDTH-3, 150
            For lCnt = %PGRID To %PGRID * (%PHEIGHT \ %PGRID) Step %PGRID
              SetPixel hMemDC, %PWIDTH-3, lCnt, Rgb(200,0,0)
            SelectObject hMemDC, hPenB
            MoveToEx hMemDC, %PWIDTH-3, OldY, ByVal 0
            LineTo   hMemDC, %PWIDTH-3, NewY
            InvalidateRect hDlg, ByVal 0, 0
          Case %WM_PAINT
            hDc = BeginPaint(hDlg, Ps)
            lRet = BitBlt (hDC, rectD.nLeft, rectD.nTop, %PWIDTH, %PHEIGHT, hMemDC, 0, 0, %SRCCOPY)
            EndPaint hDlg, ps
          Case %WM_DESTROY
            SelectObject hMemDC, hPenOld
            SelectObject hMemDC, hMapBmpOld
            DeleteObject hMapBmp
            DeleteDC hMemDC
            DeleteObject hPenB
            DeleteObject hPenC
            DeleteObject hPenBg
        End Select
      End Function
      Function PbMain () As Long
        Local lRet As Long
        Dialog New 0, "Dynamically scrolling graph",,,182,120, %WS_SYSMENU Or %WS_VISIBLE To hDlg
        hTimer = SetTimer (hDlg,%TIMERID1,10,ByVal %Null)
        Control Add Label, hDlg, %IMAGEBOX,"",5,5,%PWIDTH  * 0.68, %PHEIGHT * 0.64, _
                           %SS_BITMAP Or %SS_BLACKRECT, %WS_EX_CLIENTEDGE
        Control Handle hDlg, %IMAGEBOX To hImg
        Dialog Show Modal hDlg Call CbMain
      End Function
      mailto[email protected][email protected]</A>

      "Simplicity is a prerequisite for reliability"


      • #4
        Best teacher is many samples. Here is PB version of asked VB code:
        #COMPILE EXE
        %ID_IMAGE1 = %WM_USER + 10
        %ID_TIMER  = %WM_USER + 20
        %INTERVAL  = 200
        GLOBAL ihWnd AS LONG
        GLOBAL GraphValue() AS LONG
        ' Main entance
          LOCAL hDlg AS LONG
          DIALOG NEW 0, "Draw demo",,, 204, 58, %WS_CAPTION OR %WS_SYSMENU TO hDlg
          CONTROL ADD BUTTON,   hDlg, %IDCANCEL, "E&xit",  150, 40, 50, 14
          CONTROL ADD LABEL, hDlg, %ID_IMAGE1, "", 4, 4, 140, 50,, %WS_EX_CLIENTEDGE
          CONTROL HANDLE hDlg, %ID_IMAGE1 TO ihWnd
          DIALOG SHOW MODAL hDlg CALL DlgCallback
        ' Main dialog's callback procedure
        CALLBACK FUNCTION DlgCallback()
               LOCAL I AS LONG, rc AS RECT, hPen AS LONG, hBrush AS LONG, hBrushOld AS LONG
               LOCAL hDC AS LONG, memDC AS LONG, hBit AS LONG
               STATIC hTimer AS LONG : REDIM GraphValue(100)
               hTimer = SetTimer(CBHNDL, %ID_TIMER, %INTERVAL, 0) 'create timer
            CASE %WM_DESTROY
               IF hTimer THEN KillTimer CBHNDL, hTimer      'destroy timer
             CASE %WM_TIMER
                IF CBWPARAM = %ID_TIMER THEN
                   GetClientRect ihWnd, rc             'draw area
                   hDC = GetDC(ihWnd)                  'get label's device context
                   memDC = CreateCompatibleDC(hDC)     'create compatible memDC for smoother action
                   hbit  = CreateCompatibleBitmap(hDC, rc.nRight, rc.nBottom) 'and bitmap
                   hbit = SelectObject(memDC, hbit)    'select bitmap into DC
                   hBrush    = GetSysColorBrush(%COLOR_WINDOW) 'this is the background color
                   hBrushOld = SelectObject(memDC, hBrush)     'select new brush into DC, old is returned
                   FillRect memDC, rc, hBrush                  'paint background
                   SelectObject memDC, OldhBrush               'select old brush back into memDC
                   'Move all values down 1 (visually all pixels move left 1)
                   For I = 2 To 100
                      GraphValue(I - 1) = GraphValue(I)
                   Next I
                   GraphValue(100) = CLNG(Rnd * 70)
                   hPen = CreatePen(%PS_SOLID, 1, RGB(0,127,0))   'create green pen to draw with
                   hPen = SelectObject(memDC, hPen)               'select it into memDC
                   For I = 2 To 100
                      MoveTo memDC, I - 1, 70 - GraphValue(I - 1) 'move into position
                      LineTo memDC, I, 70 - GraphValue(I)         'draw line to..
                   Next I
                   DeleteObject SelectObject(memDC, hPen)         'select old pen backl, delete new
                   BitBlt hDC, 0, 0, rc.nRight, rc.nBottom, memDC, 0, 0, %SRCCOPY 'copy all to label
                   DeleteObject SelectObject(memDC, hbit)          'clean up to avoid memory leaks
                   DeleteDC memDC
                   ReleaseDC ihWnd, hDC
                END IF
          END SELECT
        Okay, Windows GDI is easy to make mistakes with. Added hBrushOld to
        WM_TIMER event, so proper brush is used and selected back into memDC..

        [This message has been edited by Borje Hagsten (edited August 03, 2001).]


        • #5
          Fwaarrrrrr, thanks! Its a rainy Friday night here in Perth so I'm staying in to play with graphics (for the first time), I'll go through those samples now as soon as I get a bourbon and coke.

          [3 minutes, 1 bourbon and coke later]
          Borje, strewth - thats virtually an exact copy of my demo, very impressed!! Thankyou so much for that, I find it a lot easier to learn PB when Ive got a direct VB sample to compare it, I'll be cooking with gas now

          Peter, thats also very cool - I love the scrolling grid behind the graph line, very nice

          Have a good night!

          [This message has been edited by Wayne Diamond (edited August 03, 2001).]


          • #6
            Check out Graphix Toolkit. It makes this stuff pretty easy.

            Russ Srole

            "There are two novels that can change a bookish fourteen-year old's life: The Lord of the Rings and Atlas Shrugged. One is a childish fantasy that often engenders a lifelong obsession with its unbelievable heroes, leading to an emotionally stunted, socially crippled adulthood, unable to deal with the real world. The other, of course, involves orcs." - John Rogers