Thursday, July 21, 2011

Read mouse pointer aka cursor position in Delphi!

Well this seems to a problem sometimes when someone asks us how to find where my cursor is currently located. Well why to say when we can infact show where actually is our mouse cursor at given point in time. To do this we will create a simple Delphi program as follows -

program Project1;
{$APPTYPE CONSOLE}
uses
  SysUtils, Controls, Windows;
var
  MyMouse : TMouse;
begin
  MyMouse := TMouse.Create;
  While True do
  begin
    WriteLn('(X, y) = (' + inttostr(TPoint(MyMouse.CursorPos).x) + ',' + inttostr(TPoint(MyMouse.CursorPos).y) + ')');
    sleep(300);
  end
end.

As we can see we create a simple Mouse cursor for ourselves in this small program and simply place it in a while loop of 300 ms to trace the current position of mouse cursor in our screen.

Simple isn't it?

Limit of Recursion!

Recently I was going through Linked in Delphi user's group and found that few programming sections in a programming site needs examples in Delphi to demonstrate the methodology of implementing solution to a problem in various languages. On visiting the site I found that there is a need of particular example to find - Limit of Recursion as implemented in the language. This is a very interesting question as we all use recursion for implementing various solutions but we rarely care to find the actual limit to which we can implement recursion. Although most of this problems answer depends on the memory available to particular systems however we still can check this limit by implementing a very basic recursive function.

The idea behind this simple program is to keep on iterating and visit nest level for a recursive function till the time we go out of memory. So the limiting condition here is the draining of the memory. A simple program like the following will demonstrate this limit -


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Level : integer;
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

function Recursive(Level : integer) : integer;
begin
try
Level := Level + 1;
Recursive(Level);
except on e: Exception do
Result := Level;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Level = ' + inttostr(Level));
ShowMessage('Level = ' + inttostr(Recursive(Level)));
end;

end.

This program takes up an integer value and tries to increment the value and call the function till StackOverflow error occurs and then simply prints this value. This implementation gave the following result on my Windows Vista 64 bit computer for integer value - 4375221. The values can differ if we take int64 instead of signed integer in the above example.

Recursion in Delphi!

Recursion is a very generically used term in computer programming. Some problem in computer programming which allow repetition of some particular task again and again with some terminating check can be implemented very easily with the help of the concept of recursion.

A simplest example is to find the factorial of 5. The result is derived as -

5! = 5 X 4 X 3 X 2 X 1
5! = 120

This simple program can be easily designed using recursion or using recursive calls to the same function again and  again. e.g. -

fact() = fact(i) X fact(i - 1)

The above expression at each step calls the factorial function again with one value less for variable i in each call.

A simple Delphi program to demonstrate the recursion concept in Delphi is as follows -

A button click is used to trigger the recursive function -

procedure TForm1.Button1Click(Sender: TObject);
var
    a : integer;
begin
    a := fact(5);
    ShowMessage(inttostr(a));
end;

and the simple recursive function is -

function fact(i : integer) : integer;

begin
    if (i = 1) then
    begin
        fact := 1;
    end
    else
    begin
        fact := i * fact(i - 1);
    end;
end;
As we can see in the above example fact function calls itself till the value of i variable reaches 1 which is the terminating condition for our recursive function and no further recursions are made.

The result returned for above example is 120.