Search code examples
c#compact-frameworkwindows-ce

Refresh icons in notification area (Windows CE)


A web search finds several articles with sample code showing how to clear stray icons left in the Windows tray notification area when the application is killed (eg. by Task Manager or an updater application). For example this CodeProject example or this blog post.

Both examples above use a similar technique and are reported to work on Windows XP, 7, 8.1 and 10.

But how to get them working on Windows CE using the .NET Compact Framework? One problem is that FindWindowEx is required...but is not available in coredll.dll.


Solution

  • Based on the questions linked to in the question, I finally found a solution which works. I hope this helps someone else in future with a similar problem on Windows CE / Mobile.

    [DllImport("coredll.dll")]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
    [DllImport("coredll.dll")]
    public static extern IntPtr SendMessage(IntPtr hWnd, int nMsg, IntPtr wParam, IntPtr lParam);
    
    private const int WM_MOUSEMOVE = 0x0200;
    
    public static void RefreshTrayArea()
    {
        // The client rectangle can be determined using "GetClientRect" (from coredll.dll) but
        // does require the taskbar to be visible. The values used in the loop below were
        // determined empirically.
        IntPtr hTrayWnd = FindWindow("HHTaskBar", null);
        if (hTrayWnd != IntPtr.Zero)
        {
            int nStartX = (Screen.PrimaryScreen.Bounds.Width / 2);
            int nStopX = Screen.PrimaryScreen.Bounds.Width;
            int nStartY = 0;
            int nStopY = 26;    // From experimentation...
            for (int nX = nStartX; nX < nStopX; nX += 10)
                for (int nY = nStartY; nY < nStopY; nY += 5)
                    SendMessage(hTrayWnd,
                        WM_MOUSEMOVE, IntPtr.Zero, (IntPtr)((nY << 16) + nX));
        }
    }