Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mouse
#1
I have read the tutorial about using the mouse in Freebasic

What i want is i have a selection of boxes i would like to click on but can not get the getmouse to work.

What do i do.
Reply
#2
GETMOUSE x, y, wheel, buttons
IF buttons AND 1 THEN
'left click
ELSEIF buttons AND 2 THEN
'right click
END IF
f you play a Microsoft CD backwards you can hear demonic voices. The scary part is that if you play it forwards it installs Windows.
Reply
#3
Or, more simple:
Code:
GETMOUSE x,y,wheel,buttons
IF buttons=1 THEN
    'left click
END IF
IF buttons=2 THEN
    'right click
END IF
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#4
EDIT: now with the mouse button
If (mouseposition - boxposition) >=0 and <= boxsize then it hits a box.

Try it out.

Joshy
Code:
OPTION EXPLICIT

#define max_boxes 5

type box
  as integer x,y,w,h,c
end type


function CreateBox(x=0,y=0,w=32,h=24,c=255) as box
  dim as box tmp
  tmp.x=x
  tmp.y=y
  tmp.w=w
  tmp.h=h
  tmp.c=c
  return tmp
end function

sub DrawBox( b as box)
  line (b.x,b.y)-step(b.w,b.h),b.c,BF
end sub

function IsInBox(b as box,x,y) as integer
  if (x-b.x)>0 and (x-b.x)<b.w then
    if (y-b.y)>0 and (y-b.y)<b.h then return 1
  end if
  return 0
end function

' main
dim as box myboxes(max_boxes-1)
dim as integer i,mx,my,mb

screenres 640,480

for i=0 to max_boxes-1
  '                    x      ,y ,width,height,color
  myboxes(i)=CreateBox(50+i*40,50,32   ,24    ,1+i)
  DrawBox myboxes(i)
next

while inkey<>chr$(27)
  getmouse mx,my,,mb
  ' if mouse in window and any button pressed
  if mx>-1 and mb>0 then
    for i=0 to max_boxes -1
      if IsInBox(myboxes(i),mx,my) then
        locate 1,1 : ? "in box nr " & str(i)
        exit for
      end if
    next
    if i=max_boxes then locate 1,1 : ? "not in box "
  end if
  sleep 100
wend

end
sorry about my english
Reply
#5
Sorry what i mean't was how do i click on a box that i have drawn on the screen and tell the computer to go to a sub or other part of code


------------------
| Click me |
------------------
Reply
#6
Quote:Sorry what i mean't was how do i click on a box that i have drawn on the screen and tell the computer to go to a sub or other part of code


------------------
| Click me |
------------------

Sorry added see the code.

Joshy
sorry about my english
Reply
#7
Quote:Or, more simple:
Code:
GETMOUSE x,y,wheel,buttons
IF buttons=1 THEN
    'left click
END IF
IF buttons=2 THEN
    'right click
END IF
This wont work if you want to know when the left or right button are pressed regardless of what other buttons are held down. If someone holds down the middle mouse button. (buttons would = 5(middle and left) or 6(middle and right) depending what combinations were pressed.) Using AND will always return "true" if left button is pressed regardless of what other buttons are pressed.

---

This is simmilar to d.j.peters' code. I knocked it up for my light cycle game's menus and options screens. So I've ripped it outta my game and stuck it here. Wink

Code:
DECLARE FUNCTION addHotSpot(x1 AS INTEGER, y1 AS INTEGER, x2 AS INTEGER, y2 AS INTEGER) AS USHORT
DECLARE FUNCTION checkHotSpots (x AS INTEGER, y AS INTEGER)
DECLARE SUB TLHotSpot(BYVAL handle AS USHORT, BYREF x AS INTEGER, BYREF y AS INTEGER)
DECLARE SUB removeHotSpot()
DECLARE SUB clearHotSpots

REDIM SHARED hotSpot(0,3) AS INTEGER

screenres 640,480

ClickMe=addHotSpot(20,20,100,100)
Quit=addHotSpot(50, 300, 100, 350)

DO
   SCREENSYNC
   LOCATE 1,1:? "                            "
   GETMOUSE mouseX, mouseY, , mouseB

   SELECT CASE checkHotSpots(mouseX, mouseY)
   CASE ClickMe
      Locate 1,1
      IF mouseB <> 0 THEN
          ? "    Mouse Clicked !!!!!     "
      ELSE
         ? "Hovering Over 'ClickMe' area"
      END IF
   CASE Quit
       IF mouseB AND 1 THEN EXIT DO
   END SELECT
LOOP

clearHotSpots

END

FUNCTION addHotSpot(x1 AS INTEGER, y1 AS INTEGER, x2 AS INTEGER, y2 AS INTEGER) AS USHORT
    IF x1 > x2 THEN SWAP x1, x2
    IF y1 > y2 THEN SWAP y1, y2
  
    LINE (x1, y1)-(x2, y2),,B 'Un-comment this line for debugging, it shows the hot-spot's active area.
  
    DIM hotSpots AS USHORT
    hotSpots = (UBOUND(hotSpot, 1)+1)
    REDIM PRESERVE hotSpot(hotSpots, 3)
    hotSpot(hotSpots,0) = x1
    hotSpot(hotSpots,1) = y1
    hotSpot(hotSpots,2) = x2
    hotSpot(hotSpots,3) = y2
    RETURN hotSpots
END FUNCTION

FUNCTION checkHotSpots (x AS INTEGER, y AS INTEGER)
    DIM AS USHORT hotAt, i
  
    FOR i = 1 TO UBOUND(hotSpot, 1)
        IF x > hotSpot(i,0) AND x < hotSpot(i,2) THEN
            IF y > hotSpot(i, 1) AND y < hotSpot(i, 3) THEN
                hotAt = i
            END IF
        END IF
    NEXT i
    RETURN hotAt
END FUNCTION

'returns the top-left of a hot-spot. (not used in this example but might be useful to you)
SUB TLHotSpot(BYVAL handle AS USHORT, BYREF x AS INTEGER, BYREF y AS INTEGER)
    x = hotSpot(handle,0)
    y = hotSpot(handle,1)
END SUB

SUB removeHotSpot() 'removes the last added hotspot. (not used in this example but might be useful to you)
    REDIM hotSpot(UBOUND(hotSpot,1)-1 , 3)
END SUB

SUB clearHotSpots   'removes all hotspots
    REDIM hotSpot(0, 3)
END SUB
url=http://www.spreadfirefox.com/?q=affiliates&id=60131&t=79][Image: safer.gif][/url]
END OF LINE.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)