Shell_NotifyIconW lazarus / 21-General ---------------------------------------------------------------------- pcurtis · 2021-10-06 16:35 How do I use Shell_NotifyIconW to put a message in the notification box on Win 10 ---------------------------------------------------------------------- Remy Lebeau · 2021-10-06 18:28 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? ---------------------------------------------------------------------- pcurtis · 2021-10-06 18:56 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 ---------------------------------------------------------------------- Remy Lebeau · 2021-10-06 19:36 Quote from: pcurtis on October 06, 2021, 08:56:29 pmIm 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 ---------------------------------------------------------------------- pcurtis · 2021-10-07 06:54 None of the search results from this site work  (or at least not for me) ---------------------------------------------------------------------- Remy Lebeau · 2021-10-07 17:39 Quote from: pcurtis on October 07, 2021, 08:54:50 amNone 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. ---------------------------------------------------------------------- pcurtis · 2021-10-07 18:01 So I used this sample https://forum.lazarus.freepascal.org/index.php/t opic,9467.msg46442.html#msg46442 It compiles and runs but does nothing (yes I included Application.ShowMainForm := false;) ---------------------------------------------------------------------- Remy Lebeau · 2021-10-08 15:56 Quote from: pcurtis on October 07, 2021, 08:01:29 pmSo I used this sample https://forum.lazarus.freepascal.org/index.php/topic,9467.msg46 442.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? ---------------------------------------------------------------------- pcurtis · 2021-10-11 11:24 OK this shows the message + balloon. How do I stop the balloon from showing? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type   TNotifyIconDataW2 = record    cbSize: DWORD;    hWnd: HWND;    uID: UINT;    uFlags: UINT;    uCallbackMessage: UINT;    hIcon: HICON;    szTip: array [0..127] of WideChar;    dwState: DWORD;    dwStateMask: DWORD;    szInfo: array [0..255] of WideChar;    u: record         case longint of           0 : ( uTimeout : UINT );           1 : ( uVersion : UINT );          end;    szInfoTitle: array[0..63] of WideChar;    dwInfoFlags: DWORD;  end;      const  uIDTrayIcon = 25; function WideStrLCopy(dest, source: PWideChar; maxlen: SizeInt): PWideChar;var  counter: SizeInt;begin  counter := 0;   while (Source[counter] <> #0)  and (counter < MaxLen) do  begin    Dest[counter] := Source[counter];    Inc(counter);  end;   { terminate the string }  Dest[counter] := #0;  Result := Dest;end;    function ShowBalloonHint(const ATrayIcon: TCustomTrayIcon): Boolean;const  FlagsMap: array[TBalloonFlags] of dword = (NIIF_NONE, NIIF_INFO, NIIF_WARNING, NIIF_ERROR);var  NotifyData: TNotifyIconDataW2;  w: WideString;begin  NotifyData.cbSize:=SizeOf(NotifyData);  NotifyData.hWnd := ATrayIcon.Handle;  NotifyData.uID := uIDTrayIcon;  NotifyData.uFlags:=NIF_INFO;  NotifyData.u.uTimeout:=ATrayIcon.BalloonTimeout;  UTF8ToUTF16(ATrayIcon.BalloonHint);  WideStrLCopy(@NotifyData.szInfo, PWideChar(w), High(NotifyData.szInfo));  w:=UTF8ToUTF16(ATrayIcon.BalloonTitle);  WideStrLCopy(@NotifyData.szInfoTitle, PWideChar(w), High(NotifyData.szInfoTitle));  NotifyData.dwInfoFlags:=FlagsMap[ATrayIcon.BalloonFlags];   Result:= Shell_NotifyIconW(NIM_MODIFY, @NotifyData);end; ---------------------------------------------------------------------- Remy Lebeau · 2021-10-11 17:34 Quote from: pcurtis on October 11, 2021, 01:24:15 pmOK this shows the message + balloon. How do I stop the balloon from showing? https://stackoverflow.com/a/902804/65863 QuoteTo 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 ). ---------------------------------------------------------------------- pcurtis · 2021-10-11 18:02 I tried this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   w:='';  WideStrLCopy(@NotifyData.szInfo, PWideChar(w), High(NotifyData.szInfo)); and it doesn't work ---------------------------------------------------------------------- sstvmaster · 2021-10-11 19:08 maybe? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...NotifyData.szInfo[0] := WideChar(0);...Shell_NotifyIconW(NIM_MODIFY, @NotifyData); ---------------------------------------------------------------------- pcurtis · 2021-10-11 20:18 Still doesn't work ---------------------------------------------------------------------- pcurtis · 2021-10-12 05:30 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,  Windows, ShellAPI, LazUTF8; const  uIDTrayIcon = 25;  WM_ICONTRAY = WM_USER + uIDTrayIcon; type   TNotifyIconDataW2 = record    cbSize: DWORD;    hWnd: HWND;    uID: UINT;    uFlags: UINT;    uCallbackMessage: UINT;    hIcon: HICON;    szTip: array [0..127] of WideChar;    dwState: DWORD;    dwStateMask: DWORD;    szInfo: array [0..255] of WideChar;  u: record         case longint of           0 : ( uTimeout : UINT );           1 : ( uVersion : UINT );          end;    szInfoTitle: array[0..63] of WideChar;    dwInfoFlags: DWORD;  end;   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);    procedure FormDestroy(Sender: TObject);    procedure FormShow(Sender: TObject);  private    function AddIcon : Boolean;    function ShowMessageN : Boolean;    procedure TrayMessage(var Msg: TMessage); message WM_ICONTRAY;  public   end; var  Form1: TForm1;  bFirst  : Boolean = True;  NotifyIcon: TNotifyIconDataW2; implementation {$R *.lfm} { TForm1 } function WideStrLCopy(dest, source: PWideChar; maxlen: SizeInt): PWideChar;var  counter: SizeInt;begin  counter := 0;   while (Source[counter] <> #0)  and (counter < MaxLen) do  begin    Dest[counter] := Source[counter];    Inc(counter);  end;   Dest[counter] := #0;  Result := Dest;end; procedure TForm1.Button1Click(Sender: TObject);begin  ShowMessageN;end; procedure TForm1.FormDestroy(Sender: TObject);begin  Windows.Shell_NotifyIcon(NIM_DELETE, @NotifyIcon);end; procedure TForm1.FormShow(Sender: TObject);begin  if bFirst then  begin    Hide;    bFirst := False;  end;   AddIcon;end; procedure TForm1.TrayMessage(var Msg: TMessage);begin  case Msg.lParam of      WM_LBUTTONDOWN : begin                         Show;                     end;      WM_RBUTTONDOWN : begin                         Hide;                     end;    end;end; function TForm1.ShowMessageN : Boolean;const  FlagsMap: array[TBalloonFlags] of dword = (NIIF_NONE, NIIF_INFO, NIIF_WARNING, NIIF_ERROR);var  NotifyData: TNotifyIconDataW2;  w: WideString;begin  NotifyData.cbSize:=SizeOf(NotifyData);  NotifyData.hWnd := Self.Handle;  NotifyData.uID := uIDTrayIcon;  NotifyData.uFlags:=NIF_INFO;   w:=UTF8ToUTF16('BalloonHint');  WideStrLCopy(@NotifyData.szInfo, PWideChar(w), High(NotifyData.szInfo));   w:=UTF8ToUTF16('BalloonTitle');  WideStrLCopy(@NotifyData.szInfoTitle, PWideChar(w), High(NotifyData.szInfoTitle));  NotifyData.dwInfoFlags:=FlagsMap[bfInfo]; // NotifyData.szInfo[0] := #0; // < This doesn't work  Result := Shell_NotifyIconW(NIM_MODIFY, @NotifyData);end; function TForm1.AddIcon : Boolean;var  WideBuffer: widestring;begin  FillChar(NotifyIcon, SizeOf(NotifyIcon), 0);  NotifyIcon.cbSize := SizeOf(NotifyIcon);  NotifyIcon.hWnd := Self.Handle;  NotifyIcon.uID := uIDTrayIcon;  NotifyIcon.uFlags := NIF_MESSAGE or NIF_ICON;  NotifyIcon.uFlags := NotifyIcon.uFlags or NIF_TIP;  NotifyIcon.uCallbackMessage := WM_USER + uIDTrayIcon;  NotifyIcon.hIcon := Application.Icon.Handle;   WideBuffer := UTF8ToUTF16('Hint');  WideStrLCopy(@NotifyIcon.szTip, PWideChar(WideBuffer), 127);   Result := Shell_NotifyIconW(NIM_ADD, @NotifyIcon);end; end. ---------------------------------------------------------------------- dbannon · 2021-10-12 10:47 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 ---------------------------------------------------------------------- pcurtis · 2021-10-12 11:00 Show me the code how to get the notification without the balloon using trayicon ---------------------------------------------------------------------- dbannon · 2021-10-12 11:39 maybe - Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin    TrayIcon1.ShowBalloonHint := 'Hello, I am a balloon hint');    TrayIcon1.BalloonTimeout := 50;                    // thats mS    TrayIcon1.ShowBalloonHint;end;   Tested on Linux were it produces a very ugly and obtrusive oversized yellow box (and I use libnotifier instead), 50mS is just noticable, 10mS is not but I assume the event is still fired.  I allow myself only one boot into windows each day and I did that earlier to see if it works through the windows notifier system (yes, it does).  I suggest you try it yourself. Davo ---------------------------------------------------------------------- pcurtis · 2021-10-12 12:34 I have, and it doesn't work ---------------------------------------------------------------------- balazsszekely · 2021-10-12 19:25 @pcurtis On Win10 you have to enable balloon hints firts. You can do it with the Group Policy Editor. ---------------------------------------------------------------------- pcurtis · 2021-10-12 19:34 they are enabled. i want to stop them programmatically attached is a screenshot of a balloon ---------------------------------------------------------------------- dbannon · 2021-10-13 00:31 Quote from: pcurtis on October 12, 2021, 02:34:46 pmI have, and it doesn't work Well, its not clear at all to me just what 'it' is.  As I understand it, you are talking about W10's ability to alert the user of some minor event. First we see a slide out box from the right side of screen, it goes away in a few seconds, then the user can go looking for that notification and see it in a larger, ad filled slide out ? I understand you want the notification available if the user goes looking but do not want the slide out to appear ? So, when you say "it doesn't work", just what does not work ?  Do you see the initial slideout for the full 3 seconds and cannot reduce that ?  Is the notification not recorded at all ? Does smoke come out of your computer ? Your average word count is about ten words per post in this thread, can you be  little more expansive ? Davo ---------------------------------------------------------------------- pcurtis · 2021-10-13 03:00 Yes I want to stop the slide out from appearing (hide the balloon). ---------------------------------------------------------------------- balazsszekely · 2021-10-13 06:44 @pcurtis You seem to confuse Balloon notifications with Toast notifications. 1. Balloon notifications If you wish to enable Balloon notifications on Win10, press "Windows Key + R", then type "gpedit.msc" + enter. After the "Group Policy Editor" appears, follow the instructions from the first screenshot. Please note that in order for the changes to take effect a restart is needed. Balloon notification is a well documented and tested feature. You can find a detailed Delphi implementation here:    https://github.com/coolshou/CoolTrayIcon/blob/master/Source/CoolTra yIcon.pas Search for ShowBalloonHint and HideBalloonHint functions. It shouldn't take to much time to convert it to Lazarus. Actually I tested the code on Win10 and it works fine:    https://youtu.be/MRdoWBYSDcI 2. Toast notification This is the default notification in Win10, but you must also enable it in "Notifications & actions", by switching "Get notifications from apps and other senders" to "On". The same code(see above) will show a toast notification in the right panel, however it does not hide it(see second screenshot). You should do a google search: "hide toast notification" or "hide toast notification with delphi", maybe the API has changed. ---------------------------------------------------------------------- pcurtis · 2021-10-13 09:18 OK I wan't to stop the slide out dialog. QuoteYou can find a detailed Delphi implementation here:    https://github.com/coolshou/CoolTrayIcon/blob/master/Source/CoolTra yIcon.pas Search for ShowBalloonHint and HideBalloonHint functions. It shouldn't take to much time to convert it to Lazarus. Actually I tested the code on Win10 and it works fine:    https://youtu.be/MRdoWBYSDcI   Thats above my level. Thats why I asked here for help ---------------------------------------------------------------------- balazsszekely · 2021-10-13 11:37 @pcurtis I ran a few tests with Delphi 11 Alexandria, which supports the Win10 notification center via TNotificationCenter/TNotification classes. Showing a toast message is quite simple: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  Notification: TNotification;begin  Notification := NotificationCenter.CreateNotification;  try    Notification.Name := 'myNotification';    Notification.Title := 'Notification';    Notification.AlertBody := 'Test test test';    NotificationCenter.PresentNotification(Notification);  finally    MyNotification.Free;  end;end;The above code works fine. According to de documentation, the following code should remove the notification: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i NotificationCenter.CancelAll; {Use CancelAll to cancels all notifications:- The notifications that are scheduled will not be fired.- The notifications, that belong to this application, and are available in the notification center or notification drawer are also cancelled and removed.}Unfortunately it does not work, something fishy is happening here. QuoteThats above my level. Thats why I asked here for helpI don't have time to translate that code to Lazarus, but as you can see it won't work with toast notification. Even the dedicated components from Delphi does not work and I have no idea why. ---------------------------------------------------------------------- dbannon · 2021-10-13 12:06 > Unfortunately it does not work, something fishy is happening here. Well, I had a poke around the Lazarus TrayIcon code, its identical to the block of code circulating on this thread ending with Shell_NotifyIconW(NIM_MODIFY, @NotifyData); pcurtis's "does not work" could be translated to "if you set the NotifyData.szInfo[0] := 0 it does not compile (obviously) and if you set it to WideChar(0); it compiles and runs fine but no notification is recorded by Windows.". Clearly, some time in the past, setting NotifyData.szInfo must have "worked" and setting the timeout (which is ignored) must have done what we'd expect. That says to me that MS have redefined the API, probably in a effort to make the notifier experience more uniform. They don't want different applications doing different things, if you send a notification expect a "banner" (thats the small slider) to be visible for 3-4 seconds (as long as the user has not turned the banner off). Incidentally, on my Windows install I have changed almost none of the settings, by default, that banner or Toaster as Getmem calls it, is enabled for all apps. pcurtis, what I suggest you do is accept the banner is going to appear. You can however determine what it says, so, how about settings it's title to the name of some Windows Service ?  The users who find it really annoying will blame that service and not your app ! Davo ---------------------------------------------------------------------- balazsszekely · 2021-10-13 12:22 @dbannon Quotethat banner or Toaster as Getmem calls it, is enabled for all apps. I'm afraid this is the official name : https://docs.microsoft.com/en-us/windows/apps/design/shell/tiles-and- notifications/adaptive-interactive-toasts?tabs=builder-syntax https://docs.microsoft.com/en-us/windows/apps/design/shell/tiles-and- notifications/toast-desktop-apps https://docs.trendmicro.com/all/ent/officescan/v11.0/en- us/osce_11.0_sp1_cp_agent_olh/Enabling-Toast-Notif1.html Perhaps because it looks like a toast or you're toasted and cannot hide the message. ---------------------------------------------------------------------- pcurtis · 2021-10-13 12:48 https://docs.microsoft.com/en-us/windows/win32/api/shellapi/ns- shellapi-notifyicondataa NIF_INFO (0x00000010) 0x00000010. Display a balloon notification. The szInfo, szInfoTitle, dwInfoFlags, and uTimeout members are valid. Note that uTimeout is valid only in Windows 2000 and Windows XP.     To display the balloon notification, specify NIF_INFO and provide text in szInfo.     To remove a balloon notification, specify NIF_INFO and provide an empty string through szInfo.     To add a notification area icon without displaying a notification, do not set the NIF_INFO flag. Does this mean in W10 no can do? ---------------------------------------------------------------------- balazsszekely · 2021-10-13 13:05 QuoteTo display the balloon notification, specify NIF_INFO and provide text in szInfo. To add a notification area icon without displaying a notification, do not set the NIF_INFO flag. Works with xp, win7(balloon) and win8, win10(balloon + toast). QuoteTo remove a balloon notification, specify NIF_INFO and provide an empty string through szInfo.That's exactly what cooltrayicon does: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TCoolTrayIcon.HideBalloonHint: Boolean;// Hide balloon hint. Return false if error.begin  with IconData do  begin    uFlags := uFlags or NIF_INFO;    StrPCopy(szInfo, '');  end;  Result := ModifyIcon;end;Works with xp, win7, win8, win10(balloon),  does not work with win10 toast notifications. I have nothing more to say, Picard out. ---------------------------------------------------------------------- dbannon · 2021-10-13 22:39 Quote from: GetMem on October 13, 2021, 02:22:22 pmQuotethat banner or Toaster as Getmem calls it, is enabled for all apps. I'm afraid this is the official name : I respectfully suggest that Windows users (as opposed to developers) are more likely to know it as the Notification Banner. But I do like your reasoning for the name "Toast"