Announcement

Collapse
No announcement yet.

draw a line in a graphic control???

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

  • draw a line in a graphic control???

    Hi guys,

    I'm a real newbie, coming from the PBCC side of things. I am trying to write my first program with PB9, and it's not going well.

    All the program does is draw a line in a graphic control. Alas, it's not drawing the line.

    Can someone look at this and enlighten me?

    Thanks,

    Mike...

    Code:
    #COMPILE EXE
    #DIM ALL
    %usemacros = 1
    #INCLUDE "win32api.inc"
    #INCLUDE "commctrl.inc"
    #INCLUDE "PBForms.Inc"
    
    'constants
    %idd_dialog1 = 101
    %idc_button1 = 1001
    %idc_graphic1 = 1002
    
    GLOBAL hDlg AS DWORD
    GLOBAL mylabel AS STRING
    GLOBAL hParent AS DWORD
    GLOBAL hWin AS DWORD
    
    'declarations
    DECLARE CALLBACK FUNCTION showDialog1proc()
    DECLARE FUNCTION ShowDialog1(BYVAL hParent AS DWORD) AS LONG
    
    'main entry point
    FUNCTION PBMAIN () AS LONG
        'PBFormsInitComCtrls (%icc_win95_classes or %icc_date_classes or_
        '%icc_internet_classes)
    
        showDialog1 %HWND_DESKTOP
    END FUNCTION
    
    'callbacks
    CALLBACK FUNCTION ShowDialog1Proc()
    'LOCAL hDlg AS DWORD
    LOCAL hWin AS DWORD
        SELECT CASE AS LONG CBMSG
            CASE %WM_COMMAND
                SELECT CASE AS LONG CBCTL
                CASE %idc_button1
                    IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                        'mylabel$ =  "hDlg = " + str$(hDlg)
                        'msgbox mylabel$
                        
                        'drawline
                        GRAPHIC ATTACH hDlg, %idc_graphic1
                        GRAPHIC COLOR %RED, RGB(0,0,191)
                        GRAPHIC LINE (10,-10)-(50,-50)
                    END IF
                END SELECT
        END SELECT
    END FUNCTION
    
    
    'dialogs
    FUNCTION showdialog1(BYVAL hParent AS DWORD) AS LONG
        LOCAL Rslt AS LONG
    '    LOCAL hDlg AS DWORD
    
        DIALOG NEW hParent, "Dialog1",384,184,201,121,%WS_POPUP OR %WS_SYSMENU OR _
        %WS_BORDER OR %WS_DLGFRAME OR %DS_MODALFRAME, TO hDlg
        CONTROL ADD BUTTON, hDlg, %IDC_BUTTON1, "Button1",68,90,75,20
        CONTROL ADD GRAPHIC, hDlg, %idc_graphic1, "", 50,10,100,75,%WS_BORDER
        
    
        DIALOG SHOW MODAL hDlg, CALL showDialog1Proc TO Rslt
    
    
        FUNCTION = Rslt
    END FUNCTION

  • #2
    Hi Mike,

    You're drawing your line outside of the graphic control:
    Code:
    GRAPHIC LINE (10,[COLOR="Red"]-10[/COLOR])-(50,[COLOR="Red"]-50[/COLOR])
    Those Y-coordinates are negative. Change to:
    Code:
    GRAPHIC LINE (10,10)-(50,50)
    and you'll see it.

    Regards,

    Pete.

    Comment


    • #3
      Ah finally, thanks Pete.

      Next problem... I removed the global variables and went back to the local variables, like my examples showed. Now it doesn't work again.

      Is hDlg supposed to be local or global?

      Mike...

      Code:
      #COMPILE EXE
      #DIM ALL
      %usemacros = 1
      #INCLUDE "win32api.inc"
      #INCLUDE "commctrl.inc"
      #INCLUDE "PBForms.Inc"
      
      'constants
      %idd_dialog1 = 101
      %idc_button1 = 1001
      %idc_graphic1 = 1002
      
      'GLOBAL hDlg AS DWORD
      'GLOBAL mylabel AS STRING
      'GLOBAL hParent AS DWORD
      'GLOBAL hWin AS DWORD
      
      'declarations
      DECLARE CALLBACK FUNCTION showDialog1proc()
      DECLARE FUNCTION ShowDialog1(BYVAL hParent AS DWORD) AS LONG
      
      'main entry point
      FUNCTION PBMAIN () AS LONG
          'PBFormsInitComCtrls (%icc_win95_classes or %icc_date_classes or_
          '%icc_internet_classes)
      
          showDialog1 %HWND_DESKTOP
      END FUNCTION
      
      'callbacks
      CALLBACK FUNCTION ShowDialog1Proc()
      LOCAL hDlg AS DWORD
      LOCAL hWin AS DWORD
          SELECT CASE AS LONG CBMSG
              CASE %WM_COMMAND
                  SELECT CASE AS LONG CBCTL
                  CASE %idc_button1
                      IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                          'mylabel$ =  "hDlg = " + str$(hDlg)
                          'msgbox mylabel$
      
                          'drawline
                          GRAPHIC ATTACH hDlg, %idc_graphic1
                          GRAPHIC COLOR %RED, RGB(0,0,191)
                          GRAPHIC LINE (10,10)-(50,50)
                      END IF
                  END SELECT
          END SELECT
      END FUNCTION
      
      
      'dialogs
      FUNCTION showdialog1(BYVAL hParent AS DWORD) AS LONG
          LOCAL Rslt AS LONG
          LOCAL hDlg AS DWORD
      
          DIALOG NEW hParent, "Dialog1",384,184,201,121,%WS_POPUP OR %WS_SYSMENU OR _
          %WS_BORDER OR %WS_DLGFRAME OR %DS_MODALFRAME, TO hDlg
          CONTROL ADD BUTTON, hDlg, %IDC_BUTTON1, "Button1",68,90,75,20
          CONTROL ADD GRAPHIC, hDlg, %idc_graphic1, "", 50,10,100,75,%WS_BORDER
      
      
          DIALOG SHOW MODAL hDlg, CALL showDialog1Proc TO Rslt
      
      
          FUNCTION = Rslt
      END FUNCTION

      Comment


      • #4
        Mike,

        Use CB.HNDL to access the dialog handle in the Callback function.

        Or, keep hDlg as Global. That's that way I prefer.

        Comment


        • #5
          Change this line
          Code:
          GRAPHIC ATTACH hDlg, %idc_graphic1
          to this
          Code:
          GRAPHIC ATTACH CB.HNDL, %idc_graphic1
          comment out the LOCAL hDlg as DWORD

          Posting same as Gary, same advice regarding the CB.HNDL.
          Rod
          In some future era, dark matter and dark energy will only be found in Astronomy's Dark Ages.

          Comment


          • #6
            Also, Mike,

            In gbSnippets, there's an example app with several graphics controls - multiple ways to use it, including lines. It's snippet gbs_00089.

            Here's a copy in case you're interested.

            Code:
            'Compilable Example:
            'The following compilable code demonstrates a dialog with muiltiple graphic controls,
            'a resource file, and an imagelist, Various commands to load/size images in the graphic
            'controls are used, as well as some basic drawing commands are demonstrated.
            'The Dialog Callback response to a mouse click is demonstrated.
            'Controls can also have a Callback function of their own.
            
            #Compile Exe
            #Dim All
            #Include "Win32API.inc"
            #Resource "gbsnippets.pbr"
            Global hDlg As Dword, hLst As Dword, hTemp As Dword
            Global style&, extstyle&
            Function PBMain() As Long
            Dialog New Pixels, 0, "Graphic Control Test",300,300,450,380, %WS_SysMenu, 0 To hDlg
            
            'create imagelist  w,h,depth,size
            ImageList New Icon 16,16,32,3 To hLst
            ImageList Add Icon hLst, "x"
            ImageList Add Icon hLst, "y"
            ImageList Add Icon hLst, "check"
            
            'create graphic control - various frames
            Control Add Graphic, hDlg, 301,"", 10,10,30,30, %WS_Visible Or %SS_Sunken
            Control Add Graphic, hDlg, 302,"", 50,10,30,30, %WS_Visible Or %WS_DlgFrame
            Control Add Graphic, hDlg, 303,"", 90,10,30,30, %WS_Visible Or %WS_Border
            
            'create graphic control
            style& = %WS_Visible Or %SS_Sunken Or %SS_Notify
            Control Add Graphic, hDlg, 110,"", 10,50,100,100, style&
            Control Add Graphic, hDlg, 120,"", 120,50,100,100, style&
            Control Add Graphic, hDlg, 130,"", 230,50,100,100, style&
            Control Add Graphic, hDlg, 210,"", 10,160,100,100, style&
            Control Add Graphic, hDlg, 220,"", 120,160,100,100, style&
            Control Add Graphic, hDlg, 230,"", 230,160,100,100, style&
            Control Add Graphic, hDlg, 310,"", 10,270,100,100, style&
            Control Add Graphic, hDlg, 320,"", 120,270,100,100, style&
            Control Add Graphic, hDlg, 330,"", 230,270,100,100, style&
            
            'PBR FILE
            Graphic Attach hDlg, 110
            Graphic Render "cowgirl", (0,0)-(100,100)    'same size (could resize)
            
            'BMP FILE      bmpname$, (x,y)-(x2,y2) <-- dest coordinates
            Graphic Attach hDlg, 120
            Graphic Render "icons/cowgirl.bmp", (0,0)-(100,100) 'same size (could resize)
            
            'IMAGELIST        (x,y), hList, index&, overlay&, style&
            Graphic Attach hDlg, 130
            Graphic ImageList (0,0), hLst, 1,0, %ILD_Normal
            Graphic ImageList (50,50), hLst, 2,0, %ILD_Normal
            Graphic ImageList (10,70), hLst, 3,0, %ILD_Normal
            Graphic ImageList (70,30), hLst, 3,0, %ILD_Normal
            
            'COPY   hSource, id&
            Graphic Attach hDlg, 210           'source
            Graphic Copy hDlg, 110             'copy all to 0,0
            Graphic Attach hDlg, 220           'source
            Graphic Copy hDlg, 110 To (20,20)  'copy all to 40,40
            Graphic Attach hDlg, 230           'source
            Graphic Copy hDlg, 110, (0,0)-(40,80) To (40,10) 'copy part to 40,40
            
            'STRETCH
            Graphic Attach hDlg, 310  'copy all to 0,0
            Graphic Stretch hDlg, 110, (0,0)-(100,100) To _
                            (0,0)-(50,50) 'copy part+shrink
            Graphic Attach hDlg, 320
            Graphic Stretch hDlg, 110, (0,0)-(100,100) To _
                             (0,0)-(200,200) 'copy part+expend
            
            'DRAWING
            Graphic Attach hDlg, 330
            Graphic Print "Print";" here"
            Graphic Color %Red
            Graphic Print
            Graphic Print "Now here!"
            Graphic Color %Blue
            Graphic Print
            Graphic Print "and here!"
            Graphic Ellipse (20,20)-(70,95), %Black
            Graphic Ellipse (25,15)-(35,25), %Black, %Red
            Graphic Line (80,10)-(50,100), %Black
            Graphic Paint Border (60,75),%Green, %Black
            Graphic Paint Border (60,75),%Green, %Black
            Graphic Box (75,60)-(90,90),50, %White, %Red
            
            Dialog Show Modal hDlg Call DlgProc
            End Function
            
            CallBack Function DlgProc() As Long
              If CB.Msg = %WM_Command AND CB.Ctl = 110 AND _
                                CB.Ctlmsg = %STN_Clicked Then
                 MsgBox "Clicked!"
              End If
            End Function
            
            'gbs_00089
            Last edited by Gary Beene; 28 Sep 2009, 07:56 PM.

            Comment


            • #7
              Thanks for the good information guys...

              I'll start making the hDlg global.

              The CB.HNDL thingie worked too, but I don't understand what it is or where it comes from.

              Mike...

              P.S. Gary, I love your site. Do you have a tutorial for a beginner like me that tells me where to put global variables, local variables, sub routines, dialogs, constants, declares, and callbacks. Obviously, the closer it looks to PBCC, the faster I'll be able to grasp it.

              I've done "Hello World" with a msgbox a thousand times, but that doesn't teach me where all the other stuff goes.
              Last edited by Mike Schneider; 29 Sep 2009, 03:26 AM.

              Comment


              • #8
                Mike, CB.HNDL is the current dialog handle. It will be the same as your
                hDlg variable used in the DIALOG NEW statement.

                I use Global hDlg (usally adding something more specific).
                For example, if I am writing an Employee File maintenance dialog, I might use
                hDlgEmp instead of hDlg.

                The reason I use global hDlg instead of CB.HNDL (most of the time) is that I might have more than one dialog. Each of these I use a different spelling of the variable. For me, it keeps it straight which dialog I am dealing with.

                Drawbacks:
                probably takes more memory but i don't worry much about that in windows. Also, more variables to keep track of. If you copy and paste you may need to change these variable names each time you paste.

                ***Added Later***
                Mike, you can learn a lot by studying the source code forum and examples included with PB9 and the downloads available from the powerbasic site
                Last edited by Fred Buffington; 29 Sep 2009, 11:17 AM.
                Client Writeup for the CPA

                buffs.proboards2.com

                Links Page

                Comment


                • #9
                  Mike,

                  No, I don't have such a page. But it's a good idea. I'll generate one and let you know when it is up.

                  Comment


                  • #10
                    Code:
                    CALLBACK FUNCTION ShowDialogProc()
                    is a DDT version of the SDK code:

                    Code:
                    FUNCTION ShowDialogProc(Hndl AS LONG, Msg AS LONG, wParam AS LONG, lParam AS LONG) AS LONG
                    When you use the DDT version, PB fills in a predefined UDT with the values of those four parameters that you didn't specify in the CALLBACK FUNCTION line. This UDT is called CB so you end up with CB.HNDL, CB.MSG, CB.WPARAM and CB.LPARAM.

                    Sometimes those parameters also contain other useful information in their low or high words so the DDT engine also fills in other members of the UDT to make it easier to get at them.

                    If CB.MSG = %WM_COMMAND then
                    • CB.CTL is the low word of CB.WPARAM
                    • CB.CTLMSG is the high word of CB.WPARAM

                    If CB.MSG = %WM_NOTIFY then
                    • CB.NMHDR is set to the address of an NMHDR UDT that is passed in via CB.LPARAM
                    • CB.NMHWND is set to the value of NMHDR.hwndFrom
                    • CB.NMID is set to the value of NMHDR.idFrom
                    • CB.NMCODE is set to the value of NMHDR.code
                    • CB.NMHDR$ is a string of all twelve bytes of the NMHDR (I have no idea what this would ever be used for)
                    Jeff Blakeney

                    Comment


                    • #11
                      CB.NMHDR$ is a string of all twelve bytes of the NMHDR (I have no idea what this would ever be used for)
                      Code:
                       LOCAL myNMHDR AS NMHDR 
                            
                        CASE %WM_NOTIFY 
                           .....
                              TYPE SET MyNmHDR = CB.NMHDR$ 
                              CALL SomeFunction(MyNmHdr) 
                      
                      
                      FUNCTION SomeFunction( X AS NMHDR) 
                      ...
                      MCM
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        Okay but what is wrong with doing this:

                        Code:
                        LOCAL myNMHDR AS NMHDR
                        LOCAL pTemp AS NMHDR POINTER
                        ...
                        pTemp = CB.NMHDR
                        myNMHDR = @pTemp
                        lResult = SomeFunction(myNMHDR)
                        ...
                        FUNCTION SomeFunction(X AS NMHDR)
                        Unfortunately, although CB.NMHDR is a pointer to NMHDR, you can't use myNMHDR = @CB.NMHDR but I guess that is normal. If you are using SDK style programming, you can use myNMHDR = @lParam either because the @ needs a variable defined as a pointer to work.

                        Here is an even simpler way:

                        Code:
                        lResult = SomeFunction(CB.NMHDR)
                        ...
                        FUNCTION SomeFunction(BYVAL X AS NMHDR POINTER)
                            IF @X.hwndFrom = ghDlg THEN
                                ...
                        Or you could put a myNMHDR = @X as the first line of the function so you wouldn't need to remember to put @ at the beginning of each access to the NMHDR UDT.
                        Last edited by Jeff Blakeney; 30 Sep 2009, 11:50 AM.
                        Jeff Blakeney

                        Comment


                        • #13
                          >CB.NMHDR is a pointer to NMHDR, you can't use myNMHDR = @CB.NMHDR
                          ==>
                          Code:
                              CALL SomeFunction([email protected])
                          Assumes the "CB" UDT is defined with member "nmhdr" 'AS NMHDR PTR' but since we don't define that ourselves I guess you'll just have to try it and see what happens.
                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                          • #14
                            Unfortunately, that doesn't work either. I get a "Period not allowed" error at the start of the [email protected] if SomeFunction is defined as having a parameter of X AS NMHDR.
                            Jeff Blakeney

                            Comment


                            • #15
                              Well, then I guess "CB" is not a UDT, and all the 'CB.anything's are just 'reserved words.'
                              Michael Mattias
                              Tal Systems (retired)
                              Port Washington WI USA
                              [email protected]
                              http://www.talsystems.com

                              Comment


                              • #16
                                Looking forward to that page Gary.

                                Thanks, all you guys, for that good information! I've been downloading sample programs from powerbasic.com and looking at the samples included with the program!

                                Mike...

                                Comment

                                Working...
                                X