How can I create a button?

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

How can I create a button?

Post by O.Mago.O » Fri Aug 07, 2009 3:19 am


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
Posts: 860
Joined: Mon Aug 27, 2007 10:18 pm
Location: Helsinki

Re: How can I create a button?

Post by Jare » Fri Aug 07, 2009 4:17 am

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

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

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

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest