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:
type
TForm1 = class(TForm)
...
procedure FormCreate(Sender: TObject);
class function WindowsToList(_para1: HWND; _para2: LPARAM): WINBOOL; stdcall; static;
end;
...
procedure TForm1.FormCreate(Sender: TObject);
begin
EnumChildWindows(Self.Handle, @WindowsToList, LPARAM(Self));
end;
class function TForm1.WindowsToList(_para1: HWND; _para2: LPARAM): WINBOOL; stdcall;
begin
// use _para1 and TForm1(_para2) as needed...
Result := ...;
end;
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:
unit MyOtherUnit;
interface
procedure DoSomething;
implementation
procedure DoSomething;
begin
//...
end;
end.
unit Unit1;
interface
uses
...;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
...
end;
...
implementation
...
uses
MyOtherUnit;
procedure TForm1.Button1Click(Sender: TObject);
begin
DoSomething;
end;
end.
- create a separate object instance elsewhere as needed and then assign one of its methods as the event handler in code at runtime, eg:
unit MyOtherUnit;
interface
type
TMyOtherClass = class
public
procedure DoSomething(Sender: TObject);
end;
implementation
procedure TMyOtherClass.DoSomething(Sender: TObject);
begin
//...
end;
end.
unit Unit1;
interface
uses
..., MyOtherUnit;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
...
private
MyObj: TMyOtherClass;
end;
...
implementation
...
procedure TForm1.FormCreate(Sender: TObject);
begin
MyObj := TMyOtherClass.Create;
Button1.OnClick := MyObj.DoSomething;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
MyObj.Free;
end;
end.