5-Minute-Snack: Connecting with TDataSource to datasets in FireMonkey using FNC

I grew up with the VCL in Delphi. After building user interfaces (or dialogs) with Turbo Pascal for Windows it was a blessing. I intentionally left out the console app interfaces that we had to build before Windows made additional memory a requirement.

When I taught classes at university in 2007 the young students were not able to appreciate the work that a framework like the VCL or .NET Winforms takes off our shoulders. They were already fed up with having to install the IDE and the database server seperately. Times change – and it makes me feel like a fossil, but that is a different story for a different setting…

On the other hand, old habits die hard. Whenever I drop a TDataSet  component on a form or data module, the TDataSource component follows without much afterthought. Connecting any GUI element using that TDataSource  with a few mouse-clicks is known as one of the key pillars why Delphi made RAD so successful.

With FireMonkey the Delphi world had to leave TDataSource  behind. As .NET made Data Binding popular, new frameworks tend to use it instead of the “old-fashioned” TDataSource  approach. Reasons are manifold as Data Binding tends to close the gap between source code and the user-interface even more. Furthermore, there is no need for designated “database user controls”. You can bind any property to any other property. The dataflow of these binds is very flexible. 

Still, the approach is a breaking change that makes migration of VCL Forms Applications to FireMonkey more difficult. Heck, would it be nice to use the good-old TDataSource  approach in FireMonkey. Thankfully, I can once again rely on FNC from TMS Software to come to the rescue.

As introduced in quite a few posts before, FNC offers the means to develop in any framework with the same set of components and deploy the app to any of the supported platforms by Delphi.

So far, it allows using the TDataSource  interface with its grid ( TTMSFNCGrid ) and planner ( TTMSFNCPlanner ) components. The following screenshot shows the setup with TFDMemTable :

DataSource1 is connected to FDMemTable1. The link between the grid and the datasource is handled by the TTMSFNCGridDatabaseAdapter :

You specify the datasource and the grid. If you enable the control using its Active  propery, you can even see the result during runtime. Something that we learned to appreciate. You can also setup the columns during design time in the component. 

You see, there is no difference to the approach you have been used to for years. Furthermore, you can use the exact same setup in the VCL app. FNC controls can be used in VCL and FireMonkey. That is something you might have to get used to at first, but FNC makes multi-platform-multi-framework-development a thing of reality.

Hopefully TMS will provide additional support for the TDataSource interface for other graphical user controls of the FNC component set.

Posted in Delphi

5-Minute-Snack: Loading big images into TImage without flicker

Loading images into a TImage component is easy. We do this during design-time most of the time. In the other cases it is mostly a call to LoadFromFile and we are done.

Recently, I had the task to display several images with a timer delay. Again, very simple to implement, but if you are not careful it looks hideous. This was the piece of code I implemented in the method that was triggered whenever the next image was supposed to be shown:

 The image provider returns the filename of the next image. One would think that he last image being loaded is being shown until the next image is being displayed. Sadly, the VCL handles this differently – for good reasons, I guess. 

As soon as that line is invoked, the image displayed is being removed and you end up seeing an empty space where the image has been before. Depending on the size of the image file you load that might be a significant amount of time. Even if the image has a small file size, there will always be a flicker.

A simple trick helps. Sometimes more code less compact is actually better:

In this alternative approach, we load the image into memory first. That means the delay happens when the image is still visible. Furthermore, the assignment is instantaneous, so there will be no flicker.

Subtle difference in code, big difference in the presentation of your app.

Posted in Delphi, Snacks Tagged with: , ,

5-Minute-Snack: Hiding or changing the mouse cursor system-wide

At first look, this might seem like a very easy task. Especially, when you simply want to hide the mouse cursor, you will find the method 

in the Windows API.

Simply state…

..and the mouse cursor disappears. Or does it? 

Building a very simple demo application using the VCL teaches you something totally different: The cursor is hidden whenever your form is the top-most form, the mouse is moving over that form and that windows has inout-focus. If you leave the area of your form, the cursor will reappear. Again, it will suffice to change focus away from the app. So if you have two windows and the other window takes all input the cursor will also be shown if you move the mouse over the other form. There are several instances when this behavior might not be what you want to happen. 

