TMessage and Delphi

Hi All,

I've been trying to understand Windows messaging from MSDN and Delphi help file but unfortunately I can't seem to get the idea.

So can someone please briefly explain to me in human language Windows messaging (+) provide me with a small example on how I can make the simplest internal messaging system inside my application that will send an internal message in my application once an event is fired.

Thanks in advance for your help
GiantMatrixAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

A. Cristian CsikiSenior System AdministratorCommented:
var result:integer;
...
Result := MessageBox(Handle, 'hello world!', 'Regards!', 64);
...
0
GiantMatrixAuthor Commented:
Ah, no guys I do not mean MessageBox, I mean Windows messages (WM_...etc) :)

Regards
0
ZhaawZSoftware DeveloperCommented:
Hmm.. Do you want to write whole app based on windows messages? Or you want to add message processing to a project that uses Delphi default objects (like TForm, TButton etc)?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

ZhaawZSoftware DeveloperCommented:
If you want to use plain windows API to create an app, here's the beginning - creating simple empty form ;)



program project;

uses
  windows, messages;

var
  wcls : wndClass;
  form : cardinal;
  msg  : tagMsg;

function WndProc (wnd, msg : cardinal; wParam, lParam : integer) : integer; stdcall;
begin
case msg of
  wm_close : PostQuitMessage(0);
end;
result := DefWindowProc(wnd, msg, wParam, lParam);
end;

begin
with wcls do begin
  style         := cs_parentdc;
  lpfnWndProc   := @WndProc;
  cbClsExtra    := 0;
  cbWndExtra    := 0;
  hInstance     := sysInit.hInstance;
  hIcon         := LoadIcon(0, idi_application);
  hCursor       := LoadCursor(0, idc_arrow);
  hbrBackground := color_btnface + 1;
  lpszMenuName  := nil;
  lpszClassName := 'empty form';
end;
RegisterClass(wcls);

form := CreateWindowEx(
  ws_ex_topmost or ws_ex_toolwindow or ws_ex_appwindow,
  wcls.lpszClassName, 'tutorial nr. 001 - empty form',
  ws_visible or ws_caption or ws_sizebox or ws_sysmenu,
  150, 100, 400, 300,
  0, 0, hInstance, nil
);

while GetMessage(msg, 0, 0, 0) do begin
  DispatchMessage(msg);
end;

DestroyWindow(form);
UnregisterClass(wcls.lpszClassName, wcls.hInstance);
end.
0
ZhaawZSoftware DeveloperCommented:
P.S.(1) If you want to learn WinAPI, I advise you to download Platform SDK from www.microsoft.com - it's free software. You'll need only "help" from it (it's about 200MB).
P.S.(2) You may contact me in Skype (nick: delphists) if you need some help with WinAPI from me.
0
GiantMatrixAuthor Commented:
Hi ZhaawZ,

No I need to understand how to add and use messages like example:

WM_REGENDSEARCH = WM_USER +100;

procedure Procedure100(var Message: TMessage);
procedure Procedure101(var Msg: TMessage); message WM_REGENDSEARCH;

Thanks in advance for your help and time
0
ZhaawZSoftware DeveloperCommented:
unit Unit1;

interface

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

// added manually
const
  CustomMessage_RegEndSearch = wm_user + 100;

type
  TForm1 = class(TForm)
    Button1: TButton;
    // added manually
    procedure ProcessMyMsg (var msg : TMessage); message CustomMessage_RegEndSearch;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// added manually
procedure TForm1.ProcessMyMsg(var msg : TMessage);
begin
// do something
ShowMessage('received CustomMessage_RegEndSearch message');
ShowMessage(IntToStr(msg.wParam));
ShowMessage(pchar(msg.lParam));
// set result
msg.result := 1232;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  res : integer;
begin
// send the message to form, get the result
res := SendMessage({Form1.}Handle, CustomMessage_RegEndSearch, 19938, integer(pchar('blah..')));
ShowMessageFmt('result of message is %d', [res]);
end;

end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GiantMatrixAuthor Commented:
ZhaawZ,

Thanks a lot for your help and time. :)  Now I have a couple of questions on your source code so that I can better understand Windows messages:

** In const
  CustomMessage_RegEndSearch = wm_user + 100;

- Why +100? I mean what do 100 refer to?
- What are the other types of Windows messages (other than wm_user)?

Thanks in advance for your time
0
GiantMatrixAuthor Commented:
ZhaawZ,

No need to reply for my last questions, I've found the answer in
http://www.cryer.co.uk/brian/delphi/howto_send_custom_window_message.htm

Thank you for your help and time

Regards
0
ZhaawZSoftware DeveloperCommented:
You may get info about messages in MSDN (msdn.microsoft.com or downloadable Platform SDK help).

Other messages (not wm_user) are sent when some action occurs - window creating / moving / sizing, typing something in EDIT control, pressing mouse button, releasing it, changing display modes etc - all these (and also other) actions cause receiving different messages (not only WM_*, but also BM_* for buttons, LVM_* for list views, EM_* for edits etc). Some messages are used to let the control do something, for examle, LB_SETCURSEL sets selection in list box.

I think the best way to understand this messaging system is to start making apps by using WinAPI (not Delphi stuff like TForm, TButton, TEdit etc).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.