(*  TMessageTracer v. 1.06
    Copyright (c) 1998-2003 by CoolDev.Com.
    Delphi 3/4/5/6/7
    C++Builder 3/4/5/6

    Although TMessageTracer is being distributed as Freeware with full
    source codes included, IT IS part of CoolControls package and
    should be delivered un-modified ONLY togeather with CoolControls.
*)

{$I versions.inc}
unit MsgTracer;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, StdCtrls,
  Buttons, Grids, VclTracer, Menus, Clipbrd, ExtCtrls, Dialogs, ComCtrls,
  CoolUtils, CoolCtrls;

type
  TTracerForm = class(TForm)
    MsgGrid: TCoolStringGrid;
    SrcLabel: TLabel;
    DstLabel: TLabel;
    IncludeBtn: TSpeedButton;
    IncAllBtn: TSpeedButton;
    ExcludeBtn: TSpeedButton;
    ExAllBtn: TSpeedButton;
    SrcList: TCoolListBox;
    DstList: TCoolListBox;
    btnStart: TButton;
    btnStop: TButton;
    btnClear: TButton;
    btnClose: TButton;
    mPopup: TPopupMenu;
    Copymessage1: TMenuItem;
    CopywParam1: TMenuItem;
    CopylParam1: TMenuItem;
    CopyResult1: TMenuItem;
    ImageRest: TImage;
    ImageMsg: TImage;
    btnUp: TSpeedButton;
    btnDown: TSpeedButton;
    btnOpen: TButton;
    btnSave: TButton;
    OpenDialog: TOpenDialog;
    SaveDialog: TSaveDialog;
    procedure MsgGridDrawCell(Sender: TObject; Col, Row: Integer;
      Recty: TRect; State: TGridDrawState);
    procedure IncludeBtnClick(Sender: TObject);
    procedure ExcludeBtnClick(Sender: TObject);
    procedure IncAllBtnClick(Sender: TObject);
    procedure ExAllBtnClick(Sender: TObject);
    procedure btnStartClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure Copymessage1Click(Sender: TObject);
    procedure CopywParam1Click(Sender: TObject);
    procedure CopylParam1Click(Sender: TObject);
    procedure CopyResult1Click(Sender: TObject);
    procedure btnDownClick(Sender: TObject);
    procedure btnUpClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    FWndHook: TWndHook;
    FOnce,
    FTopMost,
    FStoped: Boolean;
    //Messages
    procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
    //Methods
    procedure SetTopMost(Value: Boolean);
    procedure BeforeMessage(Sender: TObject; var Msg: TMessage; var Handled: Boolean);
    procedure AfterMessage(Sender: TObject; var Msg: TMessage; var Handled: Boolean);
    procedure HookedControlChange(Sender: TObject);
    procedure MoveSelected(List: TCustomListBox; Items: TStrings);
    procedure SetItem(List: TCustomListBox; Index: Integer);
    function GetFirstSelection(List: TCustomListBox): Integer;
    procedure SetButtons;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property TopMost: Boolean read FTopMost write SetTopMost;
    property WndHook: TWndHook read FWndHook write FWndHook;
  end;

