有没有办法将此 delphi 代码转换为 c++builder
Is there a way to convert this delphi code to c++builder
在大多数情况下,我能够将 Delphi 转换为 C++,但是这个让我有些头疼。也许你们中的一些人可以提供帮助。
如本 link here 所示,它引用了 Embarcadero (FMX) 中 TListView 的一些新功能。因为我使用 C++ 比 Delphi 更舒服,所以我使用 C++Builder。在大多数情况下,这完全可以翻译和理解,并找到解决方法。但我被困在这里:
procedure TForm1.FormCreate(Sender: TObject);
I: Integer;
begin
// ListView1 uses a classic Appearance
for I in [0..63] do
with ListView1.Items.Add do
begin
Text := Format('%d pages', [1000 + Random(1234567)]);
Detail := Format('%d kg of paper', [1000 + Random(1234)]);
ImageIndex := Random(ImageList1.Count);
end;
// ListView4 uses a dynamic appearance with items named
// Text1, Detail1, Portrait
for I in [0..63] do
with ListView4.Items.Add do
begin
Data['Text1'] := Format('%d pages', [1000 + Random(1234567)]);
Data['Detail1'] := Format('%d kg of paper', [1000 + Random(1234)]);
Data['Portrait'] := Random(ImageList1.Count);
end;
end;
end.
我纠结的部分是
with ListView4.Items.Add do
begin
Data['Text1'] := Format('%d pages', [1000 + Random(1234567)]);
Data['Detail1'] := Format('%d kg of paper', [1000 + Random(1234)]);
Data['Portrait'] := Random(ImageList1.Count);
end;
这是如何翻译的,或者这个功能根本不存在于 c++ 中?
With
有点引入了一个未命名的变量和它的作用域。在 C++ 中,你必须是显式的。 Delphi 片段等同于
var
li: TListItem;
begin
li := ListView4.Items.Add;
li.Data['Text1'] := Format('%d pages', [1000 + Random(1234567)]);
li.Data['Detail1'] := Format('%d kg of paper', [1000 + Random(1234)]);
li.Data['Portrait'] := Random(ImageList1.Count);
end;
(如果我没有搞砸的话:-))。
尝试这样的事情:
// Never use the OnCreate event in C++,
// use the class constructor instead...
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
// ListView1 uses a classic Appearance
for(int i = 0; i < 64; ++i)
{
TListViewItem *Item = ListView1->Items->Add();
Item->Text = Format(L"%d pages", ARRAYOFCONST(( 1000 + Random(1234567) )) );
Item->Detail = Format(L"%d kg of paper", ARRAYOFCONST(( 1000 + Random(1234) )) );
Item->ImageIndex = Random(ImageList1->Count);
}
// ListView4 uses a dynamic appearance with items named
// Text1, Detail1, Portrait
for(int i = 0; i < 64; ++i)
{
TListViewItem *Item = ListView4->Items->Add();
Item->Data[L"Text1"] = Format(L"%d pages", ARRAYOFCONST(( 1000 + Random(1234567) )) );
Item->Data[L"Detail1"] = Format(L"%d kg of paper", ARRAYOFCONST(( 1000 + Random(1234) )) );
Item->Data[L"Portrait"] = Random(ImageList1->Count);
}
}
当您想要向 ListView 添加项目时,您需要首先使用作为 TListView 的项目 属性 的子函数的 Add() 函数创建一个项目对象 (TListViewItem*)。
然后,项目的数据 属性 需要 TValue,因此您需要从字符串或其他要放入项目的内容中获取 TValue。
请记住在将项目添加到 ListView 的片段之前使用 BeginUpdate() 并在之后使用 EndUpdate() 以提高此操作的性能。
ListView4->BeginUpdate();
TListViewItem* item = ListView4->Items->Add();
UnicodeString string1 = "content of the String";
item->Data["Text1"] = TValue::From<UnicodeString>(string1);
item->Data["Detail1"] = TValue::From<UnicodeString>(string1);
item->Data["visitTime"] =TValue::From<int>(Random(ImageList1->Count))
ListView4->EndUpdate();
在大多数情况下,我能够将 Delphi 转换为 C++,但是这个让我有些头疼。也许你们中的一些人可以提供帮助。
如本 link here 所示,它引用了 Embarcadero (FMX) 中 TListView 的一些新功能。因为我使用 C++ 比 Delphi 更舒服,所以我使用 C++Builder。在大多数情况下,这完全可以翻译和理解,并找到解决方法。但我被困在这里:
procedure TForm1.FormCreate(Sender: TObject);
I: Integer;
begin
// ListView1 uses a classic Appearance
for I in [0..63] do
with ListView1.Items.Add do
begin
Text := Format('%d pages', [1000 + Random(1234567)]);
Detail := Format('%d kg of paper', [1000 + Random(1234)]);
ImageIndex := Random(ImageList1.Count);
end;
// ListView4 uses a dynamic appearance with items named
// Text1, Detail1, Portrait
for I in [0..63] do
with ListView4.Items.Add do
begin
Data['Text1'] := Format('%d pages', [1000 + Random(1234567)]);
Data['Detail1'] := Format('%d kg of paper', [1000 + Random(1234)]);
Data['Portrait'] := Random(ImageList1.Count);
end;
end;
end.
我纠结的部分是
with ListView4.Items.Add do
begin
Data['Text1'] := Format('%d pages', [1000 + Random(1234567)]);
Data['Detail1'] := Format('%d kg of paper', [1000 + Random(1234)]);
Data['Portrait'] := Random(ImageList1.Count);
end;
这是如何翻译的,或者这个功能根本不存在于 c++ 中?
With
有点引入了一个未命名的变量和它的作用域。在 C++ 中,你必须是显式的。 Delphi 片段等同于
var
li: TListItem;
begin
li := ListView4.Items.Add;
li.Data['Text1'] := Format('%d pages', [1000 + Random(1234567)]);
li.Data['Detail1'] := Format('%d kg of paper', [1000 + Random(1234)]);
li.Data['Portrait'] := Random(ImageList1.Count);
end;
(如果我没有搞砸的话:-))。
尝试这样的事情:
// Never use the OnCreate event in C++,
// use the class constructor instead...
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
// ListView1 uses a classic Appearance
for(int i = 0; i < 64; ++i)
{
TListViewItem *Item = ListView1->Items->Add();
Item->Text = Format(L"%d pages", ARRAYOFCONST(( 1000 + Random(1234567) )) );
Item->Detail = Format(L"%d kg of paper", ARRAYOFCONST(( 1000 + Random(1234) )) );
Item->ImageIndex = Random(ImageList1->Count);
}
// ListView4 uses a dynamic appearance with items named
// Text1, Detail1, Portrait
for(int i = 0; i < 64; ++i)
{
TListViewItem *Item = ListView4->Items->Add();
Item->Data[L"Text1"] = Format(L"%d pages", ARRAYOFCONST(( 1000 + Random(1234567) )) );
Item->Data[L"Detail1"] = Format(L"%d kg of paper", ARRAYOFCONST(( 1000 + Random(1234) )) );
Item->Data[L"Portrait"] = Random(ImageList1->Count);
}
}
当您想要向 ListView 添加项目时,您需要首先使用作为 TListView 的项目 属性 的子函数的 Add() 函数创建一个项目对象 (TListViewItem*)。 然后,项目的数据 属性 需要 TValue,因此您需要从字符串或其他要放入项目的内容中获取 TValue。 请记住在将项目添加到 ListView 的片段之前使用 BeginUpdate() 并在之后使用 EndUpdate() 以提高此操作的性能。
ListView4->BeginUpdate();
TListViewItem* item = ListView4->Items->Add();
UnicodeString string1 = "content of the String";
item->Data["Text1"] = TValue::From<UnicodeString>(string1);
item->Data["Detail1"] = TValue::From<UnicodeString>(string1);
item->Data["visitTime"] =TValue::From<int>(Random(ImageList1->Count))
ListView4->EndUpdate();