Home > Back-end >  How l ShowModa form must be displayed in the front
How l ShowModa form must be displayed in the front

Time:09-16

Application CreateForm (TFrm_editBom Frm_editBom);
Frm_editBom. ShowModal;
Frm_editBom. Free;
Normally are displayed on the front, sometimes I feel there is a point card shows not in the front, then can only quit, again

CodePudding user response:

 
//isn't it better like this:
With TFrm_editBom. Create (nil) do
The begin
ShowModal;
Free;
end;

CodePudding user response:

reference 1st floor BlueStorm response:
 
//isn't it better like this:
With TFrm_editBom. Create (nil) do
The begin
ShowModal;
Free;
end;


Good is good, but I can't solve the problem, as if just the code is omitted

CodePudding user response:

There is no way to

CodePudding user response:

The function ForceForegroundWindow (hWnd: THandle) : Boolean;
Const
SPI_GETFOREGROUNDLOCKTIMEOUT=$2000;
SPI_SETFOREGROUNDLOCKTIMEOUT=$2001;
Var
ForegroundThreadID: DWORD;
ThisThreadID: DWORD;
Timeout: DWORD;
The begin
If GetForegroundWindow=hWnd then
Result:=True
The else
The begin
Result:=False;
ForegroundThreadID:=GetWindowThreadProcessID (GetForegroundWindow, nil);
ThisThreadID:=GetWindowThreadPRocessId (hWnd, nil);
If AttachThreadInput (ThisThreadID ForegroundThreadID, True) then
The begin
The BringWindowToTop (hWnd);
The SetForegroundWindow (hWnd);
AttachThreadInput (ThisThreadID ForegroundThreadID, False);
Result:=(GetForegroundWindow=hWnd);
end;
If not the Result then
The begin
SystemParametersInfo (SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @ a timeout, 0).
SystemParametersInfo (SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject (0),
SPIF_SENDCHANGE);
The BringWindowToTop (hWnd);
The SetForegroundWindow (hWnd);
SystemParametersInfo (SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject (timeout), SPIF_SENDCHANGE);
end;
Result:=(GetForegroundWindow=hWnd);
end;
end;

CodePudding user response:

 
Or:
The function ForceForegroundWindow (hWnd: THandle) : Boolean;
Var
HelpForm: TForm;
The begin
HelpForm:=TForm. Create (nil);
Try
HelpForm. BorderStyle:=bsNone;
HelpForm. SetBounds (0, 0, 1, 1);
HelpForm. FormStyle:=fsStayOnTop;
HelpForm. Show;
Mouse_event (MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0).
Mouse_event (MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0).
The SetForegroundWindow (hWnd);
The finally
HelpForm. Free;
end;
Result:=True;
end;

CodePudding user response:

 
Example
The unit Unit4.

Interface

USES the
Winapi. Windows, Winapi Messages, System. SysUtils, System. Variants,
System. The Classes, the Vcl. Graphics, Vcl. Controls, Vcl. Forms, Vcl. Dialogs;

Const
WM_AFTER_SHOW=WM_USER + 1;

Type
TFrm_editBom=class (TForm)
Procedure FormShow (Sender: TObject);
Private
Procedure WmAfterShow (var Msg: TMessage); The message WM_AFTER_SHOW;
Public
{Public declarations}
end;

Var
Frm_editBom: TFrm_editBom;

The function ForceForegroundWindow (hWnd: THandle) : Boolean;

Implementation

{$R *. DFM}

Procedure TFrm_editBom. FormShow (Sender: TObject);
The begin
PostMessage (Handle, WM_AFTER_SHOW, 0, 0);
end;

Procedure TFrm_editBom. WmAfterShow (var Msg: TMessage);
The begin
Application. ProcessMessages;
ForceForegroundWindow (Handle);
end;

The function ForceForegroundWindow (hWnd: THandle) : Boolean;
Var
HelpForm: TForm;
The begin
HelpForm:=TForm. Create (nil);
Try
HelpForm. BorderStyle:=bsNone;
HelpForm. SetBounds (0, 0, 1, 1);
HelpForm. FormStyle:=fsStayOnTop;
HelpForm. Show;
Mouse_event (MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0).
Mouse_event (MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0).
The SetForegroundWindow (hWnd);
The finally
HelpForm. Free;
end;
Result:=True;
end;

End.

CodePudding user response:

Without AttachThreadInput first call on win10 SetForegroundWindow fail

CodePudding user response:

refer to 6th floor BlueStorm response:
 
Example
The unit Unit4.

Interface

USES the
Winapi. Windows, Winapi Messages, System. SysUtils, System. Variants,
System. The Classes, the Vcl. Graphics, Vcl. Controls, Vcl. Forms, Vcl. Dialogs;

Const
WM_AFTER_SHOW=WM_USER + 1;

Type
TFrm_editBom=class (TForm)
Procedure FormShow (Sender: TObject);
Private
Procedure WmAfterShow (var Msg: TMessage); The message WM_AFTER_SHOW;
Public
{Public declarations}
end;

Var
Frm_editBom: TFrm_editBom;

The function ForceForegroundWindow (hWnd: THandle) : Boolean;

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related