const
  WindowsMessages: array[0..436] of TIdentMapEntry = (
    (Value: WM_NULL; Name: 'WM_NULL'),
    (Value: WM_CREATE; Name: 'WM_CREATE'),
    (Value: WM_DESTROY; Name: 'WM_DESTROY'),
    (Value: WM_MOVE; Name: 'WM_MOVE'),
    (Value: WM_SIZE; Name: 'WM_SIZE'),
    (Value: WM_ACTIVATE; Name: 'WM_ACTIVATE'),
    (Value: WM_SETFOCUS; Name: 'WM_SETFOCUS'),
    (Value: WM_KILLFOCUS; Name: 'WM_KILLFOCUS'),
    (Value: WM_ENABLE; Name: 'WM_ENABLE'),
    (Value: WM_SETREDRAW; Name: 'WM_SETREDRAW'),
    (Value: WM_SETTEXT; Name: 'WM_SETTEXT'),
    (Value: WM_GETTEXT; Name: 'WM_GETTEXT'),
    (Value: WM_GETTEXTLENGTH; Name: 'WM_GETTEXTLENGTH'),
    (Value: WM_PAINT; Name: 'WM_PAINT'),
    (Value: WM_CLOSE; Name: 'WM_CLOSE'),
    (Value: WM_QUERYENDSESSION; Name: 'WM_QUERYENDSESSION'),
    (Value: WM_QUIT; Name: 'WM_QUIT'),
    (Value: WM_QUERYOPEN; Name: 'WM_QUERYOPEN'),
    (Value: WM_ERASEBKGND; Name: 'WM_ERASEBKGND'),
    (Value: WM_SYSCOLORCHANGE; Name: 'WM_SYSCOLORCHANGE'),
    (Value: WM_ENDSESSION; Name: 'WM_ENDSESSION'),
    (Value: WM_SYSTEMERROR; Name: 'WM_SYSTEMERROR'),
    (Value: WM_SHOWWINDOW; Name: 'WM_SHOWWINDOW'),
    (Value: WM_CTLCOLOR; Name: 'WM_CTLCOLOR'),
    (Value: WM_WININICHANGE; Name: 'WM_WININICHANGE WM_SETTINGCHANGE'),
    (Value: WM_DEVMODECHANGE; Name: 'WM_DEVMODECHANGE'),
    (Value: WM_ACTIVATEAPP; Name: 'WM_ACTIVATEAPP'),
    (Value: WM_FONTCHANGE; Name: 'WM_FONTCHANGE'),
    (Value: WM_TIMECHANGE; Name: 'WM_TIMECHANGE'),
    (Value: WM_CANCELMODE; Name: 'WM_CANCELMODE'),
    (Value: WM_SETCURSOR; Name: 'WM_SETCURSOR'),
    (Value: WM_MOUSEACTIVATE; Name: 'WM_MOUSEACTIVATE'),
    (Value: WM_CHILDACTIVATE; Name: 'WM_CHILDACTIVATE'),
    (Value: WM_QUEUESYNC; Name: 'WM_QUEUESYNC'),
    (Value: WM_GETMINMAXINFO; Name: 'WM_GETMINMAXINFO'),
    (Value: WM_PAINTICON; Name: 'WM_PAINTICON'),
    (Value: WM_ICONERASEBKGND; Name: 'WM_ICONERASEBKGND'),
    (Value: WM_NEXTDLGCTL; Name: 'WM_NEXTDLGCTL'),
    (Value: WM_SPOOLERSTATUS; Name: 'WM_SPOOLERSTATUS'),
    (Value: WM_DRAWITEM; Name: 'WM_DRAWITEM'),
    (Value: WM_MEASUREITEM; Name: 'WM_MEASUREITEM'),
    (Value: WM_DELETEITEM; Name: 'WM_DELETEITEM'),
    (Value: WM_VKEYTOITEM; Name: 'WM_VKEYTOITEM'),
    (Value: WM_CHARTOITEM; Name: 'WM_CHARTOITEM'),
    (Value: WM_SETFONT; Name: 'WM_SETFONT'),
    (Value: WM_GETFONT; Name: 'WM_GETFONT'),
    (Value: WM_SETHOTKEY; Name: 'WM_SETHOTKEY'),
    (Value: WM_GETHOTKEY; Name: 'WM_GETHOTKEY'),
    (Value: WM_QUERYDRAGICON; Name: 'WM_QUERYDRAGICON'),
    (Value: WM_COMPAREITEM; Name: 'WM_COMPAREITEM'),
    (Value: WM_COMPACTING; Name: 'WM_COMPACTING'),
    (Value: WM_COMMNOTIFY; Name: 'WM_COMMNOTIFY'),
    (Value: WM_WINDOWPOSCHANGING; Name: 'WM_WINDOWPOSCHANGING'),
    (Value: WM_WINDOWPOSCHANGED; Name: 'WM_WINDOWPOSCHANGED'),
    (Value: WM_POWER; Name: 'WM_POWER'),
    (Value: WM_COPYDATA; Name: 'WM_COPYDATA'),
    (Value: WM_CANCELJOURNAL; Name: 'WM_CANCELJOURNAL'),
    (Value: WM_NOTIFY; Name: 'WM_NOTIFY'),
    (Value: WM_INPUTLANGCHANGEREQUEST; Name: 'WM_INPUTLANGCHANGEREQUEST'),
    (Value: WM_INPUTLANGCHANGE; Name: 'WM_INPUTLANGCHANGE'),
    (Value: WM_TCARD; Name: 'WM_TCARD'),
    (Value: WM_HELP; Name: 'WM_HELP'),
    (Value: WM_USERCHANGED; Name: 'WM_USERCHANGED'),
    (Value: WM_NOTIFYFORMAT; Name: 'WM_NOTIFYFORMAT'),
    (Value: WM_CONTEXTMENU; Name: 'WM_CONTEXTMENU'),
    (Value: WM_STYLECHANGING; Name: 'WM_STYLECHANGING'),
    (Value: WM_STYLECHANGED; Name: 'WM_STYLECHANGED'),
    (Value: WM_DISPLAYCHANGE; Name: 'WM_DISPLAYCHANGE'),
    (Value: WM_GETICON; Name: 'WM_GETICON'),
    (Value: WM_SETICON; Name: 'WM_SETICON'),
    (Value: WM_NCCREATE; Name: 'WM_NCCREATE'),
    (Value: WM_NCDESTROY; Name: 'WM_NCDESTROY'),
    (Value: WM_NCCALCSIZE; Name: 'WM_NCCALCSIZE'),
    (Value: WM_NCHITTEST; Name: 'WM_NCHITTEST'),
    (Value: WM_NCPAINT; Name: 'WM_NCPAINT'),
    (Value: WM_NCACTIVATE; Name: 'WM_NCACTIVATE'),
    (Value: WM_GETDLGCODE; Name: 'WM_GETDLGCODE'),
    (Value: WM_NCMOUSEMOVE; Name: 'WM_NCMOUSEMOVE'),
    (Value: WM_NCLBUTTONDOWN; Name: 'WM_NCLBUTTONDOWN'),
    (Value: WM_NCLBUTTONUP; Name: 'WM_NCLBUTTONUP'),
    (Value: WM_NCLBUTTONDBLCLK; Name: 'WM_NCLBUTTONDBLCLK'),
    (Value: WM_NCRBUTTONDOWN; Name: 'WM_NCRBUTTONDOWN'),
    (Value: WM_NCRBUTTONUP; Name: 'WM_NCRBUTTONUP'),
    (Value: WM_NCRBUTTONDBLCLK; Name: 'WM_NCRBUTTONDBLCLK'),
    (Value: WM_NCMBUTTONDOWN; Name: 'WM_NCMBUTTONDOWN'),
    (Value: WM_NCMBUTTONUP; Name: 'WM_NCMBUTTONUP'),
    (Value: WM_NCMBUTTONDBLCLK; Name: 'WM_NCMBUTTONDBLCLK'),
    (Value: WM_KEYDOWN; Name: 'WM_KEYDOWN WM_KEYFIRST'),
    (Value: WM_KEYUP; Name: 'WM_KEYUP'),
    (Value: WM_CHAR; Name: 'WM_CHAR'),
    (Value: WM_DEADCHAR; Name: 'WM_DEADCHAR'),
    (Value: WM_SYSKEYDOWN; Name: 'WM_SYSKEYDOWN'),
    (Value: WM_SYSKEYUP; Name: 'WM_SYSKEYUP'),
    (Value: WM_SYSCHAR; Name: 'WM_SYSCHAR'),
    (Value: WM_SYSDEADCHAR; Name: 'WM_SYSDEADCHAR'),
    (Value: WM_KEYLAST; Name: 'WM_KEYLAST'),
    (Value: WM_INITDIALOG; Name: 'WM_INITDIALOG'),
    (Value: WM_COMMAND; Name: 'WM_COMMAND'),
    (Value: WM_SYSCOMMAND; Name: 'WM_SYSCOMMAND'),
    (Value: WM_TIMER; Name: 'WM_TIMER'),
    (Value: WM_HSCROLL; Name: 'WM_HSCROLL'),
    (Value: WM_VSCROLL; Name: 'WM_VSCROLL'),
    (Value: WM_INITMENU; Name: 'WM_INITMENU'),
    (Value: WM_INITMENUPOPUP; Name: 'WM_INITMENUPOPUP'),
    (Value: WM_MENUSELECT; Name: 'WM_MENUSELECT'),
    (Value: WM_MENUCHAR; Name: 'WM_MENUCHAR'),
    (Value: WM_ENTERIDLE; Name: 'WM_ENTERIDLE'),
    (Value: WM_CTLCOLORMSGBOX; Name: 'WM_CTLCOLORMSGBOX'),
    (Value: WM_CTLCOLOREDIT; Name: 'WM_CTLCOLOREDIT'),
    (Value: WM_CTLCOLORLISTBOX; Name: 'WM_CTLCOLORLISTBOX'),
    (Value: WM_CTLCOLORBTN; Name: 'WM_CTLCOLORBTN'),
    (Value: WM_CTLCOLORDLG; Name: 'WM_CTLCOLORDLG'),
    (Value: WM_CTLCOLORSCROLLBAR; Name: 'WM_CTLCOLORSCROLLBAR'),
    (Value: WM_CTLCOLORSTATIC; Name: 'WM_CTLCOLORSTATIC'),
    (Value: WM_MOUSEFIRST; Name: 'WM_MOUSEMOVE WM_MOUSEFIRST EN_KILLFOCUS'),
    (Value: WM_LBUTTONDOWN; Name: 'WM_LBUTTONDOWN'),
    (Value: WM_LBUTTONUP; Name: 'WM_LBUTTONUP'),
    (Value: WM_LBUTTONDBLCLK; Name: 'WM_LBUTTONDBLCLK'),
    (Value: WM_RBUTTONDOWN; Name: 'WM_RBUTTONDOWN'),
    (Value: WM_RBUTTONUP; Name: 'WM_RBUTTONUP'),
    (Value: WM_RBUTTONDBLCLK; Name: 'WM_RBUTTONDBLCLK'),
    (Value: WM_MBUTTONDOWN; Name: 'WM_MBUTTONDOWN'),
    (Value: WM_MBUTTONUP; Name: 'WM_MBUTTONUP'),
    (Value: WM_MBUTTONDBLCLK; Name: 'WM_MBUTTONDBLCLK'),
    (Value: WM_MOUSEWHEEL; Name: 'WM_MOUSEWHEEL WM_MOUSELAST'),
    (Value: WM_PARENTNOTIFY; Name: 'WM_PARENTNOTIFY'),
    (Value: WM_ENTERMENULOOP; Name: 'WM_ENTERMENULOOP'),
    (Value: WM_EXITMENULOOP; Name: 'WM_EXITMENULOOP'),
    (Value: WM_NEXTMENU; Name: 'WM_NEXTMENU'),
    (Value: WM_SIZING; Name: 'WM_SIZING'),
    (Value: WM_CAPTURECHANGED; Name: 'WM_CAPTURECHANGED'),
    (Value: WM_MOVING; Name: 'WM_MOVING'),
    (Value: WM_POWERBROADCAST; Name: 'WM_POWERBROADCAST'),
    (Value: WM_DEVICECHANGE; Name: 'WM_DEVICECHANGE'),
    (Value: WM_IME_STARTCOMPOSITION; Name: 'WM_IME_STARTCOMPOSITION'),
    (Value: WM_IME_ENDCOMPOSITION; Name: 'WM_IME_ENDCOMPOSITION'),
    (Value: WM_IME_COMPOSITION; Name: 'WM_IME_COMPOSITION'),
    (Value: WM_IME_KEYLAST; Name: 'WM_IME_KEYLAST'),
    (Value: WM_IME_SETCONTEXT; Name: 'WM_IME_SETCONTEXT'),
    (Value: WM_IME_NOTIFY; Name: 'WM_IME_NOTIFY'),
    (Value: WM_IME_CONTROL; Name: 'WM_IME_CONTROL'),
    (Value: WM_IME_COMPOSITIONFULL; Name: 'WM_IME_COMPOSITIONFULL'),
    (Value: WM_IME_SELECT; Name: 'WM_IME_SELECT'),
    (Value: WM_IME_CHAR; Name: 'WM_IME_CHAR'),
    (Value: WM_IME_KEYDOWN; Name: 'WM_IME_KEYDOWN'),
    (Value: WM_IME_KEYUP; Name: 'WM_IME_KEYUP'),
    (Value: WM_MDICREATE; Name: 'WM_MDICREATE'),
    (Value: WM_MDIDESTROY; Name: 'WM_MDIDESTROY'),
    (Value: WM_MDIACTIVATE; Name: 'WM_MDIACTIVATE'),
    (Value: WM_MDIRESTORE; Name: 'WM_MDIRESTORE'),
    (Value: WM_MDINEXT; Name: 'WM_MDINEXT'),
    (Value: WM_MDIMAXIMIZE; Name: 'WM_MDIMAXIMIZE'),
    (Value: WM_MDITILE; Name: 'WM_MDITILE'),
    (Value: WM_MDICASCADE; Name: 'WM_MDICASCADE'),
    (Value: WM_MDIICONARRANGE; Name: 'WM_MDIICONARRANGE'),
    (Value: WM_MDIGETACTIVE; Name: 'WM_MDIGETACTIVE'),
    (Value: WM_MDISETMENU; Name: 'WM_MDISETMENU'),
    (Value: WM_ENTERSIZEMOVE; Name: 'WM_ENTERSIZEMOVE'),
    (Value: WM_EXITSIZEMOVE; Name: 'WM_EXITSIZEMOVE'),
    (Value: WM_DROPFILES; Name: 'WM_DROPFILES'),
    (Value: WM_MDIREFRESHMENU; Name: 'WM_MDIREFRESHMENU'),
    (Value: WM_MOUSEHOVER; Name: 'WM_MOUSEHOVER'),
    (Value: WM_MOUSELEAVE; Name: 'WM_MOUSELEAVE'),
    (Value: WM_CUT; Name: 'WM_CUT'),
    (Value: WM_COPY; Name: 'WM_COPY'),
    (Value: WM_PASTE; Name: 'WM_PASTE'),
    (Value: WM_CLEAR; Name: 'WM_CLEAR'),
    (Value: WM_UNDO; Name: 'WM_UNDO'),
    (Value: WM_RENDERFORMAT; Name: 'WM_RENDERFORMAT'),
    (Value: WM_RENDERALLFORMATS; Name: 'WM_RENDERALLFORMATS'),
    (Value: WM_DESTROYCLIPBOARD; Name: 'WM_DESTROYCLIPBOARD'),
    (Value: WM_DRAWCLIPBOARD; Name: 'WM_DRAWCLIPBOARD'),
    (Value: WM_PAINTCLIPBOARD; Name: 'WM_PAINTCLIPBOARD'),
    (Value: WM_VSCROLLCLIPBOARD; Name: 'WM_VSCROLLCLIPBOARD'),
    (Value: WM_SIZECLIPBOARD; Name: 'WM_SIZECLIPBOARD'),
    (Value: WM_ASKCBFORMATNAME; Name: 'WM_ASKCBFORMATNAME'),
    (Value: WM_CHANGECBCHAIN; Name: 'WM_CHANGECBCHAIN'),
    (Value: WM_HSCROLLCLIPBOARD; Name: 'WM_HSCROLLCLIPBOARD'),
    (Value: WM_QUERYNEWPALETTE; Name: 'WM_QUERYNEWPALETTE'),
    (Value: WM_PALETTEISCHANGING; Name: 'WM_PALETTEISCHANGING'),
    (Value: WM_PALETTECHANGED; Name: 'WM_PALETTECHANGED'),
    (Value: WM_HOTKEY; Name: 'WM_HOTKEY'),
    (Value: WM_PRINT; Name: 'WM_PRINT'),
    (Value: WM_PRINTCLIENT; Name: 'WM_PRINTCLIENT'),
    (Value: WM_HANDHELDFIRST; Name: 'WM_HANDHELDFIRST'),
    (Value: WM_HANDHELDLAST; Name: 'WM_HANDHELDLAST'),
    (Value: WM_PENWINFIRST; Name: 'WM_PENWINFIRST'),
    (Value: WM_PENWINLAST; Name: 'WM_PENWINLAST'),
    (Value: WM_COALESCE_FIRST; Name: 'WM_COALESCE_FIRST'),
    (Value: WM_COALESCE_LAST; Name: 'WM_COALESCE_LAST'),
    (Value: WM_DDE_FIRST; Name: 'WM_DDE_FIRST'),
    (Value: WM_DDE_INITIATE; Name: 'WM_DDE_INITIATE'),
    (Value: WM_DDE_TERMINATE; Name: 'WM_DDE_TERMINATE'),
    (Value: WM_DDE_ADVISE; Name: 'WM_DDE_ADVISE'),
    (Value: WM_DDE_UNADVISE; Name: 'WM_DDE_UNADVISE'),
    (Value: WM_DDE_ACK; Name: 'WM_DDE_ACK'),
    (Value: WM_DDE_DATA; Name: 'WM_DDE_DATA'),
    (Value: WM_DDE_REQUEST; Name: 'WM_DDE_REQUEST'),
    (Value: WM_DDE_POKE; Name: 'WM_DDE_POKE'),
    (Value: WM_DDE_EXECUTE; Name: 'WM_DDE_EXECUTE'),
    (Value: WM_DDE_LAST; Name: 'WM_DDE_LAST'),
    (Value: WM_APP; Name: 'WM_APP'),
    (Value: WM_USER; Name: 'WM_USER'),
    // Controls' messages
    (Value: BN_CLICKED; Name: 'BN_CLICKED'),
    (Value: BN_PAINT; Name: 'BN_PAINT LBN_SELCHANGE CBN_SELCHANGE'),
    (Value: BN_HILITE; Name: 'BN_HILITE BN_PUSHED LBN_DBLCLK CBN_DBLCLK'),
    (Value: BN_UNHILITE; Name: 'BN_UNHILITE BN_UNPUSHED LBN_SELCANCEL CBN_SETFOCUS'),
    (Value: BN_DISABLE; Name: 'BN_DISABLE LBN_SETFOCUS CBN_KILLFOCUS'),
    (Value: BN_DOUBLECLICKED; Name: 'BN_DOUBLECLICKED BN_DBLCLK LBN_KILLFOCUS CBN_EDITCHANGE'),
    (Value: BN_SETFOCUS; Name: 'BN_SETFOCUS CBN_EDITUPDATE'),
    (Value: BN_KILLFOCUS; Name: 'BN_KILLFOCUS CBN_DROPDOWN'),

    (Value: BM_GETCHECK; Name: 'BM_GETCHECK'),
    (Value: BM_SETCHECK; Name: 'BM_SETCHECK'),
    (Value: BM_GETSTATE; Name: 'BM_GETSTATE'),
    (Value: BM_SETSTATE; Name: 'BM_SETSTATE'),
    (Value: BM_SETSTYLE; Name: 'BM_SETSTYLE'),
    (Value: BM_CLICK; Name: 'BM_CLICK'),
    (Value: BM_GETIMAGE; Name: 'BM_GETIMAGE'),
    (Value: BM_SETIMAGE; Name: 'BM_SETIMAGE'),

    (Value: LBN_ERRSPACE; Name: 'LBN_ERRSPACE'),

    (Value: LB_ADDSTRING; Name: 'LB_ADDSTRING'),
    (Value: LB_INSERTSTRING; Name: 'LB_INSERTSTRING'),
    (Value: LB_DELETESTRING; Name: 'LB_DELETESTRING'),
    (Value: LB_SELITEMRANGEEX; Name: 'LB_SELITEMRANGEEX'),
    (Value: LB_RESETCONTENT; Name: 'LB_RESETCONTENT'),
    (Value: LB_SETSEL; Name: 'LB_SETSEL'),
    (Value: LB_SETCURSEL; Name: 'LB_SETCURSEL'),
    (Value: LB_GETSEL; Name: 'LB_GETSEL'),
    (Value: LB_GETCURSEL; Name: 'LB_GETCURSEL'),
    (Value: LB_GETTEXT; Name: 'LB_GETTEXT'),
    (Value: LB_GETTEXTLEN; Name: 'LB_GETTEXTLEN'),
    (Value: LB_GETCOUNT; Name: 'LB_GETCOUNT'),
    (Value: LB_SELECTSTRING; Name: 'LB_SELECTSTRING'),
    (Value: LB_DIR; Name: 'LB_DIR'),
    (Value: LB_GETTOPINDEX; Name: 'LB_GETTOPINDEX'),
    (Value: LB_FINDSTRING; Name: 'LB_FINDSTRING'),
    (Value: LB_GETSELCOUNT; Name: 'LB_GETSELCOUNT'),
    (Value: LB_GETSELITEMS; Name: 'LB_GETSELITEMS'),
    (Value: LB_SETTABSTOPS; Name: 'LB_SETTABSTOPS'),
    (Value: LB_GETHORIZONTALEXTENT; Name: 'LB_GETHORIZONTALEXTENT'),
    (Value: LB_SETHORIZONTALEXTENT; Name: 'LB_SETHORIZONTALEXTENT'),
    (Value: LB_SETCOLUMNWIDTH; Name: 'LB_SETCOLUMNWIDTH'),
    (Value: LB_ADDFILE; Name: 'LB_ADDFILE'),
    (Value: LB_SETTOPINDEX; Name: 'LB_SETTOPINDEX'),
    (Value: LB_GETITEMRECT; Name: 'LB_GETITEMRECT'),
    (Value: LB_GETITEMDATA; Name: 'LB_GETITEMDATA'),
    (Value: LB_SETITEMDATA; Name: 'LB_SETITEMDATA'),
    (Value: LB_SELITEMRANGE; Name: 'LB_SELITEMRANGE'),
    (Value: LB_SETANCHORINDEX; Name: 'LB_SETANCHORINDEX'),
    (Value: LB_GETANCHORINDEX; Name: 'LB_GETANCHORINDEX'),
    (Value: LB_SETCARETINDEX; Name: 'LB_SETCARETINDEX'),
    (Value: LB_GETCARETINDEX; Name: 'LB_GETCARETINDEX'),
    (Value: LB_SETITEMHEIGHT; Name: 'LB_SETITEMHEIGHT'),
    (Value: LB_GETITEMHEIGHT; Name: 'LB_GETITEMHEIGHT'),
    (Value: LB_FINDSTRINGEXACT; Name: 'LB_FINDSTRINGEXACT'),
    (Value: LB_SETLOCALE; Name: 'LB_SETLOCALE'),
    (Value: LB_GETLOCALE; Name: 'LB_GETLOCALE'),
    (Value: LB_SETCOUNT; Name: 'LB_SETCOUNT'),
    (Value: LB_INITSTORAGE; Name: 'LB_INITSTORAGE'),
    (Value: LB_ITEMFROMPOINT; Name: 'LB_ITEMFROMPOINT'),
    (Value: LB_MSGMAX; Name: 'LB_MSGMAX'),

    (Value: CBN_ERRSPACE; Name: 'CBN_ERRSPACE'),
    (Value: CBN_CLOSEUP; Name: 'CBN_CLOSEUP'),
    (Value: CBN_SELENDOK; Name: 'CBN_SELENDOK'),
    (Value: CBN_SELENDCANCEL; Name: 'CBN_SELENDCANCEL'),

    (Value: CB_GETEDITSEL; Name: 'CB_GETEDITSEL'),
    (Value: CB_LIMITTEXT; Name: 'CB_LIMITTEXT'),
    (Value: CB_SETEDITSEL; Name: 'CB_SETEDITSEL'),
    (Value: CB_ADDSTRING; Name: 'CB_ADDSTRING'),
    (Value: CB_DELETESTRING; Name: 'CB_DELETESTRING'),
    (Value: CB_DIR; Name: 'CB_DIR'),
    (Value: CB_GETCOUNT; Name: 'CB_GETCOUNT'),
    (Value: CB_GETCURSEL; Name: 'CB_GETCURSEL'),
    (Value: CB_GETLBTEXT; Name: 'CB_GETLBTEXT'),
    (Value: CB_GETLBTEXTLEN; Name: 'CB_GETLBTEXTLEN'),
    (Value: CB_INSERTSTRING; Name: 'CB_INSERTSTRING'),
    (Value: CB_RESETCONTENT; Name: 'CB_RESETCONTENT'),
    (Value: CB_FINDSTRING; Name: 'CB_FINDSTRING'),
    (Value: CB_SELECTSTRING; Name: 'CB_SELECTSTRING'),
    (Value: CB_SETCURSEL; Name: 'CB_SETCURSEL'),
    (Value: CB_SHOWDROPDOWN; Name: 'CB_SHOWDROPDOWN'),
    (Value: CB_GETITEMDATA; Name: 'CB_GETITEMDATA'),
    (Value: CB_SETITEMDATA; Name: 'CB_SETITEMDATA'),
    (Value: CB_GETDROPPEDCONTROLRECT; Name: 'CB_GETDROPPEDCONTROLRECT'),
    (Value: CB_SETITEMHEIGHT; Name: 'CB_SETITEMHEIGHT'),
    (Value: CB_GETITEMHEIGHT; Name: 'CB_GETITEMHEIGHT'),
    (Value: CB_SETEXTENDEDUI; Name: 'CB_SETEXTENDEDUI'),
    (Value: CB_GETEXTENDEDUI; Name: 'CB_GETEXTENDEDUI'),
    (Value: CB_GETDROPPEDSTATE; Name: 'CB_GETDROPPEDSTATE'),
    (Value: CB_FINDSTRINGEXACT; Name: 'CB_FINDSTRINGEXACT'),
    (Value: CB_SETLOCALE; Name: 'CB_SETLOCALE'),
    (Value: CB_GETLOCALE; Name: 'CB_GETLOCALE'),
    (Value: CB_GETTOPINDEX; Name: 'CB_GETTOPINDEX'),
    (Value: CB_SETTOPINDEX; Name: 'CB_SETTOPINDEX'),
    (Value: CB_GETHORIZONTALEXTENT; Name: 'CB_GETHORIZONTALEXTENT'),
    (Value: CB_SETHORIZONTALEXTENT; Name: 'CB_SETHORIZONTALEXTENT'),
    (Value: CB_GETDROPPEDWIDTH; Name: 'CB_GETDROPPEDWIDTH'),
    (Value: CB_SETDROPPEDWIDTH; Name: 'CB_SETDROPPEDWIDTH'),
    (Value: CB_INITSTORAGE; Name: 'CB_INITSTORAGE'),
    (Value: CB_MSGMAX; Name: 'CB_MSGMAX'),

    (Value: EN_SETFOCUS; Name: 'EN_SETFOCUS'),
    (Value: EN_CHANGE; Name: 'EN_CHANGE'),
    (Value: EN_UPDATE; Name: 'EN_UPDATE'),
    (Value: EN_ERRSPACE; Name: 'EN_ERRSPACE'),
    (Value: EN_MAXTEXT; Name: 'EN_MAXTEXT'),
    (Value: EN_HSCROLL; Name: 'EN_HSCROLL'),
    (Value: EN_VSCROLL; Name: 'EN_VSCROLL'),

    (Value: EM_GETSEL; Name: 'EM_GETSEL'),
    (Value: EM_SETSEL; Name: 'EM_SETSEL'),
    (Value: EM_GETRECT; Name: 'EM_GETRECT'),
    (Value: EM_SETRECT; Name: 'EM_SETRECT'),
    (Value: EM_SETRECTNP; Name: 'EM_SETRECTNP'),
    (Value: EM_SCROLL; Name: 'EM_SCROLL'),
    (Value: EM_LINESCROLL; Name: 'EM_LINESCROLL'),
    (Value: EM_SCROLLCARET; Name: 'EM_SCROLLCARET'),
    (Value: EM_GETMODIFY; Name: 'EM_GETMODIFY'),
    (Value: EM_SETMODIFY; Name: 'EM_SETMODIFY'),
    (Value: EM_GETLINECOUNT; Name: 'EM_GETLINECOUNT'),
    (Value: EM_LINEINDEX; Name: 'EM_LINEINDEX'),
    (Value: EM_SETHANDLE; Name: 'EM_SETHANDLE'),
    (Value: EM_GETHANDLE; Name: 'EM_GETHANDLE'),
    (Value: EM_GETTHUMB; Name: 'EM_GETTHUMB'),
    (Value: EM_LINELENGTH; Name: 'EM_LINELENGTH'),
    (Value: EM_REPLACESEL; Name: 'EM_REPLACESEL'),
    (Value: EM_GETLINE; Name: 'EM_GETLINE'),
    (Value: EM_LIMITTEXT; Name: 'EM_LIMITTEXT EM_SETLIMITTEXT'),
    (Value: EM_CANUNDO; Name: 'EM_CANUNDO'),
    (Value: EM_UNDO; Name: 'EM_UNDO'),
    (Value: EM_FMTLINES; Name: 'EM_FMTLINES'),
    (Value: EM_LINEFROMCHAR; Name: 'EM_LINEFROMCHAR'),
    (Value: EM_SETTABSTOPS; Name: 'EM_SETTABSTOPS'),
    (Value: EM_SETPASSWORDCHAR; Name: 'EM_SETPASSWORDCHAR'),
    (Value: EM_EMPTYUNDOBUFFER; Name: 'EM_EMPTYUNDOBUFFER'),
    (Value: EM_GETFIRSTVISIBLELINE; Name: 'EM_GETFIRSTVISIBLELINE'),
    (Value: EM_SETREADONLY; Name: 'EM_SETREADONLY'),
    (Value: EM_SETWORDBREAKPROC; Name: 'EM_SETWORDBREAKPROC'),
    (Value: EM_GETWORDBREAKPROC; Name: 'EM_GETWORDBREAKPROC'),
    (Value: EM_GETPASSWORDCHAR; Name: 'EM_GETPASSWORDCHAR'),
    (Value: EM_SETMARGINS; Name: 'EM_SETMARGINS'),
    (Value: EM_GETMARGINS; Name: 'EM_GETMARGINS'),
    (Value: EM_GETLIMITTEXT; Name: 'EM_GETLIMITTEXT'),
    (Value: EM_POSFROMCHAR; Name: 'EM_POSFROMCHAR'),
    (Value: EM_CHARFROMPOS; Name: 'EM_CHARFROMPOS'),

    (Value: SBM_SETPOS; Name: 'SBM_SETPOS'),
    (Value: SBM_GETPOS; Name: 'SBM_GETPOS'),
    (Value: SBM_SETRANGE; Name: 'SBM_SETRANGE'),
    (Value: SBM_SETRANGEREDRAW; Name: 'SBM_SETRANGEREDRAW'),
    (Value: SBM_GETRANGE; Name: 'SBM_GETRANGE'),
    (Value: SBM_ENABLE_ARROWS; Name: 'SBM_ENABLE_ARROWS'),
    (Value: SBM_SETSCROLLINFO; Name: 'SBM_SETSCROLLINFO'),
    (Value: SBM_GETSCROLLINFO; Name: 'SBM_GETSCROLLINFO'),

    (Value: DM_GETDEFID; Name: 'DM_GETDEFID'),
    (Value: DM_SETDEFID; Name: 'DM_SETDEFID'),
    (Value: DM_REPOSITION; Name: 'DM_REPOSITION'),

    (Value: PSM_PAGEINFO; Name: 'PSM_PAGEINFO'),
    (Value: PSM_SHEETINFO; Name: 'PSM_SHEETINFO'),

    //Delphi VCL messages
    (Value: CM_BASE; Name: 'CM_BASE'),
    (Value: CM_ACTIVATE; Name: 'CM_ACTIVATE'),
    (Value: CM_DEACTIVATE; Name: 'CM_DEACTIVATE'),
    (Value: CM_GOTFOCUS; Name: 'CM_GOTFOCUS'),
    (Value: CM_LOSTFOCUS; Name: 'CM_LOSTFOCUS'),
    (Value: CM_CANCELMODE; Name: 'CM_CANCELMODE'),
    (Value: CM_DIALOGKEY; Name: 'CM_DIALOGKEY'),
    (Value: CM_DIALOGCHAR; Name: 'CM_DIALOGCHAR'),
    (Value: CM_FOCUSCHANGED; Name: 'CM_FOCUSCHANGED'),
    (Value: CM_PARENTFONTCHANGED; Name: 'CM_PARENTFONTCHANGED'),
    (Value: CM_PARENTCOLORCHANGED; Name: 'CM_PARENTCOLORCHANGED'),
    (Value: CM_HITTEST; Name: 'CM_HITTEST'),
    (Value: CM_VISIBLECHANGED; Name: 'CM_VISIBLECHANGED'),
    (Value: CM_ENABLEDCHANGED; Name: 'CM_ENABLEDCHANGED'),
    (Value: CM_COLORCHANGED; Name: 'CM_COLORCHANGED'),
    (Value: CM_FONTCHANGED; Name: 'CM_FONTCHANGED'),
    (Value: CM_CURSORCHANGED; Name: 'CM_CURSORCHANGED'),
    (Value: CM_CTL3DCHANGED; Name: 'CM_CTL3DCHANGED'),
    (Value: CM_PARENTCTL3DCHANGED; Name: 'CM_PARENTCTL3DCHANGED'),
    (Value: CM_TEXTCHANGED; Name: 'CM_TEXTCHANGED'),
    (Value: CM_MOUSEENTER; Name: 'CM_MOUSEENTER'),
    (Value: CM_MOUSELEAVE; Name: 'CM_MOUSELEAVE'),
    (Value: CM_MENUCHANGED; Name: 'CM_MENUCHANGED'),
    (Value: CM_APPKEYDOWN; Name: 'CM_APPKEYDOWN'),
    (Value: CM_APPSYSCOMMAND; Name: 'CM_APPSYSCOMMAND'),
    (Value: CM_BUTTONPRESSED; Name: 'CM_BUTTONPRESSED'),
    (Value: CM_SHOWINGCHANGED; Name: 'CM_SHOWINGCHANGED'),
    (Value: CM_ENTER; Name: 'CM_ENTER'),
    (Value: CM_EXIT; Name: 'CM_EXIT'),
    (Value: CM_DESIGNHITTEST; Name: 'CM_DESIGNHITTEST'),
    (Value: CM_ICONCHANGED; Name: 'CM_ICONCHANGED'),
    (Value: CM_WANTSPECIALKEY; Name: 'CM_WANTSPECIALKEY'),
    (Value: CM_INVOKEHELP; Name: 'CM_INVOKEHELP'),
    (Value: CM_WINDOWHOOK; Name: 'CM_WINDOWHOOK'),
    (Value: CM_RELEASE; Name: 'CM_RELEASE'),
    (Value: CM_SHOWHINTCHANGED; Name: 'CM_SHOWHINTCHANGED'),
    (Value: CM_PARENTSHOWHINTCHANGED; Name: 'CM_PARENTSHOWHINTCHANGED'),
    (Value: CM_SYSCOLORCHANGE; Name: 'CM_SYSCOLORCHANGE'),
    (Value: CM_WININICHANGE; Name: 'CM_WININICHANGE'),
    (Value: CM_FONTCHANGE; Name: 'CM_FONTCHANGE'),
    (Value: CM_TIMECHANGE; Name: 'CM_TIMECHANGE'),
    (Value: CM_TABSTOPCHANGED; Name: 'CM_TABSTOPCHANGED'),
    (Value: CM_UIACTIVATE; Name: 'CM_UIACTIVATE'),
    (Value: CM_UIDEACTIVATE; Name: 'CM_UIDEACTIVATE'),
    (Value: CM_DOCWINDOWACTIVATE; Name: 'CM_DOCWINDOWACTIVATE'),
    (Value: CM_CONTROLLISTCHANGE; Name: 'CM_CONTROLLISTCHANGE'),
    (Value: CM_GETDATALINK; Name: 'CM_GETDATALINK'),
    (Value: CM_CHILDKEY; Name: 'CM_CHILDKEY'),
    (Value: CM_DRAG; Name: 'CM_DRAG'),
    (Value: CM_HINTSHOW; Name: 'CM_HINTSHOW'),
    (Value: CM_DIALOGHANDLE; Name: 'CM_DIALOGHANDLE'),
    (Value: CM_ISTOOLCONTROL; Name: 'CM_ISTOOLCONTROL'),
    (Value: CM_RECREATEWND; Name: 'CM_RECREATEWND'),
    (Value: CM_INVALIDATE; Name: 'CM_INVALIDATE'),
    (Value: CM_SYSFONTCHANGED; Name: 'CM_SYSFONTCHANGED'),
    (Value: CM_CONTROLCHANGE; Name: 'CM_CONTROLCHANGE'),
    (Value: CM_CHANGED; Name: 'CM_CHANGED'),

    (Value: CN_BASE; Name: 'CN_BASE'),
    (Value: CN_CHARTOITEM; Name: 'CN_CHARTOITEM'),
    (Value: CN_COMMAND; Name: 'CN_COMMAND'),
    (Value: CN_COMPAREITEM; Name: 'CN_COMPAREITEM'),
    (Value: CN_CTLCOLORBTN; Name: 'CN_CTLCOLORBTN'),
    (Value: CN_CTLCOLORDLG; Name: 'CN_CTLCOLORDLG'),
    (Value: CN_CTLCOLOREDIT; Name: 'CN_CTLCOLOREDIT'),
    (Value: CN_CTLCOLORLISTBOX; Name: 'CN_CTLCOLORLISTBOX'),
    (Value: CN_CTLCOLORMSGBOX; Name: 'CN_CTLCOLORMSGBOX'),
    (Value: CN_CTLCOLORSCROLLBAR; Name: 'CN_CTLCOLORSCROLLBAR'),
    (Value: CN_CTLCOLORSTATIC; Name: 'CN_CTLCOLORSTATIC'),
    (Value: CN_DELETEITEM; Name: 'CN_DELETEITEM'),
    (Value: CN_DRAWITEM; Name: 'CN_DRAWITEM'),
    (Value: CN_HSCROLL; Name: 'CN_HSCROLL'),
    (Value: CN_MEASUREITEM; Name: 'CN_MEASUREITEM'),
    (Value: CN_PARENTNOTIFY; Name: 'CN_PARENTNOTIFY'),
    (Value: CN_VKEYTOITEM; Name: 'CN_VKEYTOITEM'),
    (Value: CN_VSCROLL; Name: 'CN_VSCROLL'),
    (Value: CN_KEYDOWN; Name: 'CN_KEYDOWN'),
    (Value: CN_KEYUP; Name: 'CN_KEYUP'),
    (Value: CN_CHAR; Name: 'CN_CHAR'),
    (Value: CN_SYSKEYDOWN; Name: 'CN_SYSKEYDOWN'),
    (Value: CN_SYSCHAR; Name: 'CN_SYSCHAR'),
    (Value: CN_NOTIFY; Name: 'CN_NOTIFY'));

