如何在Delphi中为变量分配语句?

Bro*_*wJr 5 delphi

下面的C代码如何在Delphi中完成?我尝试翻译,但似乎Delphi不允许使用此语法。基本上,我需要像在C代码中那样将函数分配给变量。在Delphi上怎么做?

这是参考的C代码:

void EnumWindowsTopToDown(HWND owner, WNDENUMPROC proc, LPARAM param)
{
    HWND currentWindow = GetTopWindow(owner);
    if (currentWindow == NULL)
        return;
    if ((currentWindow = GetWindow(currentWindow, GW_HWNDLAST)) == NULL)
        return;
    while (proc(currentWindow, param) && (currentWindow = GetWindow(currentWindow, GW_HWNDPREV)) != NULL);
}
Run Code Online (Sandbox Code Playgroud)

这是我的尝试:

type
  TFNWndEnumProc = function(_hwnd: HWND; _lParam: LPARAM): BOOL; stdcall;

    procedure EnumWindowsTopToDown(Owner: HWND; Proc: TFNWndEnumProc;
      _Param: LPARAM);
    var
      CurrentWindow: HWND;
    begin
      CurrentWindow := GetTopWindow(Owner);
      if CurrentWindow = 0 then
        Exit;

      CurrentWindow := GetWindow(CurrentWindow, GW_HWNDLAST);
      if CurrentWindow = 0 then
        Exit;

      while Proc(CurrentWindow, _Param) and (CurrentWindow :=  GetWindow(CurrentWindow, GW_HWNDPREV)) <> 0;
    end;
Run Code Online (Sandbox Code Playgroud)

Rem*_*eau 5

Delphi不能像C / C ++一样在whileor if语句内分配变量。您需要对while语句进行拆分,就像您if在调用时必须拆分该语句一样GetWindow(GW_HWNDLAST),例如:

type
  TFNWndEnumProc = function(_hwnd: HWND; _lParam: LPARAM): BOOL; stdcall;

procedure EnumWindowsTopToDown(Owner: HWND; Proc: TFNWndEnumProc; Param: LPARAM);
var
  CurrentWindow: HWND;
begin
  CurrentWindow := GetTopWindow(Owner);
  if CurrentWindow = 0 then
    Exit;

  CurrentWindow := GetWindow(CurrentWindow, GW_HWNDLAST);
  if CurrentWindow = 0 then
    Exit;

  while Proc(CurrentWindow, Param) do
  begin
    CurrentWindow := GetWindow(CurrentWindow, GW_HWNDPREV);
    if CurrentWindow = 0 then Exit;
  end;
end;
Run Code Online (Sandbox Code Playgroud)