Recent

Author Topic: The EnumChildWindows compilation error  (Read 859 times)

yazigegeda

  • New Member
  • *
  • Posts: 29
The EnumChildWindows compilation error
« on: May 14, 2024, 09:44:32 am »
Code: Pascal  [Select][+][-]
  1.  
  2. unit Unit1;
  3.  
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls,
  10.  
  11.   windows;
  12.  
  13. type
  14.  
  15.   { TForm1 }
  16.  
  17.   TForm1 = class(TForm)
  18.     Label1: TLabel;
  19.     ListBox1: TListBox;
  20.     ListView1: TListView;
  21.  
  22.     procedure FormCreate(Sender: TObject);
  23.  
  24.     function WindowsToList(_para1:HWND; _para2:LPARAM):WINBOOL;stdcall;
  25.  
  26.   private
  27.  
  28.   public
  29.  
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. {$R *.lfm}
  38.  
  39. { TForm1 }
  40.  
  41. procedure TForm1.FormCreate(Sender: TObject);
  42.  
  43. begin
  44.  
  45.     EnumChildWindows(Form1.Handle,@WindowsToList,0);
  46. end;
  47.  
  48. function TForm1.WindowsToList(_para1: HWND; _para2: LPARAM):WINBOOL;stdcall;
  49. begin
  50.  
  51. end;
  52.  
  53.  
  54.  
  55. end.
  56. [size=10pt][/size]
  57.  
  58.  
  59.  


unit1.pas(44,49) Error: Incompatible type for arg no. 2: Got "<procedure variable type of function(QWord;Int64):LongBool of object;StdCall>", expected "<procedure variable type of function(QWord;Int64):LongBool;StdCall>"




I don't know what's wrong. Help me :'(

Bart

  • Hero Member
  • *****
  • Posts: 5317
    • Bart en Mariska's Webstek
Re: The EnumChildWindows compilation error
« Reply #1 on: May 14, 2024, 09:54:08 am »
Your function must not be part of a class (being a method of a class makes it "of object").

Bart

yazigegeda

  • New Member
  • *
  • Posts: 29
Re: The EnumChildWindows compilation error
« Reply #2 on: May 14, 2024, 10:03:47 am »
Your function must not be part of a class (being a method of a class makes it "of object").

Bart


Oh, he bothered me for a long time. Thank you so much! O:-)

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: The EnumChildWindows compilation error
« Reply #3 on: May 14, 2024, 10:13:33 am »
User 440x has made some Windows Api examples, also for EnumChildWindows usage. Search on forum for his sources.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

yazigegeda

  • New Member
  • *
  • Posts: 29
Re: The EnumChildWindows compilation error
« Reply #4 on: May 14, 2024, 10:15:42 am »
User 440x has made some Windows Api examples, also for EnumChildWindows usage. Search on forum for his sources.


I'll check it out :'(

440bx

  • Hero Member
  • *****
  • Posts: 4339
Re: The EnumChildWindows compilation error
« Reply #5 on: May 14, 2024, 10:19:53 am »
Here are the links to the examples that involve windows and child windows:


https://forum.lazarus.freepascal.org/index.php/topic,53690.msg397423.html#msg397423
https://forum.lazarus.freepascal.org/index.php/topic,53567.msg396385.html#msg396385

Note that those examples are pure Windows API, no "Forms" and stuff like that but, they should be useful for anyone who wants to learn how Windows works.  Use the debugger "Luke". ;)

ETA:

Thank you for the "plug" KodeZwerg.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

yazigegeda

  • New Member
  • *
  • Posts: 29
Re: The EnumChildWindows compilation error
« Reply #6 on: May 14, 2024, 01:18:11 pm »
Here are the links to the examples that involve windows and child windows:


https://forum.lazarus.freepascal.org/index.php/topic,53690.msg397423.html#msg397423
https://forum.lazarus.freepascal.org/index.php/topic,53567.msg396385.html#msg396385

Note that those examples are pure Windows API, no "Forms" and stuff like that but, they should be useful for anyone who wants to learn how Windows works.  Use the debugger "Luke". ;)

ETA:

Thank you for the "plug" KodeZwerg.


I want to put the implementation of the button click event of Form1 in other units, I don't know if it is feasible, help me

440bx

  • Hero Member
  • *****
  • Posts: 4339