var
  TracerForm: TTracerForm;

implementation

{$R *.DFM}

const
  UM_ALWAYSONTOP = WM_USER+101;

procedure TTracerForm.HookedControlChange(Sender: TObject);
begin
  if Assigned(Sender) then
    Caption := 'Messages for [' + TComponent(Sender).Name + ']' else
      Caption := 'Messages for []';

  btnClearClick(Self);
  btnStopClick(Self);  
  SetButtons;
end;

procedure TTracerForm.BeforeMessage(Sender: TObject; var Msg: TMessage; var Handled: Boolean);
begin
  Handled := False;
end;

procedure TTracerForm.AfterMessage(Sender: TObject; var Msg: TMessage; var Handled: Boolean);
var
  S: string;
begin
  with MsgGrid, Msg do
  begin
    IntToIdent(Msg, S, WindowsMessages);

    if ((S  = '') or (DstList.Items.IndexOf(S) <> -1)) and not FStoped then
    begin
      if S = '' then
        S := IntToStr(Msg);

      if FOnce then
        if RowCount < 999 then
          RowCount := RowCount + 1 else
            RowCount := 2 else
              FOnce := True;

      Cells[0, RowCount-1] := IntToStr(RowCount-1);
      Cells[1, RowCount-1] := S;
      Cells[2, RowCount-1] := IntToStr(wParam);
      Cells[3, RowCount-1] := IntToStr(lParam);
      Cells[4, RowCount-1] := IntToStr(Result);

      Row := RowCount-1;

      Fonts[1, RowCount-1].Color := clHighlight;
      Fonts[4, RowCount-1].Color := clHighlight;
      Colors[1, RowCount-1] := $00DEFFFF;
      Colors[4, RowCount-1] := $00DEFFFF;
    end;
  end;
