下面的 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);
}
这是我的尝试:
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;
最佳答案
Delphi 无法像 C/C++ 那样在 while
或 if
语句内分配变量。您需要分解 while
语句,就像在调用 GetWindow(GW_HWNDLAST)
时必须分解 if
语句一样,例如:
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;
关于delphi - Delphi中如何给变量赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55884420/