ContextMenuStrip from a left click on a Notify Icon

G

Guest

I've written an application that can minimize to the tray and can reappear if
you double click on the icon in the tray (ShowInTaskbar is set to false).
I've also created a ContextMenuStrip (Visual C# 2005) so that you can write
click on the icon in the tray for options. I then captured the event for
mouse clicks on the Notify Icon so that I could detect the left click event
and then display the contextmenustrip using either:
contextMenuStrip1.Show(this,
this.PointToClient(Cursor.Position));
or:
contextMenuStrip1.Show(this.PointToClient(Cursor.Position));

This works well and displays the contextmenustrip appropriately, however
whilst doing this it also creates an icon for the application in the taskbar
which isn't intended as the form has been set to ShowInTaskbar = false. I've
also tried using a dummy control as the parameter to show to no avail.
 
L

Linda Liu [MSFT]

Hi Simon,

I performed a test based on your description and did see the problem.

I also performed a test on ContextMenu and found that the problem doesn't
exist on ContextMeu. So it looks to me like a limitation of
ContextMenuStrip.

I have searched in our inner database. However, I didn't find a similar
issue in it.

As for a workaround, I think you have two options.

One option is to use ContextMenu intead of ContextMenuStrip.

The other is to set the ContextMenuStrip property of the NotifyIcon to the
ContextMenuStrip on your form. Thus, you could show up the ContextMenuStrip
by right-clicking the NotifyIcon in the system tray.

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Linda,
Many thanks for your response. Unfortunately I am already using the
ContextMenuStrip property of the Notify Icon but as you state this only works
for right clicks and not also for left clicks hence the addtional code I
spoke of.

Do you know if there are any plans for Microsoft to fix this bug or do you
know if there are any other work arounds? If I remember correctly, by using
just ContextMenu instead you don't get the Office 2003 style menus.

Thanks
 
L

Linda Liu [MSFT]

Hi Simon,

Thank you for your prompt response.

I will report this issue to our product team to ask if there's a better
workaround. And I will get the result back to you ASAP. I appreciate your
patience.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Simon,

I received a response from our product team. The following is the response
I received.

The reason it works as a ContextMenu and not when Show is just called is
that the NotifyIcon p/invokes SetForgroundWindow before showing its
ContextMenu. To get this behavior, the user would have to do the same and
p/invoke SetForegroundWindow in the notifyIcon1_Click EventHandler:

using System.Runtime.InteropServices;

[DllImport("User32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern bool SetForegroundWindow(HandleRef hWnd);

void notifyIcon1_Click(object sender, EventArgs e)
{
SetForegroundWindow(new HandleRef(this, this.Handle));

this.contextMenuStrip1.Show(this, this.PointToClient(Cursor.Position));
}

I have tested the above code on my machine and confirmed it works.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu [MSFT]

Hi Simon,

How about the problem now?

If the problem is still not solved, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top