How can I create a button?

Post your coding questions such as 'how to' here.
Post Reply
O.Mago.O
Newcomer
Posts: 2
Joined: Fri Aug 07, 2009 2:56 am

How can I create a button?

Post 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.
User avatar
Jare
Devoted Member
Posts: 877
Joined: Mon Aug 27, 2007 10:18 pm
Location: Pori
Contact:

Re: How can I create a button?

Post 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
Post Reply