?
Solved

Translate message numbers into message text names

Posted on 2008-10-17
3
Medium Priority
?
1,378 Views
Last Modified: 2012-06-22
Windows sends a lot of window messages , now and then people need to figure out what the "names" of these numbers are

When you process a WndProc of a windowed control a lot message pass through this control

I was wondering i anybody had a translation for the TMessage.Msg number to the text version ?

i could then do something like in below code
  aMessageName := TranslatedMsg(Msg.Msg);


procedure TEdit.AddMsg(aMsg: string; Msg: TMessage);
var aMessageName: string;
begin
  if fEnabledLog then
    if Assigned(Owner) and (Owner <> nil) and (Owner is TForm1) then
    begin
      // aMessageName := TranslatedMsg(Msg.Msg);
      TForm1(Owner).Memo1.Lines.Add(Format('X: %s, Msg: %d, MsgName: %s, WParam: %d, LParam: %d, Result: %d',
        [aMsg, Msg.Msg, Msg.WParam, Msg.LParam, Msg.Result]));
    end;
end;
 
procedure TEdit.WndProc(var Message: TMessage);
begin
  AddMsg('WndProc start', Message);
  inherited;
  AddMsg('WndProc end', Message);
end;

Open in new window

0
Comment
Question by:Geert Gruwez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Expert Comment

by:ahalya
ID: 22741395
I don't think there is a built in translation table to give you the names.

But, you can easily set up a function using a case statement.  (You'll find most of the definitions in Messages.pas).

case msgID of
  $0000: result = 'WM_NULL     ';
  $0001: result = 'WM_CREATE   ';
  $0002: result = 'WM_DESTROY  ';
  $0003: result = 'WM_MOVE     ';
  $0005: result = 'WM_SIZE     ';
  $0006: result = 'WM_ACTIVATE ';
  $0007: result = 'WM_SETFOCUS ';
etc..
0
 
LVL 28

Accepted Solution

by:
2266180 earned 2000 total points
ID: 22741532
I wrote one. autogenerating so that it can be used in all delphi versions. might require some modificaiotn for other delphi versions than 7, which is what I tested it on. this code will generate the unit named winmessages which contains the translate function which is what you want.

first compile and run. then comment the generateunit define and compile and run.
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
{$DEFINE GENERATEUNIT}// comment this line after generation
 
{$IFNDEF GENERATEUNIT}
uses winmessages;
{$ELSE}
 
procedure generate(messages:string);// path to messages.pas
var l,t,vv:tstringlist;
    i,j,k,cnt:integer;
    s, n, v:string;
begin
  l:=tstringlist.create;
  try
    l.loadfromfile(messages);
    t:=tstringlist.create;
    try
      i:=1;
      while (i<=l.count) do
      begin
        if trim(l[i])='{ Window Messages }' then// NOTE: change this if needed
          break;
        inc(i);
      end;
      t.add('unit WinMessages;');
      t.add('');
      t.add('{This is a generated file by Ciuly (http://www.ciuly.com)}');
      t.add('');
      t.add('interface');
      t.add('');
      t.add('function translate(wm:cardinal):string;');
      t.add('');
      t.add('implementation');
      t.add('');
      t.add('type');
      t.add('  TMessageName=record');
      t.add('    name:string;');
      t.add('    value:cardinal;');
      t.add('  end;');
      t.add('');
      t.add('const');
      t.add('line 18');
      vv:=TStringList.create;
      try
        cnt:=0;
        while (i<=l.count) and (trim(l[i])<>'{ Button Notification Codes }') do// NOTE: change this limit if needed
        begin
          s:=l[i];
          j:=pos('=', s);
          if j>0 then
          begin
            n:=trim(copy(s, 1, j-1));
            v:=trim(copy(s, j+1, length(s)));
            delete(v, pos(';', v), length(v));
            k:=pos('+', v);
            if k=0 then
            begin
              try
                v:=inttostr(strtoint(v));// this will error if something is wrong
              except
                v:=vv.Values[v];
              end;
            end              else
            begin
              v:=inttostr(strtoint(vv.Values[trim(copy(v, 1, k-1))])+strtoint(trim(copy(v, k+1, length(v)))));
            end;
            vv.Values[n]:=v;
            t.add('    (name:'''+n+'''; value:'+v+'),');
            inc(cnt);
          end;
          inc(i);
        end;
      finally
        freeandnil(vv);
      end;
      t[17]:='  messages:array[0..'+inttostr(cnt-1)+'] of TMessageName=(';
      t[t.count-1]:=copy(t[t.count-1], 1, length(t[t.count-1])-1);
      t.add('  );');
      t.add('');
      t.add('function translate(wm:cardinal):string;');
      t.add('var i:integer;');
      t.add('begin');
      t.add('  i:=0;');
      t.add('  while (i<length(messages)) and (messages[i].value<>wm) do');
      t.add('    inc(i);');
      t.add('  if i<length(messages) then result:=messages[i].name');
      t.add('                        else result:='''';');
      t.add('end;');
      t.add('');
      t.add('end.');
      t.savetofile('winmessages.pas');
    finally
      freeandnil(t);
    end;
  finally
    freeandnil(L);
  end;
end;
 
{$ENDIF}
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  {$IFDEF GENERATEUNIT}
  generate('c:\Program Files\Borland\Delphi7\Source\Rtl\Win\Messages.pas');// NOTE: modify this if needed
  {$ELSE}
  showmessage(translate(WM_DDE_FIRST));
  {$ENDIF}
end;
 
end.

Open in new window

0
 
LVL 38

Author Comment

by:Geert Gruwez
ID: 22741883
ciuly,
cool man, didn't think about this approach

it was allready weekend for 1 minute so i wasn't paid to use my brains anymore

thx
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question