Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

TMessage and Delphi

Posted on 2006-04-23
10
Medium Priority
?
3,847 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:GiantMatrix
  • 5
  • 4
10 Comments
 
LVL 11

Expert Comment

by:A. Cristian Csiki
ID: 16519217
var result:integer;
...
Result := MessageBox(Handle, 'hello world!', 'Regards!', 64);
...
0
 

Author Comment

by:GiantMatrix
ID: 16519250
Ah, no guys I do not mean MessageBox, I mean Windows messages (WM_...etc) :)

Regards
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 16519891
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:ZhaawZ
ID: 16519915
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 16519924
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
 

Author Comment

by:GiantMatrix
ID: 16520009
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
 
LVL 11

Accepted Solution

by:
ZhaawZ earned 2000 total points
ID: 16520263
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
 

Author Comment

by:GiantMatrix
ID: 16520901
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
 

Author Comment

by:GiantMatrix
ID: 16521125
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 16522940
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Integration Management Part 2
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

569 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