Delphi Central - Hints Page 1

'

IDE Shortcuts

There are two IDE features that I find I use all the time. These are:

Indent and unindent

Simply select the code to be indented and press and hold CTRL Shift then press i to indent or u to unindent

Bookmarks

To create a bookmark press and hold CTRL k then press any numeric key, to instantly go to a bookmark press and hold CTRL q then press the corresponding numeric key that you created the bookmark with.

Detecting if the Num Lock key is down

  function IsNumLockOn() : boolean;
  begin
    Result := (GetKeyState(VK_NUMLOCK) and $01) <> 0;
  end;

You can also use this for Caps Lock or Scroll Lock by replacing VK_NUMLOCK with VK_CAPITAL or SCROLL.

Making the drop down of a ComboBox Wider

If you need to use a TCombobox to select long strings you can always make the dropdown part wider with this simple tip:

    ComboBox1.Perform(CB_SETDROPPEDWIDTH, 600, 0);

Where 600 is the new width in pixels

Activating the screen saver

To activate the current screen saver use:

    PostMessage(GetDesktopWindow, WM_SYSCOMMAND, SC_SCREENSAVE, 0);

Displaying the Find Files Dialog

The Find Files dialog is usually accessed from Explorer, but its also very easy to do from on your own programs using the ShellExecute command. Here's how:
ShellExecute(
    handle,
    'find',	 
    'c:\windows',	// The directory to start searching in
    '',	
    '',
    SW_SHOW
   );

Getting rid of the highlighted cell in a TStringGrid

If you want to get rid of the highlighted cell in a TStringGrid which does not have the focus or is used only to display data, try the following small procedure.
procedure TMyproject.GridClean(Sender: TObject);
var Xr: TGridRect;
begin
   Xr.Top := -1;
   Xr.Left := -1;
   Xr.Right := -1;
   Xr.Bottom := -1;
   (Sender as TStringgrid).Selection := Xr;
end;
Use it either in the grid's OnExit event or as needed in your code,
        var Mydata: TStringGrid;
        GridClean(Mydata);
This hint was provided by Pat White

Implementing a Sleep procedure

In Delphi 2 and 3 this is easy, you just use:
  Windows.Sleep(TimeInMilliseconds);
Whilst in Delphi 1 you would need:
procedure Sleep(milliseconds: LongInt);
var
  iTemp : Longint;
begin
  iTemp :=  GetTickCount + milliseconds;
  while GetTickCount <  iTemp do 
    Application.ProcessMessages;
end;

Checking for Big fonts

It can often create havoc on your interface if a user is using large fonts (or normal if you have designed in large fonts. A simple check to see what size the a user has is:
if Screen.PixelsPerInch = 96 then
  ;// Its the normal font size;

if Screen.PixelsPerInch = 120 then
  ;// Its the large font size;

How to Create Shortcuts

To Create shortcuts in Delphi you have to use the com engine. Fortunately this is not too difficult. The following code shows you how:
var
  slTemp: IShellLink;
  pfTemp: IPersistFile;
  sTarget: String;  { This is the path and name of the file that the   
                      shortcut is to point too }
  sIconFile : String; { This is the path and name of the file that contains
                        the icon (it can be an .exe or .ico file) }
  iIconIndex : Integer { The index of which icon to use in the specified file }
  sName : String  { The name for the shortcut including the path}

begin
  // initialize the COM engine
  CoInitialize(nil);
  try
    // create an uninitialized IShellLink (a shortcut)
    if CoCreateInstance(CLSID_ShellLink,
                        nil, 
                        CLSCTX_INPROC_SERVER,
                        IShellLink, slTemp) = S_OK then
    begin
      slTemp.SetPath(PChar(sTarget));
      slTemp.SetIconLocation(PChar(sIconFile), iIconIndex);
    //Get an IPersistFile interface (needed to specify the name of the Shortcut)
      if slTemp.QueryInterface(IPersistFile, pfTemp) = S_OK then
        if pfTemp.Save(StringToOleStr(sName), True) <> S_OK then
          ShowMessage('could not save the shortcut');
    // do not Release slTemp, it is done automatically by Delphi
    end;
  // deinitialize COM
  finally
    CoUninitialize;
  end;
end;

Avoiding the Beep in an EditBox

The default behaviour in Delphi, when you press the enter key in a EditBox a beep is generated. This can be avoided by adding the following line to the onKeyPress event:
  if key=#13 then key := #0;

How to show the CPU Debug Window

Built into Delphi there is a hidden Debug window that shows you a disassembly of your code and also the registers, flags, stack, and data dump. To enable it, use Regedit and go to the following folder:
  HKEY_CURRENT_USER\Software\Borland\Delphi\3.0\Debugging
In this folder add a new string value called EnableCPU and set its value to 1. Now next time you run Delphi you will have a new menu item on the view menu called CPU which will bring up this new window.

Hiding the TaskBar in Windows 95

To hide the task bar use
   ShowWindow( FindWindow( 'Shell_TrayWnd',nil), SW_HIDE);
To show the task bar use
   ShowWindow( FindWindow( 'Shell_TrayWnd',nil), SW_SWOWNA);

Waiting for a process to finish

Var 
   Handle : Word;
begin
   Handle := WinExec('Appname.exe',SW_ShowNormal);
   While GetModuleUsage(Handle) > 0 do
      Application.Processmessages;
end;

Adding a Horizontal ScrollBar to a ListBox

SendMessage(ListBox1.Handle,       
            LB_SetHorizontalExtent,
            5000,
            longint(0));
Where
ListBox1.Handle is the Handle of the ListBox to add the scroll bar to
LB_SetHorizontalExtent is the message to add the scroll bar
5000 is the width in pixels that you wish to be scrollable
longint(0) is not used
					  

Google
Web www.Delphi-Central.com
Delphi Central - Delphi Programming Tutorials, Hints and Tips