The Delphi application checks if a form instance is already open this way:
form := FindWindow(PAnsiChar(FormClassName), nil);
if form > 0 then
begin
SendMessage(form, WM_ACTIVATEAPP, 0, 0);
Result := True;
end else
Result := False
The problem is that when the form is open as a Delphi Designer windows it says that the form is open. I've just lost some working hours because when I opened a form and clicked to breakpoint a method, the application stopped to work:-(
How do I make this function return false if the form class instance is created in the Designer?
Answer:
opc0de's suggestion below leaded me to a good solution, so I'm accepting his comment as the answer. I needed to change the code to iterate through all open forms. Here is the final version of the code for the happiness of the copy'n pasters of the world :
function VerifyFormIsOpen(formClass: String): Boolean;
var
windowHndl, windowOld: HWND;
processId: Cardinal;
begin
windowOld := 0;
windowHndl := 0;
Result := False;
repeat
windowHndl := Windows.FindWindowEx(0, windowOld,
PAnsiChar(formClass), nil);
if (windowHndl > 0) then
begin
Windows.GetWindowThreadProcessId(windowHndl, processId);
if processId = Windows.GetCurrentProcessId() then
begin
Windows.SendMessage(windowHndl, Messages.WM_ACTIVATEAPP, 0, 0);
Result := True;
break;
end;
end;
windowOld := windowHndl;
until windowHndl = 0;
end;