Thursday, December 04, 2008

Capturing application messages

Source : http://www.latiumsoftware.com/en/delphi/00047.php

 

Sometimes we need to capture Windows messages at an application level. One way is by a hook, and another way is by using the Message event of the Application object which is triggered every time the application gets a Windows message. We are going to follow the second path. There are two ways to set an event handler for this event: one is using an ApplicationEvents object that comes in Delphi 5 (simply double click its OnMessage combo box in the Object Inspector) and the other is doing it by hand:

  1. In the private section of your main form add the following declaration:

    procedure ApplicationMessage(var Msg: TMsg; var Handled: Boolean);
  2. Assign the OnMessage property in the Create event of the form for example:

    procedure TForm1.FormCreate(Sender: TObject); begin   Application.OnMessage := ApplicationMessage; end;
  3. When you want to stop capturing messages all you have to do is set OnMessage to nil:

    procedure TForm1.FormDestroy(Sender: TObject); begin   Application.OnMessage := nil; end;

Finally you should implement the procedure. For example, we are going to trap the keyboard messages WM_KEYUP and WM_KEYDOWN to convert the decimal point of the numeric keypad to a comma (this is useful in Spanish applications).

procedure TForm1.ApplicationMessage(var Msg: TMsg;   var Handled: Boolean); begin   case Msg.Message of   WM_KEYDOWN, WM_KEYUP:     case Msg.wParam of     // Replace the period in the numeric keypad (key code = 110)     // with a comma (key code = 188).     110: Msg.wParam := 188;     end;   end; end;

Now you can drop an Edit control on your form and test it.



Selalu bersama teman-teman di Yahoo! Messenger
Tambahkan mereka dari email atau jaringan sosial Anda sekarang!

0 Comments: