Problem with WM_COMMAND on Lazarus/FPC
delphi, fpc, freepascal, lazarus, message
Solution
In a LCL application you have the following layers:
- Application
- LCL
- Widget set Interface (e.g. win32/win64, qt, gtk2, carbon)
- Widget set
WM_COMMAND is a winapi message from the widgetset Layer to the Widget Set interface layer. These messages are not passed to the higher layers, having portability in mind, other widget sets don't produce such messages.
If you want to capture the message, then you must write non-portable widgetset specific code (winapi code in this case). You can override the windowproc with setwindowlong. See the Lazarus wiki for an example.
Problem
I have form with MainMenu and I want to intercept when the user selects a command item from a menu. This works in Delphi: ``` type TForm1 = class(TForm) ... // Memo and MainMenu created protected procedure WMCommand(var Info: TWMCommand); message WM_COMMAND; end; procedure TForm1.WMCommand(var Info: TWMCommand); begin if (Info.ItemID < 10) then Memo1.Lines.Add('WMCommand ' + IntToStr(Info.ItemID)); end; ``` In MainMenu I added some items and when I select those items from menu then my Memo1 is filled with: ``` WMCommand 2 WMCommand 3 WMCommand 3 WMCommand 2 WMCommand 5 ... ``` I ported this application to FPC/Lazarus, but it seems that WM_COMMAND handler is not called! When I set breakpoint in `TForm1.WMCommand` in Delphi then Delphi stopped many times before main form appeared. Lazarus never stopped on this breakpoint. I think something is broken with WM_COMMAND in Lazarus, but maybe I don't know something. Any idea? I use Lazarus 0.9.28.2 beta with FPC 2.2.4 on WinXP. EDIT: Using Winspector I checked that MainMenu generates WM_COMMAND: ``` WM_COMMAND Code: 0 Control ID: 2 Control HWND: 0x00000000 Message Posted Time: 09:37:14.0968 ``` I think there is bug in Lazarus/FPC in WM_COMMAND message method handling and I reported it: http://bugs.freepascal.org/view.php?id=15521