delphi - Delphi中如何给变量赋值?

标签 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);
}

这是我的尝试:

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++ 那样在 whileif 语句内分配变量。您需要分解 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/

相关文章:

delphi - 如何在普通应用程序和 UAC 提升权限应用程序之间启用拖放

delphi - 如何在 Delphi 中使用 http 身份验证打开 url?

delphi - 是否可以让属性的读/写具有不同的可见性?

Delphi,如何在鼠标移动时显示覆盖控件

mysql - ZeosLib DataSets 是否需要执行 FetchAll 方法才能返回真实的总行数?

delphi - 如何在组件中添加对操作的支持

delphi - 我需要在 Delphi 中完成记录数组吗?

delphi - 为什么 CM_CONTROLLISTCHANGE 对间接父控件执行?

delphi - 具有参数约束的泛型构造函数?

delphi - 如何在 Delphi 中将 PNG 图像的颜色从白色更改为黑色?