end;

procedure TTracerForm.MsgGridDrawCell(Sender: TObject; Col, Row: Integer;
  Recty: TRect; State: TGridDrawState);
begin
  if (Row = 0) and (Col <> 0) then
    with MsgGrid.Canvas, Recty do
    begin
      FillRect(Recty);

      if Col = 1 then
        Draw(Left+2, Top+2, ImageMsg.Picture.Bitmap) else
          Draw(Left+2, Top+2, ImageRest.Picture.Bitmap);

      SetTextColor(Handle, ColorToRGB(Font.Color));
      TextOut(Left+18, Top+2, MsgGrid.Cells[Col, Row]);
    end;
end;

procedure TTracerForm.IncludeBtnClick(Sender: TObject);
var
  Index: Integer;
begin
  SrcList.Items.BeginUpdate;
  try
    DstList.Items.BeginUpdate;
    try
      Index := GetFirstSelection(SrcList);
      MoveSelected(SrcList, DstList.Items);
      SetItem(SrcList, Index);
    finally
      DstList.Items.EndUpdate;
    end;
  finally
    SrcList.Items.EndUpdate;
  end;
end;

procedure TTracerForm.ExcludeBtnClick(Sender: TObject);
var
  Index: Integer;
begin
  SrcList.Items.BeginUpdate;
  try
    DstList.Items.BeginUpdate;
    try
      Index := GetFirstSelection(DstList);
      MoveSelected(DstList, SrcList.Items);
      SetItem(DstList, Index);
    finally
      DstList.Items.EndUpdate;
    end;
  finally
    SrcList.Items.EndUpdate;
  end;
