DelphiFeeds.com

  • Dashboard
  • Popular Stories
  • Trending Stories
  • Feeds
  • Login
Trending now

Incredibly Powerful Enhanced Terminal And Network Toolkit For Windows Is Built In Delphi

Learn How To Use C++ Explicit Virtual Overrides In Windows Development

Learn Python With The 5 Best Python GUI Libraries Video

Trial – TMS VCL WebGMaps

Trial – TMS VCL UI Pack

Trial – TMS VCL Cloud Pack

Trial – TMS VCL Chart

Trial – TMS Scripter

Brook Framework

Get These Visually Stunning FireMonkey Styles Free To Enhance User Experience In Your Delphi Apps

VCL Styles: Master The Secrets Of Beautiful Modern Apps In Windows 10

grep for Delphi .dproj file containing copy commands for certain DLLs

Powerful Cross Platform Multitrack Music Recording Software Built In Delphi FireMonkey

Ultra-Fast Enterprise-Grade List And Label Reporting Tool For Delphi

Learn An Efficient Way to Use C++ Extern Templates For Robust Windows Development

Receive Windows Messages In Your Custom Delphi Class – NonWindowed Control/Object

1
zarkogajic zarkogajic 5 months ago in Delphi, twincontrol, windows, windows api, windows messages 0

Windows messages are a key ingredient in communication between Windows and (your) application and also in communication between (two) applications.

Even without your knowledge Windows messages are being posted and handled by forms in your application.

For example, when the user closes the form in your application, the WM_CLOSE message is sent to the window/form and the form gets closed (if you do not react programmatically).

For an application to receive a Window message, the application must provide a *window* a message will be sent to. In normal situation this window is the (main) form in your application. You write a procedure to handle a specific message, like WM_NCHitTest, and you are done.

BUT, what if you do NOT have a window to receive a message? What if you want to handle messages in your custom class derived from TObject?

Handle Windows Messages in TMyObject = class(TObject)

A Delphi control that has a window handle (derives from TWinControl) can receive Windows messages. The TObject does not expose a window handle, and therefore any of your custom classes (deriving from TObject) cannot receive and handle Windows messages, at least not “by default”.

To enable your custom class to receive Windows messages you must provide a window handle to the message sender.

The trick is in using the following methods (defined in classes.pas – therefore straightforward to use):

  • AllocateHWnd(WndMethod : TWndMethod). AllocateHWnd is used to create a window that is not associated with a windowed control.
  • The WndMethod : TWndMethod specifies the window procedure that the generated window uses to respond to messages.
  • DeallocateHWnd. DeallocateHWnd destroys window that was created using the AllocateHWnd function.

The TMsgReceiver skeleton below is a custom class derived from TObject capable of receiving and handling Windows messages.

interface

TMsgReceiver = class(TObject)
private
  fMsgHandlerHWND : HWND;
  procedure WndMethod(var Msg: TMessage);
public
  constructor Create;
  destructor Destroy; override;
end;

implementation

constructor TMsgReceiver.Create;
begin
  inherited Create;

  fMsgHandlerHWND := AllocateHWnd(WndMethod);
end;

destructor TMsgReceiver.Destroy;
begin
  DeallocateHWnd(fMsgHandlerHWND);
  inherited;
end;

procedure TMsgReceiver.WndMethod(var Msg: TMessage);
begin
  if Msg.Msg = WM_MY_UNIQUE_MESSAGE then
  begin
    //do something
  end
  else
    Msg.Result := DefWindowProc(fMsgHandlerHWND, Msg.Msg, Msg.wParam, Msg.lParam);
end

In the WndMethod procedure (the window procedure for the hidden window) you handle all the messages you are interested in. For all other messages a call to DefWindowProc is needed to ensure default processing for any messages that your code does not process.

Handle a Message From Another Application

With the above skeleton, you can now handle messages sent from other applications.

Suppose some application registers Windows message using RegisterWindowMessage API call. The RegisterWindowMessage function is typically used to register messages for communicating between two cooperating applications.

The “sending” application would have a line like:

WM_MY_APP_MESSAGE := RegisterWindowMessage('MSG_MY_APP_MESSAGE');

Where WM_MY_APP_MESSAGE is a cardinal value field used when posting the message to (all) windows.

The WM_MY_UNIQUE_MESSAGE also needs to be registered in TMsgReceiver using the same “’MSG_MY_APP_MESSAGE’” – as if two different applications register the same message string, the applications return the same message value.

Let’s say we post this message in a form’s OnMouseDown event:

procedure TClickSendForm.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  PostMessage(HWND_BROADCAST, WM_MY_APP_MESSAGE, x, y);
end;

The HWND_BROADCAST parameter ensures that our WM_MY_APP_MESSAGE is posted to all top-level windows in the system, including disabled or
invisible unowned windows, overlapped windows, and pop-up windows AND our TMsgReceiver hidden window.

To handle the message in the TMsgReceiver instance have the WndMethod as:

procedure TMsgReceiver.WndMethod(var Msg: TMessage);
begin
  if Msg.Msg = WM_MY_UNIQUE_MESSAGE then
  begin
    Point.X := Msg.LParam;
    Point.Y := Msg.WParam;
    // just to have some "output"
    Windows.Beep(Point.X, Point.Y);
  end
  else
    Msg.Result := DefWindowProc(fMsgHandlerHWND, Msg.Msg, Msg.wParam, Msg.lParam);
end;

The “Point” is a field in the TMsgReceiver. And there you have it – TMsgReceiver receiving where the user has clicked on the form in some other application.

And that’s it.

Trending Stories

  • Incredibly Powerful Enhanced Terminal And Network Toolkit For Windows Is...

  • Learn How To Use C++ Explicit Virtual Overrides In Windows...

  • Learn Python With The 5 Best Python GUI Libraries Video

  • Trial – TMS VCL WebGMaps

  • Trial – TMS VCL UI Pack

Embarcadero GetIt

  • Brook Framework

    Microframework which helps to develop web Pascal applications.

  • Trial - TMS Scripter

    Add the ultimate flexibility and power into your apps with native Pascal or Basic scripting and […]

  • Trial - TMS VCL Chart

    DB-aware and non DB-aware feature-rich charting components for business, statistical, financial […]

  • Trial - TMS VCL Cloud Pack

    TMS VCL Cloud Pack is a Delphi and C++Builder component library to seamlessly use all major cloud […]

  • Trial - TMS VCL UI Pack

    Create modern-looking & feature-rich Windows applications faster with well over 600 components […]

  • Learn Delphi Programming
  • Learn C++
  • Embarcadero Blogs
  • BeginEnd.net
  • Python GUI
  • Firebird News
  • Torry’s Delphi Pages
Copyright DelphiFeeds.com 2021. All Rights Reserved
Embarcadero
Login Register

Login

Lost Password

Register

Lost Password