topmost

With desktop (Windows) applications, a message (dialog) box is used to alert the user of the application that some action needs to be taken, that some operation was completed or, in general, to get users' attention.

对于桌面(Windows)应用程序, 消息(对话框)框用于警告应用程序用户需要采取某些措施,某些操作已完成或通常引起用户注意。

In Delphi, there are several ways of displaying a message to the user. You can either use any of the ready-made message displaying routines provided in the RTL, like ShowMessage or InputBox; or you can create your own dialog box (for reuse): CreateMessageDialog.

Delphi中 ,有几种向用户显示消息的方法。 您可以使用RTL中提供的任何现成的消息显示例程,例如ShowMessage或InputBox。 或者,您可以创建自己的对话框(以供重用):CreateMessageDialog。

A common problem with all the above dialog boxes is that they require the application to be active to be displayed to the user. "Active" refers to when your application has the "input focus."

上述所有对话框的一个常见问题是它们要求应用程序处于活动状态才能显示给用户 。 “活动”是指您的应用程序具有“输入焦点”。

If you really want to grab the user's attention and stop them from doing anything else, you need to be able to display a system-modal topmost message box even when your application is not active.

如果您确实想引起用户的注意并阻止他们执行其他任何操作,则即使您的应用程序未处于活动状态 ,您也需要能够显示系统模式的最高消息框



( System-Modal Top Most Message Box )

Even though this might sound complicated, in actuality it really is not.

即使这听起来很复杂,但实际上并非如此。

Since Delphi can easily access most of the Windows API calls, executing the "MessageBox" Windows API function will do the trick.

由于Delphi可以轻松访问大多数Windows API调用,因此执行“ MessageBox” Windows API函数即可解决问题。

Defined in the "windows.pas" unit -- the one included by default in the uses clause of every Delphi form, the MessageBox function creates, displays, and operates a message box. The message box contains an application-defined message and title, along with any combination of predefined icons and push buttons.

MessageBox函数定义在“ windows.pas”单元中,该单元默认包含在每个Delphi表单的uses子句中,该单元创建,显示和操作一个消息框。 消息框包含应用程序定义的消息和标题,以及预定义的图标和按钮的任意组合。

Here's how the MessageBox is declared:

以下是MessageBox的声明方式:

function MessageBox(



  hWnd: HWND;


hWnd:HWND;


  lpText,


lpText,


  lpCaption : PAnsiChar;


lpCaption:PAnsiChar;


  uType : Cardinal) : integer;


uType:Cardinal):整数;


The first parameter, hwnd, is the handle of the owner window of the message box to be created. if you create a message box while a dialog box is present, use a handle to the dialog box as the hWnd parameter.

第一个参数hwnd是要创建的消息框的所有者窗口的句柄。 如果在出现对话框时创建消息框,请使用对话框的句柄作为hWnd参数。

The lpText and lpCaption specify the caption and the message text that is displayed in the message box.

lpTextlpCaption指定标题和在消息框中显示的消息文本。

Last is the uType parameter and is the most interesting. This parameter specifies the contents and behavior of the dialog box. This parameter can be a combination of various flags.

最后是uType参数,这是最有趣的。 此参数指定对话框的内容和行为。 此参数可以是各种标志的组合。



( Example: System Modal Warning Box When the System Date/Time Changes )

Let's take a look at an example of creating a system modal topmost message box. You'll handle the Windows message that is dispatched to all the running applications when the system date/time changes—for example using the "Date and Time Properties" Control Panel applet.

让我们看一个创建系统模式最顶部消息框的示例。 当系统日期/时间更改时,您将处理分配给所有正在运行的应用程序的Windows消息 ,例如使用“日期和时间属性”控制面板小程序。

The MessageBox function will be called as:

MessageBox函数将被称为:

Windows.MessageBox(
handle,
'This is a system modal message'#13#10'from an inactive application',
'A message from an inactive application!',
MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONHAND) ;

The most important piece is the last parameter. The "MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST" ensures the message box is system modal, top most and becomes the foreground window.

最重要的部分是最后一个参数。 “ MB_SYSTEMMODAL或MB_SETFOREGROUND或MB_TOPMOST”确保消息框是系统模式的,位于最顶部,并成为前台窗口。

  • MB_SYSTEMMODAL flag ensures that the user must respond to the message box before continuing work in the window identified by the hWnd parameter.
    MB_SYSTEMMODAL标志可确保用户必须先响应消息框,然后才能在hWnd参数标识的窗口中继续工作。
  • MB_TOPMOST flag specifies that the message box should be placed above all non-topmost windows and should stay above them, even when the window is deactivated.
    MB_TOPMOST标志指定该消息框应放置在所有非最上面的窗口上方,并且即使在停用该窗口时也应保持在它们上方。
  • MB_SETFOREGROUND flag ensures that the message box becomes the foreground window.
    MB_SETFOREGROUND标志确保消息框成为前台窗口。

Here is the full example code (TForm named "Form1" defined in unit "unit1"):

这是完整的示例代码(在单元“ unit1”中定义的名为“ Form1”的TForm):

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes,
Graphics, Controls, Forms, Dialogs, ExtCtrls;
type
TForm1 = class(TForm)
private
procedure WMTimeChange(var Msg: TMessage) ; message WM_TIMECHANGE;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation{$R *.dfm}
procedure TForm1.WMTimeChange(var Msg: TMessage) ;
begin
Windows.MessageBox(
handle,
'This is a system modal message'#13#10'from an inactive application',
'A message from an inactive application!',
MB_SYSTEMMODAL or MB_SETFOREGROUND or MB_TOPMOST or MB_ICONHAND) ;
end;
end.

Try running this simple application. Make sure the application is minimized or at least that some other application is active. Run the "Date and Time Properties" Control Panel applet and change the system time. As soon as you hit the "Ok" button (on the applet) the system modal topmost message box from your inactive application will be displayed.

尝试运行这个简单的应用程序。 确保将应用程序最小化,或者至少使其他应用程序处于活动状态。 运行“日期和时间属性”控制面板小程序并更改系统时间。 按下“确定”按钮(在applet上 )后,将显示非活动应用程序中系统模式最上方的消息框。

翻译自: https://www.thoughtco.com/display-a-topmost-system-modal-message-1058468

topmost