C++ 到 Delphi:FOR 循环中的变量具有已分配的声明
C++ to Delphi: variable in FOR loop with assigned declaration
Delphi 中这个 C++ 代码中 for
循环 ChildWindowFromPoint()
的等价物是什么:
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++ 代码中 for
循环 ChildWindowFromPoint()
的等价物是什么:
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);