Canvas

From Real Software Documentation

Jump to: navigation, search
Class (inherits from RectControl)

Canvas controls are very useful for implementing your own graphical controls because you can use the Graphics class drawing commands or the Object2D classes to draw inside the Canvas region. It can also be used to display existing graphics, like the ImageWell.

Events
Activate KeyDown
Close KeyUp
ConstructContextualMenu LostFocus
ContextualMenuAction MouseDown
Deactivate MouseDrag
DoubleClick MouseEnter
DragEnter MouseExit
DragExit MouseMove
DragOver MouseUp
DropObject MouseWheel
EnableMenuItems Open
GotFocus Paint


Properties
AcceptFocus HelpTag PanelIndex
AcceptTabs Index Parent
Active Left Scope
AutoDeactivate LockBottom TabIndex
Backdrop LockLeft TabStop
DoubleBuffer LockRight Top
Enabled LockTop TrueWindow
EraseBackground MouseCursor UseFocusRing
MouseX Visible
Handle MouseY Width
Height Name Window


Methods
AcceptFileDrop Invalidate
AcceptPictureDrop Refresh
AcceptRawDataDrop RefreshRect
AcceptTextDrop Scroll
Close SetFocus
DrawInto


Notes

Coordinates passed to Canvas events are local to the Canvas object.

To use the Scroll method to scroll the picture in a Canvas control, you need to store the last scroll value for the axis you are scrolling so you can use this to calculate the amount to scroll. This can be done by adding properties to the window that contains the Canvas control or by creating a new class based on the Canvas control that contains properties to hold the last X scroll amount and last Y scroll amount.

If the ScrollControls parameter is True, any controls on top of the Canvas control will also be scrolled. This allows the implementation of a scrolling pane of controls.

The following example scrolls a picture that was added to the project. The properties XScroll and YScroll have been added to the window to hold the amounts the picture has been scrolled. The picture is scrolled 8 pixels at a time. In the Keydown event of the window, the following code calls the Scroll method whenever the Up, Down, Left, or Right arrow keys are pressed.

Const LeftArrow = 28
Const RightArrow = 29
Const UpArrow = 30
Const DownArrow = 31
Const ScrollUnit = 8 // pixels

Select Case Asc(Key)

Case LeftArrow
XScroll = XScroll + ScrollUnit
Canvas1.Scroll ScrollUnit, 0

Case RightArrow
XScroll = XScroll - ScrollUnit
Canvas1.Scroll -ScrollUnit, 0

Case UpArrow
YScroll = YScroll + ScrollUnit
Canvas1.Scroll 0, ScrollUnit

Case DownArrow
YScroll = YScroll - ScrollUnit
Canvas1.Scroll 0, -ScrollUnit

End Select


The Scroll method calls the Paint event of the canvas that redraws the picture with the new values of XScroll and YScroll. The Paint event has the following line of code:

g.DrawPicture myPicture,Xscroll,Yscroll

Examples

This example draws a 3D rectangle with a raised look.

Sub Paint(g as Graphics)
Const White = &cffffff
Const DarkGray = &c8c8c8c
Const LightGray = &cefefef

g.forecolor=White
g.drawline 1,1,canvas1.width,1
g.drawline 1,canvas1.height-1,1,1
g.forecolor=DarkGray
g.drawline canvas1.width-1,2,canvas1.width-1,canvas1.height
g.drawline 1,canvas1.height-1,canvas1.width,canvas1.height-1
//fill in the light gray rectangle
g.forecolor=LightGray
g.fillrect 2,2,canvas1.width-3,canvas1.height-3


This example assigns a picture that has been added to the Project Editor to the Backdrop property in the Open event:

Me.backdrop=OSLogo


You can use the methods of the Graphics class to modify the picture in any way you like. For example, the following code in the object's Paint event handler adds a caption to the picture:

Me.Graphics.TextFont="Helvetica"
Me.Graphics.TextSize=14
Me.Graphics.ForeColor=&c0080C0
Me.Graphics.DrawString "Joe Bob",10,100


If you instead assigned the graphic to the BackDrop property using the Picture constructor, as shown below, you could manipulate the graphic at the pixel level using the RGBSurface property of the Picture object.

Me.backdrop=New Picture(210,30,32)
Me.backdrop.Graphics.DrawPicture madeWithREALStudioLogo,0,0


See also the examples for the Control class, which give an example of dragging from a Canvas control and the ImageWell class example, which show drag and drop to and from the ImageWell.Image property.


Simulating a Focus Ring on Windows and Linux

Unfortunately, a Focus Ring does not appear on Windows or Linux when a Canvas control gets the focus, but the GotFocus and LostFocus events fire normally. You can easily use them to simulate a focus ring.

In the GotFocus event, use:

#If TargetWin32 or TargetLinux
Me.Graphics.forecolor=HighlightColor //or FrameColor, whichever you wish
Me.Graphics.DrawRect 0,0,me.width-1,me.height-1
#endif


In the LostFocus event, use:

#If TargetWin32
Me.Graphics.forecolor=RGB(178,178,178) //gray
Me.Graphics.DrawRect 0,0,me.width-1,me.height-1
#endif

The simulated focus ring looks like this:

A Canvas showing simulated focus ring.

See Also

Graphics, Picture classes.Link title

Personal tools