Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-10

Windows API translation - or better way to update system tray?

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Reinier Olislagers

Hi all,

(See
http://lazarus.freepascal.org/index.php/topic,14847.msg79038.html#msg79038 for initial question)

My program starts and stops external programs that have icons. It would be nice to immediately clean up the icons from the system tray once those programs have stopped.

I've found this code:
http://www.computersecurityarticles.info/security/refreshing-the-taskbar-notification-area/ #define FW(x,y) FindWindowEx(x, NULL, y, L”")

void RefreshTaskbarNotificationArea()
{
    HWND hNotificationArea;
    RECT r;

GetClientRect(
hNotificationArea = FindWindowEx(
FW(FW(FW(NULL, L”Shell_TrayWnd”), L”TrayNotifyWnd”), L”SysPager”),
NULL,
L”ToolbarWindow32″,
L”Notification Area”),

        &r);

for (LONG x = 0; x < r.right; x += 5)
for (LONG y = 0; y < r.bottom; y += 5)
SendMessage(
hNotificationArea,
WM_MOUSEMOVE,
0,

                (y << 16) + x);

}

and got as far as:
uses
JwaTlHelp32 {for running processes},
JwaWinType {for processes declarations},
JwaWinBase {just a guess: for closing process handles}, JwaWinSvc {for services declarations, always required}, jwawinuser {for clearing tray icon/notification area}, ... some more...
procedure CleanSystemTray;
{description Clean dead icons from system tray/notification area} var

  hNotificationArea: HWND;
  r: RECT;
  x: integer;
  y: integer;

begin

  hNotificationArea:=FindWindowEx(
    FindWindowEx(FindWindowEx(FindWindowEx
    (nil,nil,'Shell_TrayWnd', ''),nil,'TrayNotifyWnd',

''),nil,'SysPager',''),
nil,
'ToolbarWindow32',

    'Notification Area');
  GetClientRect(hNotificationArea,r);
  //Now we've got the area, force it to update
  //by sending mouse messages to it.
  x:=0;
  y:=0;
  while x < r.Right do begin
    while y < r.Bottom do begin
      SendMessage(hNotificationArea, WM_MOUSEMOVE, 0, (y shl 16) + x);
      y:=y+5;
    end;
    x:=x+5;
  end;
end;

However compiler errors on the 2nd nil:
(nil,nil,'Shell_TrayWnd', ''),nil,'TrayNotifyWnd', Error: Incompatible type for arg no. 2: Got "Pointer", expected "LongWord"

Should I just pass 0 to those functions or bogus HWNDs... or do something else?
(If you haven't figured it out by now, I understand C++ even less than Pascal ;)

Finally, perhaps there is an easier way of cleaning up the tray icon?

Sven Barth
to Reinier Olislagers
Reinier Olislagers wrote 8 lines
However compiler errors on the 2nd nil: (nil,nil,'Shell_TrayWnd', ''),nil,'TrayNotifyWnd', Error: Incompatible type for arg no. 2: Got "Pointer", expected "LongWord" Should I just pass 0 to those functions or bogus HWNDs... or do something else? (If you haven't figured it out by now, I understand C++ even less than Pascal ;)

That's not C++, but C, but in both "NULL" can be used for both: pointers and ordinals. In Pascal HWND is an ordinal and there the correct equivalent is of course "0". So yes, you should use "0". Maybe you'll need to do this for the first param as well.

As a sidenote: It's always good to inform oneself about the used functions using MSDN. There you'd see what the meaning of "0" or "NULL" in this function is. Here it is btw:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633500%28v=vs.85%29.aspx

Regards,
Sven

Reinier Olislagers
to Sven Barth
Sven Barth wrote
That's not C++, but C, but in both "NULL" can be used for both: pointers and ordinals. In Pascal HWND is an ordinal and there the correct equivalent is of course "0". So yes, you should use "0". Maybe you'll need to do this for the first param as well.

Just goes to show: C, C++, all the same to me ;)
Thanks for the clear explanation. I just put in 0 for all HWNDs...

Show quoted text 5 lines
As a sidenote: It's always good to inform oneself about the used functions using MSDN. There you'd see what the meaning of "0" or "NULL" in this function is. Here it is btw: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633500%28v=vs.85%29.aspx

Yep, thanks.

Amazingly, the code even seems to work ;) - see
https://bitbucket.org/reiniero/checkride/

Thanks again, Sven!

Reinier