| Rapid-Q Documentation by William Yu (c)1999 | Chapter 12 |
DECLARE SUB CanvasPaint (Sender AS QCanvas)
CREATE Form AS QForm
CREATE Canvas AS QCanvas
OnPaint = CanvasPaint
END CREATE
ShowModal
END CREATE
SUB CanvasPaint (Sender AS QCanvas)
Sender.FillRect(10,10,100,100,&HFF0000) '-- Blue box
END SUB
If you try hiding the form under another Window and then refocus the program, the
blue box will still appear. Now try writing something that doesn't utilize the
OnPaint procedure:
CREATE Form AS QForm
CREATE Canvas AS QCanvas
FillRect(10,10,100,100,&HFF0000) '-- Blue box
END CREATE
Form.ShowModal
END CREATE
Notice anything? Exactly, there is no blue box. The paint message was sent
when your form first appears on your desktop, and since you didn't write any
OnPaint procedure, there's nothing to draw. So what really happens to that FillRect
code? Nothing really, it's still executed, but the next paint message offset it.
To understand this better, try this code which will display the blue box when
your press the button, but try hiding the window again or minimizing it. Redisplay your window and see
what happens:
DECLARE SUB ButtonClick
CREATE Form AS QForm
CREATE Button AS QButton
OnClick = ButtonClick
END CREATE
CREATE Canvas AS QCanvas
END CREATE
ShowModal
END CREATE
SUB ButtonClick
Canvas.FillRect(10,10,100,100,&HFF0000)
END SUB
When you click your button the blue box will be drawn, however, once you hide
part of the window and redisplay your window, you'll probably notice that your blue box
has dissappeared.
DECLARE SUB CanvasPaint (Sender AS QCanvas)
DECLARE SUB ButtonClick (Sender AS QButton)
' Create bitmap for off-screen use
DIM BitMap AS QBITMAP
BitMap.Height = 100
BitMap.Width = 100
BitMap.Paint(0,0,0,0)
CREATE Form AS QForm
Center
Caption = "Simple graphics demonstration"
CREATE Canvas AS QCanvas
OnPaint = CanvasPaint
END CREATE
CREATE SquareButton AS QButton
Caption = "Draw Square"
OnClick = ButtonClick
Left = 150
END CREATE
CREATE CircleButton AS QButton
Caption = "Draw Circle"
OnClick = ButtonClick
Left = 150
Top = 50
END CREATE
CREATE LineButton AS QButton
Caption = "Draw Line"
OnClick = ButtonClick
Left = 150
Top = 100
END CREATE
ShowModal
END CREATE
SUB CanvasPaint (Sender AS QCanvas)
Sender.Draw(0,0,Bitmap.BMP)
END SUB
SUB ButtonClick (Sender AS QButton)
SELECT CASE Sender.Caption
CASE "Draw Square"
Bitmap.FillRect(10,10,50,50,&HFF0000)
CASE "Draw Circle"
Bitmap.Circle(10,60,50,110,&H0000FF,&H0000FF)
CASE "Draw Line"
Bitmap.Line(50,50,90,90,&H00FF00)
END SELECT
Canvas.Repaint '-- Tell Canvas to repaint itself.
END SUB
|

SUB ListBoxDrawItem(Index AS INTEGER, State AS BYTE, Rect AS QRECT)
IF State = 0 THEN
'-- Selected
ListBox.FillRect(Rect.Left, Rect.Top, Rect.Right, Rect.Bottom, &H00FF00)
ELSE
ListBox.FillRect(Rect.Left, Rect.Top, Rect.Right, Rect.Bottom, &HFFFFFF)
END IF
ListBox.TextOut(100, Rect.Top+(Rect.Bottom-Rect.Top)/4, ListBox.Item(index), 0, -1)
ListBox.Draw(Rect.Left, Rect.Top, Bitmap(Index).BMP)
END SUB
Hopefully the code is self explanatory, but a few things you may notice is that
you don't paint the entire listbox (ie. all items), but rather a paint message is
sent everytime an item is changed. This item occupies a rectangular region in your
listbox, so what the OnDrawItem sends to you is the region of this item (this is
the Rect parameter). You should not overstep this bound that has been
given to you (ie. don't try to draw outside this region, it's not yours to draw on!).
I have not covered QStringGrid in this section, but the same idea persists.
You'll have to implement the OnDrawCell event, which is similar to the way
you handle OnDrawItem in list/combo boxes.
| Prev Chapter | Contents | Next Chapter |