Announcement

Collapse
No announcement yet.

Mouse Click coordinates on Graphics Window?

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

  • Mouse Click coordinates on Graphics Window?

    Is there any way to discover the x y location of a
    mouse click on the Graphics Window (Using PBCC4)

    ------------------
    Lee Allen
    www.barcodeman.com
    Email
    Lee Allen
    www.barcodeman.com
    Email

  • #2
    You can subclass the graphics window and then catch WM_LBUTTONDOWN, WM_RBUTTONDOWN, and many other mouse messages.
    The horizontal location of where the click occurred is in the lo word of the lParam and the vertical position is
    in the hi word of lparam. Here is an example of creating a callback routine for the graphics window in PBCC 4.x

    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "Win32Api.Inc"
    '
    GLOBAL GraphicWindowChildOldProc AS DWORD
    GLOBAL hGraphicWindow AS DWORD
    GLOBAL finished AS LONG
    '
    ' Timer callback routines, called every 200ms
    ' Just a little routine to display colored boxes on the graphic window
    SUB DisplayUpdate(BYVAL uID AS LONG, BYVAL uMsg AS LONG, _
                      BYVAL dwUser AS LONG, BYVAL dw1 AS LONG, BYVAL dw2 AS LONG)
      LOCAL r, g, b AS LONG
      LOCAL c AS LONG
      LOCAL x, y AS LONG
      LOCAL w, h AS LONG
      '
      GRAPHIC ATTACH hGraphicWindow, 0&, REDRAW
      GRAPHIC GET CLIENT TO w, h
      '
      r = RND(0, 255)
      g = RND(0, 255)
      b = RND(0, 255)
      '
      x = RND(0, w-21)
      y = RND(30, h-21)
      '
      c = RGB(r,g,b)
      '
      GRAPHIC BOX (x,y) - (x+25,y+25), 20, c, c
      '
      GRAPHIC REDRAW
    END SUB
    
    ' This is the function that is called whenever a Key or Mouse click is detected in the Graphics Window
    FUNCTION GraphicWindowChildNewProc(BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
      SELECT CASE wMsg
         CASE %WM_NCHITTEST
         CASE %WM_MOUSEMOVE
         CASE %WM_LBUTTONDOWN
           ? "you clicked the left mouse button at ("+FORMAT$(LO(WORD, lParam))+","+FORMAT$(HI(WORD, lParam))+")"
         CASE %WM_RBUTTONDOWN
          ? "you clicked the right mouse button at ("+FORMAT$(LO(WORD, lParam))+","+FORMAT$(HI(WORD, lParam))+")"
         CASE %WM_KEYDOWN
           IF CHR$(WPARAM) = "X" THEN
             finished = 1
           ELSE
             ? "you pressed the "+CHR$(wparam)+ " key"
           END IF
      END SELECT
      '
      FUNCTION = CallWindowProc(GraphicWindowChildOldProc, hWnd, wMsg, wParam, lParam)
    END FUNCTION
    
    FUNCTION PBMAIN
      LOCAL w AS LONG
      LOCAL tid AS LONG
      '
      RANDOMIZE
      '
      finished = 0
      '
      GRAPHIC WINDOW "SubClass", 0, 0, 320, 240 TO hGraphicWindow
      '
      ' This function subclasses the Graphic Window
      ' Which means that any key or mouse presses in the Window
      ' Will cause the GraphicWindowChildNewProc Callback function to be called
      GraphicWindowChildOldProc = SetWindowLong(hGraphicWindow, %GWL_WNDPROC, CODEPTR(GraphicWindowChildNewProc))
      '
      ' Create an timer that runs the routine DisplayUpdate every 200ms
      tid = TimeSetEvent(200, 10, CODEPTR(DisplayUpdate), BYVAL 0, %TIME_PERIODIC)
      '
      GRAPHIC ATTACH hGraphicWindow, 0&, REDRAW
      GRAPHIC CLEAR %BLACK
      GRAPHIC COLOR %BLUE, %YELLOW
      GRAPHIC FONT "Times New Roman", 18, 1
      GRAPHIC PRINT "press X to quit"
      GRAPHIC REDRAW
      '
      w = 0
      '
      ' loop until an X is pressed on the graphic window
      WHILE finished = 0
        INCR w
        ' Every third iteration of the loop give back
        ' one time slick to Windows too prevent 100% CPU utilization
        ' See the [url="http://www.powerbasic.com/support/help/pbcc/sleep_statement.htm"]SLEEP[/url] statement in the Help file
        IF w MOD 3 = 0 THEN
          SLEEP 0
        END IF
      WEND
      '
      ' Stop the timer from running
      TimeKillEvent(tid)
      '
      ' Set the graphics window to use the original Callback routine
      SetWindowLong(hGraphicWindow, %GWL_WNDPROC, GraphicWindowChildOldProc)
      '   
      GRAPHIC ATTACH hGraphicWindow, 0&
      GRAPHIC WINDOW END
    END FUNCTION
    Sincerely,
    Steve Rossell
    PowerBASIC Staff
    Sincerely,

    Steve Rossell
    PowerBASIC Staff

    Comment


    • #3
      I'm sure that works, but maybe the doc should be updated under the GRAPHIC WINDOW statement by adding the italicized text..
      hWin??? The handle of the newly-created window. If the window could not be created, hWin??? will be 0.
      This handle may be used to set up a standard Windows subclass procedure if you want to receive and process keystroke [and other?] notifications [any special limitations or restrictions here]
      MCM




      [This message has been edited by Michael Mattias (edited May 11, 2007).]
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Just noticed this GRAPHIC WINDOW stuff for the first time
        I wonder why it can not be a child window (control).



        ------------------
        http://www.hellobasic.com
        hellobasic

        Comment


        • #5
          Originally posted by Edwin Knoppert:
          Just noticed this GRAPHIC WINDOW stuff for the first time
          I wonder why it can not be a child window (control).
          PowerBASIC For Windows (but not PBCC) has a GRAPHIC CONTROL. See CONTROL ADD GRAPHIC in the help file.

          Sincerely,
          Steve Rossell
          PowerBASIC Staff
          Sincerely,

          Steve Rossell
          PowerBASIC Staff

          Comment


          • #6
            Thank you Steve.

            Can I suggest in a future version of PBCC your function is wrapped
            up in a simple command with the result already scaled to match the
            current GRAPHIC SCALE. It seems to me clicking on a graphic
            widget would be quite a basic requirement for many programs.
            Something like this maybe...

            x=GRAPHICMOUSECLICKX
            y=GRAPHICMOUSECLICKY



            ------------------
            Lee Allen
            www.barcodeman.com
            Email
            Lee Allen
            www.barcodeman.com
            Email

            Comment


            • #7
              Hi Lee,

              You should send in your request to [email protected]
              I once took a request from the forums and was quickly admonished
              by a forum user for doing so. True, the forums are not an official
              support line and you will have to send it in via email. Thanks.

              Sincerely,
              Steve Rossell
              PowerBASIC Staff
              Sincerely,

              Steve Rossell
              PowerBASIC Staff

              Comment


              • #8
                "I once took a request from the forums and was quickly admonished
                by a forum user for doing so.

                Steve Rossell
                PowerBASIC Staff"

                That is a crazy restriction Steve. I encourage you to use these forums for ideas to expand the product.

                In the fairly unlikely event that I make a suggestion that you think is useful, you have my permission, no you have my encouragement, to consider it and use it.

                Kia Ora



                ------------------
                Kerry Farmer
                dynamicintelligence.co.nz
                [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
                Kerry Farmer

                Comment

                Working...
                X