我正在使用以下代码。我想要隐藏应用程序,但在系统中显示try(works),但是当我尝试在鼠标左键单击时显示主窗体时,没有任何反应。你能帮忙吗?我已经包含了所有代码。
主要表格代码:
unit Main_Unit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,shellApi,AppEvnts;
type
TMain = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
TrayIconData: TNotifyIconData;
procedure TrayMessage(var Msg: TMessage);
{ Private declarations }
public
{ Public declarations }
end;
var
Main: TMain;
const
WM_ICONTRAY = WM_USER + 1;
implementation
{$R *.dfm}
uses Functions;
procedure TMain.TrayMessage(var Msg: TMessage);
begin
case Msg.lParam of
WM_LBUTTONDOWN:
begin
ShowMessage('Left button clicked - let''s SHOW the Form!');
Main.Show;
end;
WM_RBUTTONDOWN:
begin
ShowMessage('Right button clicked - let''s HIDE the Form!');
Main.Hide;
end;
end;
end;
procedure TMain.FormCreate(Sender: TObject);
begin
with TrayIconData do
begin
cbSize := SizeOf();
Wnd := Handle;
uID := 0;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallbackMessage := WM_ICONTRAY;
hIcon := Application.Icon.Handle;
StrPCopy(szTip, Application.Title);
end;
Shell_NotifyIcon(NIM_ADD, @TrayIconData);
end;
procedure TMain.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @TrayIconData);
end;
end.
初始化代码:
program Test;
uses
Vcl.Forms,
Main_Unit in 'Main_Unit.pas' {Main},
Functions in 'Functions.pas';
{$R *.res}
begin
Application.Initialize;
Application.ShowMainForm := False;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TMain, Main);
Application.Run;
end.
谢谢
答案 0 :(得分:1)
您定义了一个消息处理程序,但未连接到消息ID。在表单类型的声明中,将TrayMessage
声明更改为:
procedure TrayMessage(var Msg: TMessage); message WM_ICONTRAY;
除此之外,我还有以下评论:
Main.Show
方法撰写Main.Hide
和TMain
。您只需删除Main.
并在隐式Self
对象上调用这些方法。or
而非arithmethic +
来组合标记。AllocateHWnd
获得的稳定窗口句柄。