Delphi Application.CreateForm是每个表单的唯一句柄吗? [英] Delphi Application.CreateForm is the Handle Unique for each Form?
问题描述
我有一个TreeList,其中包含许多Items,每个item都有其自己的唯一ID. 我允许用户一次打开多个ID.但我想防止用户两次打开相同的ID.
I have a TreeList , with many Items , each item has it's own unique ID . I allow the user to open multiple IDs at once . But I would like to prevent the user from opening the same ID twice .
因此,我考虑创建一个简单的动态数组,在其中存储将哪个TreeList ID连接到哪个Form HWND.如果我在列表中找到具有匹配HWND的ID,则只需将已经创建的表单带到前台.
So I thought about creating a simple Dynamic Array where I store which TreeList ID is connected to which Form HWND . If I find a ID on my list with a Matching HWND, then I simply bring the Form which is already Created to Foreground.
Application.CreateForm(TChapter, Chapter);
Chapter.PopupParent:=Main;
Chapter.FID:=qryTreeID.Value;
Chapter.Caption:=qryTreeName.Value+Cardinal(Chapter.Handle).ToString;
Chapter.Show;
这就是我创建Form的方式.这只是一个基本"示例.我只是想确保Handle是Unique,所以我打开了多个表格,编号始终是不同的.但我想确定.
This is how I create a Form . This is just a "basic" example . I just wanted to make sure that the Handle is Unique , I opened Multiple Forms the Numbers were always different. But I want to make sure.
谢谢!
推荐答案
如果要维护自己的查找,则TDictionary
比动态数组更有意义.但是无论哪种方式,您都应该将ID映射到实际的TForm
对象,而不是其HWND
.保证HWND
是唯一的,但不是持久的,因为它可以在Form的生命周期中更改.这也将使您免于必须从HWND
获取TForm
对象的额外步骤.
If you want to maintain your own lookup, a TDictionary
would make more sense than a dynamic array. But either way, you should map the ID to the actual TForm
object rather than to its HWND
. The HWND
is guaranteed to be unique, but not persistent, as it can change during the Form's lifetime. It would also save you from the extra step of having to get the TForm
object from the HWND
.
例如:
var
Chapters: TDictionary<Integer, TChapter> = nil;
procedure ChapterDestroyed(Self: Pointer; Sender: TObject);
begin
if Chapters <> nil then
Chapters.Remove(TChapter(Sender).FID);
end;
function FindChapterByID(ID: Integer): TChapter;
// var I: Integer;
begin
{
for I := 0 to Screen.FormCount-1 do
begin
if Screen.Forms[I] is TChapter then
begin
Result := TChapter(Screen.Forms[I]);
if Result.FID = ID then Exit;
end;
end;
Result := nil;
}
if not Chapters.TryGetValue(ID, Result) then
Result := nil;
end;
function CreateChapter(ID: Integer): TChapter;
var
Event: TNotifyEvent;
begin
TMethod(Event).Data := nil;
TMethod(Event).Code = @ChapterDestroyed;
Result := TChapter.Create(Main);
try
Result.FID := ID;
Result.PopupParent := Main;
Result.Caption := qryTreeName.Value + ID.ToString;
Result.OnDestroy := Event;
Chapters.Add(ID, Result);
except
Result.Free;
raise;
end;
end;
function ShowChapterByID(ID: Integer): TChapter;
begin
Result := FindChapterByID(ID);
if Result = nil then Result := CreateChapter(ID);
Result.Show;
end;
initialization
Chapters := TDictionary<Integer, TChapter>.Create;
finalization
FreeAndNil(Chapters);
Chapter := ShowChapterByID(qryTreeID.Value);
这篇关于Delphi Application.CreateForm是每个表单的唯一句柄吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!