我有一个在 Windows 中显示气球托盘的功能,它的结构也像这样:

const
NIF_INFO      =       $00000010;
NIIF_NONE     =       $00000000;
NIIF_INFO     =       $00000001;
NIIF_WARNING  =       $00000002;
NIIF_ERROR    =       $00000003;

type
 BalloonData = record
 cbSize: DWORD;
 Wnd: HWND;
 uID: UINT;
 uFlags: UINT;
 uCallbackMessage: UINT;
 hIcon: HICON;
 szTip: array[0..MAXCHAR] of AnsiChar;
 dwState: DWORD;
 dwStateMask: DWORD;
 szInfo: array[0..MAXBYTE] of AnsiChar;
 uTimeout: UINT;
 szInfoTitle: array[0..63] of AnsiChar;
 dwInfoFlags: DWORD;
end;

type
 TBalloonTimeout = 2..30;
 TBalloonIconType = (bitNone, bitInfo, bitWarning, bitError);

function DZBalloonTrayIcon(const Window: HWND; const IconID: Byte; const Timeout: TBalloonTimeout; const BalloonText, BalloonTitle: String; const BalloonIconType: TBalloonIconType): Boolean;
const
  aBalloonIconTypes : array[TBalloonIconType] of Byte = (NIIF_NONE, NIIF_INFO, NIIF_WARNING, NIIF_ERROR);
var
 TheBalloon : BalloonData;
begin
 FillChar(TheBalloon, SizeOf(BalloonData), 0);
 with TheBalloon do begin
  cbSize := SizeOf(BalloonData);
  Wnd := Window;
  uID := IconID;
  uFlags := NIF_INFO;
  StrCopy(szInfo, pchar(BalloonText));
  uTimeout := Timeout * 1000;
  StrCopy(szInfoTitle, pchar(BalloonTitle));
  dwInfoFlags := aBalloonIconTypes[BalloonIconType];
 end;
 Result := Shell_NotifyIcon(NIM_MODIFY, @TheBalloon);
end;

用法 :
procedure MakeBaloonTray;
var
 TrayIconData : TNotifyIconData;
begin
 DZBalloonTrayIcon(TrayIconData.Wnd, TrayIconData.uID, 2,'Test', 'Test', bitInfo);
end;

然后我将所有内容更改为 WideString:
const
 NIF_INFO      =       $00000010;
 NIIF_NONE     =       $00000000;
 NIIF_INFO     =       $00000001;
 NIIF_WARNING  =       $00000002;
 NIIF_ERROR    =       $00000003;

type
 BalloonData = record
 cbSize: DWORD;
 Wnd: HWND;
 uID: UINT;
 uFlags: UINT;
 uCallbackMessage: UINT;
 hIcon: HICON;
 szTip: array[0..MAXCHAR] of WideChar;
 dwState: DWORD;
 dwStateMask: DWORD;
 szInfo: array[0..MAXBYTE] of WideChar;
 uTimeout: UINT;
 szInfoTitle: array[0..63] of WideChar;
 dwInfoFlags: DWORD;
end;

type
 TBalloonTimeout = 2..30;
 TBalloonIconType = (bitNone, bitInfo, bitWarning, bitError);

function StrLCopyW(Dest, Source: PWideChar; MaxLen: Cardinal): PWideChar;
var
 Count: Cardinal;
begin
 Result := Dest;
 Count := 0;
 While (Count < MaxLen) and (Source^ <> #0) do begin
  Dest^ := Source^;
  Inc(Source);
  Inc(Dest);
  Inc(Count);
 end;
 Dest^ := #0;
end;


function StrCopyW(Dest, Source: PWideChar): PWideChar;
begin
 Result := StrLCopyW(Dest, Source, MaxInt);
end;

function DZBalloonTrayIcon(const Window: HWND; const IconID: Byte; const Timeout: TBalloonTimeout; const BalloonText, BalloonTitle: WideString; const BalloonIconType: TBalloonIconType): Boolean;
const
  aBalloonIconTypes : array[TBalloonIconType] of Byte = (NIIF_NONE, NIIF_INFO, NIIF_WARNING, NIIF_ERROR);
var
 TheBalloon : BalloonData;
begin
 FillChar(TheBalloon, SizeOf(BalloonData), 0);
 with TheBalloon do begin
  cbSize := SizeOf(BalloonData);
  Wnd := Window;
  uID := IconID;
  uFlags := NIF_INFO;
  StrCopyW(szInfo, pwidechar(BalloonText));
  uTimeout := Timeout * 1000;
  StrCopyW(szInfoTitle, pwidechar(BalloonTitle));
  dwInfoFlags := aBalloonIconTypes[BalloonIconType];
 end;
 Result := Shell_NotifyIcon(NIM_MODIFY, @TheBalloon);
end;

我也试过:
procedure MakeBaloonTray;
var
 TrayIconData   : TNotifyIconData;
 WideStringTest : WideString;
begin
 WideStringTest := 'someunicodechars';
 DZBalloonTrayIcon(TrayIconData.Wnd, TrayIconData.uID, 2,UTF8Encode(WideStringTest), UTF8Encode(WideStringTest), bitInfo);
end;

我以为 Windows 在 Balloons 中支持 UTF8,但我只有问号。
知道如何在气球中显示 WideString/Unicode 吗?
感谢您的帮助 :)

最佳答案

您需要明确使用 Shell_NotifyIconW(注意 W)函数及其相关的 WideString 结构,两者都在 ShellAPI.pas 中定义。

关于delphi - Unicode/WideString BalloonTray,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10840527/

10-13 08:10