In my case, I wanted the mouse cursor to disappear completely. No matter what was happening on the desktop PC the application was running on.

Let’s have a look at mouse cursors in Windows to evaluate our options. There is not only one mouse cursor, depending on the “target” the cursor is pointing towards, its appearance might change. Thus, we can load several cursors into the system.

The VCL wraps a mouse cursor in the class TCursor . The following cursor “shapes” have been defined:

Internally, a cursor is very similar to an icon with masking. I explicitly exclude animated cursors — completely different.

Using the Windows API you can load cursor files and assign them to these cursor types. Again, if you are just changing the cursor inside your VCL application, it will suffice to stay in the VCL world.

Changing it system-wide does, of course, require usage of the Windows API.

MSDN reveals a function called 

This function allows you to define a cursor, identified by an HICON-handle, for a certain appearance. Here very nicely referred to as “id”. You could not name it any better, I guess… 

Browing the documentation yields the following table for the DWORD-parameter:

Another thing to realize is that you are unable to assign “no cursor” to a certain id. Thus, we will need to create a cursor that is 100% transparent. You will find hundreds of cursor files on the internet that will provide just that (e.g. Google for “hidden cursor .cur”). 

In order to load the cursor file, we use

which will yield the desired reference for SetSystemCursor .

We are able to replace the cursor now, but how can we get back to the initital setting when our app is closed? The TCursor class is very tightly tied to the Windows API. So we can store the initial cursor in another HCURSOR-reference. Be careful to get this reference before replacing the cursor! Otherwise, the VCL will load your replacement into TScreen.Cursors !

Above, you see the source code that replaces the cursor and also takes care of saving the state before your application was started. We only replace the standard arrow using OCR_NORMAL . Of course, you can change multiple cursor appearances using this approach.

Posted in Delphi, Snacks Tagged with: ,

5-Minute-Snack: Using a Custom Delphi Component with FmxLinux

A couple of months ago, I described the Pexels component in detail. The component allows integration of pictures from Pexels.com into your Delphi application accessing the REST API provided.

Furthermore, I described the necessary steps using inheritance and abstraction that are necessary to develop the component for two frameworks: VCL and FireMonkey.

With the arrival of Delphi Tokyo and the recent announcement of FmxLinux which will give us the ability to write GUI applications with FireMonkey for Linux, I was eager to see if the demo application will also compile.

The demo application did compile without any issues. However, the main form was not being shown when I started the application. 

The reason was rather simple:

Debugging the application showed that the Midas Library was missing as I use the TClientDataset  component. I never installed the Midas libraries for Linux. They do not come with Tokyo. You need to download them separately. Marco Cantu blogged about that (link).

I decided to do yet another migration as the latest migration I tried worked out so nicely. As FireDAC is the way to go when it comes to databases, I replaced TClientDataset  with TFDMemTable .

I have to admit that I was not able to approach this as easily as expected. Whenever I opened a form that I created with the Pexels component referencing TClientDataset  the IDE was not able to open the form designer. Furthermore, I was unable to open the text view of the form. Thus, I had to replace the references in the form files using Notepad++. On the one hand I am very much aware that a data-type change is something not to be done with existing properties, especially if the components have been deployed to customers. On the other hand the IDE should be able to handle it. 

However, after the replacements were taken care of, I was able to run the app in Linux:

This is quite an achievement for FmxLinux. The component uses:

  • Database Technologies: FireDAC
  • Parallel Programming: Asynchronous Web Requests
  • Embarcadero REST Library
  • Data Binding

Finally, let me repeat: No code change was necessary.

After supporting Windows 32 and 64, MacOS, iOS and Android, I can add Linux to the list of supported platforms.

If you are interested in all the details about component development: There will be a whole day on component development at the next Delphi Code Camp! All details here.

(For the record, I do know that I named my virtual Ubunto instead of Ubuntu by mistake. It’s a typo.)

Posted in Delphi Tagged with: , , , , , ,