gpt4 book ai didi

delphi - C++ 到 Delphi : variable in FOR loop with assigned declaration

转载 作者:行者123 更新时间:2023-12-03 15:19:52 26 4
gpt4 key购买 nike

Delphi 中的 for 循环 ChildWindowFromPoint() 的等效项是什么?来自此 C++ 代码:

HWND  hWnd;
POINT point;

...

for (HWND currHwnd = hWnd;;)
{
hWnd = currHwnd;
ScreenToClient(currHwnd, &point);
currHwnd = ChildWindowFromPoint(currHwnd, point);
if (!currHwnd || currHwnd == hWnd)
break;
}

我的尝试是这样的,但我不确定它是否正确:

var
hWndWindow, currHwnd: HWND;
MousePoint: TPoint;

...

while True do
begin
currHwnd := hWndWindow;
hWndWindow := currHwnd;
ScreenToClient(currHwnd, MousePoint);
currHwnd := ChildWindowFromPoint(currHwnd, MousePoint);

if (currHwnd = 0) or (currHwnd = hWndWindow) then
Break;
end;

最佳答案

您的翻译几乎正确,但您确实犯了一个错误。您需要将 currHwnd 的初始赋值移到 while 循环之外:

var
hWndWindow, currHwnd: HWND;
MousePoint: TPoint;

...

currHwnd := hWndWindow; // <-- moved here!
while True do
begin
hWndWindow := currHwnd;
ScreenToClient(currHwnd, MousePoint);
currHwnd := ChildWindowFromPoint(currHwnd, MousePoint);

if (currHwnd = 0) or (currHwnd = hWndWindow) then
Break;
end;

for loop在 cppreference.com 上:

formal syntax:

attr(optional) for ( init-statement condition(optional) ; iteration_expression(optional) ) statement

informal syntax:

attr(optional) for ( declaration-or-expression(optional) ; declaration-or-expression(optional) ; expression(optional) ) statement

attr(C++11) - any number of attributes

init-statement - either

  • an expression statement (which may be a null statement ";")

  • a simple declaration, typically a declaration of a loop counter variable with initializer, but it may declare arbitrary many variables

    Note that any init-statement must end with a semicolon ;, which is why it is often described informally as an expression or a declaration followed by a semicolon.

condition - either

  • an expression which is contextually convertible to bool. This expression is evaluated before each iteration, and if it yields false, the loop is exited.

  • declaration of a single variable with a brace-or-equals initializer. the initializer is evaluated before each iteration, and if the value of the declared variable converts to false, the loop is exited.

iteration_expression - any expression, which is executed after every iteration of the loop and before re-evaluating condition. Typically, this is the expression that increments the loop counter

statement - any statement, typically a compound statement, which is the body of the loop

Explanation

The above syntax produces code equivalent to:

{
init_statement 
while ( condition ) { 
statement 
iteration_expression ; 
}
}

话虽这么说,我会将 C++ 循环转换为 Delphi repeat..until 循环(并编写 C++ 代码以使用 do..while 循环):

HWND hWnd;
POINT point;

...

HWND currHwnd = hWnd;
do
{
hWnd = currHwnd;
ScreenToClient(currHwnd, &point);
currHwnd = ChildWindowFromPoint(currHwnd, point);
}
while (currHwnd && currHwnd != hWnd);
var
hWndWindow, currHwnd: HWND;
MousePoint: TPoint;

...

currHwnd := hWndWindow;
repeat
hWndWindow := currHwnd;
ScreenToClient(currHwnd, MousePoint);
currHwnd := ChildWindowFromPoint(currHwnd, MousePoint);
until (currHwnd = 0) or (currHwnd = hWndWindow);

关于delphi - C++ 到 Delphi : variable in FOR loop with assigned declaration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56572150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com