Canvas Control


The examples in the rest of the chapter all rely on the Canvas control. This is something that can be thought of as a general purpose control, and it can be used to create a lot of custom controls, especially custom buttonlike controls. This will also provide a good introduction to the Graphics class and how you can use it.

Canvas Properties

The following Canvas properties should be familiar to you:

Canvas.AcceptFocus as Boolean Canvas.AcceptTabs as Boolean Canvas.UseFocusRing as Boolean


These properties are unique to a Canvas control and provide a means to associate a Picture object to serve as the backdrop for whatever else is taking place on the Canvas.

Canvas.Backdrop as Picture


One of the most important properties is the reference to the Graphics object, which is what you will use to draw onto the Canvas.

Canvas.Graphics as Graphics


The following is a Windows-only feature that gives you the option of erasing the background of the Canvas when it is resized. Only Windows erases the contents of the control.

Canvas.EraseBackground as Boolean


Canvas Events

By now, the events available in a Canvas control should be quite familiar to you. All of the following work the same as they do in the other REALbasic controls:

Canvas.EnableMenuItems Canvas.KeyDown(Key as String) Canvas.GotFocus Canvas.LostFocus Canvas.MouseDown(x as Integer, y as Integer) as Boolean Canvas.MouseDrag(x as Integer, y as Integer) Canvas.MouseUp(x as Integer, y as Integer) Canvas.Paint(g as Graphics)


The Canvas control does not have an Action event like buttons do, but a button Action event is really a MouseUp event. Keeping that in mind, you can replicate an Action event with the MouseUp event, and that gives you the basic structure of a button. The other thing that buttons do, however, is provide visual feedback when your mouse clicks it and when it is released. Some buttons (not REALbasic buttons) have cool effects, such as not appearing to be raised unless the mouse is moved over them. (This effect is often used on Windows buttons.) All these features are easy to replicate using the Canvas control.

The first step is to create three properties whose type is Picture. You can call them ButtonImage, MouseOverImage, and MouseDownImage.

Then, in the Open event, do the following:

Me.Backdrop = ButtonImage

In the MouseOver event:

Me.Backdrop = MouseOverImage

In the MouseDown event:

Me.Backdrop = MouseDownImage

And in the MouseUp event:

Me.Backdrop = ButtonImage




REALbasic Cross-Platform Application Development
REALbasic Cross-Platform Application Development
ISBN: 0672328135
EAN: 2147483647
Year: 2004
Pages: 149

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net