end;

procedure TTracerForm.IncAllBtnClick(Sender: TObject);
var
  I: Integer;
begin
  SrcList.Items.BeginUpdate;
  try
    DstList.Items.BeginUpdate;
    try
      for I := 0 to SrcList.Items.Count - 1 do
        DstList.Items.AddObject(SrcList.Items[I], SrcList.Items.Objects[I]);
      SrcList.Items.Clear;
      SetItem(SrcList, 0);
    finally
      DstList.Items.EndUpdate;
    end;
  finally
    SrcList.Items.EndUpdate;
  end;
end;

procedure TTracerForm.ExAllBtnClick(Sender: TObject);
var
  I: Integer;
begin
  SrcList.Items.BeginUpdate;
  try
    DstList.Items.BeginUpdate;
    try
      for I := 0 to DstList.Items.Count - 1 do
        SrcList.Items.AddObject(DstList.Items[I], DstList.Items.Objects[I]);
      DstList.Items.Clear;
      SetItem(DstList, 0);
    finally
      DstList.Items.EndUpdate;
    end;
  finally
    SrcList.Items.EndUpdate;
  end;
end;

procedure TTracerForm.MoveSelected(List: TCustomListBox; Items: TStrings);
var
  I: Integer;
begin
  for I := List.Items.Count - 1 downto 0 do
    if List.Selected[I] then
    begin
      Items.AddObject(List.Items[I], List.Items.Objects[I]);
      List.Items.Delete(I);
    end;
