Sign in Join
Wiki copytemporary

Lazarus Custom Drawn Controls: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11The **Lazarus Custom Drawn Controls** are a set of controls equivalent to the standard Lazarus controls, but which draw themselves. They can have many uses, including the ability to fully customize the drawing, the ability to have exactly the same look in different platforms and also a higher consistency of behavior.
22
33This set of controls is divided into two parts:
44
55- the custom drawn controls which are necessary for implementing Lazarus widgetsets and are located in the unit lazarus/lcl/customdrawncontrols.pas. These are described here.
66- all other custom drawn controls, which are used often, but aren't indispensable to implement a LCL custom drawn widgetset. Those are located in the package lazarus/components/customdrawn. These are described in the page [[Lazarus Custom Drawn Package]]
77
88## How start using these components?
99
1010To be able to use them in the Lazarus GUI designer install the package lazarus/components/customdrawn.lpk
1111
1212To only create customdrawn components by code one can simply add customdrawncontrols, customdrawndrawers and customdrawn_common to the uses clause, declare them and use them. Note that currently only the Win2000 drawer is in the LCL, so any other drawers need to be obtained from the package customdrawn.lpk
1313
1414It is important to note that styles are only available if you add the corresponding unit of the style to the uses clause.
1515
1616## How do these components work?
1717
1818The basic programming technique utilized by this set of controls is explained at the [[Developing with Graphics|Developing with Graphics#Create a custom control which draws itself]] page.
1919
2020The code of the controls itself is implemented in the unit customdrawncontrols.pas, but this unit has no drawing code at all. Customdrawncontrols.pas provides the code which processes all the control's keyboard and mouse events, implementing all the mouse and key response behaviour. Each control instance has a Drawer connected to it. A Drawer in our nomenclature (an instance of the TCDDrawer class) is similar to a theme rendering engine or suchlike. The unit customdrawndrawers.pas manages the list of all known drawers and it also declares all the basic data types needed for State and StateEx. These properties contain all the information about the control's current state, which is passed to the drawer so that it can have the information it requires to draw the control correctly. Only one instance of each drawer can exist in a program, and all controls refer to that single instance through the drawer's manager in customdrawndrawers.pas. Each control has a property called DrawStyle which allows you to choose (from an enumerated type) which draw style to use, and then customdrawndrawers.pas converts this information into a Drawer instance. You can specify the dsDefault DrawStyle which will then use the style specified in the global variable DefaultStyle. This was done to allow the style of all controls to be changed at once. Simply change this global variable and then call Invalidate on the controls. You can see below a code snippet from customdrawndrawers.pas which shows the methods which have been exposed to manage the list of drawers and also the Default style:
2121
2222``` pascal
2323unit customdrawndrawers;
2424//...
2525 TCDDrawStyle = (
2626 // The default is given by the DefaultStyle global variable
2727 // Don't implement anything for this drawer
2828 dsDefault = 0,
2929 // This is a common drawer, the base for all others. It implements the look from dsWin2000
3030 dsCommon,
3131 // Operating system styles
3232 dsWinCE, dsWin2000, dsWinXP,
3333 dsKDEPlastique, dsGNOME, dsMacOSX,
3434 dsAndroid,
3535 // Other special styles for the user
3636 dsExtra1, dsExtra2, dsExtra3, dsExtra4, dsExtra5,
3737 dsExtra6, dsExtra7, dsExtra8, dsExtra9, dsExtra10
3838 );
3939//...
4040procedure RegisterDrawer(ADrawer: TCDDrawer; AStyle: TCDDrawStyle);
4141function GetDefaultDrawer: TCDDrawer;
4242function GetDrawer(AStyle: TCDDrawStyle): TCDDrawer;
4343
4444var
4545 DefaultStyle: TCDDrawStyle = dsCommon; // For now default to the most complete one, later per platform
4646```
4747
4848Each Drawer class has methods to draw all controls and also all available primitives. All new drawers created by users should inherit from the class TCDCommonDrawer (which is the basic common drawer declared in the unit customdrawn_common.pas). If you don't inherit from TCDCommonDrawer your application might crash if it hits an abstract method. This is the most complete drawer, which implements all abstract methods from its base class, so inheriting from it guarantees that there will never be crashes owing to methods you have not yet implemented. Other drawers can override the desired methods to change the drawing style.
4949
5050The dsCommon drawer uses only TCanvas for all its rendering, but other themes might also use TLazIntfImage and TFPImageCanvas instead, in order to have a faster pixel access. This programming technique is described in [[Developing with Graphics|Developing with Graphics#Working with TLazIntfImage]].
5151
5252### Color Palette
5353
5454Drawers have also access to a color Palette, which has the usual LCL system colors. Default drawers will load their colors from the operating system via the LCL acording to the native platform. However, outside their native platform, drawers will use a standard color palette.
5555
5656``` pascal
5757 TCDColorPalette = class
5858 public
5959 ScrollBar, Background, ActiveCaption, InactiveCaption,
6060 Menu, Window, WindowFrame, MenuText, WindowText, CaptionText,
6161 ActiveBorder, InactiveBorder, AppWorkspace, Highlight, HighlightText,
6262 BtnFace, BtnShadow, GrayText, BtnText, InactiveCaptionText,
6363 BtnHighlight, color3DDkShadow, color3DLight, InfoText, InfoBk,
6464 //
6565 HotLight, GradientActiveCaption, GradientInactiveCaption,
6666 MenuHighlight, MenuBar, Form: TColor;
6767 end;
6868
6969 { There are 5 possible sources of input for color palettes:
7070 palDefault - Uses palNative when the operating system matches the drawer style,
7171 palFallback otherwise
7272 palNative - Obtain from the operating system
7373 palFallback - Use the fallback colors of the drawer
7474 palUserConfig-Load it from the user configuration files, ToDo
7575 palCustom - The user application has set its own palette
7676 }
7777 TCDPaletteKind = (palDefault, palNative, palFallback, palUserConfig, palCustom);
7878
7979 { TCDDrawer }
8080
8181 TCDDrawer = class
8282 protected
8383 public
8484 Palette: TCDColorPalette;
8585 constructor Create; virtual;
8686 destructor Destroy; override;
8787 procedure SetPaletteKind(APaletteKind: TCDPaletteKind);
8888 procedure LoadNativePaletteColors;
8989 procedure LoadFallbackPaletteColors; virtual;
9090```
9191
9292As the code above shows, you can choose where the colors should come from - either from the operating system current theme, from the drawer's default, or from a user-defined scheme provided by your application. This allows you to change all yor application's colors at runtime simply by changing the palette.
9393
9494## The Drawers
9595
9696### dsCommon
9797
9898This is the base drawer for all others and it implements a Win2000-look with some small improvements, like better focus drawings on controls
9999
100100### dsWinCE
101101
102102Imitates the Windows CE look, with some small improvements like better focus drawings on controls
103103
104104### dsWin2000
105105
106106Imitates the Windows 2000 look. Since dsCommon provides the Windows 2000 look already, this class implements no methods at all
107107
108108### dsWinXP
109109
110110Imitates the Windows XP look
111111
112112### dsKDEPlastique
113113
114114Imitates the look of the Plastique Qt/KDE Theme
115115
116116### dsAndroid
117117
118118Imitates the Android look
119119
120120### dsWindows7
121121
122122![](/wiki-images/0/09/Customdrawn_windowsxp.png) ![](/wiki-images/f/f5/Customdrawn_windows7.png) ![](/wiki-images/1/1c/Customdrawn_windows8.png) ![](/wiki-images/1/16/Customdrawn_windows11.png) ![](/wiki-images/b/b8/Customdrawn_3d.png)
123123
124124Imitates the Windows look. Currently is available only with BGRABitmap graphic library, and you can get this drawer in the BGRAControls package: [[BGRAControls]]
125125
126126### BGRAControls Custom Drawn default style
127127
128-<figure>
129-<img src="customdrawn_dark.png" title="customdrawn_dark.png" />
130-<figcaption>customdrawn_dark.png</figcaption>
131-</figure>
128+![](/wiki-images/3/34/Customdrawn_dark.png)
132129
133130## Controls which imitate the Standard Palette
134131
135132### TCDButton
136133
137134This is a fully custom drawn button.
138135
139136Usage example:
140137
141138``` pascal
142139uses customdrawnextras;
143140
144141procedure TForm1.FormCreate(Sender: TObject);
145142var
146143 MyButton: TCDButton;
147144begin
148145 MyButton := TCDButton.Create(Self);
149146 MyButton.Parent := Self;
150147 MyButton.DrawStyle := dsWin2000;
151148 MyButton.Left := 100;
152149 MyButton.Top := 100;
153150 MyButton.Width := 200;
154151 MyButton.Height := 50;
155152 MyButton.Caption := 'My Button';
156153 MyButton.Color := clRed;
157154 MyButton.OnClick := @HandleButtonClick;
158155end;
159156```
160157
161-<figure>
162-<img src="Custom_drawn_buttons.png" title="Custom_drawn_buttons.png" />
163-<figcaption>Custom_drawn_buttons.png</figcaption>
164-</figure>
158+![](/wiki-images/5/54/Custom_drawn_buttons.png)
165159
166160### TCDEdit
167161
168162This is a fully custom drawn edit control.
169163
170-<figure>
171-<img src="TCDEdit.png" title="TCDEdit.png" />
172-<figcaption>TCDEdit.png</figcaption>
173-</figure>
164+![](/wiki-images/b/bd/TCDEdit.png)
174165
175166### TCDCheckBox
176167
177168This is a fully custom drawn check box.
178169
179-<figure>
180-<img src="TCDCheckBox.png" title="TCDCheckBox.png" />
181-<figcaption>TCDCheckBox.png</figcaption>
182-</figure>
170+![](/wiki-images/2/20/TCDCheckBox.png)
183171
184172### TCDRadioButton
185173
186-<figure>
187-<img src="TCDRadioButton.png" title="TCDRadioButton.png" />
188-<figcaption>TCDRadioButton.png</figcaption>
189-</figure>
174+![](/wiki-images/3/34/TCDRadioButton.png)
190175
191176### TCDScrollBar
192177
193-<figure>
194-<img src="TCDScrollBar.png" title="TCDScrollBar.png" />
195-<figcaption>TCDScrollBar.png</figcaption>
196-</figure>
178+![](/wiki-images/e/e8/TCDScrollBar.png)
197179
198180### TCDGroupBox
199181
200182This is a fully custom drawn group box.
201183
202-<figure>
203-<img src="TCDGroupBox.png" title="TCDGroupBox.png" />
204-<figcaption>TCDGroupBox.png</figcaption>
205-</figure>
184+![](/wiki-images/4/48/TCDGroupBox.png)
206185
207186## Controls which imitate the Additional Palette
208187
209188### TCDStaticText
210189
211190## Controls which imitate the Common Controls Palette
212191
213192### TCDTrackBar
214193
215194A substitute for TTrackBar
216195
217-<figure>
218-<img src="TCDTrackBar.png" title="TCDTrackBar.png" />
219-<figcaption>TCDTrackBar.png</figcaption>
220-</figure>
196+![](/wiki-images/b/b5/TCDTrackBar.png)
221197
222198### TCDProgressBar
223199
224-<figure>
225-<img src="TCDProgressBar.png" title="TCDProgressBar.png" />
226-<figcaption>TCDProgressBar.png</figcaption>
227-</figure>
200+![](/wiki-images/a/af/TCDProgressBar.png)
228201
229202### TCDListView
230203
231204Under construction.
232205
233206### TCDTabControl
234207
235208A substitute for TTabControl
236209
237210### TCDPageControl
238211
239212A substitute for TPageControl
240213
241-<figure>
242-<img src="TCDPageControl.png" title="TCDPageControl.png" />
243-<figcaption>TCDPageControl.png</figcaption>
244-</figure>
214+![](/wiki-images/a/ab/TCDPageControl.png)
245215
246216## Custom Drawn Packages
247217
248218Moved here: [[Lazarus Custom Drawn Package]]
249219
250220## Other good custom drawn components for Lazarus
251221
252222- [[VirtualTreeview]]
253223- [[ATTabs]]
254224- [[ATStatusBar]]
255225- [[ATScrollBar]]
256226
257227## Maintainers
258228
259229- Felipe Monteiro de Carvalho
260230- JiXian Yang
261231
262232## See Also
263233
264234- [[Lazarus Components Directory]]
265235- [[Lazarus Custom Drawn Package]]
266236- [[Custom Drawn Interface]]
267237
268238---
269239*From the Free Pascal wiki, page "Lazarus Custom Drawn Controls", as it stood on 2024-02-08 after a change by 007, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history