1. Home
  2. Computing & Technology
  3. Delphi Programming

About Delphi Programming Tips - 2006

Delphi tips from the free About Delphi Programming newsletter.
More tips by year: 2008 | 2007 | 2006 | 2005 | 2004 | 2003 | 2002 | 2001 | 2000 | 1999
Memory Leak Notification in Delphi
One of the nicest features of the "new" memory manager in Delphi allows applications to register (and unregister) expected memory leaks, and optionally report unexpected memory leaks on program shutdown.
Convert RGB to TColor or How to Get More TColor values for Delphi
If you have values for red, green and blue intensities (a number from 0 to 256 - byte type), here's how to get the Delphi TColor value...
Using the Percentage Character in Delphi's Format function
Delphi's Format (declared in the SysUtils unit) function can be used to construct a formatted string assembled from a format string and an array of arguments. Here's how to make it display a "stand-alone" percentage character.
Programmatically Select a File in Windows Explorer from Delphi code
In order to select a file in Windows Explorer you need to launch an instance of the "explorer.exe" using the ShellExecute API method with a special Windows Explorer switch. Here's how to do that programmatically from Delphi code.
Register a File Command for a Windows Shell Menu from Delphi code
Most of the file types (files "grouped" by their extension) in Windows have an associated application which gets executed when a user double clicks a file in the Windows Explorer. Sometimes, you will need to add (register) a custom command for a known file type. For example, you may want an option to open PAS files (Delphi units) with Notepad.
How to Select a "Bar" in the TChart Delphi control
Here's a method of simulating a selection in the TChart Delphi control. When a user clicks on a bar of a chart, the respective bar changes its color.
Troubleshoot "Left Side Cannot Be Assigned To" Compile Time Delphi Error
Troubleshoot Delphi's Compile Time error: "Left Side Cannot Be Assigned To" when using Record as a Property and trying to assign a value to a field in the record.
Understaning and Using the Goto Delphi Statement
A Delphi Goto statement transfers program execution to the statement marked by the specified label. Learn how to use it and when not to use Goto code jumps!
Advanced Record Structure Type in Turbo Delphi and Delphi 2006
Record data types in Delphi represent a mixed set of elements. Records in Turbo Delphi (Delphi 2006) are even more powerful, records support: constructor, properties, methods and much more.
Allow Only One Application Execution Per Windows Session
Here's how to allow only one execution of your Delphi application per Windows session. A warning message appears if a user tries to execute more than once without restarting Windows.
How to Highlight Non-Focused DBGrid Selected Row
In scenarios where you have two DBGrid controls on the same form displaying data for a master-detail database relationshiplike Customer -> Orders, for a selected record/row in the "master" grid only related records are displayed in the "child" grid. As soon as the grid looses the focus, the selected row does no longer appear selected. Here's how to make the selected row appear as focused even when the DBGrid is not focused.
How to Declare and Initialize Constant Arrays in Delphi
Sometimes you need to declare a constant array in Delphi - a read-only array. You cannot change the value of a constant or a read-only variable. Therefore, while declaring a constant array you have to initialize it. Here are a few examples of declaring and initializing constant arrays in Delphi...
Enumerating and Playing System Sounds from Delphi code
To play a system sound ("application even" or "system even" as called by MS), you can call the PlaySound API function, available in the mmsystem unit. Here's how to enumerate all the system sounds and play them from Delphi code.
Reset the TWebBrowser Delphi control to an Empty (Blank) Page
To navigate to an empty, blank page with Delphi's TWebBrowser control, use the navigate method and point the broswer to 'about:blank'. Here's how...
How to Create the !DocType and ?XML Elements using TXmlDocument Delphi component
Delphi's implementation of the TXMLDocument component, which basically uses Microsoft XML parser by default, does not provide a way to add a node of the "ntDocType" (TNodeType type). Here's how to solve this problem.
How to Draw Custom Text on a Delphi Form's Caption Bar
If you want to add some custom text on the caption bar of a form, without changing the Caption property of the form you need to handle one special Windows message: WM_NCPAINT. Here's how in Delphi.
Execute a Custom Action on the Form's Help button Click
If the form's BorderStyle property is bsDialog and biHelp is included in BorderIcons, a question mark appears in the form's title bar and when clicked, the cursor changes to crHelp. Here's how to execute some custom action when the users clicks the help button on the form's title bar.
How to "Pan" an Image inside a ScrollBox
"Panning" (in computer terms) refers to being able to move an object both horizontally and vertically by holding down a given mouse button and then moving the mouse. Here's how to implement image panning in Borland Delphi.
How to Capture the Screen Shot of the Active Window
Need to take a screen shoot from Delphi code? No problem ... here's how to capture the Windows desktop or the Active Window's image into a TBitmap object - and display it inside a TImage control.
Remove the Windows Constraint on Minimum Form Size: Width and Height
By Windows design a form (window) has a size constraint that sets the minimum form height to the height of the caption bar and the width to 112 pixels (118 in XP theme). By handling the WM_GETMINMAXINFO Windows message, you can set your own size constraint that are applied when the user is resizing the form (or the size is set programmatically). Here's how...
How to Browse Delphi's VCL Source Code
If you have a Delphi version that comes with the source code (YES! some Delphi versions include the full VCL / RTL source code), you can easily browse through it! Here's how...
How to Execute a JavaScript Code on a TWebBrowser Document
The TWebBrowser Delphi control provides access to the Web browser functionality from your Delphi applications. Here's how to execute a custom script (JavaScript or VBScript) function on a HTML document loaded in the TWebBrowser control...
Add Console Input and Output for a GUI Delphi Application
If you want to have console input/output for GUI applications you should use the AllocConsole and FreeConsole functions. The AllocConsole function allocates a new console for the calling process. The FreeConsole function detaches the calling process from its console. Here's a Delphi example...
Is there an Image in the TImage Delphi Control?
How to check if a TImage Delphi control has an image assigned.
Copy RTF Text/Formatting From one TRichEdit to Another
Delphi's TRichEdit control is a text edit control that allows the user to enter text that includes variation in font attributes and paragraph formatting information. Here's how to copy the text from one RicheEdit to another, without loosing the RTF formatting.
Programmatically Get and Set the MediaPlayer Sound Volume
The TMediaPlayer Delphi component enables your application to control a media playing or recording device such as a CD-ROM player, video player/recorder. Find out how to get and set the sound volume while media is being played.
Programmatically Enable and Disable CD Auto-Run / Auto-Play
When a user inserts a CD into a CD drive, Windows AutoRun feature opens up an instance of Windows Explorer to display the content of the CD. There are many situations where AutoRun may need to be temporarily or persistently disabled. Here's how to programmatically enable or disable auto-play.
How to Re-Initialize an Object to its Default State
In Delphi OOP, the Create constructor is used to create an instance of a class. In most cases the constructor is uses to initialize property values. In the lifetime of an object, properties are assigned with non-default values. Here's the trick to re-initialize all the fields of a custom Delphi object.
How to Disable TEdit's Default PopUp (Context) Menu
When we right-click on an Edit component (or any other component that allows editing such as MaskEdit, Memo, DbEdit, etc.), by default the system's context menu pops up with options to undo, copy, paste, etc. Here's how to remove or disable the default context menu.
Programmatically Get and Set the State of the CapsLock and NumLock Keys
The VK_CAPITAL Windows virtual key constant defines the Caps Lock key. The VK_NUMLOCK constant defines the Num Lock key. Here's how to programmatically get and set the state of those two keys.
How to Translate a Virtual Key Code to a Character
Windows defines special constants for each key the user can press. The virtual-key codes identify various virtual keys. In Delphi, the OnKeyDown and OnKeyUp events provide the lowest level of keyboard response. To use OnKeyDown or OnKeyUp to test for keys the user presses, you must use Virtual key codes to get the key pressed. Here's how to translate the virtual key code to the corresponding Windows character.
Add your Application to the Windows
Another approach to allowing the user to send a file to your Delphi application for processing, without the need to register a file type, is to add your applicaation to the special Windows "Send To" menu. Here's how to programmatically add your application to the "Send To" menu.
Drawing a Focus Rectangle Around the Active Control
By using the OnActiveControlChange event of the Delphi's TScreen class you can change the background color of the active input control. To create event more eye-catching effect you could "draw" a rectange around the focused control.
How to Detach an Event Handler from a Control Event
Almost all the code you write in your Delphi applications is executed, directly or indirectly, in response to events. For example, when a user clicks a button, an OnClick event is being raised. You can write an event handling procedure to react on this event. Here's how to detach (remove, disable) the event handling code from the event...
How to Draw a Gradient Fill on a Canvas
Here a two custom Delphi functions to paint a Canvas object using gradient fill from one color value to another, either vertical or horizontal.
How to Animate (Scroll) Application Title on the TaskBar
If a lengthy process is about to start in you Delphi application, you can indicate that the process is still running by, for example, animating the title of your application, i.e. animating the caption of your application's TaskBar button. Here's how to grab a user attention...
Create a Resizable Form with No Border (FormStyle = bsNone)
There may be situations when you want to remove a form's border (and title bar) *but* still leave the option for a user to resize the form. Here's a handy trick to enable form resizing when BorderStyle is set to bsNone for Delphi forms.
TListBox with Radio Buttons
Delphi's TListBox VCL component displays a collection of items in a scrollable list. The MultiSelect property determines if a user can select more than one item. Here's how to draw a radio button for each of the items in a List Box...
How to Display "Bold" Items in the TTreeView Delphi component
By default (Windows drawing), all items (tree nodes) of a tree view share the same font styling. If you only want to apply *bold* to some tree items (their Captions), you can use a "special" TreeView API. Here's how to *bold* items in Delphi applications.
How to Access a TRadioButton from a TRadioGroup - Disable, Change Font, etc.
Here's how to access a specific radio button either by Index or by Caption. When you "grab" the TRadioButton you can disable it, change its color, etc...
How to Jump to the Last Item in the TListBox Delphi component
When dynamically adding items to the ListBox you might want to ensure that the last added item is visible. Here's how to use the TopIndex property to ensure the visibility of the last item (and all the items at the end of the list that can "fit" into the box).
How to Programmatically Open Recycle Bin from Delphi Code
When you delete a file in Windows Explorer or My Computer, the file appears in the Recycle Bin. The file remains in the Recycle Bin until you empty the Recycle Bin or restore the file. Here's how to programmatically display the Recycle Bin to a user of your Delphi application.
Activate a Running Instance of an Application when a User Openes an Associated File by Extension
When a user select more than one file to be opened by your Delphi application (after you have registered your own file type - extension), Windows will start as many new instances of your application as there are files being opened. Here's how to make sure that all the processed files are being handled by only one instance of the application.
How to Run your Delphi Application by Double-Clicking on a File (Register a FileType - Extension with Your Program)
When creating applications, it is often necessary to store (application related) data on the file system. You may choose to create your own file type (with a *unique* extension) and then associate that extension with your application through Windows. This tells Windows to automatically run your application when a user double-clicks a file in Windows Explorer, for example.
How to Use a Function or a Procedure as a Parameter in another Function in Delphi
Here's how to send a function (or a procedure) as a parameter to another function in Delphi.
How to Convert RGB Color to HSB (HSV) Color
Here's a function to convert a RGB color value to a HSV color using Delphi.
How to Move ListBox Items with the Mouse (Drag and Drop) in Delphi application
Here's how to allow a user to rearrange (change item's position) the items of a ListBox using drag and drop...
How to Detect a TPopupMenu's OnClose (OnPopDown) Event in Delphi applications
Unfortunately, the TPopupMenu does not expose an event you can handle that will fire when the menu gets closed - either after a user has selected an item from the menu or has activated some other UI element. Here's a fix to this behavior.
How to Remove the "Today" Mark from the TDateTimePicker Delphi component
The TDateTimePicker component displays a list box for entering dates or times. By design it displays the label "today" at the bottom of the control allowing a user to quickly select the today's date. Here's how to remove the "today" marker from the TDateTimePicker.
How to Clone a Delphi Form
Beside instantiating Delphi Forms dynamically, you can deside to create a *clone* of an existing form - by using the TMemoryStream class.
How to Change the Colors of the TProgressBar
By design, the colors of a progress bar are determined by Windows. Here's how to change the background color and the bar color of a TProgressBar Delphi component, at run-time.
How to Right Align a Menu Item in a Delphi application
How to align a form's Help menu item (for example) on the right side of the menu bar, in Delphi applications.
Creating Custom Code Templates in Delphi 2006
Here's how to create a custom code template, in Delphi 2006, to open an URL in your default browser.

Explore Delphi Programming

More from About.com

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2006 Tips

©2008 About.com, a part of The New York Times Company.

All rights reserved.