换做是我,我就直接用 Form1.lst1.Items.Add(IntToStr(Hwnd) + ':' + Text); 而不去考虑 with 块不好用的原因。费时费力还没意义,程序中不建议使用 with 块,不利于阅读代码。
with只要合理使用,也可以增加代码可读性啊,比如,连续使用多个Add
with Form1.lst1.Items do begin Add(IntToStr(Hwnd) + ':' + Text); end;with中的Text实际上是Form1.lst1.Items.Text,即function TStrings.GetTextStr: string; 此函数中有两个for循环,都遍历了ListBox中的所有条目,调用Get逐一取了每个条目的内容,其中Get是 function TListBoxStrings.Get(Index: Integer): string; 此函数中调用两次SendMessage,先后发送LB_GETTEXTLEN和LB_GETTEXT!
另外,在function TStrings.GetTextStr: string;的一开始就调用了 function TListBoxStrings.GetCount: Integer; 此函数也发送了一条消息,是LB_GETCOUNT!
而不去考虑 with 块不好用的原因。费时费力还没意义,程序中不建议使用 with 块,不利于阅读代码。
begin
Add(IntToStr(Hwnd) + ':' + Text);
end;with中的Text实际上是Form1.lst1.Items.Text,即function TStrings.GetTextStr: string;
此函数中有两个for循环,都遍历了ListBox中的所有条目,调用Get逐一取了每个条目的内容,其中Get是
function TListBoxStrings.Get(Index: Integer): string;
此函数中调用两次SendMessage,先后发送LB_GETTEXTLEN和LB_GETTEXT!
function TListBoxStrings.GetCount: Integer;
此函数也发送了一条消息,是LB_GETCOUNT!