Re: The EnumChildWindows compilation error
« Reply #7 on: May 14, 2024, 01:56:21 pm »
I want to put the implementation of the button click event of Form1 in other units, I don't know if it is feasible, help me
I know the Windows API reasonably well and can help you with that.  I know next to nothing about writing a program the Lazarus/Delphi way, therefore it is unlikely I can be of any real help in your case, it's likely that whatever I suggest will likely be "less" than optimal (if it works at all.)

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: The EnumChildWindows compilation error
« Reply #8 on: May 14, 2024, 03:54:53 pm »
Thank you for the "plug" KodeZwerg.
You are welcomed and apologies that I forgot the "b" in your nick  O:-)
I want to put the implementation of the button click event of Form1 in other units, I don't know if it is feasible, help me
You should explain more what you actually want to achieve.
In what unit the execution code is does not matter much.

short example
unit2 got the method for a click event
unit1 got some control where the click event has matching header as the one in unit2, in code = "mycontrol.onclick := unit2.clickmethod...."
be aware about that "clickmethod" knows nothing of unit1 scope.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1351
    • Lebeau Software
Re: The EnumChildWindows compilation error
« Reply #9 on: May 14, 2024, 06:30:12 pm »
unit1.pas(44,49) Error: Incompatible type for arg no. 2: Got "<procedure variable type of function(QWord;Int64):LongBool of object;StdCall>", expected "<procedure variable type of function(QWord;Int64):LongBool;StdCall>"

You can't use a non-static class method for an Win32 API callback, because it has a hidden Self parameter that the API doesn't know what to do with.

For what you are attempting, you can use a class static method instead, eg:

Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.     ...
  4.     procedure FormCreate(Sender: TObject);
  5.     class function WindowsToList(_para1: HWND; _para2: LPARAM): WINBOOL; stdcall; static;
  6.   end;
  7.  
  8. ...
  9.  
  10. procedure TForm1.FormCreate(Sender: TObject);
  11. begin
  12.   EnumChildWindows(Self.Handle, @WindowsToList, LPARAM(Self));
  13. end;
  14.  
  15. class function TForm1.WindowsToList(_para1: HWND; _para2: LPARAM): WINBOOL; stdcall;
  16. begin
  17.   // use _para1 and TForm1(_para2) as needed...
  18.   Result := ...;
  19. end;
  20.  

I want to put the implementation of the button click event of Form1 in other units, I don't know if it is feasible, help me

If you define the event handler as a method of the Form class (ie, by creating it at design-time), then its implementation must be in the same unit as the Form class.  Pascal simply has no concept of "partial classes" that span multiple source files, like some languages do, such as C#.

If you want the implementation to be in a different unit, then you will have to either:

- make the handler normally and simply have it call functions/methods of other units, eg:

Code: Pascal  [Select][+][-]
  1. unit MyOtherUnit;
  2.  
  3. interface
  4.  
  5. procedure DoSomething;
  6.  
  7. implementation
  8.  
  9. procedure DoSomething;
  10. begin
  11.   //...
  12. end;
  13.  
  14. end.
  15.  

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   ...;
  7.  
  8. type
  9.   TForm1 = class(TForm)
  10.     Button1: TButton;
  11.     procedure Button1Click(Sender: TObject);
  12.     ...
  13.   end;
  14.  
  15. ...
  16.  
  17. implementation
  18.  
  19. ...
  20.  
  21. uses
  22.   MyOtherUnit;
  23.  
  24. procedure TForm1.Button1Click(Sender: TObject);
  25. begin
  26.   DoSomething;
  27. end;
  28.  
  29. end.
  30.  

- create a separate object instance elsewhere as needed and then assign one of its methods as the event handler in code at runtime, eg:

Code: Pascal  [Select][+][-]
  1. unit MyOtherUnit;
  2.  
  3. interface
  4.  
  5. type
  6.   TMyOtherClass = class
  7.   public
  8.     procedure DoSomething(Sender: TObject);
  9.   end;
  10.  
  11. implementation
  12.  
  13. procedure TMyOtherClass.DoSomething(Sender: TObject);
  14. begin
  15.   //...
  16. end;
  17.  
  18. end.
  19.  

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   ..., MyOtherUnit;
  7.  
  8. type
  9.   TForm1 = class(TForm)
  10.     Button1: TButton;
  11.     procedure FormCreate(Sender: TObject);
  12.     procedure FormDestroy(Sender: TObject);
  13.     ...
  14.   private
  15.     MyObj: TMyOtherClass;
  16.   end;
  17.  
  18. ...
  19.  
  20. implementation
  21.  
  22. ...
  23.  
  24. procedure TForm1.FormCreate(Sender: TObject);
  25. begin
  26.   MyObj := TMyOtherClass.Create;
  27.   Button1.OnClick := MyObj.DoSomething;
  28. end;
  29.  
  30. procedure TForm1.FormDestroy(Sender: TObject);
  31. begin
  32.   MyObj.Free;
  33. end;
  34.  
  35. end.
  36.  
« Last Edit: May 14, 2024, 06:45:04 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018