Page 1 of 1

How can I create a button?

Posted: Fri Aug 07, 2009 3:19 am
by O.Mago.O
Hi:

Let me know how can I create a button, I mean a button to which you can click with the mouse and something happens.

I need that in my program is as pressing a button (with left-click) an object appears on the screen.

Please .. I need help!

Greetings. They appreciate that.

Re: How can I create a button?

Posted: Fri Aug 07, 2009 4:17 am
by Jare
When making buttons, the main issue to meet is that CB cannot produce native Windows buttons. So you must make up your own look for the buttons (or you can try to imitate the Windows button style).

I made a simple function which can be used to make basic buttons. Not very beautiful, but you can make it look better. The function takes parameters: x and y to place the button, width and height are dimensions for the button and finally txt is the button's label. If width or height is too small for the label, they are resized automatically. The function returns True if the button was clicked - otherwise False.

Code: Select all

Repeat
	If Button(50,50,100,30, "Hello World!") Then MakeError "Click"
	DrawScreen
Forever

Function Button(x,y,width,height, txt$)
	width		= Max(width,	TextWidth(txt)+2)
	height		= Max(height,	TextHeight(txt)+2)
	result		= False
	mouse_over	= False
	If MouseX()>=x And MouseX()<=x+width-1 Then
		If MouseY()>=y And MouseY()<=y+height-1 Then
			mouse_over = True
			If MouseHit(1) Then result = True
		EndIf
	EndIf
	c = 128+mouse_over*50
	Color c,c,c
	Box x,y, width,height, ON
	Color 255,255,255
	CenterText x+width/2,y+height/2, txt, 2
	Return result
EndFunction