Sky Software Homepage LogicNP Software Knowledge Base And FAQ

 
Contact Product Support    SearchSearch      Subscribe to the RSS feed for this forum

HOWTO: Display a context menu when the ShellNotifyIcon is clicked/right-clicked

 
Subscribe to the RSS feed for this forum  Forum Index -> ShellObjects.Net/WPF
View previous topic :: View next topic  
Author Message
Support



Joined: 18 Jul 2005
Posts: 731

Posted: Wed Sep 10, 2008 4:58 pm    Post subject: HOWTO: Display a context menu when the ShellNotifyIcon is clicked/right-clicked

To display a context menu when the ShellNotifyIcon is right-clicked, call the InvokeContextMenu method in the MouseUp event. The ContextMenuPopup event is raised in response to this; menu items should be added to the context menu in this event. If the user selects any menu item, the ContextMenuItemSelect event is raised. The following code (taken from the ShellNotifyIcon Starter sample) demonstrates this:

[C#]

// Event handler for the ShellNotifyIcon.MouseUp event
private void shlIcon_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button== MouseButtons.Right) // Only show menu on right-click
shlIcon.InvokeContextMenu();
}

// Event handler for the ShellNotifyIcon.ContextMenuPopup event (called in response to InvokeContextMenu method)
private void shlIcon_ContextMenuPopup(object sender, LogicNP.ShellObjects.ContextMenuEventArgs e)
{
ShellMenuItem item;
item = e.Menu.AddItem("Maximise From Tray");

item = e.Menu.AddItem("Separator");
item.Separator = true;

item = e.Menu.AddItem("Checked Menu item");
item.Checked = true;

item = e.Menu.AddItem("Separator");
item.Separator = true;

item = e.Menu.AddItem("Parent Menu item");
item.HasSubMenu = true;
item.SubMenu.AddItem("Sub menu item 1");
item.SubMenu.AddItem("Sub menu item 2");

}

// Event handler for the ShellNotifyIcon.ContextMenuItemSelect event (called when a menu item is selected)
private void shlIcon_ContextMenuItemSelect(object sender, LogicNP.ShellObjects.ContextMenuItemEventArgs e)
{
if(e.CommandText == "Maximise From Tray")
shlIcon.MaximizeFromTray(this,FormAnimStyles.SystemDefault);
}


[VB.Net]
' Event handler for the ShellNotifyIcon.MouseUp event
Private Sub shlIcon_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles shlIcon.MouseUp
If (e.Button = MouseButtons.Right) Then ' Only show menu on right-click
shlIcon.InvokeContextMenu()
End If
End Sub

' Event handler for the ShellNotifyIcon.ContextMenuPopup event (called in response to InvokeContextMenu method)
Private Sub shlIcon_ContextMenuPopup(ByVal sender As System.Object, ByVal e As LogicNP.ShellObjects.ContextMenuEventArgs) Handles shlIcon.ContextMenuPopup
Dim item As ShellMenuItem
item = e.Menu.AddItem("Maximise From Tray")

item = e.Menu.AddItem("Separator")
item.Separator = True

item = e.Menu.AddItem("Checked Menu item")
item.Checked = True

item = e.Menu.AddItem("Separator")
item.Separator = True

item = e.Menu.AddItem("Parent Menu item")
item.HasSubMenu = True
item.SubMenu.AddItem("Sub menu item 1")
item.SubMenu.AddItem("Sub menu item 2")
End Sub

' Event handler for the ShellNotifyIcon.ContextMenuItemSelect event (called when a menu item is selected)
Private Sub shlIcon_ContextMenuItemSelect(ByVal sender As System.Object, ByVal e As LogicNP.ShellObjects.ContextMenuItemEventArgs) Handles shlIcon.ContextMenuItemSelect
If e.CommandText = "Maximise From Tray" Then
shlIcon.MaximizeFromTray(Me, FormAnimStyles.SystemDefault)
End If
End Sub
Back to top
Display posts from previous:   
Forum Index -> ShellObjects.Net/WPF All times are GMT
Page 1 of 1

 
Jump to:  


Powered by phpBB © 2001, 2005 phpBB Group