[CLOSED] How to capture messages sent to the parent form of a TComponent lazarus / 31-LCL ---------------------------------------------------------------------- pcurtis · 2021-10-14 17:08 Can a component derived from TComponent receive messages? Answered, subject changed. How to capture user messages sent to the parent form (from the system) of a TComponent and handle them in that component. Consider this small app Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,  mytrayapp, Windows; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private    procedure TrayMessage(var Msg: TMessage); message WM_ICONTRAY;  public   end; var  Form1: TForm1;  MyTray : TMyTrayApp; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  MyTray := TMyTrayApp.Create(Self);end; procedure TForm1.TrayMessage(var Msg: TMessage);begin  case Msg.lParam of      WM_LBUTTONDOWN : begin                       Show;                       end;      WM_RBUTTONDOWN : begin                         Hide;                       end;    end;end; end.  This works fine (complete project attached). I want to move the message handling to the component. How do I do that? I can make the component generate events so that's not an issue. PLEASE DO NOT SUGGEST TRAYICON. It has issues. ---------------------------------------------------------------------- Handoko · 2021-10-15 02:08 Yes, it can. This code below has no compile time error: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyComponent = class(TComponent)  private    procedure CMHitTest(var Message: TCMHittest); message CM_HITTEST;  end; implementation procedure TMyComponent.CMHitTest(var Message: TCMHittest);begin  // Do somethingend; ---------------------------------------------------------------------- pcurtis · 2021-10-15 05:00 Thanks - Ive changed the question ---------------------------------------------------------------------- Handoko · 2021-10-15 07:31 This code below does what you want. The message handling is handled inside the component but the message activation is activated on the main form. See line #19 and #39. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1;  {$mode objfpc}{$H+}  interface  uses   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,   mytrayapp, Windows;  type    { TForm1 }    TForm1 = class(TForm)     Button1: TButton;     procedure Button1Click(Sender: TObject);   private     procedure TrayMessage(var Msg: TMessage); message WM_ICONTRAY;   end;  var   Form1: TForm1;   MyTray : TMyTrayApp;  implementation  {$R *.lfm}  { TForm1 }  procedure TForm1.Button1Click(Sender: TObject); begin   MyTray := TMyTrayApp.Create(Self); end;  procedure TForm1.TrayMessage(var Msg: TMessage); begin   MyTray.TrayMessage(Msg);   Exit;    // Disable this function   case Msg.lParam of       WM_LBUTTONDOWN : begin                          Show;                        end;       WM_RBUTTONDOWN : begin                        Hide;                        end;     end; end;  end. The message handling in the component unit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyTrayApp.TrayMessage(var Msg: TMessage); begin   if not(Owner is TForm) then Exit;   case Msg.lParam of       WM_LBUTTONDOWN : begin                        (Owner as TForm).Show;                        end;       WM_RBUTTONDOWN : begin                          (Owner as TForm).Hide;                        end;     end; end; ---------------------------------------------------------------------- pcurtis · 2021-10-15 08:28 Close, but no cigar :-) It works, but it my copy of the project the message comes from the system. Not the program. The messages are a callback for the Shell_NotifyIconW function. ---------------------------------------------------------------------- jamie · 2021-10-15 11:00 use "Dispatch" of the class. That directly pumps a message through the component. if your code has a TCONTROL and up in it you can use the "Perform" etc I hope that was what you were asking for? Your header is a little miss leading comparing to your post responses. If you are looking to interview a message loop you can over ride the WinProc or at the system level change the destination window handler. ---------------------------------------------------------------------- pcurtis · 2021-10-15 12:38 I'm not sure what you mean ---------------------------------------------------------------------- jamie · 2021-10-15 13:53 If you have a component that stems from a TCONTROL you can use Perform(TheMessage, WParam, LParam); If you don't have a component that is based from a TCONTROL then you can directly send a message to a class via the Dispatch where all the messages end up anyways  MyCOmponent.Dispatch(TheMessageObect) The Message record must have the proper format ocourse. this will directly call the method assigned to that Message index. you most likely can use the TLmesasge for this. ---------------------------------------------------------------------- pcurtis · 2021-10-15 14:21 Fine, but I want to receive system messages in a control based on TComponent. I'm supposing I have to hook into the components parent forms message chain. The question is how. ---------------------------------------------------------------------- howardpc · 2021-10-15 15:00 There is a Windows-only example here which may be what you are looking for (or it may not). ---------------------------------------------------------------------- pcurtis · 2021-10-15 15:19 I'm looking. Thanks. ---------------------------------------------------------------------- Remy Lebeau · 2021-10-15 15:41 Quote from: pcurtis on October 15, 2021, 04:21:14 pmFine, but I want to receive system messages in a control based on TComponent. I'm supposing I have to hook into the components parent forms message chain. The question is how. If the parent Form is the one receiving the messages, then yes, you would need to use a hook.  You can have the component either: - subclass the parent Form's WindowProc property. - subclass the parent Form's HWND directly using SetWindowSubclass() or SetWindowLongPtr(GWL_WNDPROC).  See Subclassing Controls on MSDN. - use a Win32 message hook via SetWindowsHookEx(WH_CALLWNDPROC), looking for messages targeting the desired HWND. On the other hand, if the component is the one calling Shell_NotifyIcon() to begin with, then it should not be using someone else's HWND to receive system messages for the icon.  It should be creating its own HWND instead.  So, either derive the component from TWinControl, or just create a hidden HWND using CreateWindow/Ex() directly. ---------------------------------------------------------------------- pcurtis · 2021-10-15 16:28 @howardpc It works. Just one small unrelated problem. How do I call class procedures from function WndCallback. See lines 87 / 90? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit MyTrayApp; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,  StdCtrls, ComCtrls, Windows, ShellAPI, LazUTF8; const  uIDTrayIcon = 25;  WM_ICONTRAY = WM_USER + uIDTrayIcon; type   TNotifyIconDataW2 = record    cbSize: DWORD;    hWnd: HWND;    uID: UINT;    uFlags: UINT;    uCallbackMessage: UINT;    hIcon: HICON;    szTip: array [0..127] of WideChar;    dwState: DWORD;    dwStateMask: DWORD;    szInfo: array [0..255] of WideChar;    u: record         case longint of           0 : ( uTimeout : UINT );           1 : ( uVersion : UINT );          end;    szInfoTitle: array[0..63] of WideChar;    dwInfoFlags: DWORD;  end;    { TMyTestComponent }   TMyTrayApp = class(TComponent)  private    fNotifyIcon : TNotifyIconDataW2;  protected   public    constructor Create(AOwner : TComponent); override;    destructor Destroy; override;    function AddIcon : Boolean;    procedure HideMe;    procedure ShowMe;  published  end; procedure Register; var  fPrevWndProc : WndProc; implementation //{$R myselectdialog.res} function WideStrLCopy(dest, source: PWideChar; maxlen: SizeInt): PWideChar;var  counter: SizeInt;begin  counter := 0;   while (Source[counter] <> #0)  and (counter < MaxLen) do  begin    Dest[counter] := Source[counter];    Inc(counter);  end;   Dest[counter] := #0;  Result := Dest;end; function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam; lParam: LParam):LRESULT; stdcall;begin  if uMsg = WM_ICONTRAY then  begin    result := DefWindowProc(Ahwnd, uMsg, WParam, LParam);  //not sure about this one     case lParam of      WM_LBUTTONDOWN : begin                         // TMyTrayApp.HideMe;                     end;      WM_RBUTTONDOWN : begin                         // TMyTrayApp.ShowMe;                       end;    end;    exit;  end;  result := CallWindowProc(fPrevWndProc , Ahwnd, uMsg, WParam, LParam);end; constructor TMyTrayApp.Create(AOwner : TComponent);begin  inherited Create(AOwner);  if not (csDesigning in ComponentState) then    begin      fPrevWndProc := Windows.WNDPROC(SetWindowLongPtr((Self.Owner as TForm).Handle,                                   GWL_WNDPROC,                                   PtrUInt(@WndCallback)));      AddIcon;    end;end; destructor TMyTrayApp.Destroy;begin  Shell_NotifyIconW(NIM_DELETE, @fNotifyIcon);  inherited;end; procedure TMyTrayApp.HideMe;begin  (Self.Owner as TForm).Hide;end; procedure TMyTrayApp.ShowMe;begin  (Self.Owner as TForm).Show;end; function TMyTrayApp.AddIcon : Boolean;var  WideBuffer: widestring;begin  FillChar(fNotifyIcon, SizeOf(fNotifyIcon), 0);  fNotifyIcon.cbSize := SizeOf(fNotifyIcon);  fNotifyIcon.hWnd := (Self.Owner as TForm).Handle;  fNotifyIcon.uID := uIDTrayIcon;  fNotifyIcon.uFlags := NIF_MESSAGE or NIF_ICON;  fNotifyIcon.uFlags := fNotifyIcon.uFlags or NIF_TIP;  fNotifyIcon.uCallbackMessage := WM_USER + uIDTrayIcon;  fNotifyIcon.hIcon := Application.Icon.Handle;   WideBuffer := UTF8ToUTF16('TrayIconHint');  WideStrLCopy(@fNotifyIcon.szTip, PWideChar(WideBuffer), 127);   Result := Shell_NotifyIconW(NIM_ADD, @fNotifyIcon);end; procedure Register; begin  RegisterComponents('Misc',[TMyTrayApp]);end; end. ---------------------------------------------------------------------- Remy Lebeau · 2021-10-15 16:52 Quote from: pcurtis on October 15, 2021, 06:28:03 pm@howardpc It works. Perhaps, but from a design standpoint, you should NOT be using the parent Form's HWND in this situation.  Your Tray component should be creating its own HWND instead, so the OS can send messages directly to the component itself.  Look at TTimer as an example. It is a non- visual component that has to receive WM_TIMER messages from the OS.  You should do the same thing in your Tray component. ---------------------------------------------------------------------- pcurtis · 2021-10-15 17:11 fNotifyIcon needs the form handle. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TMyTrayApp.AddIcon : Boolean;var  WideBuffer: widestring;begin  FillChar(fNotifyIcon, SizeOf(fNotifyIcon), 0);  fNotifyIcon.cbSize := SizeOf(fNotifyIcon);[b]  fNotifyIcon.hWnd := (Self.Owner as TForm).Handle;[/b]  fNotifyIcon.uID := uIDTrayIcon;  fNotifyIcon.uFlags := NIF_MESSAGE or NIF_ICON;  fNotifyIcon.uFlags := fNotifyIcon.uFlags or NIF_TIP;  fNotifyIcon.uCallbackMessage := WM_USER + uIDTrayIcon;  fNotifyIcon.hIcon := Application.Icon.Handle;   WideBuffer := UTF8ToUTF16('TrayIconHint');  WideStrLCopy(@fNotifyIcon.szTip, PWideChar(WideBuffer), 127);   Result := Shell_NotifyIconW(NIM_ADD, @fNotifyIcon);end; ---------------------------------------------------------------------- Remy Lebeau · 2021-10-15 17:20 Quote from: pcurtis on October 15, 2021, 07:11:15 pmfNotifyIcon needs the form handle. Only because TMyTrayApp is not creating its own HWND.  That is my point, it SHOULD be creating its own HWND, then it can use that HWND for fNotifyIcon.hWnd and thus be able to receive WM_USER + uIDTrayIcon messages directly from the OS without involving the parent Form at all. Delphi has an AllocateHWnd() function for exactly this purpose.  I don't know what FreePascal's equivalent is, and I can't find TTimer's source code to check.  You will likely have to call the Win32 CreateWindow/Ex() function directly.  See Receive and Handle Windows Messages in Lazarus on StackOverflow for an example. ---------------------------------------------------------------------- pcurtis · 2021-10-15 20:44 Ok. This works. Thanks. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit MyTrayApp; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, LResources, LCLIntf, Forms, Controls, Graphics, Dialogs, ExtCtrls,  StdCtrls, ComCtrls, Windows, ShellAPI, LazUTF8; const  uIDTrayIcon = 25;  WM_ICONTRAY = WM_USER + uIDTrayIcon; type   TNotifyIconDataW2 = record    cbSize: DWORD;    hWnd: HWND;    uID: UINT;    uFlags: UINT;    uCallbackMessage: UINT;    hIcon: HICON;    szTip: array [0..127] of WideChar;    dwState: DWORD;    dwStateMask: DWORD;    szInfo: array [0..255] of WideChar;    u: record         case longint of           0 : ( uTimeout : UINT );           1 : ( uVersion : UINT );          end;    szInfoTitle: array[0..63] of WideChar;    dwInfoFlags: DWORD;  end;    { TMyTestComponent }   TMyTrayApp = class(TComponent)  private    fNotifyIcon : TNotifyIconDataW2;    fHWND : HWND;  protected   public    constructor Create(AOwner : TComponent); override;    destructor Destroy; override;    function AddIcon : Boolean;    procedure WndProc(var Msg: TMessage);  published   end; procedure Register; var  fPrevWndProc : WndProc; implementation //{$R myselectdialog.res} function WideStrLCopy(dest, source: PWideChar; maxlen: SizeInt): PWideChar;var  counter: SizeInt;begin  counter := 0;   while (Source[counter] <> #0)  and (counter < MaxLen) do  begin    Dest[counter] := Source[counter];    Inc(counter);  end;   Dest[counter] := #0;  Result := Dest;end; constructor TMyTrayApp.Create(AOwner : TComponent);begin  inherited Create(AOwner);  fHWND := AllocateHWnd(@WndProc);  if not (csDesigning in ComponentState) then    begin      AddIcon;    end;end; destructor TMyTrayApp.Destroy;begin  Shell_NotifyIconW(NIM_DELETE, @fNotifyIcon);  DeallocateHWnd(fHWND);  inherited;end; procedure TMyTrayApp.WndProc(var Msg: TMessage);begin  if Msg.msg = WM_ICONTRAY then    begin      case Msg.lParam of        WM_LBUTTONDOWN : begin                           (Self.Owner as TForm).Show;                         end;        WM_RBUTTONDOWN : begin                           (Self.Owner as TForm).Hide;                       end;      end;    end;end; function TMyTrayApp.AddIcon : Boolean;var  WideBuffer: widestring;begin  FillChar(fNotifyIcon, SizeOf(fNotifyIcon), 0);  fNotifyIcon.cbSize := SizeOf(fNotifyIcon);  fNotifyIcon.hWnd := fHWND;  fNotifyIcon.uID := uIDTrayIcon;  fNotifyIcon.uFlags := NIF_MESSAGE or NIF_ICON;  fNotifyIcon.uFlags := fNotifyIcon.uFlags or NIF_TIP;  fNotifyIcon.uCallbackMessage := WM_USER + uIDTrayIcon;  fNotifyIcon.hIcon := Application.Icon.Handle;   WideBuffer := UTF8ToUTF16('TrayIconHint');  WideStrLCopy(@fNotifyIcon.szTip, PWideChar(WideBuffer), 127);   Result := Shell_NotifyIconW(NIM_ADD, @fNotifyIcon);end; procedure Register; begin  RegisterComponents('Misc',[TMyTrayApp]);end; end.