Mouse in FreeBasic
Mouse in FreeBasic
Why does this not work? All I get is Beep on the first line but not on the others
If (MouseX>3 And MouseY>3) And (MouseX<12 And MouseY<15) Then Beep
If (MouseX>13 And MouseY>20) And (MouseX<12 And MouseY<22) Then Beep
If (MouseX>637 And MouseY>650) And (MouseX<8 And MouseY<19) Then Beep
If (MouseX>3 And MouseY>3) And (MouseX<12 And MouseY<15) Then Beep
If (MouseX>13 And MouseY>20) And (MouseX<12 And MouseY<22) Then Beep
If (MouseX>637 And MouseY>650) And (MouseX<8 And MouseY<19) Then Beep
Re: Mouse in FreeBasic
Here is the full routine:
Sub MouseHit
TestMouse=GetMouse(MouseX,MouseY,,Buttons)
Locate 12,40:Print MouseX;MouseY;Buttons
If (MouseX>3 And MouseX<12) And (MouseY>3 And MouseY<15) Then Beep
If (MouseX>13 And MouseX<12) And (MouseY>20 And MouseY<22) Then Beep
If (MouseX>637 And MouseX<8) And (MouseY>650 And MouseY<19) Then Beep
End Sub
Only one of the traps work. The rest do nothing. Anyone know how to fix this?
Sub MouseHit
TestMouse=GetMouse(MouseX,MouseY,,Buttons)
Locate 12,40:Print MouseX;MouseY;Buttons
If (MouseX>3 And MouseX<12) And (MouseY>3 And MouseY<15) Then Beep
If (MouseX>13 And MouseX<12) And (MouseY>20 And MouseY<22) Then Beep
If (MouseX>637 And MouseX<8) And (MouseY>650 And MouseY<19) Then Beep
End Sub
Only one of the traps work. The rest do nothing. Anyone know how to fix this?
Re: Mouse in FreeBasic
Reread your last two 'If' lines and you will understand that whatever the values of 'MouseX' and 'MouseY', neither of the two conditions can not be verified:
'(MouseX>13 And MouseX<12)' is never true.
'(MouseX>637 And MouseX<8)' is never true.
'(MouseY>650 And MouseY<19)' is never true.
I can not guess the correct syntax without an explanation in words of what you want to do.
'(MouseX>13 And MouseX<12)' is never true.
'(MouseX>637 And MouseX<8)' is never true.
'(MouseY>650 And MouseY<19)' is never true.
I can not guess the correct syntax without an explanation in words of what you want to do.
Re: Mouse in FreeBasic
I don't understand how GetMouse works.
When I use Print MouseX,MouseY it goes from 0,0 to X,Y (Size of the screen) so why is the statements invalid?
When I use Print MouseX,MouseY it goes from 0,0 to X,Y (Size of the screen) so why is the statements invalid?
Re: Mouse in FreeBasic
This behavior is independent of GetMouse.
It occurs with any variables 'X' and 'Y'.
is equivalent to:
Whatever the values of 'X' and 'Y', never 'Beep' (any value 'X' > 13 is not < 12)
It occurs with any variables 'X' and 'Y'.
Code: Select all
If (X>13 And X<12) And (Y>20 And Y<22) Then Beep
Code: Select all
If X>13 Then
If X<12 Then
If Y>20 Then
If Y<22 Then Beep
End If
End If
End If
Re: Mouse in FreeBasic
Let me explain.
I created a program (Dimensions are 658x404)
I was trying to use GetMouse to be able to click on portians of the screen.
I used Beep only as a audible way to know if the command worked or not.
Is there another (better?) way to do this than get mouse?
I created a program (Dimensions are 658x404)
I was trying to use GetMouse to be able to click on portians of the screen.
I used Beep only as a audible way to know if the command worked or not.
Is there another (better?) way to do this than get mouse?
Re: Mouse in FreeBasic
What are coordinates (X, Y) portions of screen you want to exclude?
Re: Mouse in FreeBasic
I was able to get everything working using nested If/Then statements.
Here is the code:
Here is the code:
Code: Select all
Sub MouseHit
TestMouse=GetMouse(MouseX,MouseY,,Buttons)
If MouseX>3 And MouseY>3 Then
If MouseX<12 And MouseY<15 Then
If Buttons=1 Then
Rem LoadFile
EndIf
EndIf
EndIf
If MouseX>13 And MouseY>10 Then
If MouseX<22 And MouseY<22 Then
If Buttons=1 Then
Rem Options
EndIf
EndIf
EndIf
If MouseX>637 And MouseY>6 Then
If MouseX<652 And MouseY<19 Then
If Buttons=1 Then
If FileFlag=1 Then Close #1
End
EndIf
EndIf
EndIf
End Sub
Re: Mouse in FreeBasic
Code: Select all
Sub MouseHit
TestMouse=GetMouse(MouseX,MouseY,,Buttons)
If (MouseX>3 And MouseY>3) And (MouseX<12 And MouseY<15) And (Buttons=1) Then
Rem LoadFile
EndIf
If (MouseX>13 And MouseY>10) And (MouseX<22 And MouseY<22) And (Buttons=1) Then
Rem Options
EndIf
If (MouseX>637 And MouseY>6) And (MouseX<652 And MouseY<19) And (Buttons=1) Then
If FileFlag=1 Then Close #1
End
EndIf
End Sub
Re: Mouse in FreeBasic
Others have said this already, but I will try to say in simpler words......take a close look at your > and < conditions in your original post.
Nothing can be > 637 and < 8 at the same time. The other way round, yes.
Try drawing it on a piece of paper.
Nothing can be > 637 and < 8 at the same time. The other way round, yes.
Try drawing it on a piece of paper.
Re: Mouse in FreeBasic
deleted
Last edited by alfakilo on Jul 07, 2014 19:31, edited 1 time in total.
Re: Mouse in FreeBasic
Another example, with no dependence on beep:
Code: Select all
dim as integer x,y,w,b
screenres 640,480,32
width 80,30 '' force 8x16 font
line (220,190)-(420,290),-1,B
do
getmouse(x,y,w,b)
locate 1,1
print x,y,w,b;
if b and 1 then
if x > 220 and x < 420 then
if y > 190 and y < 290 then
print ,"click";
end if
end if
else
print ," ";
end if
loop until inkey <> ""
Re: Mouse in FreeBasic
I've made up two or three posts using this kind of stuff.
I found that the easiest way for this is to define a type box.
The fields within the type are x(upper left x), y(upper left y), wide( (box width), high (box height)
You can also include a colour for the box, a caption for the box, an index for the box if you have boxes held in an array.
You can also define a little sub to draw the box.
Keep track of mouse in a box with a little function (inside), as in this example.
In the event of a click in a box, you can do a task in another sub (do_task) here.
Here I have set up a grid of boxes, but you can position your boxes anywhere, the grid is just an example.
I found that the easiest way for this is to define a type box.
The fields within the type are x(upper left x), y(upper left y), wide( (box width), high (box height)
You can also include a colour for the box, a caption for the box, an index for the box if you have boxes held in an array.
You can also define a little sub to draw the box.
Keep track of mouse in a box with a little function (inside), as in this example.
In the event of a click in a box, you can do a task in another sub (do_task) here.
Here I have set up a grid of boxes, but you can position your boxes anywhere, the grid is just an example.
Code: Select all
Type box
As Integer x,y 'start position
As Integer wide,high,index'index copies an array() index
Dim As Uinteger colour
As String caption
Declare Sub show 'draw the box
End Type
Sub box.show
Line(this.x,this.y)-(this.x+this.wide,this.y+this.high),this.colour,bf
Line(this.x,this.y)-(this.x+this.wide,this.y+this.high),Rgb(200,200,200),b
Draw String(this.x+.5*this.wide-4*Len(this.caption),this.y+.5*this.high-4),this.caption,Rgb(0,0,0)
End Sub
'mouse in a box
Function inside(B As box,px As Integer,py As Integer) As Integer
Return (px>B.x)*(px<(B.x+B.wide))*(py>B.y)*(py<(B.y+B.high))
End Function
Sub setup_grid(boxes() As box,xp As Integer,yp As Integer,w As Integer,h As Integer)
Dim As Integer index,n=Sqr(Ubound(boxes))-1'+1
For y As Integer=yp To yp+h*n Step h
For x As Integer=xp To xp+w*n Step w
index=index+1
'the grid positions for each box (.x and .y)
With boxes(index)
.x=x
.y=y
.wide=w
.high=h
.index=index
.caption=Str(index)
End With
Next x
Next y
'example -- setup a single box, quit box(0)
With boxes(0)
.x=780
.y=0
.wide=20
.high=20
.index=0
.caption="X"
End With
End Sub
'program ends when all boxes have been clicked
Function all_clicked(b() As box) As Integer
For z As Integer=1 To Ubound(b)
If b(z).caption <>"OK" Then Return 0
Next z
Return -1
End Function
Sub do_task(a() As box,b As box)
'do things in event of a click e.g.
b.caption="OK"
If all_clicked(a()) Then Screenunlock:End
If b.index=0 Then Screenunlock:End
End Sub
Function get_events(boxes() As box) As Integer
Static released As Integer
Dim As Integer mousex,mousey,mousebutton
Getmouse mousex,mousey,,mousebutton
Screenlock
Cls
For n As Integer=0 To Ubound(boxes):boxes(n).show:Next 'draw them
Screenunlock
Sleep 1,1
For n As Integer=0 To Ubound(boxes)
If inside(boxes(n),mousex,mousey) Then
If released Then boxes(n).colour=Rgb(233,236,216) 'highlight colour
If mousebutton=1 Then
If released Then do_task(boxes(),boxes(n))
Exit For
End If
Else
boxes(n).colour=Rgb(133,136,016)
End If
Next n
If mousebutton=0 Then released=1 Else released=0 'clean clicks
Return 0
End Function
'=============== RUN ============================
'main screen
Screen 19,32
Color ,Rgb(233,236,216)'background colour
Dim As Integer num=8 'grid is 8 by 8 here, but is changeable
Dim As box boxes(0 To num^2)
'grid (boxes,grid topleft x,grid topleft y,width of each box,height of each box)
setup_grid(boxes(),200,100,50,30)
Do
get_events(boxes())
Loop