我正在为Windows开发Delphi 10.1 VCL应用程序.
对于整数或浮点输入,我需要一个与滑块连接的数字输入字段.当用户更改输入字段中的数字时,滑块位置会相应地改变.当用户更改滑块位置时,将更新数字字段中的数字.
我可以通过使用TEdit和TTrackBar来解决这个问题,并在他们的OnChange事件处理程序中添加必要的更新功能.
问题是我需要不同形式的许多此类输入.因此,我想创建一个新组件,它将两个控件TEdit和TTrackBar组合在一个组件中.
创建一个新组件是多次使用这种滑块输入的最佳策略吗?
创建这样一个新组件的最佳方法是什么?
我想在Delphi XE5中使用带有子列表的通用TList记录:
type
TMyRecord=record
Value1: Real;
SubList: TList<Integer>;
end;
TMyListOfRecords=TList<TMyRecord>;
var
MyListOfRecords: TMyListOfRecords;
Run Code Online (Sandbox Code Playgroud)
无法分配记录字段:
MyListOfRecords[0].Value1:=2.24;
Run Code Online (Sandbox Code Playgroud)
要么
MyListOfRecords[0].SubList:=TList<Integer>.Create;
Run Code Online (Sandbox Code Playgroud)
将导致编译器"左侧无法分配"错误.
另请参见:如何修改TList <record>值?
以下解决方法有效:
AMyRecord:=MyListOfRecords[0];
AMyRecord.Value1:=2.24;
AMyRecord.SubList:=TList<Integer>.Create;
AMyRecord.SubList.Add(33);
MyListOfRecords[0]:=AMyRecord;
Run Code Online (Sandbox Code Playgroud)
由于性能问题,我想避免将数据复制到临时AMyrecord.我宁愿直接访问记录字段和子列表.
处理这个问题的最佳方法是什么?
我正在寻找一种算法来计算以下内容:
我有:
3D三角网格.三角形不一定位于一个平面中.两个相邻三角形的范数向量之间的角度小于90度.
两点.这两个点位于三角形网格的边缘或网格的三角形内.
我需要计算折线,它代表网格上两点之间的最短路径.
这样做最简单和/或最有效的策略是什么?
在柏林的Delphi 10.1中,我想添加一个可能性来停止我的问题中的响应式TParallel。&For循环。如何使TParallel。&For循环响应并将值存储在TList <T>中?。
循环计算值并将这些值存储在TList中。它与TTask.Run在单独的线程中运行以使其响应:
type
TCalculationProject=class(TObject)
private
Task: ITask;
...
public
List: TList<Real>;
...
end;
procedure TCalculationProject.CancelButtonClicked;
begin
if Assigned(Task) then
begin
Task.Cancel;
end;
end;
function TCalculationProject.CalculateListItem(const AIndex: Integer): Real;
begin
//a function which takes a lot of calculation time
//however in this example we simulate the calculation time and
//use a simple alogorithm to verify the list afterwards
Sleep(30);
Result:=10*AIndex;
end;
procedure TCalculationProject.CalculateList;
begin
List.Clear;
if Assigned(Task) then
begin
Task.Cancel;
end;
Task:=TTask.Run(
procedure
var
LoopResult: TParallel.TLoopResult;
Lock: …
Run Code Online (Sandbox Code Playgroud) delphi parallel-processing multithreading thread-safety wait
我在Delphi 10中有一个三角形网格结构.
出于性能原因,我将网格顶点,三角形面等的数据存储在TList的后代中.
我让TLists为列表的每个成员进行计算.对于这些计算,我需要访问TMesh结构的某些字段.因此,在创建TMesh并随后创建列表期间,我将父TMesh分配给列表.我使用TMesh的前向声明来做到这一点.请参阅以下代码:
type
{forward declaration}
TMesh=class;
TVertex=record
Point: TPoint3D;
//other fields
end;
TVertices=class(TList<TVertex>)
Mesh: TMesh;
procedure DoSomethingWithAllVertices; //uses some fields of TMesh
constructor Create(const AMesh: TMesh);
//other methods
end;
TTriangleFace=record
Vertices: Array[0..2] of Integer;
//other fields
end;
TTriangleFaces=class(TList<TTriangleFace>)
Mesh: TMesh;
procedure DoSomethingWithAllTriangleFaces; //uses some fields of TMesh
constructor Create(const AMesh: TMesh);
//other methods
end;
TMesh=class(TComponent)
Vertices: TVertices;
TriangleFaces: TTriangleFaces;
constructor Create(AOwner: TComponent);
//other fields & methods
end;
implementation
constructor TMesh.Create(AOwner: TComponent);
begin
inherited;
Vertices:=TVertices.Create(Self);
TriangleFaces:=TTriangleFaces.Create(Self);
end;
constructor TVertices.Create(const …
Run Code Online (Sandbox Code Playgroud) 我想使用TParallel.&For
循环来计算例如1到100000之间的素数,并将所有这些素数保存在AList: TList<Integer>
:
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
AList: TList<Integer>;
LoopResult: Tparallel.TLoopResult;
begin
AList:=TList<Integer>.Create;
TParallel.&For(1, 100000,
procedure(AIndex: Integer)
begin
if IsPrime(AIndex) then
begin
//add the prime number to AList
end;
end);
//show the list
for i := 0 to AList.Count-1 do
begin
Memo1.Lines.Add(IntToStr(AList[i]));
end;
end;
Run Code Online (Sandbox Code Playgroud)
计算可以并行执行而没有问题,但它TList
是共享资源.如何以线程安全的方式将已确认的素数添加到列表中?
delphi parallel-processing multithreading delphi-10.1-berlin
我有以下 XML 结构:
<?xml version="1.0"?>
<main>
<node1>
<subnode1>
<value1>101</value1>
<value2>102</value2>
<value3>103</value3>
</subnode1>
<subnode2>
<value1>501</value1>
<value2>502</value2>
<value3>503</value3>
</subnode2>
</node1>
</main>
Run Code Online (Sandbox Code Playgroud)
在 Delphi 中,我正在寻找一个函数,它将节点的内部文本和 XML 作为字符串返回。例如对于<node1>
字符串应该是(如果可能,包括缩进和换行符):
<subnode1>
<value1>101</value1>
<value2>102</value2>
<value3>103</value3>
</subnode1>
<subnode2>
<value1>501</value1>
<value2>502</value2>
<value3>503</value3>
</subnode2>
Run Code Online (Sandbox Code Playgroud)
我在 Delphi 10 中找不到这样的功能。
有这样的功能吗?
或者在 Delphi 10 中实现一个的最佳方法是什么?