0

I have a C# application running on Windows CE 6.0 with CF 3.5, I think. The application starts minimized in the system tray with an icon. It's working so far but now I want to add context menu to this icon and I really don't get behind the secret with this context menu. The Icon is created with this code:

 public event EventHandler Click;

    private MyMessageWindow messageWindow;
    private int uID = 5000;

    private System.Drawing.Icon m_Icon;

    public NotifyIcon()
    {
      messageWindow = new MyMessageWindow(this);
      messageWindow.uID = uID;
    }

    public System.Drawing.Icon Icon
    {
      set { m_Icon = value; }
    }

    ~NotifyIcon()
    {
      Remove();
    }

    public void Add(IntPtr hIcon)
    {
      NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint) uID, hIcon);
    }

    public void Add(string IconRes)
    {
      IntPtr hIcon = LoadIcon(GetModuleHandle(null), IconRes);
      NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint) uID, hIcon);
    }

    public void Add(System.Drawing.Icon icon)
    {
      NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint) uID, icon.Handle);
    }

    public void Add()
    {
      if (m_Icon != null)
      {
        NotifyMessage(messageWindow.Hwnd, NIM_ADD, (uint) uID, m_Icon.Handle);
      }
    }

    public void Remove()
    {
      NotifyMessage(messageWindow.Hwnd, NIM_DELETE, (uint) uID, IntPtr.Zero);
    }

    public void Modify(IntPtr hIcon)
    {
      NotifyMessage(messageWindow.Hwnd, NIM_MODIFY, (uint) uID, hIcon);
    }



    private void NotifyMessage(IntPtr hwnd, int dwMessage, uint uID, IntPtr hIcon)
    {
      NOTIFYICONDATA notdata = new NOTIFYICONDATA();

      notdata.cbSize = 152;
      notdata.hIcon = hIcon;
      notdata.hWnd = hwnd;
      notdata.uCallbackMessage = WM_NOTIFY_TRAY;
      notdata.uFlags = NIF_MESSAGE | NIF_ICON;
      notdata.uID = uID;

      int ret = Shell_NotifyIcon(dwMessage, ref notdata);
    }

    //Definition of the message.
    private const int NIF_MESSAGE = 0x00000001;
    private const int NIF_ICON = 0x00000002;
    internal const int WM_LBUTTONDOWN = 0x0201;

    internal const int NIM_ADD = 0x00000000;
    internal const int NIM_MODIFY = 0x00000001;
    internal const int NIM_DELETE = 0x00000002;

    //Custom message
    internal const int WM_NOTIFY_TRAY = 0x0400 + 2001;

    internal struct NOTIFYICONDATA
    {
      internal int cbSize;
      internal IntPtr hWnd;
      internal uint uID;
      internal uint uFlags;
      internal uint uCallbackMessage;
      internal IntPtr hIcon;
    }

    [DllImport("coredll.dll")]
    internal static extern int Shell_NotifyIcon(
      int dwMessage, ref NOTIFYICONDATA pnid);

    [DllImport("coredll.dll")]
    internal static extern int SetForegroundWindow(IntPtr hWnd);

    [DllImport("coredll.dll")]
    internal static extern int ShowWindow(
      IntPtr hWnd,
      int nCmdShow);

    [DllImport("coredll.dll")]
    internal static extern IntPtr GetFocus();

    [DllImport("coredll.dll")]
    internal static extern IntPtr LoadIcon(IntPtr hInst, string IconName);

    [DllImport("coredll.dll")]
    internal static extern IntPtr GetModuleHandle(String lpModuleName);

    #endregion

    #region MessageWindow

    internal class MyMessageWindow : Microsoft.WindowsCE.Forms.MessageWindow
    {

      private int _uID = 0;
      private NotifyIcon notifyIcon;


      public MyMessageWindow(NotifyIcon notIcon)
      {
        notifyIcon = notIcon;
      }

      public int uID
      {
        set { _uID = value; }
      }

      protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message msg)
      {
        if (msg.Msg == WM_NOTIFY_TRAY)
        {
          if ((int) msg.LParam == WM_LBUTTONDOWN)
          {
            if ((int) msg.WParam == _uID)
            {

              if (notifyIcon.Click != null)
                notifyIcon.Click(notifyIcon, null);
            }
          }
        }
      }
    }

I know windows CE is not state of the art but I need to do this because of old hardware.

Martijn
  • 2,268
  • 3
  • 25
  • 51
apo2k7
  • 1
  • 2
  • What's your question? Presumably it's 'How do I add a ContextMenu', but you don't actually say. What have you tried? – simon at rcl Sep 22 '15 at 15:05
  • Exactly thats the point... With the normal .NET framework it's super easy but the CF dosn't provide this funktionality. And i'am looking for some ideas to that problem or maby a solution because i have no idea how to solve this. – apo2k7 Sep 24 '15 at 07:09
  • Have you even googled? I just tried "compact framework notifyicon context menu" and quite a few examples were in the first page of results, for instance: http://stackoverflow.com/questions/3853746/how-to-make-program-that-be-in-the-taskbar-windows-ce – C.Evenhuis Sep 26 '15 at 17:45
  • 1
    Yes i did but the "SDF" what ever, that ist mentiond, isn't available anymore and the second answer isn't working on CF. – apo2k7 Sep 28 '15 at 07:45
  • @apo2k7 My google skills are _insane_: https://opennetcf.codeplex.com/SourceControl/latest#OpenNETCF.Windows.Forms/OpenNETCF.Windows.Forms/NotifyIcon.cs – C.Evenhuis Sep 30 '15 at 08:52

0 Answers0