Recent

Author Topic: [SOLVED]Control '' has no parent window  (Read 1569 times)

jamie

  • Hero Member
  • *****
  • Posts: 7308
Re: Control '' has no parent window
« Reply #15 on: October 10, 2025, 02:57:00 pm »
You guys are wasting your time the LCL blocks a lot of messages even in the wind procedure please look at the code I posted
The only true wisdom is knowing you know nothing

CM630

  • Hero Member
  • *****
  • Posts: 1527
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Control '' has no parent window
« Reply #16 on: October 10, 2025, 03:10:19 pm »
You guys are wasting your time the LCL blocks a lot of messages even in the wind procedure please look at the code I posted
A sample LPK using your code is attached here: https://forum.lazarus.freepascal.org/index.php/topic,72444.msg567171.html#msg567171, I could not compile it.
Лазар 4,2 32 bit (sometimes 64 bit); FPC3,2,2

LeP

  • Jr. Member
  • **
  • Posts: 57
Re: Control '' has no parent window
« Reply #17 on: October 10, 2025, 03:34:17 pm »
I made some adjusts to second example but works in Delphi and not in Lazarus.

The AllocateHWnd is not implemented in Lazarus (or better return always an error at runtime).

But if you use the standard Handle (it's not that the issue) instead to create a new one, this works like the previous example: no messages except mouse and keyboard in Lazarus.

Code: Pascal  [Select][+][-]
  1. //This is the declaration in the classes unit
  2. function AllocateHWnd(Method: TWndMethod): HWND;
  3.   begin
  4.     runerror(211);
  5.     AllocateHWnd:=0;
  6.   end;

My experience with FPC and Lazarus is not enough, you must try the other suggestions or someone that know more must intervene.

You guys are wasting your time the LCL blocks a lot of messages even in the wind procedure .....

I saw ...  :(

... please look at the code I posted

I think @CM630 can work with it.

LeP

  • Jr. Member
  • **
  • Posts: 57
Re: Control '' has no parent window
« Reply #18 on: October 10, 2025, 04:05:02 pm »
I found a solution from old topics .... instead to use AllocateHwnd we must use LCLIntf.AllocateHwnd  (thanks to @DonAlfredo)  :D

I post here the code that run in LAZARUS !!! (EDIT: insert uses units too, modify definition like suggested by @jamie)

Code: Pascal  [Select][+][-]
  1. uses
  2.   Windows, Messages, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, stdctrls, jwaWinUser, LCLIntf;
  3.  
  4. type
  5. TMyCustomControl = class(tcombobox)
  6.     private
  7.        procedure InitResources_1;
  8.        procedure InitResources_2;
  9.     protected
  10.       procedure CreateWnd; override;
  11.       procedure WndProcNew(var Message: TMessage);
  12.    public
  13.      destructor Destroy; override;
  14.   end;
  15.  
  16. const
  17.  DBT_DEVTYP_DEVICEINTERFACE = $5;
  18.  DEVICE_NOTIFY_WINDOW_HANDLE = $0;
  19.  DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = $4;
  20.  //THIS IS A GUI FOR USB
  21.  GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';
  22.  
  23. type
  24.   DEV_BROADCAST_HDR_DEVICE_TYPE = Cardinal;
  25.   DEV_BROADCAST_HDR = record
  26.     dbch_size: Cardinal;
  27.     dbch_devicetype: DEV_BROADCAST_HDR_DEVICE_TYPE;
  28.     dbch_reserved: Cardinal;
  29.   end;
  30.  
  31. type
  32.   DEV_BROADCAST_DEVICEINTERFACE_W = record
  33.     dbcc_size: Cardinal;
  34.     dbcc_devicetype: Cardinal;
  35.     dbcc_reserved: Cardinal;
  36.     dbcc_classguid: TGuid;
  37.     dbcc_name: array[0..0] of WChar;
  38.   end;
  39.  
  40. var _dev: Dev_Broadcast_DeviceInterface_W;
  41.  
  42. var DevBlockRem: DEV_BROADCAST_HDR;
  43.     FDevNotify: HDEVNOTIFY;
  44.     NewHandle: HWND;
  45.  
  46.  
  47. procedure TMyCustomControl.CreateWnd;
  48. begin
  49.   inherited CreateWnd;
  50.   //These two calls are equivalent like the have constructed here
  51.   //USE ONLY ONE OF THEM !!!
  52.   //Call this if you want define some type of resources, see documentation
  53.   InitResources_1;
  54.   //Call this if you want define Hardware resources
  55.   //InitResources_2;
  56. end;
  57.  
  58. procedure TMyCustomControl.InitResources_1;
  59. begin
  60.   NewHandle:= LCLIntf.AllocateHwnd(@WndProcNew);
  61.   ZeroMemory(@_dev, sizeOf(Dev_Broadcast_DeviceInterface_W));
  62.   with _dev do
  63.     begin
  64.       dbcc_size:=sizeOf(Dev_Broadcast_DeviceInterface_W);
  65.       dbcc_devicetype:= DBT_DEVTYP_DEVICEINTERFACE;
  66.       dbcc_reserved:=0; //doesn't need, there is ZeroMemory
  67.       dbcc_classguid:= GUID_DEVINTERFACE_USB_DEVICE;
  68.       dbcc_name:= chr(0);  //doesn't need, there is ZeroMemory
  69.     end;
  70.   //Use "DEVICE_NOTIFY_ALL_INTERFACE_CLASSES" if you want catch all messages from all classes (ignore dbcc_classguid)
  71.   FDevNotify:=RegisterDeviceNotification(NewHandle, @_dev, DEVICE_NOTIFY_WINDOW_HANDLE or DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
  72.   Win32Check(FDevNotify <> nil);
  73. end;
  74.  
  75. procedure TMyCustomControl.InitResources_2;
  76. begin
  77.   NewHandle := LCLIntf.AllocateHWnd(@WndProcNew);
  78.   ZeroMemory(@DevBlockRem, sizeOf(DevBlockRem));
  79.   DevBlockRem.dbch_size := sizeof(DEV_BROADCAST_HDR)+sizeof(DEV_BROADCAST_DEVICEINTERFACE_W);
  80.   DevBlockRem.dbch_devicetype := DBT_DEVTYP_DEVICEINTERFACE;
  81.   DevBlockRem.dbch_reserved := 0;
  82.   FDevNotify :=  RegisterDeviceNotification(NewHandle, @DevBlockRem, DEVICE_NOTIFY_WINDOW_HANDLE or DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
  83.   Win32Check(FDevNotify <> nil);
  84. end;
  85.  
  86. destructor TMyCustomControl.Destroy;
  87. begin
  88.   if FDevNotify <> nil then
  89.     UnregisterDeviceNotification(FDevNotify);
  90.  
  91.   inherited;
  92. end;
  93.  
  94. procedure TMyCustomControl.WndProcNew(var Message: TMessage);
  95. begin
  96.   case Message.Msg of
  97.     WM_DEVICECHANGE: //for example
  98.       begin
  99.         writeln(Message.WParam);
  100.         //Message.Result := 0; // Messagge handled
  101.       end;
  102.   end;
  103. end;
« Last Edit: October 10, 2025, 09:46:57 pm by LeP »

jamie

  • Hero Member
  • *****
  • Posts: 7308
Re: Control '' has no parent window
« Reply #19 on: October 10, 2025, 07:17:27 pm »
U have an error in the ....deviceinterface_w record, should be widechar not ansichar
The only true wisdom is knowing you know nothing

LeP

  • Jr. Member
  • **
  • Posts: 57
Re: Control '' has no parent window
« Reply #20 on: October 10, 2025, 09:48:13 pm »
U have an error in the ....deviceinterface_w record, should be widechar not ansichar
You are right.

CM630

  • Hero Member
  • *****
  • Posts: 1527
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Control '' has no parent window
« Reply #21 on: October 10, 2025, 10:29:54 pm »
It seems to work now  :D
I added some code of @jamie's code into @LeP's, so now I get notifications when COM ports are (dis)connected.
For the record:
  PDEV_BROADCAST_HDR(Message.Lparam)^.dbch_deviceType returns 2 for memory sticks; 3 for serial port devices and 5 for everything else (USB keyboard and WiFI card).

U have an error in the ....deviceinterface_w record, should be widechar not ansichar
I tried to change     dbcc_name: array[0..0] of AnsiChar; to     dbcc_name: array[0..0] of WideChar;, but then
dbcc_name:=Char(0); did not compile. It compiles with dbcc_name:=''; but I am not sure if this is the expected value.

@LeP's, do you mind if I add this snippet in the Lazarus wiki?
Лазар 4,2 32 bit (sometimes 64 bit); FPC3,2,2

LeP

  • Jr. Member
  • **
  • Posts: 57
Re: Control '' has no parent window
« Reply #22 on: October 10, 2025, 10:40:41 pm »
U have an error in the ....deviceinterface_w record, should be widechar not ansichar
I tried to change     dbcc_name: array[0..0] of AnsiChar; to     dbcc_name: array[0..0] of WideChar;, but then
dbcc_name:=Char(0); did not compile. It compiles with dbcc_name:=''; but I am not sure if this is the expected value.

I already changed that code. You can use chr(0) or ''. But you can also don't write anything, there is zeromemory before ...

@LeP's, do you mind if I add this snippet in the Lazarus wiki?

Do it.

jamie

  • Hero Member
  • *****
  • Posts: 7308
Re: [SOLVED]Control '' has no parent window
« Reply #23 on: October 10, 2025, 10:52:33 pm »
@CM630

 
Code: Pascal  [Select][+][-]
  1.  .. := WideChar(0);
  2.  

Jamie
The only true wisdom is knowing you know nothing

CM630

  • Hero Member
  • *****
  • Posts: 1527
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: [SOLVED]Control '' has no parent window
« Reply #24 on: October 11, 2025, 10:19:36 pm »
The snippet is now added in the wiki: https://wiki.freepascal.org/Win32/64_Interface#Examples

...
 
Code: Pascal  [Select][+][-]
  1.  .. := WideChar(0);
  2.  
...
It was the first thing that I tried, but it does not compile. := ''; does.

Linux is on the way, but some other day.
« Last Edit: October 11, 2025, 10:21:23 pm by CM630 »
Лазар 4,2 32 bit (sometimes 64 bit); FPC3,2,2

BrunoK

  • Hero Member
  • *****
  • Posts: 721
  • Retired programmer
Re: Control '' has no parent window
« Reply #25 on: October 12, 2025, 03:47:52 pm »
You guys are wasting your time the LCL blocks a lot of messages even in the wind procedure please look at the code I posted
https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/41862

 

TinyPortal © 2005-2018