end;

procedure TTracerForm.SetButtons;
var
  SrcEmpty, DstEmpty: Boolean;
begin
  SrcEmpty := SrcList.Items.Count = 0;
  DstEmpty := DstList.Items.Count = 0;
  IncludeBtn.Enabled := not SrcEmpty;
  IncAllBtn.Enabled := not SrcEmpty;
  ExcludeBtn.Enabled := not DstEmpty;
  ExAllBtn.Enabled := not DstEmpty;
  BtnStart.Enabled := FStoped and Assigned(FWndHook.WinControl);
  BtnStop.Enabled := not FStoped and Assigned(FWndHook.WinControl);
end;

function TTracerForm.GetFirstSelection(List: TCustomListBox): Integer;
begin
  for Result := 0 to List.Items.Count - 1 do
    if List.Selected[Result] then
      Exit;
  Result := LB_ERR;
end;

procedure TTracerForm.SetItem(List: TCustomListBox; Index: Integer);
var
  MaxIndex: Integer;
begin
  with List do
  begin
    SetFocus;
    MaxIndex := List.Items.Count - 1;

    if Index = LB_ERR then
      Index := 0 else
        if Index > MaxIndex then
          Index := MaxIndex;

    Selected[Index] := True;
  end;
  SetButtons;
end;

