In this chapter, we will cover the following recipes:
Changing your application's look and feel with VCL styles and no code
Changing the style of your VCL application at runtime
Customizing TDBGrid
Using the owner's draw combos and listboxes
Creating a stack of embedded forms
Manipulating JSON
Manipulating and transforming XML documents
I/O in the twenty-first century – knowing streams
Putting your VCL application in the tray
Creating a Windows service
Associating a file extension with your application on Windows
This chapter explains some of the day-to-day needs of a Delphi programmer. These are ready-to-use recipes that will be useful every day and have been selected ahead of a lot of others because although they may be obvious for some experienced users, they are still very useful. Even if there is no specifically database-related code, many of the recipes can also be used (or sometimes especially used) when you are dealing with data.
VCL styles are a major new entry in the latest versions of Delphi. They have been introduced in Delphi XE2 and are still one of the less-known features for the good old Delphi developers. However, as usual, some businessmen say looks matter, so the look and feel of your application could be one of the reasons to choose your product over one from a competitor. Consider that with a few mouse clicks you can apply many different styles to your application to change the look and feel of your applications. So why not give it a try?
VCL styles can be used to revamp an old application or to create a new one with a nonstandard GUI. VCL styles are a completely different beast to FireMonkey styles. They are both styles but with completely different approaches and behavior.
To get started with VCL styles, we'll use a new application. Let's create a new VCL application and drag-and-drop some components onto the main form (for example, two TButton components, one TListBox component, one TComboBox component, and a couple of TCheckBox components).
The following screenshot is the resultant form that runs on a Windows 7 machine:

A form without style
Now we've to apply a set of nice styles. To do this, perform the following steps:
Navigate to Project | Options. In the resultant dialog, go to Application | Appearance and select all the styles that we want to include in our application.
Using the Preview button, the IDE shows a simple demo form with some controls, and we can get an idea about the final result of our styled form. Feel free to experiment and choose the style—or set of styles—that you like. Only one style will be used at a time, but we can link the necessary resources to the executable and select the proper one at runtime.
After selecting all the required styles from the list, we've to select one in the combobox at the bottom of the screen. This style will be the default style for our form and will be loaded as soon as the application starts. You can delay this choice and make it at runtime using code if you prefer.
Click on OK and hit F9 (or navigate to Run | Run) and your application is styled! The resultant form is shown in the following screenshot:
The same form as the preceding one but with the Iceberg Classico style applied
Selecting one or more styles by navigating to
Project | Options | Application | Appearance can cause the Delphi linker to link the style resource to your executable. It is possible to link many styles to your executable, but you can use only one style at time. So, how does Delphi know which style you want to use when there are more than one styles? If we check the Project
file (the file with the .dpr
extension) by navigating to Project | View Source, you can see where and how this little magic happens.
The following lines are the interesting part:
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
TStyleManager.TrySetStyle('Iceberg Classico');
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.
When we've selected the Iceberg Classico style as the default style, the Delphi IDE adds a line just before the creation of the main form, setting the default style for the application using the TStyleManager.TrySetStyle
static method.
TStyleManager
is a very important class when dealing with VCL styles. We'll see more about it in the next recipe when we'll learn how to change a style at runtime.
Delphi and C++Builder XE6 come with 29 VCL styles available in C:\Program Files (x86)\Embarcadero\Studio\14.0\Redist\styles\vcl\
(with a standard installation).
Moreover, it is possible to create your own styles or modify the existing ones by using the Bitmap Style Designer available at Tools | Bitmap Style Designer menu. The Bitmap Style Designer also provides test applications to test VCL styles.
For more details on how to create or customize a VCL style, check the following link:
http://docwiki.embarcadero.com/RADStudio/XE6/en/Creating_a_Style_using_the_Bitmap_Style_Designer
VCL styles are a powerful way to change the appearance of your application, but using them only as design-time tools is way too limited. One of the main features of a VCL style is the ability to change the style while an application is running.
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Because a VCL style is simply a particular kind of binary file, we can allow our users to load their preferred style at runtime, and we can even provide new styles—publishing them on a website or sending them by an e-mail to our customers.
In this recipe, we'll be able to change the style while an application is running using a style already linked at design time or let the user choose between a set of styles deployed inside a folder.
Styles manipulation at runtime is done using the class methods of the TStyleManager
class:
Create a brand new VCL application and add the
Vcl.Themes
andVcl.Styles
units to the main implementation form. These units are required to use VCL styles at runtime.Drop on the form a TListBox component, two TButton components, and two TOpenDialog components. Leave the default component names.
Go to Project | Appearance and select eight styles of your choice from the list. Leave the Default style option to Windows.
The
TStyleManager.StyleNames
property contains all names of the available styles. In theFormCreate
event handler, we have to load the already linked styles present in the executable to the listbox to let the user choose one of them. So, create a new procedure calledStylesListRefresh
with the following code and call it from theFormCreate
event handler:procedure TMainForm.StylesListRefresh; var stylename: string; begin ListBox1.Clear; // retrieve all the styles linked in the executable for stylename in TStyleManager.StyleNames do begin ListBox1.Items.Add(stylename); end; end;
In the
Button1Click
event handler, we've to set the current style according to the one selected fromListBox1
using the following code:TStyleManager.SetStyle(ListBox1.Items[ListBox1.ItemIndex]);
The
Button2Click
event handler should allow the user to select a style from disk. So, we have to create a folder namedstyles
at level of our executable and copy a few.vsf
files from the default style directory which isC:\Program Files (x86)\Embarcadero\Studio\14.0\Redist\styles\vcl\
in RAD Studio XE6.After copying the files, write the following code under the
Button2Click
event handler. This code allows the user to chose a style file directly from the disk. Then you can select one of the loaded styles from the listbox and click on Button1 to apply it to the application. The code is as follows:if OpenDialog1.Execute then begin if TStyleManager.IsValidStyle(OpenDialog1.FileName) then begin //load the style file TStyleManager.LoadFromFile(OpenDialog1.FileName); //refresh the list with the currently available styles StylesListRefresh; ShowMessage('New VCL Style has been loaded'); end else ShowMessage('The file is not a valid VCL Style!'); end; end;
Just to have an idea of how the different controls appear with the selected style, drag-and-drop some controls to the right-hand side of the form. The following screenshot shows an application with some styles loaded, some at design time and some from the disk. Hit F9 (or go to Run | Run) and play with your application using and loading styles from the disk.
The Style Chooser form with a Turquoise Gray style loaded
The TStyleManager
class has all the methods we need:
Inspect the loaded styles with
TStyleManager.StyleNames
Apply an already loaded style to the running application using
TStyleManager.SetStyle('StyleName')
Check if a file is a valid style with
TStyleManager.IsValidStyle('StylePathFileName')
Load a style file from disk using
TStyleManager.LoadFromFile('StylePathFileName')
After loading new styles from the disk, these new styles are completely similar to the styles linked to the executable during the compile and link phases and can be used in the same way.
Other things to consider are third-party controls. If your application uses third-party controls, take care with their style support. If your external components do not support styles, you will end up with some controls styled (the original included in Delphi) and some not (your external third-party controls)!
By navigating to Tools | Bitmap Style Designer and using a custom VCL style, we can also perform the following actions:
Change the application's colors (for example,
ButtonNormal
,ButtonPressed
,ButtonFocused
,ButtonHot
, and so on)Override the system's colors (for example,
clCaptionText
,clBtnFace
,clActiveCaption
, and so on)Change the font color and font name for particular controls (for example,
ButtonTextNormal
,ButtonTextPressed
,ButtonTextFocused
,ButtonTextHot
, and so on)
The following screenshot shows the Bitmap Style Designer window while working on a custom style:

The Bitmap Style Designer while it is working on a custom style
The adage a picture is worth a thousand words refers to the notion that a complex idea can be conveyed with just a single still image. Sometimes, even a simple concept is easier to understand and nicer to see if it is represented by images. In this recipe, we'll see how to customize the TDBGrid
object to visualize graphical representation of data.
Many VCL controls are able to delegate their drawing, or part of it, to user code. This means that we can use simple event handlers to draw standard components in different ways. It is not always simple, but TDBGrid
is customizable in a really easy way. Let's say that we have a class of musicians that have to pass a set of exams. We want to show the percentage of musicians who have already passed exams with a progress bar, and if the percent is higher than 50 percent, there should also be a check in another column.
We'll use a special in-memory table from the FireDAC
library. FireDAC
is a new data access library from Embarcadero, which is included in RAD Studio since Version XE5. If some of the code seems unclear at the moment, consider the in-memory table as a normal TDataSet
descendant. However, at the end of this section, there are some links to the FireDAC documentation, and I strongly suggest you to read them if you still don't know FireDAC. To customize TDBGrid
, perform the following steps:
Create a brand new VCL application and drop on the form the
TFDMemTable
,TDBGrid
,TDataSource
,TDBNavigator
component. Connect all these components in the usual way (TDBGrid
->TDataSource
->TFDMemTable
). Set theTDBGrid
font size to24
. This will create more space in the cell for our graphical representation.Using the
TFDMemTable
fields editor, add the following fields and then activate the dataset setting by setting itsActive
property toTrue
:Field name
Field datatype
Field type
FullName
String (size
50
)Data
TotalExams
Integer
Data
PassedExams
Integer
Data
PercPassedExams
Float
Calculated
MoreThan50Percent
Boolean
Calculated
In a real application, we should load real data from some sort of database. But for now, we'll use some custom data generated in code. We have to load this data into the dataset with the following code:
procedure TMainForm.FormCreate(Sender: TObject); begin FDMemTable1.InsertRecord( ['Ludwig van Beethoven',30,10]); FDMemTable1.InsertRecord( ['Johann Sebastian Bach',24,10]); FDMemTable1.InsertRecord( ['Wolfgang Amadeus Mozart',30,30]); FDMemTable1.InsertRecord( ['Giacomo Puccini',25,10]); FDMemTable1.InsertRecord( ['Antonio Vivaldi',20,20]); FDMemTable1.InsertRecord( ['Giuseppe Verdi',30,5]); end;
Do you remember? We've two calculated fields that need to be filled in some way. Create the
OnCalcFields
event handler on theTFDMemTable
component and fill it with the following code:procedure TMainForm.FDMemTable1CalcFields( DataSet: TDataSet); var p: Integer; t: Integer; begin p := FDMemTable1.FieldByName('PassedExams').AsInteger; t := FDMemTable1.FieldByName('TotalExams').AsInteger; if t = 0 then begin FDMemTable1.FieldByName('PercPassedExams').AsFloat := 0 end else begin FDMemTable1. FieldByName('PercPassedExams'). AsFloat := p / t * 100; end; FDMemTable1.FieldByName('MoreThan50Percent').AsBoolean := FDMemTable1. FieldByName('PercPassedExams').AsFloat > 50; end;
Run the application by hitting F9 (or navigating to Run | Run) and you will get the following screenshot:
A normal form with some data
This is useful, but a bit boring. Let's start our customization. Close the application and return to Delphi IDE.
Go to the
TDBGrid
properties and setDefaultDrawing
tofalse
.Go to the
TDBGrid
event and create an event handler forOnDrawColumnCell
. All the customization code goes in this event.Include the
Vcl.GraphUtil
unit and write the following code in theDBGrid1DrawColumnCell
event:procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var R: TRect; Grid: TDBGrid; S: string; WPerc: Extended; SSize: TSize; SavedPenColor: Integer; SavedBrushColor: Integer; SavedPenStyle: TPenStyle; SavedBrushStyle: TBrushStyle; begin Grid := TDBGrid(Sender); if [gdSelected, gdFocused] * State <> [] then Grid.Canvas.Brush.Color := clHighlight; if Column.Field.FieldKind = fkCalculated then begin R := Rect; SavedPenColor := Grid.Canvas.Pen.Color; SavedBrushColor := Grid.Canvas.Brush.Color; SavedPenStyle := Grid.Canvas.Pen.Style; SavedBrushStyle := Grid.Canvas.Brush.Style; end; if Column.FieldName.Equals('PercPassedExams') then begin S := FormatFloat('##0', Column.Field.AsFloat) + ' %'; Grid.Canvas.Brush.Style := bsSolid; Grid.Canvas.FillRect(R); WPerc := Column.Field.AsFloat / 100 * R.Width; Grid.Canvas.Font.Size := Grid.Font.Size - 1; Grid.Canvas.Font.Color := clWhite; Grid.Canvas.Brush.Color := clYellow; Grid.Canvas.RoundRect(R.Left, R.Top, Trunc(R.Left + WPerc), R.Bottom, 2, 2); InflateRect(R, -1, -1); Grid.Canvas.Pen.Style := psClear; Grid.Canvas.Font.Color := clBlack; Grid.Canvas.Brush.Style := bsClear; SSize := Grid.Canvas.TextExtent(S); Grid.Canvas.TextOut( R.Left + ((R.Width div 2) - (SSize.cx div 2)), R.Top + ((R.Height div 2) - (SSize.cy div 2)), S); end else if Column.FieldName.Equals('MoreThan50Percent') then begin Grid.Canvas.Brush.Style := bsSolid; Grid.Canvas.Pen.Style := psClear; Grid.Canvas.FillRect(R); if Column.Field.AsBoolean then begin InflateRect(R, -4, -4); Grid.Canvas.Pen.Color := clRed; Grid.Canvas.Pen.Style := psSolid; DrawCheck(Grid.Canvas, TPoint.Create(R.Left, R.Top + R.Height div 2), R.Height div 3); end; end else Grid.DefaultDrawColumnCell(Rect, DataCol, Column, State); if Column.Field.FieldKind = fkCalculated then begin Grid.Canvas.Pen.Color := SavedPenColor; Grid.Canvas.Brush.Color := SavedBrushColor; Grid.Canvas.Pen.Style := SavedPenStyle; Grid.Canvas.Brush.Style := SavedBrushStyle; end; end;
That's all, folks! Hit F9 (or navigate to Run | Run) and we now have a nicer grid with more direct information about our data:
The same grid with a bit of customization
By setting the DBGrid
property DefaultDrawing
to false
, we told the grid that we want to manually draw all the data into every cell. The OnDrawColumnCell
event allows us to actually draw using the standard Delphi code. For each cell we are about to draw, the event handler is called with a list of useful parameters to know which cell we're about to draw and what data we have to read considering the column currently drawn. In this case, we want to draw only the calculated columns in a customized way. This is not a rule, but this can be done to manipulate all cells. We can draw any cell in the way we like. For the cells where we don't want to do custom drawing, a simple DefaultDrawColumnCell
call method passing the same parameters we got from the event and the VCL code will draw the current cell as usual.
Among the event parameters, there is Rect
(of the TRect
type) that represents the specific area we're about to draw, there is Column
(of the TColumn
type) that is a reference to the current column of the grid, and there is State
(of the TGridDrawState
type) that is a set of the grid cell states (for example, Selected
, Focused
, HotTrack
, and so on). If our drawing code ignores the State
parameter, all the cells will be drawn in the same way and users cannot see which cell or row is selected.
The event handler uses a sets intersection to know whether the current cell should be drawn as a selected or focused cell:
if [gdSelected, gdFocused] * State <> [] then Grid.Canvas.Brush.Color := clHighlight;
Owner drawing is a really large topic and can be simple or tremendously complex involving much Canvas
related code. However, often the kind of drawing you need will be relatively similar. So, if you need checks, arrows, color gradients, and so on, check the procedures in the Vcl.GraphUtil
unit. Otherwise, if you need images, you could use a TImageList
class to hold all the images needed by your grid.
The good news is that the drawing code can be reused by different kind of controls, so try to organize your code in a way that allows code reutilization avoiding direct dependencies to the form where the control is.
The code in the drawing events should not contain business logic or presentation logic. If you need presentation logic, put it in a separate and testable function or class.
Many things are organized in a list. Lists are useful when you have to show items or when your user has to choose among a set of possible options. Usually, standard lists are flat, but sometimes you need to transmit more information in addition to a list of items. Let's think about when you go to choose a font in an advanced text editor such as Microsoft Word or OpenOffice.org. Having the name of the font drawn in the font style itself helps users to make a faster and more reasoned choice. In this recipe, we'll see how to make listboxes more useful. The code is perfectly valid also for a TComboBox
.
As we saw in the Customizing TDBGrid recipe, many VCL controls are able to delegate their drawing, or part of it, to user code. This means that we can use simple event handlers to draw standard components in different ways. Let's say that we have a list of products in our store and we have to set discounts on these products. As there are many products, we want to make it simple so that our users can make a fast selection between the available discount percentages using a color code.
Create a brand new VCL application and drop on the form a
TListBox
component. Set the following properties:Property
Value
Style
lbOwnerDrawFixed
Font.Size
14
In the listbox
Items
property, add seven levels of discount. For example, you can use the following: no discount, 10 percent discount, 20 percent discount, 30 percent discount, 40 percent discount, 50 percent discount, and 70 percent discount.Then, drop a
TImageList
component on the form and set the following properties:Property
Value
ColorDepth
cd32Bit
DrawingStyle
dsTransparent
Width
32
Height
32
The
TImageList
component is our image repository and will be used to draw an image by index. Load seven PNG images (of 32 x 32 size) intoTImageList
. You can find some nice PNG icons in the recipe's project folder (ICONS\PNG\32
).Create an
OnDrawItem
event handler for theTListBox
component and write the following code:procedure TCustomListControlsForm.ListBox1DrawItem( Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); var LBox: TListBox; R: TRect; S: string; TextTopPos, TextLeftPos, TextHeight: Integer; const IMAGE_TEXT_SPACE = 5; begin LBox := Control as TListBox; R := Rect; LBox.Canvas.FillRect(R); ImageList1.Draw(LBox.Canvas, R.Left, R.Top, Index); S := LBox.Items[Index]; TextHeight := LBox.Canvas.TextHeight(S); TextLeftPos := R.Left + ImageList1.Width + IMAGE_TEXT_SPACE; TextTopPos := R.Top + R.Height div 2 - TextHeight div 2; LBox.Canvas.TextOut(TextLeftPos, TextTopPos, S); end;
Run the application by hitting F9 (or navigate to Run | Run) and you will see the following screenshot:
Our listbox with some custom icons read from TImageList
The TListBox.OnDrawItem
event handler allows us to customize the drawing of the listbox. In this recipe, we used a TImageList
component as the image repository for the listbox. Using the Index
parameter, we read the correspondent image in the image list and drawn on the Canvas
listbox. After this, all the other code is related to the alignment of image and text inside the listbox row.
Remember that this event handler will be called for each item in the list, so the code must be fast and should not do too much slow Canvas
writing. Otherwise, all your GUI will be unresponsive. If you want to create complex graphics on the fly in the event, I strongly suggest you to prepare your images the first time you draw the item and then put them in a sort of cache memory (TObjectList<TBitmap>
is enough).
While you are in the OnDrawITem
function, you can do whatever you want with the TListBox Canvas
. Moreover, the State
parameter (of the TOwnerDrawState
type) tells you in which states the listbox item is (for example, Selected
, Focused
, HotTrack
, and so on), so you can use different kind of drawings depending on the item's state. You can check the Customizing TDBGrid recipe to know about the TDBGrid
owner drawing for an example ofthe State
parameter.
If you want to make your code aware of the selected VCL style, changing the color used according to it, you can use StyleServices.GetStyleColor()
, StyleServices.GetStyleFontColor()
, and StyleServices.GetSystemColor()
into the Vcl.Themes
unit.
The icons used in this recipe are from the Icojam website (http://www.icojam.com). The specific set used is available at http://www.icojam.com/blog/?p=259.
Every modern browser has a tabbed interface. Also, many other kinds of multiple views software have this kind of interface. Why? Because it's very useful. While you are reading one page, you can rapidly check another page, and then still come back to the first one at the same point you left some seconds ago. You don't have to redo a search or redo a lot of clicks to just go back to that particular point. You simply have switched from one window to another and back to the first. I see too many business applications that are composed by a bounce of dialog windows. Every form is called with the TForm.ShowModal
method. So, the user has to navigate into your application one form at time. This is simpler to handle for the programmer, but it's less user-friendly for your customers. However, providing a switchable interface to your customer is not that difficult. In this recipe, we'll see a complete example on how to do it.
This recipe is a bit more complex than the previous recipes, so I'll not explain all the code but only the fundamental parts. You can find the complete code in the book's code repository (Chapter1\RECIPE05
).
Let's say we want to create a tabbed interface for our software that is used to manage product orders, sales, and invoices. All the forms must be usable at the same time without having to close the previous one. Before we begin, the following screenshot is what we want to create:

The main form containing seven embedded child forms
The project is composed by a bounce of forms. The main form has a TTabControl
component that allows switching between the active forms. All embedded forms inherit from EmbeddableForm
. The most important is the Show
method shown as follows:
procedure TEmbeddableForm.Show(AParent: TPanel); begin Parent := AParent; BorderStyle := bsNone; BorderIcons := []; Align := alClient; Show; end;
Note
Note that all the forms apart from the main form have been removed from the Auto-Create Form list (Project | Options | Forms).
All the other forms descend from the EmbeddableForm
method and are added to the TTabControl
component on the main form with a line of code similar to the following:
procedure TMainForm.MenuOrdersClick(Sender: TObject); begin AddForm(TForm1.Create(self)); end;
The MainForm AddForm
method is in charge of adding an actual instance of a form into the tabs, keeping a reference of it. The following code shows you how this is done:
//Add a form to the stack procedure TMainForm.AddForm( AEmbeddableForm: TEmbeddableForm); begin AEmbeddableForm.Show(Panel1); //each tab show the caption of the containing form and //hold the reference to it TabControl1.Tabs.AddObject( AEmbeddableForm.Caption, AEmbeddableForm); ResizeTabsWidth; ShowForm(AEmbeddableForm); end;
Other methods are in charge of bringing an already created form to the front when a user clicks on the related tab and then to close a form when the related tab is removed (check the ShowForm
and WMEmbeddedFormClose
methods).
There is a bit of code, but the concepts are simple:
When we need to create a new form, add it in the
TabControl1.Tabs
property. The caption of the form is the caption of the tab, and the object is the form itself. This is what theAddForm
method does with the following line:TabControl1.Tabs.AddObject( AEmbeddableForm.Caption, AEmbeddableForm);
When a user clicks on a tab, we have to find the associated form cycling through the
TabControl1.Tabs.Objects
list and bring it to the front.When a form asks for closing (sending a
WM_EMBEDDED_CLOSE
message), we have to set theParentWantClose
property and then call theClose
method of the correspondent form.If a user wants to close a form by closing the correspondent tab (in the recipe code, there is a
TPopMenu
component connected to theTabControl
component, which is used to close a form with a right-click), we have to call theClose
method on the correspondent form.Every form frees itself in the
OnClose
event handler. This is done once for all in theTEmbeddableForm.CloseForm
event handler using thecaFree
action.
Embedding a form into another TWinControl
is not difficult and allows you to create flexible GUIs without using TPageControl
and frames. For the end user, this multitabbed GUI is probably more familiar because all the modern browsers use it, and probably your user already knows how to use a browser with different pages or screens opened. From the developer point of view, the multitabbed interface allows for much better programming patterns and practices. This technique can also be used for other scenarios where you have to embed one screen into another.
More flexible (and complex) solutions can be created involving the use of Observers, but in simple cases, this recipe's solution based on Windows Messaging is enough.
More information about the Observer design pattern can be found at http://sourcemaking.com/design_patterns/observer/delphi.
Another interesting solution (that does not rely on Windows Messaging and so is also cross platform) may be based on the System.Messaging.TMessageManager
class. More info about TMessageManager
can be found at http://docwiki.embarcadero.com/Libraries/XE6/en/System.Messaging.TMessageManager.
The code in this recipe can be used with every component that uses TStringList
to show items (TListBox
, TComboBox
, and so on) and can be adapted easily for other scenarios.
In the recipe code, you'll also find a nice way to show status messages generated by the embedded forms and a centralized way to show application hints in the status bar.
JavaScript Object Notation (JSON) is a lightweight data-interchange format. As the reference site (http://www.json.org) says:
It is easy for humans to read and write. It is easy for machines to parse and generate.
It is based on a subset of the JavaScript programming language, but it is not limited to JavaScript in any way. Indeed, JSON is a text format that is completely language agnostic. These properties make JSON an ideal data-interchange language for many utilizations. In recent years, JSON has superseded XML in many applications, especially on data exchange and in general when the data size matters, because of its intrinsic conciseness and simplicity.
JSON provides the following five datatypes: string, number, object, array, Boolean, and null.
This simplicity is a plus when you have to read a JSON string into some kind of language-specific structures, because every modern language supports JSON datatypes as simple types, HashMap (in case of JSON object), or List (in case of JSON array). So, it makes sense that a data format that is interchangeable with programming languages is also based on these types and structures.
Since Version 2009, Delphi provides built-in support for JSON. The System.JSON.pas
unit contains all JSON types with a nice object-oriented interface. In this recipe, we'll see how to generate, modify, and parse a JSON string.
Create a new VCL application and drop three TButton and a TMemo. Align all the buttons as a toolbar at the top of the form and the memo to all the remaining form client area.
From left- to right-hand side, name the buttons as
btnGenerateJSON
,btnModifyJSON
, andbtnParseJSON
.We'll use static data as our data source. A simple matrix is enough for this recipe. Just after the start of the
implementation
section of the unit, write the following code:type TCarInfo = ( Manufacturer = 1, Name = 2, Currency = 3, Price = 4); var Cars: array [1 .. 4] of array [Manufacturer .. Price] of string = ( ('Ferrari','360 Modena','EUR', '250000'), ('Ford', 'Mustang', 'USD', '80000'), ('Lamborghini', 'Countach', 'EUR','300000'), ('Chevrolet', 'Corvette', 'USD', '100000') );
The
TMemo
component is used to show our JSON and our data. To keep things clear, create on the form a public property calledJSON
and map itssetter
andgetter
to theMemo1.Lines.Text
property. Use the following code for this://…other form methods declaration private procedure SetJSON(const Value: String); function GetJSON: String; public property JSON: String read GetJSON write SetJSON; end; //…then in the implementation section function TMainForm.GetJSON: String; begin Result := Memo1.Lines.Text; end; procedure TMainForm.SetJSON(const Value: String); begin Memo1.Lines.Text := Value; end;
Now, create event handlers for each button and write the following code. Pay attention to the event names. The code is as follows:
procedure TMainForm.btnGenerateJSONClick(Sender: TObject); var i: Integer; JSONCars: TJSONArray; Car, Price: TJSONObject; begin JSONCars := TJSONArray.Create; try for i := Low(Cars) to High(Cars) do begin Car := TJSONObject.Create; JSONCars.AddElement(Car); Car.AddPair('manufacturer', Cars[i][TCarInfo.Manufacturer]); Car.AddPair('name', Cars[i][TCarInfo.Name]); Price := TJSONObject.Create; Car.AddPair('price', Price); Price.AddPair('value', TJSONNumber.Create( Cars[i][TCarInfo.Price].ToInteger)); Price.AddPair('currency', Cars[i][TCarInfo.Currency]); end; JSON := JSONCars.ToString; finally JSONCars.Free; end; end; procedure TMainForm.btnModifyJSONClick(Sender: TObject); var JSONCars: TJSONArray; Car, Price: TJSONObject; begin JSONCars := TJSONObject.ParseJSONValue(JSON) as TJSONArray; try Car := TJSONObject.Create; JSONCars.AddElement(Car); Car.AddPair('manufacturer', 'Hennessey'); Car.AddPair('name', 'Venom GT'); Price := TJSONObject.Create; Car.AddPair('price', Price); Price.AddPair('value', TJSONNumber.Create(600000)); Price.AddPair('currency', 'USD'); JSON := JSONCars.ToString; finally JSONCars.Free; end; end; procedure TMainForm.btnParseJSONClick(Sender: TObject); var JSONCars: TJSONArray; i: Integer; Car, JSONPrice: TJSONObject; CarPrice: Double; s, CarName, CarManufacturer, CarCurrencyType: string; begin s := ''; JSONCars := TJSONObject.ParseJSONValue(JSON) as TJSONArray; if not Assigned(JSONCars) then raise Exception.Create('Not a valid JSON'); try for i := 0 to JSONCars.Size - 1 do begin Car := JSONCars.Get(i) as TJSONObject; CarName := Car.Get('name').JsonValue.Value; CarManufacturer := Car.Get('manufacturer') .JsonValue.Value; JSONPrice := Car.Get('price') .JsonValue as TJSONObject; CarPrice := (JSONPrice.Get('value').JsonValue as TJSONNumber).AsDouble; CarCurrencyType := JSONPrice.Get('currency') .JsonValue.Value; s := s + Format( 'Name = %s' + sLineBreak + 'Manufacturer = %s' + sLineBreak + 'Price = %.0n%s' + sLineBreak + '-----' + sLineBreak, [CarName, CarManufacturer, CarPrice, CarCurrencyType]); end; JSON := s; finally JSONCars.Free; end; end;
Run the application by hitting F9 (or navigate to Run | Run).
Click on the btnGenerateJSON button, and you should see a JSON array and some JSON objects inside in the memo.
Click on the btnModifyJSON button and you should see one more JSON object inside the outer JSON array in the memo.
Click on the last button and you should see the same data as before, but in a normal text representation.
After the third click, you should see something like the following screenshot:
Text representation of the JSON data generated and modified
Although not the fastest or the most standard compliant on the market (at the time of writing), it is important to know the JSON Delphi parser because other Delphi technologies such as DataSnap use it. Luckily, there are a lot of alternative JSON parsers for Delphi if you find you have trouble with the standard ones.
Other notable JSON parsers are as follows:
Superobject (https://code.google.com/p/superobject/)
The JSON Delphi library (http://sourceforge.net/projects/lkjson/)
The one included in the Delphi Web Script library (https://code.google.com/p/dwscript/)
If your main concern is speed, then check the Delphi Web Script or the superobject parsers.
There are also a lot of serialization libraries that use JSON as a serialization format. In general, every parser has its own way to serialize an object to JSON. Find your favorite. For example, in the Serializing objects to JSON and back using RTTI recipe in Chapter 5, Putting Delphi on the Server, you will see an open source library containing a set of serialization helpers using the default Delphi JSON parser.
However, JSON is not the right tool for every interchange or data representation job. XML has been creating other technologies that can help if you need to search, transform, and validate your data in a declarative way. In JSON land, there is no such level of standardization apart from the format itself. However, over the years, there is an effort to include at least the XML Schema counterpart in JSON, and you can find more details at http://json-schema.org/.
XML stands for eXtensible Markup Language (http://en.wikipedia.org/wiki/XML) and is designed to represent, transport, and store hierarchical data in trees of nodes. You can use XML to communicate with different systems to store configuration files, complex entities, and so on. All of these use a standard and powerful format. Delphi has had good support for XML for more than a decade now.
All the basic XML-related activities can be summarized with the following points:
Generating XML data
Parsing XML data
Parsing XML data and modifying it
In this recipe, we will see how to do all these activities.
Create a new VCL application and drop three TButton and a TMemo. Align all the buttons as a toolbar at the top of the form and the memo to the remaining form client area.
From left- to right-hand side, name the buttons as
btnGenerateXML
,btnModifyXML
, andbtnParseXML
.The real work on the XML will be done by the
TXMLDocument
component. So, drop one instance of the form and set itsDOMVendor
property toADOM XML v4
.We'll use static data as our data source. A simple matrix is enough for this recipe. Just after the
implementation
section of the unit, write the following code:type TCarInfo = ( Manufacturer = 1, Name = 2, Currency = 3, Price = 4); var Cars: array [1 .. 4] of array [Manufacturer .. Price] of string = ( ( 'Ferrari','360 Modena','EUR', '250,000' ), ( 'Ford', 'Mustang', 'USD', '80,000' ), ( 'Lamborghini', 'Countach', 'EUR','300,000' ), ( 'Chevrolet', 'Corvette', 'USD', '100,000' ) );
We will use a
TMemo
component to display the XML and the data. To keep things clear, create on the form a public property calledXML
and map itssetter
andgetter
methods to theMemo1.Lines.Text
property. Use the following code://…other form methods declaration private procedure SetXML(const Value: String); function GetXML: String; public property Xml: String read GetXML write SetXML; end; //…then in the implementation section function TMainForm.GetXML: String; begin Result := Memo1.Lines.Text; end; procedure TMainForm.SetXML(const Value: String); begin Memo1.Lines.Text := Value; end;
Now, create event handlers for each button. For the btnGenerateXML button, write the following code:
procedure TMainForm.btnGenerateXMLClick(Sender: TObject); var RootNode, Car, CarPrice: IXMLNode; i: Integer; s: String; begin XMLDocument1.Active := True; try XMLDocument1.Version := '1.0'; RootNode := XMLDocument1.AddChild('cars'); for i := Low(Cars) to High(Cars) do begin Car := XMLDocument1.CreateNode('car'); Car.AddChild('manufacturer').Text := Cars[i][TCarInfo.Manufacturer]; Car.AddChild('name').Text := Cars[i][TCarInfo.Name]; CarPrice := Car.AddChild('price'); CarPrice.Attributes['currency'] := Cars[i][TCarInfo.Currency]; CarPrice.Text := Cars[i][TCarInfo.Price]; RootNode.ChildNodes.Add(Car); end; XMLDocument1.SaveToXML(s); Xml := s; finally XMLDocument1.Active := False; end; end;
Now we've to write the code to change the XML. In the
btnModifyXML
click event handler, write the following code:procedure TMainForm.btnModifyXMLClick(Sender: TObject); var Car, CarPrice: IXMLNode; s: string; begin XMLDocument1.LoadFromXML(Xml); try Xml := ''; Car := XMLDocument1.CreateNode('car'); Car.AddChild('manufacturer').Text := 'Hennessey'; Car.AddChild('name').Text := 'Venom GT'; CarPrice := Car.AddChild('price'); CarPrice.Attributes['currency'] := 'USD'; CarPrice.Text := '600,000'; XMLDocument1.DocumentElement.ChildNodes.Add(Car); XMLDocument1.SaveToXML(s); Xml := s; finally XMLDocument1.Active := False; end; end;
Write the following code under the
btnParseXML
click event handler:procedure TMainForm.btnParseXMLClick(Sender: TObject); var CarsList: IDOMNodeList; CurrNode: IDOMNode; childidx, i: Integer; CarName, CarManufacturer, CarPrice, CarCurrencyType: string; begin XMLDocument1.LoadFromXML(Xml); try Xml := ''; CarsList := XMLDocument1. DOMDocument.getElementsByTagName('car'); for i := 0 to CarsList.length - 1 do begin CarName := ''; CarManufacturer := ''; CarPrice := ''; CarCurrencyType := ''; for childidx := 0 to CarsList[i].ChildNodes.length - 1 do begin CurrNode := CarsList[i].ChildNodes[childidx]; if CurrNode.nodeName.Equals('name') then CarName := CurrNode.firstChild.nodeValue; if CurrNode.nodeName.Equals('manufacturer') then CarManufacturer := CurrNode.firstChild.nodeValue; if CurrNode.nodeName.Equals('price') then begin CarPrice := CurrNode.firstChild.nodeValue; CarCurrencyType := CurrNode.Attributes. getNamedItem('currency').nodeValue; end; end; Xml := Xml + 'Name = ' + CarName + sLineBreak + 'Manufacturer = ' + CarManufacturer + sLineBreak + 'Price = ' + CarPrice + CarCurrencyType + sLineBreak + '-----' + sLineBreak; end; finally XMLDocument1.Active := False; end; end;
Run the application by hitting F9 (or navigate to Run | Run).
Click on the btnGenerateXML button and you should see some XML data in the memo.
Click on the btnModifyXML button and you should see some more XML in the memo.
Click on the last button and you should see the same data as before, but in normal text representation.
After the third click, you should see something like the following screenshot:
Text representation of the XML data generated and modified
The first button generates the XML representation of the data in our matrix. We've used some car information as sample data.
To create an XML, there are three fundamental
TXMLDocument
methods:XMLNode := XMLDocument1.CreateNode('node');
XMLNode.AddChild('childnode');
XMLNode.Attributes['attrname'] := 'attrvalue';
There are other very useful methods but these are the basics of XML generation.
The btnModifyXML button loaded the XML into the memo and appended some other data (another car) to the list. Then, it updated the memo with the new updated XML. The following are the most important lines to note:
//Create a node without adding it to the DOM Car := XMLDocument1.CreateNode('car'); //fill Car XMLNode… and finally add it to the DOM //as child of the root node XMLDocument1.DocumentElement.ChildNodes.Add(Car);
The code under the
btnParseXMLClick
event handler allows you to read the data in the XML tree as simple text.
There are many things to say about XML ecospace. There are XML engines that provide facilities to search data in an XML tree (XPath), validate an XML using another XML (XML Schema or DTD), transform an XML into another kind of format using another XML (XSLT), and for many others uses (http://en.wikipedia.org/wiki/List_of_XML_markup_languages). The good thing is that, just like XML itself, the DOM
object is also standardized, so every library that is compliant to the standard has the same methods, from Delphi to JavaScript and from Python to C#.
TXMLDocument
allows you to select the DOMVendor
implementation. By default, there are three implementations available:
MSXML: This is from Microsoft and implemented as a
COM
object. This supports XML transformations and is available only on Windows (so no Android, iOS, or Mac OS X).ADOM XML: This is an open source Delphi implementation and does not support transformations. This is available on all the supported Delphi platforms, so if you plan to write XML handling code on a mobile or Mac, this is the way to go.
XSLT: This allows you to transform an XML into something else, using another XML as a stylesheet. The following function loads an XML and an XSLT from two string variables. Then, use the XSLT document to transform the XML document. The following code shows the details:
function Transform(XMLData: string; XSLT: string): WideString; var XML: IXMLDocument; XSL: IXMLDocument; begin XML := LoadXMLData(XMLData); XSL := LoadXMLData(XSLT); XML.DocumentElement.TransformNode(XSL.DocumentElement, Result) end;
This function doesn't know about the output format because it is defined by the XSLT document. The result could be an XML, an HTML, a CSV, or a plain text, or whatever the XSLT defines, the code doesn't change.
XSLT can be really useful—go to http://www.w3schools.com/xsl/xsl_languages.asp for further details about the language.
Many of the I/O related activities handle streams of data. A stream is a sequence of data elements made available over time. According to Wikipedia:
A stream can be thought of as a conveyor belt that allows items to be processed one at a time rather than in large batches.
At the lowest level, all the streams are bytes, but using a high-level interface could obviously help the programmer to handle his data. This is the reason why a stream object usually has methods such as read
, seek
, write
, and many more, just to make the handling of byte stream a bit simpler.
In this recipe, we'll see some streams utilization examples.
In the good old Pascal days, there was a set of functions to handle the I/O (AssignFile
, Reset
, Rewrite
, CloseFile
, and so on), now we've a bounce of classes. All Delphi streams inherit from TStream
and can be used as an internal stream of one of the adapter classes (as adapter, I mean an implementation of the Adapter or Wrapper design pattern from the famous Gang of Four, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Professional, book about design patterns).
There are 10 fundamental types of streams:
You can check the complete list and their intended use directly on the Embarcadero website at http://docwiki.embarcadero.com/RADStudio/XE6/en/Streams,_Reader_and_Writers.
As Joel Spolsky (http://www.joelonsoftware.com/articles/Unicode.html) says, "You can no longer pretend that plaintext is ASCII", so while we write streams, we've to pay attention to which encoding our text has and which encoding our counterpart is waiting for. One of the most frequent necessities is to efficiently read and write a text file using the correct encoding.
"The Single Most Important Fact About Encodings" It does not make sense to have a string without knowing what encoding it uses. You can no longer stick your head in the sand and pretend that "plain" text is ASCII. | ||
--Joel Spolsky |
The point Joel is making is that the content of a string doesn't know about the type of character encoding it uses.
When you think about file handling, ask yourself: could this file become 10 MB? And 100 MB? 1 GB? How will my program behave in that case? Handling a file one line at time and not loading all the files contents in memory is usually a good insurance for these cases. A stream of data is a good way to do this kind of thing. In this recipe, we'll see the practical utilization of streams, stream writers, and streams readers.
The project is not complex, all the interesting stuff happens in the btnWriteFile
and btnReadFile
files.
To write the file, we use TStreamWriter
. The TStreamWriter
class (as its counterpart TStreamReader
) is a wrapper for a TStream
descendent and adds some useful high-level methods to write to the stream. There are a lot of overloaded methods (Write
/WriteLine
) to allow an easy writing to the underlying stream. However, you can access the underlying stream using the BaseStream
property of the wrapper. Just after writing the file, the memo reloads the file using the same encoding used to write it and shows it. This is only a fast check for this recipe, you don't need the TMemo
component at all in your real project. The btnReadFile
file simply opens the file using a stream and passes the stream to a TStreamReader
that, using the right encoding, reads the file one line at time.
Now, let's do some checks. Run the program and with the encoding set to ASCII, click on btnWriteFile. The memo will show garbage text, as shown in the following screenshot. This is because we are using the wrong encoding for the data we are writing in the file.

Garbage text written to the file using the wrong encoding. No one line of text is equal to the original one. It is necessary to know the encoding for the text before writing and reading it.
Now select UTF8 from the RadioGroup and retry. Clicking on btnWriteFile, you will see the correct text in the memo. Try to change the Current Encoding setting using ASCII and click on btnReadFile. You will still get garbage text. Why? Because the file has been read with the wrong encoding. You have to know the encoding before safely reading the file contents. To read the text that we wrote, we have to use the very same encoding. Play with the other encodings to see different behaviors.
Streams are very powerful and their uniform interface helps us to write portable and generic code. With the help of streams and polymorphism, we can write code that uses a TStream
component to do some work without knowing which kind of stream it is!
Also, a lesser known possibility, if you ever write a program that needs to access to the good-old STD
_INPUT
, STD_OUTPUT
, or STD_ERROR
, you can use THandleStream
to wrap these system handles to a nice TStream
interface with the following code:
program StdInputOutputError; //the following directive instructs the compiler to create a //console application and not a GUI one, which is the default. {$APPTYPE CONSOLE} uses System.Classes, // required for Stream classes Winapi.Windows; // required to have access to the STD_* handles var StdInput: TStreamReader; StdOutput, StrError: TStreamWriter; begin StdInput := TStreamReader.Create( THandleStream.Create(STD_INPUT_HANDLE)); StdInput.OwnStream; StdOutput := TStreamWriter.Create( THandleStream.Create(STD_OUTPUT_HANDLE)); StdOutput.OwnStream; StdError := TStreamWriter.Create( THandleStream.Create(STD_ERROR_HANDLE)); StdError.OwnStream; { HERE WE CAN USE OURS STREAMS } // Let's copy a line of text from STD_IN to STD_OUT StdOutput.writeln(StdInput.ReadLine); { END - HERE WE CAN USE OURS STREAMS } StdError.Free; StdOutput.Free; StdInput.Free; end.
Moreover, when you work with file-related streams, the TFile
class (contained in System.IOUtils.pas
) is very useful, and it has some helper methods to write shorter and more readable code.
Some applications are designed to be always in the Windows tray bar. For almost all their running time, the user knows where that particular application is in the tray. Think about antivirus, custom audio processors, and video management tools provided by hardware vendors and many other things. Instead, some other applications need to go in the tray only when a long operation is running and the user should otherwise attend in front of a boring please wait animation. In these cases, users will be very happy if our application is not blocked and lets them do some other things. Then, a not intrusive notification will bring up an alert if some thing interesting happens. Think about heavy queries, statistics, heavy report generation, file upload or download, or huge data import or export. Think for a second: what if Google Chrome showed one modal dialog with a message Please wait, while this 2 GB file is downloading… stopping you to navigate to other pages? Crazy! Many applications could potentially behave like this.
In such cases, the users knows that they have to wait, but the application should be so "polite" as to let them do other things. Usually, programmers think that their software is the only reason the user bought a computer. Very often, this is not the case. So, let's find a way to do the right thing at the right moment.
This recipe is about creating a good Windows citizen application. Let's say our application allows us to execute a huge search in a database. When the user starts this long operation, the application UI remains usable. During the request execution, the user can decide to wait in front of the form or minimize it to the taskbar. If the user minimizes the application window, it also goes on the tray bar and when the operation finishes and it will alert the user with a nonintrusive message.
Create a new VCL application and drop on it a TButton, a TLabel, a TTrayIcon, a TApplicationEvents, a TImagelist, a TDataSource, and a TDBGrid component. Connect the TDBGrid to the TDataSource. Leave the default component names (I'll refer to the components using their default names). Use the disposition and the captions to make the form similar to the following screenshot:
The form and its components as they should look
In the
implementation
section of the unit, add the following units:AnonThread
: Add this unit to the project (this is located underC:\Users\Public\Documents\Embarcadero\Studio\14.0\Samples\Object Pascal\RTL\CrossPlatform Utils
on my machine). You can avoid adding this unit in the project and add the path to the IDE library path by navigating to Tools | Options and then clicking on Delphi Options | Library.RandomUtilsU
: Add this unit to the project (this is located under theCommons
folder of the recipes).FireDAC.Comp.Client
: Add this unit in theimplementation uses
section of the form.
We'll start with the code that will actually do the heavy work. In the
Button1.OnClick
method, put this code:procedure TMainForm.Button1Click(Sender: TObject); var I: Integer; ds: TDataSet; begin Button1.Enabled := False; if Assigned(DataSource1.DataSet) then begin ds := DataSource1.DataSet; DataSource1.DataSet := nil; RemoveComponent(ds); FreeAndNil(ds); end; Label1.Caption := 'Data retrieving... may take a while'; TAnonymousThread<TFDMemTable>.Create( function: TFDMemTable var MemTable: TFDMemTable; I: Integer; begin Result := nil; MemTable := TFDMemTable.Create(nil); try MemTable.FieldDefs.Add('EmpNo', ftInteger); MemTable.FieldDefs.Add('FirstName', ftString, 30); MemTable.FieldDefs.Add('LastName', ftString, 30); MemTable.FieldDefs.Add('DOB', ftDate); MemTable.CreateDataSet; for I := 1 to 400 do begin MemTable.AppendRecord([ 1000 + Random(9000), GetRndFirstName, GetRndLastName, EncodeDate(1970, 1, 1) + Random(10000) ]); end; MemTable.First; //just mimic a slow operation TThread.Sleep(2*60*1000); Result := MemTable; except FreeAndNil(MemTable); raise; end; end, procedure(MemTable: TFDMemTable) begin InsertComponent(MemTable); DataSource1.DataSet := MemTable; Button1.Enabled := True; Label1.Caption := Format('Retrieved %d employee', [MemTable.RecordCount]); ShowSuccessBalloon(Label1.Caption); end, procedure(Ex: Exception) begin Button1.Enabled := True; Label1.Caption := Format('%s (%s)', [Ex.Message, Ex.ClassName]); ShowErrorBalloon(Label1.Caption); end); end;
Now, create the following event handler for the
Tray1
.OnBalloonClick
method and connect it to theTra1.OnDoubleClick
event handler:procedure TMainForm.TrayIcon1BalloonClick(Sender: TObject); begin TrayIcon1.Visible := False; WindowState := wsNormal; SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOMOVE); SetWindowPos(Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOMOVE); end;
In the next step, the two raw
SetWindowPos
calls will be less obscure, believe me.Now, to keep things clear, we need the following two procedures. Create them as private methods of the form:
procedure TMainForm.ShowErrorBalloon(const Mess: String); begin if TrayIcon1.Visible then begin TrayIcon1.IconIndex := 2; TrayIcon1.BalloonFlags := bfError; TrayIcon1.BalloonTitle := 'Errors occurred'; TrayIcon1.BalloonHint := Label1.Caption; TrayIcon1.ShowBalloonHint; end; end; procedure TMainForm.ShowSuccessBalloon(const Mess: String); begin if TrayIcon1.Visible then begin TrayIcon1.IconIndex := 0; TrayIcon1.BalloonFlags := bfInfo; TrayIcon1.BalloonTitle := 'Request terminated'; TrayIcon1.BalloonHint := Label1.Caption; TrayIcon1.ShowBalloonHint; end; end;
Create one last event handler for the
ApplicationEvents1.OnMinimize
method:procedure TMainForm.ApplicationEvents1Minimize( Sender: TObject); begin TrayIcon1.Visible := True; TrayIcon1.BalloonTitle := 'Employee Manager'; TrayIcon1.BalloonHint := 'Employee Manager is still running in the tray.' + sLineBreak + 'Reactivate it with a double click on the tray icon'; TrayIcon1.BalloonFlags := bfInfo; TrayIcon1.ShowBalloonHint; TrayIcon1.IconIndex := 0; end;
Run the application by hitting F9 (or navigate to Run | Run).
Click on the Get Employee button and then minimize the application (note that as the GUI is responsive, you can resize, minimize, and maximize the form).
An icon is shown in the tray and shows a message about what the application is doing.
As soon as the data has been retrieved, a Request terminated message will pop up. Click on the balloon. The application will come to the front and you will see the data in the TDBGrid.
Try to repeat the procedure without minimizing the window. All is working as usual (this time without the tray messages) and the GUI is responsive.
This recipe is a bit articulated. Let's start from the beginning.
The actual code that executes the request uses a nice helper class provided by Embarcadero in the Samples
folder of RADStudio (not officially supported, it is just an official sample). The TAnonymousThread<T>
constructor is a class that simplifies the process of starting a thread and when the thread ends, this class updates the UI with data retrieved by the thread.
The TAnonymousThread<T>
constructor (there are other overloads, but this the most used) expects three anonymous methods:
function: T
: This function is executed in the background thread context created internally (so you should avoid accessing the UI). ItsResult
value will be used after the thread execution.procedure (Value: T)
: This procedure is called after the thread is executed. Itsinput
parameter is the result value of the first function. This procedure is executed in the context of the main thread, so it can update the UI safely. It is not called in the case of an exception raised by the first function.procedure (E: Exception)
: This procedure is called in the case of an exception during the thread execution and is executed in the context of the main thread, so it can update the UI safely. It is not called if there isn't an exception during thread execution.
The background thread (the first function passed to the TAnonymousThread<T>
constructor) creates a memory table using the TFDMemTable
component (we will talk about this component in the FireDAC-related section) and then that object is passed to the second anonymous method that adds it to the form's components using the InsertComponent()
method and binds it to the DBGrid causing the data visualization.
When the data is ready in the grid, a call to the ShowSuccessBalloon()
function shows a balloon message in the tray area, informing users that their data is finally available. If the user clicks on the balloon (or double-clicks on the tray icon), the application is restored. The balloon message is shown in the following screenshot:

The balloon message when the data are ready in DBGrid
If the user clicks on the balloon, the form is restored. However, since Windows XP (with some variation in subsequent versions), the system restricts which processes can set the foreground window. An application cannot force a window to the foreground while the user is working with another window. The calls to SetWindowPos
are needed to bring the form to the front.
In the included code, there is also another version of the recipe (20_VCLAppFlashNotification
) that uses the most recent flash on the taskbar to alert the user. Consider this approach when you want to implement an application that, when minimized, has to alert the user in some way. The tray area may become rapidly crowded with icons. So consider to flash your icons in the taskbar instead.
The other code is required to correctly handle the memory ownership of the TFDMemTable
instance.
The use of a tray icon is a well-known pattern in Windows development. However, the concept of I'll go into the background for a while, if you want, and I'll show you the notification as soon something happens is used very often on Android, iOS, and Mac OS X. In fact, some part of this recipe code is reusable also on Mac OS X, iOS, and Android. Obviously, using the right system to alert the user when the background thread finishes (for example, on a mobile platform) execution should use the notification bar. The thread handling of this recipe works on every platform supported by Delphi.
Some kind of application needs to be running H24. Usually, these are network servers or data transfer / monitoring applications. In these cases, you probably start with a normal GUI or console application; however, when the systems are to be used in production, you face a lot of problems related to the Windows session termination, reboots, user rights, and other issues related to the server environment.
The way to go, in the previous scenario, is to develop a Windows service. In this recipe, we'll see how to write a good Windows service scaffold and this can be the skeleton for many other services, so feel free to use this code as a template to create all services that you will need.
The project has been created starting from the default project template accessible from File | New | Other | Delphi Projects | Service Application and then has been integrated with a set of functionalities to make it real.
All the low-level interfacing with Windows Service Manager is done by the TService
class. In the ServiceU.pas
component, there is the actual descendant of TService
that represents the Windows service we are implementing. Its event handlers are used to communicate with the operating system.
Usually, a service needs to respond to the Windows ServiceController
commands independently of what it is doing, so we need a background thread to do the actual work, while the TService.OnExecute
event should not do any real work (this is not a must, but usually is the way to go). The unit named WorkerThreadU.pas
contains the thread and the main service needed to hold a reference to the instance of this thread.
The background thread starts when the service is started (the OnStart
event) and stops when the service is stopped (the OnStop
event). The OnExecute
event waits and handles the ServiceController
commands but doesn't do any actual functional work. This is done using the ServiceThread.ProcessRequests(false);
event in a while
loop.
Usually, the OnExecute
event handler looks like the following:
procedure TSampleService.ServiceExecute(Sender: TService); begin while not Terminated do begin ServiceThread.ProcessRequests(false); TThread.Sleep(1000); end; end;
The waiting time of 1000
milliseconds is not a must, but consider that the wait time should be not too high because the service needs to be responsive to the Windows service controller messages, and not too low because, otherwise, the thread context switch may waste resources.
The background thread writes a line in a logfile once a second. While it is in a Paused
state, the service stops writing. When the service continues, the thread will restart writing the log line. In the service event handlers, there is a logic to implement this change of state:
procedure TSampleService.ServiceContinue(Sender: TService; var Continued: Boolean); begin FWorkerThread.Continue; Continued := True; end; procedure TSampleService.ServicePause(Sender: TService; var Paused: Boolean); begin FWorkerThread.Pause; Paused := True; end;
In the thread, there is the actual logic to implement the Paused
state and in this case, it is fairly simple; we've to pause the writing of the logfile.
Here's an extract:
Log := TStreamWriter.Create( TFileStream.Create(LogFileName, fmCreate or fmShareDenyWrite)); try while not Terminated do begin if not FPaused then begin Log.WriteLine('Message from thread: ' + TimeToStr(now)); end; TThread.Sleep(1000); end; finally Log.Free; end;
The boolean
instance variable FPaused
can be considered as a thread safe for this use.
Delphi services don't have a default description under Windows Service Manager. If we want to give a description, we have to write a specific key in the Windows registry. Usually, this is done in the AfterInstall
event. In our service, write the following code in the AfterInstall
event handler:
procedure TSampleService.ServiceAfterInstall( Sender: TService); var Reg: TRegistry; //declared in System.Win.Registry; begin Reg := TRegistry.Create(KEY_READ or KEY_WRITE); try Reg.RootKey := HKEY_LOCAL_MACHINE; if Reg.OpenKey( '\SYSTEM\CurrentControlSet\Services\' + name, False {do not create if not exists}) then begin Reg.WriteString('Description', 'My Fantastic Windows Service'); Reg.CloseKey; end; finally Reg.Free; end; end;
It is not necessary to delete this key in the AfterUnInstall
event because Windows deletes all the keys related to the service (under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<MyServiceName>
) when it is actually uninstalled.
Let's try an installation. Build the project, open the Windows command prompt, and go to the folder where the project has been built and run this command:
C:\<ExeProjectPath>\WindowsService.exe /install
If all is okay, you should see this message:

The service installation is okay
Now, you can check in the Windows Services Console. You should find the service installed. Click on Start, wait for the confirmation, and the service should start to write its logfile.
Windows services are very powerful. Using the abstractions that Delphi provides, you can also create an application that can act as a normal GUI application or as a Windows service after reading a parameter on the command line.
In the recipe folder, there is another recipe called 20_WindowsServiceOrGUI
.
This application can be used as a normal Windows service using the normal command-line switches used so far, but if launched with /GUI
, it acts as a GUI application and can use the same application code (not TService
). In our example, the GUI version uses the same worker thread as the service version. This can be very useful for debugging purposes.
Run the application with the following command:
C:\<ExeProjectPath>\WindowsServiceOrGUI.exe /GUI
You will get a GUI version of the service, as shown in the following screenshot:

The GUI version of the Windows service
If something happens during the execution of your service which you want to log and you want to log into the system logger, you can use the LogMessage
method to save a message, which can be viewed later using Windows built-in event viewer.
You can call the LogMessage
method using appropriate logging type:
LogMessage('Your message goes here for SUCCESS', EVENTLOG_SUCCESS, 0, 1);
If you check the event in Event Viewer, you will find a lot of garbage text that complains about the lack of description for the event.
If you really want to use Event Viewer to view your log messages (when I can, I use a text logfile and don't care about Event Viewer, but there are scenarios where Event Viewer log is needed), you have to use the Microsoft Message Compiler.
The Microsoft Message Compiler is a tool able to compile a file of messages in a set of RC files. Then those files must be compiled by a resource compiler and linked to your executable.
More information about message compiler and steps needed to provide the needed description for the log event can be found at the following link:
http://www.codeproject.com/Articles/4166/Using-MC-exe-message-resources-and-the-NT-event-lo
In some cases, your fantastic application needs to be opened with just a double-click on a file with an extension associated with it. This is the case with MS Word, MS Excel, and many other well-known pieces of software. If you have a file generated with a program, double-click on the file and the program that generated the file will bring up pointing to that file. So, if you click on a mywordfile.docx
file, MS Word will be opened and the mywordfile.docx
file's content will be shown. This is what we'd like to do in this recipe. The association can be useful also when you have multiple configurations for a program. Double-click on the ConfigurationXYZ.myext
file and the program will start using that configuration.
The hard work is done by the operating system itself. We have to instruct Windows to provide the following information:
The file extension to associate
The description of file type (this will be shown by Windows Explorer describing the file type)
The default icon for the file type (in this recipe, we'll use the application icon itself, but it is not mandatory)
The application that we want to associate
Let's start!
Create a new VCL application and drop two TButton components and a TMemo component. Align all the buttons as a toolbar at the top of the form and the memo to all the remaining form client area.
The button on the left-hand side will be used to register a file type while the button on the right-hand side will be used to unregister the association (cleaning the registry).
We have to handle some MS Windows-specific features, so we need some Windows-related units. Under the
implementation
section of the unit, write this use clause:uses System.Win.registry, Winapi.shlobj, System.IOUtils;
In the
implementation
section, we need two procedures to do the real work; so just after theuses
clause, put this code:procedure UnregisterFileType( FileExt: String; OnlyForCurrentUser: boolean = true); var R: TRegistry; begin R := TRegistry.Create; try if OnlyForCurrentUser then R.RootKey := HKEY_CURRENT_USER else R.RootKey := HKEY_LOCAL_MACHINE; R.DeleteKey('\Software\Classes\.' + FileExt); R.DeleteKey('\Software\Classes\' + FileExt + 'File'); finally R.Free; end; SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0); end; procedure RegisterFileType( FileExt: String; FileTypeDescription: String; ICONResourceFileFullPath: String; ApplicationFullPath: String; OnlyForCurrentUser: boolean = true); var R: TRegistry; begin R := TRegistry.Create; try if OnlyForCurrentUser then R.RootKey := HKEY_CURRENT_USER else R.RootKey := HKEY_LOCAL_MACHINE; if R.OpenKey('\Software\Classes\.' + FileExt, true) then begin R.WriteString('', FileExt + 'File'); if R.OpenKey('\Software\Classes\' + FileExt + 'File', true) then begin R.WriteString('', FileTypeDescription); if R.OpenKey('\Software\Classes\' + FileExt + 'File\DefaultIcon', true) then begin R.WriteString('', ICONResourceFileFullPath); if R.OpenKey('\Software\Classes\' + FileExt + 'File\shell\open\command', true) then R.WriteString('', ApplicationFullPath + ' "%1"'); end; end; end; finally R.Free; end; SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0); end;
These two procedures allows us to register (and unregister) a file type considering only the current user or all the machine users. Note that if you want to register the association for every user, write your data to the following location:
HKEY_LOCAL_MACHINE\Software\Classes
If you want to register the association for the current user only, write your data to the following location:
HKEY_CURRENT_USER\Software\Classes
On the newest Windows versions, you need admin rights to register a file type for all the machine users. The last line of the procedures tells Explorer (the MS Windows graphic interface) to refresh its setting to reflect the changes made to the file associations. As a result, for instance, the Explorer file list views will be updated.
We've almost finished. Change the left-hand side button name to
btnRegister
, the right-hand side button name tobtnUnRegister
, and put the following code on theironclick
event handlers:procedure TMainForm.btnRegisterClick(Sender: TObject); begin RegisterFileType( 'secret', 'This file is a secret', Application.ExeName, Application.ExeName, true); ShowMessage('File type registered'); end; procedure TMainForm.btnUnRegisterClick(Sender: TObject); begin UnregisterFileType('secret', true); ShowMessage('File type unregistered'); end;
Now, when our application is invoked with a double-click, we'll get the file name as a parameter. It is possible to read a parameter passed by Windows Explorer (or command line) using the
ParamStr(1)
function. Create aFormCreate
event handler using the following code:procedure TMainForm.FormCreate(Sender: TObject); begin if TFile.Exists(ParamStr(1)) then Memo1.Lines.LoadFromFile(ParamStr(1)) else begin Memo1.Lines.Text := 'No valid secret file type'; end; end;
Now the application should be complete. However, a nice integration with the operating system requires a nice icon as well. In the code, the associated file will get the same icon as the main program, so let's change our default icon by navigating to Project | Options | Application and choose a nice icon. Click on the Load Icon button, choose an ICO file, and then select the third item from the resultant dialog:
Changing the default application icon for our application
Now, create some text files with our registered
.secret
extension.These files will appear with the default Windows icon, but in some seconds, they will have a brand new icon.
Run the application by hitting F9 (or navigate to Run | Run).
Click on the btnRegister button and close the application. Now the files get a new icon, as shown in the following screenshot:
The files in Windows Explorer before and after having registered the .secret extension
Now, with the application not running, double-click on the
.secret
file. Our program will be started by Windows itself, using the information stored in the registry about the.secret
file, and we'll get this form (the text shown in the memo is the text contained into the file):Our application, launched by the operating system, while showing the content of the file
One application can register many file types. In some cases, I've used this technique to register some specific desktop database files to my application (FirebirdSQL Embedded database files or SQLite database files). So a double-click on such database file (registered with an application-specific extension) was actually a connection to that database.