gpt4 book ai didi

delphi - Delphi XE5 中的 Firemonkey。如何为托盘中的图标创建弹出菜单?

转载 作者:行者123 更新时间:2023-12-03 15:58:09 25 4
gpt4 key购买 nike

注意到了这一点 - FireMonkey tray icon with a menu

但是这个主题根本没有帮助。

我做了一个功能,我的程序可以最小化到托盘(靠近时钟的角落),但我不明白如何为其执行弹出菜单(可以通过右键单击托盘图标来显示) 。我尝试了很多例子,但没有任何效果。

这是我的代码:

unit MainCode;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
FMX.Edit, FMX.Objects, FMX.ListBox, WinApi.ShellApi, WinApi.Windows, WinApi.Messages, FMX.Platform.Win, FMX.Menus, FMX.Platform;

const
WM_ICONTRAY = WM_USER + 1;

type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
MenuItem1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure MenuItem1Click(Sender: TObject);

private
TrayWnd: HWND;
TrayIconData: TNotifyIconData;
TrayIconAdded: Boolean;
procedure TrayWndProc(var Message: TMessage);

public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.FormCreate(Sender: TObject);
begin
TrayWnd := AllocateHWnd(TrayWndProc);
with TrayIconData do
begin
cbSize := SizeOf();
Wnd:= TrayWnd; // was before Wnd:= FmxHandleToHWND(self.Handle);
uID:= 0;
uFlags:= NIF_MESSAGE + NIF_ICON + NIF_TIP;
uCallbackMessage:= WM_ICONTRAY;
hIcon:= GetClassLong(FmxHandleToHWND(self.Handle), GCL_HICONSM);
szTip:= 'Hearthspinner';
end;

if not TrayIconAdded then
TrayIconAdded := Shell_NotifyIcon(NIM_ADD, @TrayIconData);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
if TrayIconAdded then
Shell_NotifyIcon(NIM_DELETE, @TrayIconData);
DeallocateHWnd(TrayWnd);
end;

procedure TForm1.MenuItem1Click(Sender: TObject);
begin
Application.Terminate;
end;

上面的代码工作正常。下一个代码不起作用:

procedure TForm1.TrayWndProc(var Message: TMessage);
var P: TPoint;
begin
if Message.MSG = WM_ICONTRAY then
begin

case Message.LParam of
WM_RBUTTONDOWN:
begin
GetCursorPos(P);
PopupMenu1.Popup(P.X,P.Y);
end;
end;
end
else
Message.Result := DefWindowProc(TrayWnd, Message.Msg, Message.WParam, Message.LParam);
end;

最佳答案

您使用了错误的窗口句柄。而不是

Wnd:= FmxHandleToHWND(self.Handle);

你需要

Wnd:= TrayWnd;

您还应该从 TrayWndProc 中删除消息 WM_ICONTRAY。它在 FMX 环境中没有任何作用,只会造成困惑。

关于delphi - Delphi XE5 中的 Firemonkey。如何为托盘中的图标创建弹出菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24688532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com