procedure TTracerForm.btnStartClick(Sender: TObject);
begin
  FWndHook.Active := True;
  FStoped := False;
  SetButtons;
end;

procedure TTracerForm.btnStopClick(Sender: TObject);
begin
  FWndHook.Active := False;
  FStoped := True;
  SetButtons;
end;

procedure TTracerForm.btnClearClick(Sender: TObject);
begin
  with MsgGrid do
  begin
    RowCount := 2;
    Cells[0, 1] := '';
    Cells[1, 1] := '';
    Cells[2, 1] := '';
    Cells[3, 1] := '';
    Cells[4, 1] := '';
    Cells[5, 1] := '';
  end;
  FOnce := False;
end;

procedure TTracerForm.btnCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TTracerForm.Copymessage1Click(Sender: TObject);
begin
  MsgGrid.CopyToClipboardCell(1, MsgGrid.Row);
end;

procedure TTracerForm.CopywParam1Click(Sender: TObject);
begin
  MsgGrid.CopyToClipboardCell(2, MsgGrid.Row);
end;

procedure TTracerForm.CopylParam1Click(Sender: TObject);
begin
  MsgGrid.CopyToClipboardCell(3, MsgGrid.Row);
end;

procedure TTracerForm.CopyResult1Click(Sender: TObject);
begin
  MsgGrid.CopyToClipboardCell(4, MsgGrid.Row);
