Delphi Create A Drawing Space Using DelphiVCL.Canvas

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Create A Drawing Space Using DelphiVCL.Canvas
By Muhammad Azizul Hakim April 13, 2021

DelphiVCL.Canvas provides an abstract drawing space for objects that must render their own images.

Use DelphiVCL.Canvas as a drawing surface for objects that draw an image of themselves. Standard window controls such as edit controls or list boxes do not require a canvas, as they are drawn by the system.

DelphiVCL.Canvas provides properties, events, and methods that assist in creating an image by:
  • Specifying the type of brush, pen, and font to use.
  • Drawing and filling a variety of shapes and lines.
  • Writing text.
  • Rendering graphic images.
  • Enabling a response to changes in the current image.
Let’s browse all the properties, methods, and built-in properties of the DelphiVCL.Canvas using dir() command:
Python:
import DelphiVCL
 
dir(DelphiVCL.Canvas)
See the responses in our Windows command prompt:
1618389963249.png
Here is the working example of the implementation of DelphiVCL.Canvas:
Python:
def grdTestDrawCell(Sender, Col, Row, Rect, State):
    if gdSelected in State:
        Sender.Canvas.Brush.Color = clBlue # 0x00ff0000 # blue
        Sender.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, "%d @ %d" % (Col, Row))
To see the result, let’s run the complete script:
Python:
from DelphiVCL import *
 
# Create a Class to build a basic Form
class MainForm(Form):
    def __init__(self, Owner):
        self.Caption = "Introduction to VCL Components"
        self.Name = "BaseForm"
        self.SetBounds(10, 10, 500, 450)
 
        # Create a Main Panel component
        pnlMain = CreateComponent('TPanel',Owner)
        pnlMain.SetProps(Parent=self, Caption="",align = "alClient", Name = "MainPanel")
 
        # Page control creation
        pgConMain = PageControl(pnlMain)
        pgConMain.Name = "MyPageControl"
        pgConMain.Parent = pnlMain
        pgConMain.Align = "alClient"
 
        # Tabsheet one
        pgOne = TabSheet(pnlMain)
        pgOne.PageControl = pgConMain
        pgOne.Caption = "Tab 1"
 
        # Draw grid
        grdTest = DrawGrid(pgOne)
        grdTest.Parent = pgOne
        grdTest.SetBounds(20, 20, 420, 180)
 
        def grdTestDrawCell(Sender, Col, Row, Rect, State):
            if gdSelected in State:
                Sender.Canvas.Brush.Color = clBlue # 0x00ff0000 # blue
                Sender.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, "%d @ %d" % (Col, Row))
 
        def grdTestSelectCell(Sender, Col, Row, CanSelect):
            if Col == 2 and Row == 2:
                CanSelect.Value = False
 
        grdTest.OnDrawCell = grdTestDrawCell
        grdTest.OnSelectCell = grdTestSelectCell
 
 
# Initialize your application
def main():
    Application.Initialize()
    Application.Title = "MyDelphiApp"
    f = MainForm(Application)
    f.Show()
    FreeConsole()
    Application.Run()
    Application.Destroy()
 
main()
The result:
1618390009582.png
Для просмотра ссылки Войди или Зарегистрируйся