windows Delphi:最小化应用程序到系统托盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2889291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delphi: Minimize application to systray
提问by caw
I want to minimize a Delphi application to the systray instead of the task bar.
我想将 Delphi 应用程序最小化到系统托盘而不是任务栏。
The necessary steps seem to be the following:
必要的步骤似乎如下:
- Create icon which should then be displayed in the systray.
- When the user clicks the [-] to minimize the application, do the following:
- Hide the form.
- Add the icon (step #1) to the systray.
- Hide/delete the application's entry in the task bar.
- When the user double-clicks the application's icon in the systray, do the following:
- Show the form.
- Un-minimize the application again and bring it to the front.
- If "WindowState" is "WS_Minimized" set to "WS_Normal".
- Hide/delete the application's icon in the systray.
- When the user terminates the application, do the following:
- Hide/delete the application's icon in the systray.
- 创建图标,然后应显示在系统托盘中。
- 当用户单击 [-] 以最小化应用程序时,请执行以下操作:
- 隐藏表格。
- 将图标(第 1 步)添加到系统托盘。
- 在任务栏中隐藏/删除应用程序的条目。
- 当用户双击系统托盘中的应用程序图标时,执行以下操作:
- 显示表格。
- 再次取消最小化应用程序并将其置于最前面。
- 如果“WindowState”是“WS_Minimized”设置为“WS_Normal”。
- 隐藏/删除系统托盘中的应用程序图标。
- 当用户终止应用程序时,请执行以下操作:
- 隐藏/删除系统托盘中的应用程序图标。
That's it. Right?
就是这样。对?
How could one implement this in Delphi?
如何在 Delphi 中实现这一点?
I've found the following code but I don't know why it works. It doesn't follow my steps described above ...
我找到了以下代码,但我不知道它为什么有效。它没有按照我上面描述的步骤...
unit uMinimizeToTray;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellApi;
const WM_NOTIFYICON = WM_USER+333;
type
TMinimizeToTray = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure CMClickIcon(var msg: TMessage); message WM_NOTIFYICON;
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
MinimizeToTray: TMinimizeToTray;
implementation
{$R *.dfm}
procedure TMinimizeToTray.CMClickIcon(var msg: TMessage);
begin
if msg.lparam = WM_LBUTTONDBLCLK then Show;
end;
procedure TMinimizeToTray.FormCreate(Sender: TObject);
VAR tnid: TNotifyIconData;
HMainIcon: HICON;
begin
HMainIcon := LoadIcon(MainInstance, 'MAINICON');
Shell_NotifyIcon(NIM_DELETE, @tnid);
tnid.cbSize := sizeof(TNotifyIconData);
tnid.Wnd := handle;
tnid.uID := 123;
tnid.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
tnid.uCallbackMessage := WM_NOTIFYICON;
tnid.hIcon := HMainIcon;
tnid.szTip := 'Tooltip';
Shell_NotifyIcon(NIM_ADD, @tnid);
end;
procedure TMinimizeToTray.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caNone;
Hide;
end;
end.
回答by Ignacio Vazquez-Abrams
If it still works, it's probably easiest to use JVCL's TJvTrayIconto handle it automatically.
如果它仍然有效,使用JVCL的TJvTrayIcon自动处理它可能是最简单的。
回答by user327359
I would recommend using CoolTrayIcon. The author has already worked out all the issues involved with tray icons. Its free with source and examples and very debugged.
我建议使用 CoolTrayIcon。关于托盘图标的问题,笔者已经解决了。它免费提供源代码和示例,并且经过调试。
回答by Alx
Instead of Application.BringToFront;
use SetforegroundWindow(Application.Handle);
而不是 Application.BringToFront;
使用SetforegroundWindow(Application.Handle);
回答by caw
In the following text I'll be referring to the step numbers mentioned in the question:
在下面的文本中,我将参考问题中提到的步骤编号:
The following solution is without any additional components. It's very easy to implement.
以下解决方案没有任何附加组件。这很容易实现。
Step #1:
第1步:
Just use the application's main icon (see following code).
只需使用应用程序的主图标(请参阅以下代码)。
Step #2:
第2步:
procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
Shell_NotifyIcon(NIM_ADD, @TrayIconData);
Form1.Hide;
end;
Step #3:
第 3 步:
procedure TForm1.TrayMessage(var Msg: TMessage);
begin
if Msg.lParam = WM_LBUTTONDOWN then begin
Form1.Show;
Form1.WindowState := wsNormal;
Application.BringToFront;
Shell_NotifyIcon(NIM_DELETE, @TrayIconData);
end;
end;
Step #4:
第四步:
procedure TForm1.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @TrayIconData);
end;
Necessary code in interface part:
接口部分必要的代码:
uses
[...], ShellApi;
const
WM_ICONTRAY = WM_USER + 1;
type
TForm1 = class(TForm)
[...]
procedure TrayMessage(var Msg: TMessage); message WM_ICONTRAY;
end;
The only problem: The application can be minimized to the systray only once. The next time you want to minimize it, nothing will happen. Why?
唯一的问题:应用程序只能最小化到系统托盘一次。下次你想最小化它时,什么都不会发生。为什么?
Source: delphi.about.com
资料来源:delphi.about.com