end;

procedure TTracerForm.btnDownClick(Sender: TObject);
begin
  Height := 500;
  btnDown.Enabled := False;
  btnUp.Enabled := True;
end;

procedure TTracerForm.btnUpClick(Sender: TObject);
begin
  Height := 210;
  btnDown.Enabled := True;
  btnUp.Enabled := False;
end;

procedure TTracerForm.btnSaveClick(Sender: TObject);
var
  MessagesList: TStringList;
begin
  if SaveDialog.Execute then
  begin
    MessagesList := TStringList.Create;
    try
      MessagesList.AddStrings(SrcList.Items);
      MessagesList.Add('^');
      MessagesList.AddStrings(DstList.Items);
      MessagesList.SaveToFile(SaveDialog.FileName);
      MessageBeep(MB_OK);
    finally
      MessagesList.Free;
    end;
  end;
end;

procedure TTracerForm.btnOpenClick(Sender: TObject);
var
  MessagesList: TStringList;
  I: Integer;
  Switch: Boolean;
begin
  if OpenDialog.Execute then
  begin
    Screen.Cursor := crHourGlass;
    try
      SrcList.Clear;
      DstList.Clear;

      SrcList.Items.BeginUpdate;
      try
        DstList.Items.BeginUpdate;
        try
          MessagesList := TStringList.Create;
          try
            Switch := False;
            MessagesList.LoadFromFile(OpenDialog.FileName);

            for I := 0 to MessagesList.Count-1 do
              if not Switch then
              begin
                Switch := MessagesList[I] = '^';
                if Switch then Continue;
                SrcList.Items.Add(MessagesList[I]);
              end else
                DstList.Items.Add(MessagesList[I]);
        finally
          MessagesList.Free;
        end;
      finally
        DstList.Items.EndUpdate;
      end;
    finally
      SrcList.Items.EndUpdate;
    end;

      SetButtons;
      MessageBeep(MB_OK);
    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

constructor TTracerForm.Create(AOwner: TComponent);
var
  S: string;
  I: Integer;
begin
  inherited Create(AOwner);
  FWndHook := TWndHook.Create(Self);
  FWndHook.OnBeforeMessage := BeforeMessage;
  FWndHook.OnAfterMessage := AfterMessage;
  FWndHook.OnChangeWinControl := HookedControlChange;

  FTopMost := False;
  AppendMenu(GetSystemMenu(Handle, False), MF_SEPARATOR, 0, '');
  AppendMenu(GetSystemMenu(Handle, False), MF_STRING, UM_ALWAYSONTOP, '&Always on Top');

  with MsgGrid do
  begin
    ColWidths[0] := 25;
    ColWidths[1] := 165;
    ColWidths[2] := 90;
    ColWidths[3] := 90;
    ColWidths[4] := 90;

    Cells[1, 0] := 'Message';
    Cells[2, 0] := 'wParam';
    Cells[3, 0] := 'lParam';
    Cells[4, 0] := 'Result';
  end;

  with SrcList.Items do
  begin
    BeginUpdate;
    for I := Low(WindowsMessages) to High(WindowsMessages) do
    begin
      IntToIdent(WindowsMessages[I].Value, S, WindowsMessages);
      Add(S);
    end;
    EndUpdate;
  end;

  if Screen.Fonts.IndexOf('Tahoma') <> -1 then
    Font.Name := 'Tahoma';

  DstList.Font.Color := clHighlight;

  FOnce := False;
  FStoped := True;
  SetButtons;
end;

destructor TTracerForm.Destroy;
begin
  if Assigned(FWndHook) then
    FWndHook.Free;

  TracerForm := nil;
  inherited Destroy;
end;

procedure TTracerForm.SetTopMost(Value: Boolean);
const
  Topmosts: array[Boolean] of THandle = (HWND_NOTOPMOST, HWND_TOPMOST);
  Checks: array[Boolean] of Integer = (0, MF_CHECKED);
begin
  if FTopMost <> Value then
  begin
    FTopMost := Value;

    if IsWindowVisible(Handle) then
      SetWindowPos(Handle, Topmosts[FTopMost], 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);

    ModifyMenu(GetSystemMenu(Handle, False), UM_ALWAYSONTOP, MF_BYCOMMAND or MF_STRING or Checks[FTopMost], UM_ALWAYSONTOP, '&Always on Top');
  end;
end;

procedure TTracerForm.WMSysCommand(var Msg: TWMSysCommand);
begin
  inherited;
  if Msg.CmdType = UM_ALWAYSONTOP then
    TopMost := not TopMost;
end;

procedure TTracerForm.FormActivate(Sender: TObject);
const
  Topmosts: array[Boolean] of THandle = (HWND_NOTOPMOST, HWND_TOPMOST);
begin
  SetWindowPos(Handle, Topmosts[FTopMost], 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);
end;

end.
