Recent

Author Topic: Shell_NotifyIconW  (Read 9057 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Shell_NotifyIconW
« on: October 06, 2021, 06:35:58 pm »
How do I use Shell_NotifyIconW to put a message in the notification box on Win 10
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Shell_NotifyIconW
« Reply #1 on: October 06, 2021, 08:28:31 pm »
Is there something in the documentation that is unclear to you?

Notifications and the Notification Area

Shell_NotifyIconW function

NotificationIcon Sample

Can you please clarify your issue?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Shell_NotifyIconW
« Reply #2 on: October 06, 2021, 08:56:29 pm »
Im not a 'C' guy and I would need help to implement / translate it to FP

On Win 10 I have a notification app icon (see attached pic). I just want to put messages there
« Last Edit: October 06, 2021, 09:11:56 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Shell_NotifyIconW
« Reply #3 on: October 06, 2021, 09:36:48 pm »
Im not a 'C' guy and I would need help to implement / translate it to FP

Have you searched this forum yet? There are several discussions showing how to use Shell_NotifyIcon/W() in FreePascal.  For example:

Can Lazarus make a system tray app
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Shell_NotifyIconW
« Reply #4 on: October 07, 2021, 08:54:50 am »
None of the search results from this site work  :( (or at least not for me)
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Shell_NotifyIconW
« Reply #5 on: October 07, 2021, 07:39:37 pm »
None of the search results from this site work  :( (or at least not for me)

In what way exactly? Please show your actual code that you are having trouble with, and show any compiler errors you are getting.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Shell_NotifyIconW
« Reply #6 on: October 07, 2021, 08:01:29 pm »
So I used this sample https://forum.lazarus.freepascal.org/index.php/topic,9467.msg46442.html#msg46442
It compiles and runs but does nothing (yes I included Application.ShowMainForm := false;)
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Shell_NotifyIconW
« Reply #7 on: October 08, 2021, 05:56:27 pm »
So I used this sample https://forum.lazarus.freepascal.org/index.php/topic,9467.msg46442.html#msg46442
It compiles and runs but does nothing (yes I included Application.ShowMainForm := false;)

Offhand, the code looks OK.  Are you checking the return value of Shell_NotifyIcon() for failure?

Also, which version of Windows are you testing with?  Are you taking into account that modern Windows versions do not display notification icons near the clock unless the user chooses to display them there?  They may be in an overflow area instead, or even hidden completely.  Did you check the Taskbar config to make sure your icon is not being hidden?

Also, are you taking into account that in Windows 10+ (or was in 7+?), Shell_NotifyIcon() notifications are displayed in the Action Center instead of on the Taskbar?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Shell_NotifyIconW
« Reply #8 on: October 11, 2021, 01:24:15 pm »
OK this shows the message + balloon. How do I stop the balloon from showing?

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   TNotifyIconDataW2 = record
  4.     cbSize: DWORD;
  5.     hWnd: HWND;
  6.     uID: UINT;
  7.     uFlags: UINT;
  8.     uCallbackMessage: UINT;
  9.     hIcon: HICON;
  10.     szTip: array [0..127] of WideChar;
  11.     dwState: DWORD;
  12.     dwStateMask: DWORD;
  13.     szInfo: array [0..255] of WideChar;
  14.     u: record
  15.          case longint of
  16.            0 : ( uTimeout : UINT );
  17.            1 : ( uVersion : UINT );
  18.           end;
  19.     szInfoTitle: array[0..63] of WideChar;
  20.     dwInfoFlags: DWORD;
  21.   end;    
  22.  
  23. const
  24.   uIDTrayIcon = 25;
  25.  
  26. function WideStrLCopy(dest, source: PWideChar; maxlen: SizeInt): PWideChar;
  27. var
  28.   counter: SizeInt;
  29. begin
  30.   counter := 0;
  31.  
  32.   while (Source[counter] <> #0)  and (counter < MaxLen) do
  33.   begin
  34.     Dest[counter] := Source[counter];
  35.     Inc(counter);
  36.   end;
  37.  
  38.   { terminate the string }
  39.   Dest[counter] := #0;
  40.   Result := Dest;
  41. end;  
  42.  
  43. function ShowBalloonHint(const ATrayIcon: TCustomTrayIcon): Boolean;
  44. const
  45.   FlagsMap: array[TBalloonFlags] of dword = (NIIF_NONE, NIIF_INFO, NIIF_WARNING, NIIF_ERROR);
  46. var
  47.   NotifyData: TNotifyIconDataW2;
  48.   w: WideString;
  49. begin
  50.   NotifyData.cbSize:=SizeOf(NotifyData);
  51.   NotifyData.hWnd := ATrayIcon.Handle;
  52.   NotifyData.uID := uIDTrayIcon;
  53.   NotifyData.uFlags:=NIF_INFO;
  54.   NotifyData.u.uTimeout:=ATrayIcon.BalloonTimeout;
  55.   UTF8ToUTF16(ATrayIcon.BalloonHint);
  56.   WideStrLCopy(@NotifyData.szInfo, PWideChar(w), High(NotifyData.szInfo));
  57.   w:=UTF8ToUTF16(ATrayIcon.BalloonTitle);
  58.   WideStrLCopy(@NotifyData.szInfoTitle, PWideChar(w), High(NotifyData.szInfoTitle));
  59.   NotifyData.dwInfoFlags:=FlagsMap[ATrayIcon.BalloonFlags];
  60.  
  61.   Result:= Shell_NotifyIconW(NIM_MODIFY, @NotifyData);
  62. end;
  63.  
« Last Edit: October 11, 2021, 01:50:33 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Shell_NotifyIconW
« Reply #9 on: October 11, 2021, 07:34:57 pm »
OK this shows the message + balloon. How do I stop the balloon from showing?

https://stackoverflow.com/a/902804/65863

Quote
To immediately hide a balloon, set the szInfo member of the NOTIFYICONDATA to an empty string, like nid.szInfo[0] = 0; and call Shell_NotifyIcon( NIM_MODIFY, &nid ).
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Shell_NotifyIconW
« Reply #10 on: October 11, 2021, 08:02:52 pm »
I tried this

Code: Pascal  [Select][+][-]
  1.   w:='';
  2.   WideStrLCopy(@NotifyData.szInfo, PWideChar(w), High(NotifyData.szInfo));  
  3.  

and it doesn't work
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

sstvmaster

  • Sr. Member
  • ****
  • Posts: 299
Re: Shell_NotifyIconW
« Reply #11 on: October 11, 2021, 09:08:54 pm »
maybe?
Code: Pascal  [Select][+][-]
  1. ...
  2. NotifyData.szInfo[0] := WideChar(0);
  3. ...
  4. Shell_NotifyIconW(NIM_MODIFY, @NotifyData);
  5.  
« Last Edit: October 11, 2021, 09:19:22 pm by sstvmaster »
greetings Maik

Windows 10,
- Lazarus 2.2.6 (stable) + fpc 3.2.2 (stable)
- Lazarus 2.2.7 (fixes) + fpc 3.3.1 (main/trunk)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Shell_NotifyIconW
« Reply #12 on: October 11, 2021, 10:18:37 pm »
Still doesn't work
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Shell_NotifyIconW
« Reply #13 on: October 12, 2021, 07:30:09 am »
Here's the full source = it shows the tray icon. When you click it it shows the app. When you click the button
it shows the notification, but still shows the balloon. If I uncomment NotifyData.szInfo[0] := #0; (line 130)  then nothing shows.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  9.   Windows, ShellAPI, LazUTF8;
  10.  
  11. const
  12.   uIDTrayIcon = 25;
  13.   WM_ICONTRAY = WM_USER + uIDTrayIcon;
  14.  
  15. type
  16.  
  17.   TNotifyIconDataW2 = record
  18.     cbSize: DWORD;
  19.     hWnd: HWND;
  20.     uID: UINT;
  21.     uFlags: UINT;
  22.     uCallbackMessage: UINT;
  23.     hIcon: HICON;
  24.     szTip: array [0..127] of WideChar;
  25.     dwState: DWORD;
  26.     dwStateMask: DWORD;
  27.     szInfo: array [0..255] of WideChar;
  28.     u: record
  29.          case longint of
  30.            0 : ( uTimeout : UINT );
  31.            1 : ( uVersion : UINT );
  32.           end;
  33.     szInfoTitle: array[0..63] of WideChar;
  34.     dwInfoFlags: DWORD;
  35.   end;
  36.  
  37.   { TForm1 }
  38.  
  39.   TForm1 = class(TForm)
  40.     Button1: TButton;
  41.     procedure Button1Click(Sender: TObject);
  42.     procedure FormDestroy(Sender: TObject);
  43.     procedure FormShow(Sender: TObject);
  44.   private
  45.     function AddIcon : Boolean;
  46.     function ShowMessageN : Boolean;
  47.     procedure TrayMessage(var Msg: TMessage); message WM_ICONTRAY;
  48.   public
  49.  
  50.   end;
  51.  
  52. var
  53.   Form1: TForm1;
  54.   bFirst  : Boolean = True;
  55.   NotifyIcon: TNotifyIconDataW2;
  56.  
  57. implementation
  58.  
  59. {$R *.lfm}
  60.  
  61. { TForm1 }
  62.  
  63. function WideStrLCopy(dest, source: PWideChar; maxlen: SizeInt): PWideChar;
  64. var
  65.   counter: SizeInt;
  66. begin
  67.   counter := 0;
  68.  
  69.   while (Source[counter] <> #0)  and (counter < MaxLen) do
  70.   begin
  71.     Dest[counter] := Source[counter];
  72.     Inc(counter);
  73.   end;
  74.  
  75.   Dest[counter] := #0;
  76.   Result := Dest;
  77. end;
  78.  
  79. procedure TForm1.Button1Click(Sender: TObject);
  80. begin
  81.   ShowMessageN;
  82. end;
  83.  
  84. procedure TForm1.FormDestroy(Sender: TObject);
  85. begin
  86.   Windows.Shell_NotifyIcon(NIM_DELETE, @NotifyIcon);
  87. end;
  88.  
  89. procedure TForm1.FormShow(Sender: TObject);
  90. begin
  91.   if bFirst then
  92.   begin
  93.     Hide;
  94.     bFirst := False;
  95.   end;
  96.  
  97.   AddIcon;
  98. end;
  99.  
  100. procedure TForm1.TrayMessage(var Msg: TMessage);
  101. begin
  102.   case Msg.lParam of
  103.       WM_LBUTTONDOWN : begin
  104.                          Show;
  105.                        end;
  106.       WM_RBUTTONDOWN : begin
  107.                          Hide;
  108.                        end;
  109.     end;
  110. end;
  111.  
  112. function TForm1.ShowMessageN : Boolean;
  113. const
  114.   FlagsMap: array[TBalloonFlags] of dword = (NIIF_NONE, NIIF_INFO, NIIF_WARNING, NIIF_ERROR);
  115. var
  116.   NotifyData: TNotifyIconDataW2;
  117.   w: WideString;
  118. begin
  119.   NotifyData.cbSize:=SizeOf(NotifyData);
  120.   NotifyData.hWnd := Self.Handle;
  121.   NotifyData.uID := uIDTrayIcon;
  122.   NotifyData.uFlags:=NIF_INFO;
  123.  
  124.   w:=UTF8ToUTF16('BalloonHint');
  125.   WideStrLCopy(@NotifyData.szInfo, PWideChar(w), High(NotifyData.szInfo));
  126.  
  127.   w:=UTF8ToUTF16('BalloonTitle');
  128.   WideStrLCopy(@NotifyData.szInfoTitle, PWideChar(w), High(NotifyData.szInfoTitle));
  129.   NotifyData.dwInfoFlags:=FlagsMap[bfInfo];
  130.  // NotifyData.szInfo[0] := #0; // < This doesn't work
  131.   Result := Shell_NotifyIconW(NIM_MODIFY, @NotifyData);
  132. end;
  133.  
  134. function TForm1.AddIcon : Boolean;
  135. var
  136.   WideBuffer: widestring;
  137. begin
  138.   FillChar(NotifyIcon, SizeOf(NotifyIcon), 0);
  139.   NotifyIcon.cbSize := SizeOf(NotifyIcon);
  140.   NotifyIcon.hWnd := Self.Handle;
  141.   NotifyIcon.uID := uIDTrayIcon;
  142.   NotifyIcon.uFlags := NIF_MESSAGE or NIF_ICON;
  143.   NotifyIcon.uFlags := NotifyIcon.uFlags or NIF_TIP;
  144.   NotifyIcon.uCallbackMessage := WM_USER + uIDTrayIcon;
  145.   NotifyIcon.hIcon := Application.Icon.Handle;
  146.  
  147.   WideBuffer := UTF8ToUTF16('Hint');
  148.   WideStrLCopy(@NotifyIcon.szTip, PWideChar(WideBuffer), 127);
  149.  
  150.   Result := Shell_NotifyIconW(NIM_ADD, @NotifyIcon);
  151. end;
  152.  
  153. end.
  154.  
  155.  
« Last Edit: October 12, 2021, 07:40:52 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

dbannon

  • Hero Member
  • *****
  • Posts: 2791
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Shell_NotifyIconW
« Reply #14 on: October 12, 2021, 12:47:23 pm »

pcurtis, this may, for what ever reason be a silly suggestion and if so, I apologize. But the standard Lazarus TrayIcon appears to my (windows uneducated) eyes do what you want to do, "out of the box".  Have you done one of -

a. Considered taking the easy way out and using it, "Additional", roughly the middle component.

b. Having a look through the Lazarus component's code to see how its done there ?

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018