WPF NotifyIcon

--------------
CHG   Generally improved sample project - added more bindings, welcome balloon, changed layout.
CHG   Changed namespace in sample project. Preparing tutorials.

git-svn-id: https://svn.evolvesoftware.ch/repos/evolve.net/WPF/NotifyIcon@101 9f600761-6f11-4665-b6dc-0185e9171623
This commit is contained in:
Philipp Sumi
2009-05-13 09:32:55 +00:00
parent 34c589eac4
commit f349d3d339
25 changed files with 612 additions and 197 deletions

View File

@@ -1,17 +1,20 @@
<Window
x:Class="Sample_Project.Window1"
x:Class="Samples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tb="http://www.hardcodet.net/taskbar"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="WPF NotifyIcon Sample"
Height="833"
Width="769"
Height="938"
Width="880"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:Sample_Project"
xmlns:Commands="clr-namespace:Sample_Project.Commands" MinWidth="750" MinHeight="800" ResizeMode="NoResize">
xmlns:local="clr-namespace:Samples"
xmlns:Commands="clr-namespace:Samples.Commands"
MinWidth="750"
MinHeight="800"
ResizeMode="NoResize">
<Window.Resources>
<BooleanToVisibilityConverter
@@ -43,20 +46,21 @@
<Window.Triggers>
<EventTrigger
RoutedEvent="tb:TaskbarIcon.TrayToolTipOpen"
SourceName="tb" />
SourceName="tb" />
</Window.Triggers>
<Grid>
<Grid
Hyperlink.RequestNavigate="OnNavigationRequest">
<!--
THE TASKBARICON ELEMENT WAS DECLARED INLINE IN ORDER TO USE DATABINDING
FOR ITS PROPERTIES. IN A REAL-LIFE APP, YOU'D PROBABLY RATHER DECLARE
IT IN A RESOURCE DICTIONARY SO YOU CAN EVEN USE IT IF THERE IS NO WINDOW
IT IN A RESOURCE DICTIONARY SO YOU CAN ALSO USE IT IF THERE IS NO WINDOW
OPEN.
-->
<tb:TaskbarIcon
x:Name="tb"
VerticalAlignment="Top"
@@ -66,8 +70,7 @@
Visibility="{Binding Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=iconVisibility, Mode=Default}"
MenuActivation="{Binding Path=SelectedItem, ElementName=lstMenuTrigger, Mode=Default}"
PopupActivation="{Binding Path=SelectedItem, ElementName=lstPopupTrigger, Mode=Default}"
DoubleClickCommand="{Commands:ShowMainWindowCommand}"
>
DoubleClickCommand="{Commands:ShowMainWindowCommand}">
<tb:TaskbarIcon.TrayPopup>
<!-- the control will be put into a popup with an explicit DataContext -->
@@ -85,57 +88,19 @@
<!-- EVERYTHING BELOW IS JUST PLUMBING FOR THE SAMPLE -->
<!-- ************************************************************************************* -->
<!-- ************************************************************************************* -->
<!-- EVERYTHING BELOW IS JUST PLUMBING FOR THE SAMPLE -->
<CheckBox
HorizontalAlignment="Left"
Margin="10,38.62,0,0"
VerticalAlignment="Top"
Width="155.42"
Content="NotifyIcon Visible"
x:Name="iconVisibility"
IsChecked="True" />
<ListBox
HorizontalAlignment="Left"
Margin="12,86.58,0,0"
Width="123"
IsSynchronizedWithCurrentItem="True"
Height="51"
VerticalAlignment="Top"
x:Name="iconList"
SelectedIndex="0">
<Image
Width="16"
Margin="0,4,0,0"
Height="16"
Source="Icons\Inactive.ico" />
<Image
Width="16"
Margin="0,4,0,0"
Height="16"
Source="Icons\Error.ico" />
</ListBox>
<TextBlock
HorizontalAlignment="Left"
Margin="12,64.58,0,0"
VerticalAlignment="Top"
Width="Auto"
Height="22"
TextWrapping="Wrap"><Run
Text="Taskbar Icon:"
Language="de-ch" /></TextBlock>
<Grid
Margin="0,544.58,10,0"
Margin="486.5,617.58,0,0"
VerticalAlignment="Top"
Height="210"
Width="358"
HorizontalAlignment="Right"
x:Name="Balloons">
Height="248"
x:Name="Balloons"
Width="372"
HorizontalAlignment="Left">
<Border
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
@@ -148,8 +113,13 @@
Height="23">WPF NotifyIcon</TextBox>
<TextBox
Margin="125,0,17,76"
x:Name="txtBalloonText" AcceptsReturn="True" Height="47" VerticalAlignment="Bottom" d:LayoutOverrides="VerticalAlignment"
TextWrapping="Wrap" Text="You should see a LED icon in your system tray. This is your NotifyIcon."/>
x:Name="txtBalloonText"
AcceptsReturn="True"
Height="47"
VerticalAlignment="Bottom"
d:LayoutOverrides="VerticalAlignment"
TextWrapping="Wrap"
Text="You should see a LED icon in your system tray. This is your NotifyIcon." />
<RadioButton
HorizontalAlignment="Left"
Margin="14,0,0,54"
@@ -166,15 +136,16 @@
Width="85"
Height="23"
TextWrapping="Wrap"><Run
Text="Balloon Title"
Language="de-ch" /></TextBlock>
Text="Balloon Title" /></TextBlock>
<TextBlock
Margin="14,0,0,100"
TextWrapping="Wrap"
HorizontalAlignment="Left"
Width="85" Height="23" VerticalAlignment="Bottom" d:LayoutOverrides="VerticalAlignment"><Run
Text="Balloon Text"
Language="de-ch" /></TextBlock>
Width="85"
Height="23"
VerticalAlignment="Bottom"
d:LayoutOverrides="VerticalAlignment"><Run
Text="Balloon Text" /></TextBlock>
<RadioButton
Margin="14,0,0,32"
VerticalAlignment="Bottom"
@@ -201,12 +172,14 @@
Height="23"
Click="showBalloonTip_Click" />
<TextBlock
Margin="14,10,17,0"
Margin="10,35.96,21,0"
VerticalAlignment="Top"
Height="33"
Height="56.04"
TextWrapping="Wrap"><Run
Text="Standard balloon tips. You can use custom icons under all OS versions."
Language="de-ch" /></TextBlock>
Text="Displays default balloon tips as supported by the OS. " /><Run
Text="You can use custom icons under all OS versions" /><Run
Text=" (not supported by WinForms NotifyIcon)." /><Run
Text="." /></TextBlock>
<Button
HorizontalAlignment="Right"
Margin="0,0,17,12.52"
@@ -216,13 +189,23 @@
VerticalAlignment="Bottom"
Height="23"
Click="hideBalloonTip_Click" />
<TextBlock
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="Auto"
Height="Auto"
FontWeight="Bold"
TextDecorations="Underline"
TextWrapping="Wrap"><Run
Text="Standard (OS) Balloon Tips" /></TextBlock>
</Grid>
<Grid
HorizontalAlignment="Left"
Margin="12,512.58,0,0"
Margin="12,582.88,0,0"
VerticalAlignment="Top"
Width="307"
Height="242"
Width="445.5"
Height="282.7"
x:Name="Popups">
<Border
HorizontalAlignment="Stretch"
@@ -233,34 +216,58 @@
IsSynchronizedWithCurrentItem="False"
x:Name="lstPopupTrigger"
ItemsSource="{Binding Mode=OneWay, Source={StaticResource ActivationModes}}"
Margin="14,115,97,10"
SelectedIndex="0" />
Margin="10,0,190,10"
SelectedIndex="0"
Height="117"
VerticalAlignment="Bottom" />
<TextBlock
Margin="14,10,10,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Height="105"><Run
Text="If the user clicks on the Taskbar Icon, a popup can be opened and displayed. Unlike custom ToolTips, this works under Windows xp as well. In case left-clicks are used, popups are displayed with a delay to ensure the user does not double-click. Trigger is determined by the "
Language="de-ch" /><Run
Margin="10,31.7,14,137"
TextWrapping="Wrap"><Run
Text="If the user clicks on the " /><Run
Text="Notify" /><Run
Text="Icon, a " /><Run
Text="P" /><Run
Text="opup can be opened and displayed" /><Run
Text=" that allows the user to quickly interact with the application" /><Run
Text=". " /><Run
Text="Unlike custom ToolTips, this works under all OS versions" /><Run
Text=". " /><Run
Text="Which mouse button(s) opens the Popup is " /><Run
Text="determined by the " /><Run
FontStyle="Italic"
FontWeight="Bold"
Text="PopupActivation "
Language="de-ch" /><Run
Text="property."
Language="de-ch" /></TextBlock>
Text="PopupActivation " /><Run
Text="property." /><Run
Text=" If both Popup and ContextMenu are configured for the same mouse buttons, ContextMenu takes precedence." /><LineBreak /><Run
Text="(Note: " /><Run
Text="In case left-clicks are used, popups are displayed with a delay to ensure the user does not double-click." /><Run
Text=")" /></TextBlock>
<TextBlock
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="Auto"
Height="Auto"
FontWeight="Bold"
TextDecorations="Underline"
TextWrapping="Wrap"><Run
Text="Popup Controls" /></TextBlock>
</Grid>
<Grid
Margin="0,156.38,10,0"
Width="358"
HorizontalAlignment="Right"
x:Name="ToolTips" Height="233" VerticalAlignment="Top" d:LayoutOverrides="VerticalAlignment">
Margin="10,317,0,0"
x:Name="ToolTips"
Height="255.88"
VerticalAlignment="Top"
d:LayoutOverrides="HorizontalAlignment, VerticalAlignment"
Width="447.5"
HorizontalAlignment="Left">
<Border
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderBrush="#FF000000"
BorderThickness="2,2,2,2" />
<TextBox
Margin="10,0,25,70"
Margin="10,0,25,60"
x:Name="txtToolTipText"
VerticalAlignment="Bottom"
Height="23"
@@ -268,36 +275,39 @@
Foreground="#FFFF0000" />
<TextBlock
Margin="10,0,25,93"
TextWrapping="Wrap" Height="21" VerticalAlignment="Bottom"><Run
Text="ToolTipText:"
Language="de-ch" /></TextBlock>
TextWrapping="Wrap"
Height="21"
VerticalAlignment="Bottom"><Run
Text="ToolTipText" /><Run
Text=" (assigned to property and also used by several bindings)" /><Run
Text=":" /></TextBlock>
<TextBlock
Margin="10,10,10,0"
VerticalAlignment="Top"
Height="112"
Margin="10,29.88,10,114"
TextWrapping="Wrap"><Run
Text="If the user moves the mouse over the Taskbar Icon, a tooltip can be displayed. Starting from Windows Vista, we have convenient events to display and hide tooltips. If we can, the "
Language="de-ch" /><Run
Text="If the user moves the mouse over the " /><Run
Text="Notify" /><Run
Text="Icon, " /><Run
Text="a ToolTip " /><Run
Text="can be displayed. Starting from Windows Vista, we have convenient events to display and hide tooltips." /><Run
Text=" You can assign arbitrary UIElements (e.g. User Controls) to the " /><Run
FontStyle="Italic"
FontWeight="Bold"
Text="TrayToolTip "
Language="de-ch" /><Run
Text="control is being displayed."
Language="de-ch" /><LineBreak /><Run
Text="If "
Language="de-ch" /><Run
Text="TrayToolTip " /><Run
Text="property." /><LineBreak /><Run
Text="" /><Run
Text="If " /><Run
FontStyle="Italic"
FontWeight="Bold"
Text="TrayToolTip "
Language="de-ch" /><Run
Text="is not set or the app runs under an older OS (e.g. xp), the control falls back to the "
Language="de-ch" /><Run
Text="TrayToolTip " /><Run
Text="is not set or the app runs under an older OS (e.g. " /><Run
Text="Windows " /><Run
Text="xp), the " /><Run
Text="NotifyIcon " /><Run
Text="falls back to the " /><Run
FontStyle="Italic"
FontWeight="Bold"
Text="ToolTipText "
Language="de-ch" /><Run
Text="property."
Language="de-ch" /></TextBlock>
Text="ToolTipText " /><Run
Text="property." /></TextBlock>
<Button
HorizontalAlignment="Left"
Margin="10,0,0,10"
@@ -312,14 +322,26 @@
VerticalAlignment="Bottom"
Height="16"
TextWrapping="Wrap"><Run
Text="Click to fall back to ToolTipText (sets TrayToolTip to null):"
Language="de-ch" /></TextBlock>
Text="Click to fall back to ToolTipText (sets TrayToolTip to null):" /></TextBlock>
<TextBlock
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="Auto"
Height="Auto"
FontWeight="Bold"
TextDecorations="Underline"
TextWrapping="Wrap"><Run
Text="ToolTips and ToolTipText" /></TextBlock>
</Grid>
<Grid
HorizontalAlignment="Left"
Margin="12,231.58,0,0"
Width="307"
x:Name="ContextMenus" Height="271" VerticalAlignment="Top">
Margin="488,153.5,0,0"
Width="370.5"
x:Name="ContextMenus"
Height="255.88"
VerticalAlignment="Top"
d:LayoutOverrides="VerticalAlignment">
<Border
BorderBrush="#FF000000"
BorderThickness="2,2,2,2" />
@@ -332,31 +354,37 @@
VerticalAlignment="Bottom"
SelectedIndex="1" />
<TextBlock
Margin="10,108,48,0"
Margin="10,97,48,0"
VerticalAlignment="Top"
Height="22"
TextWrapping="Wrap"><Run
Text="Mouse events that open the context menu:"
Language="de-ch" /></TextBlock>
Text="Mouse events that open the context menu:" /></TextBlock>
<TextBlock
Margin="10,10,10,0"
Margin="10,30,10,0"
VerticalAlignment="Top"
Height="78"
Height="57"
TextWrapping="Wrap"><Run
Text="Assign a custon context menu through the "
Language="de-ch" /><Run
Text="Assign a custon context menu through the " /><Run
FontStyle="Italic"
FontWeight="Bold"
Text="ContextMenu "
Language="de-ch" /><Run
Text="property of the TaskbarIcon. The "
Language="de-ch" /><Run
Text="ContextMenu " /><Run
Text="property of the " /><Run
Text="Notify" /><Run
Text="Icon. The " /><Run
FontStyle="Italic"
FontWeight="Bold"
Text="MenuActivation "
Language="de-ch" /><Run
Text="property determines what mouse events open the context menu."
Language="de-ch" /></TextBlock>
Text="MenuActivation " /><Run
Text="property determines what mouse events open the context menu." /></TextBlock>
<TextBlock
Margin="10,10,0,0"
VerticalAlignment="Top"
Height="Auto"
TextWrapping="Wrap"
Width="Auto"
HorizontalAlignment="Left"
FontWeight="Bold"
TextDecorations="Underline"><Run
Text="Context Menu" /></TextBlock>
</Grid>
<TextBlock
Margin="10,10,0,0"
@@ -364,28 +392,163 @@
Height="Auto"
TextWrapping="Wrap"
FontWeight="Bold"
FontSize="14" Width="309" HorizontalAlignment="Left"><Run
Text="WPF NotifyIcon - RC 1.0"
Language="de-ch" /></TextBlock>
FontSize="14"
Width="309"
HorizontalAlignment="Left"><Run
Text="WPF NotifyIcon 1.0.1" /></TextBlock>
<TextBlock
HorizontalAlignment="Right"
Margin="0,10,10,0"
Margin="12,38.62,0,0"
VerticalAlignment="Top"
Width="358"
Height="146.38"
Height="104.88"
FontSize="14"
FontStyle="Italic"
Foreground="#FF303030"
TextWrapping="Wrap"><Run Text="This is my first shot at this control and a think a few additions might make sense." Language="de-ch"/><LineBreak/><Run Text="Play around and feel free to commit critical feedback - it's appreciated :)" Language="de-ch"/><LineBreak/><Run Text="" Language="de-ch"/><LineBreak/><Run Foreground="#FF067EBD" Text="Feedback and Updates:" Language="de-ch"/><LineBreak/><Run Foreground="#FF067EBD" Text="http://www.hardcodet.net " Language="de-ch"/><Run FontSize="10" Foreground="#FF067EBD" Text="(watch the spelling" Language="de-ch"/><Run Foreground="#FF067EBD" Text=")" Language="de-ch"/></TextBlock>
<TextBlock Margin="12,0,10,10" VerticalAlignment="Bottom" Height="22.42" TextWrapping="Wrap" FontWeight="Bold" Foreground="#FFFFA051"><Run Text="Copyright (c) 2009 Philipp Sumi. This is prerelease software, realeased under the CodeProject Open License (CPOL)" Language="de-ch"/></TextBlock>
<Grid HorizontalAlignment="Right" Margin="0,399.38,10,272.42" Width="358" x:Name="CustomBalloons">
<Border HorizontalAlignment="Stretch" Width="Auto" BorderThickness="2,2,2,2" BorderBrush="#FF000000"/>
<Button Content="Show" x:Name="showCustomBalloon"
Click="showCustomBalloon_Click" HorizontalAlignment="Right" Margin="0,0,91.377,10" Width="71.623" Height="23" VerticalAlignment="Bottom" />
<TextBox VerticalAlignment="Bottom" Height="23" Text="WPF Balloon" TextWrapping="Wrap" Margin="10,0,173,10" x:Name="customBalloonTitle"/>
<TextBlock Margin="10,10,24.377,0" VerticalAlignment="Top" TextWrapping="Wrap" Height="66.68"><Run Text="Custom Balloon Tips are more flexible then standard tips when it comes to styling and they provide attached properties and events that can be used to control behavior. Hover over the tooltip to suspend the fade-out." Language="de-ch"/></TextBlock>
<Button Content="Close" x:Name="hideCustomBalloon"
Click="hideCustomBalloon_Click" HorizontalAlignment="Right" Margin="0,0,9.754,10.52" Width="71.623" Height="23" VerticalAlignment="Bottom" />
TextWrapping="Wrap"
HorizontalAlignment="Left"
Width="846.5"><Run
Text="This " /><Run
Text="is a showcase of the different features of the WPF NotifyIcon. Have a look at the used controls and styles in order to see how binding can be supported. For a real-life " /><Run
Text="example" /><Run
Text=", have a look at" /><Run
Text=" " /><Hyperlink
NavigateUri="http://www.hardcodet.net/netdrives/"><Run
Text="NetDrives" /></Hyperlink><LineBreak /><Run
Text="" /><LineBreak /><Run
Foreground="#FF067EBD"
Text="Project Page:" /><LineBreak /><Hyperlink
NavigateUri="http://www.hardcodet.net/wpf-notifyicon/"><Run
Text="http://www.hardcodet.net/wpf-notifyicon/" /></Hyperlink></TextBlock>
<TextBlock
Margin="12,0,29.5,10"
VerticalAlignment="Bottom"
Height="22.42"
TextWrapping="Wrap"
FontWeight="Bold"
Foreground="#FFFFA051"><Run Text="The WPF NotifyIcon is free software, released under the"/><Run Text=" "/><Hyperlink NavigateUri="http://www.codeproject.com/info/cpol10.aspx"><Run Text="CodeProject Open License"/></Hyperlink></TextBlock>
<Grid
Margin="486.5,419.38,0,0"
x:Name="CustomBalloons"
Height="188.2"
VerticalAlignment="Top"
Width="372"
HorizontalAlignment="Left"
d:LayoutOverrides="VerticalAlignment">
<Border
HorizontalAlignment="Stretch"
Width="Auto"
BorderThickness="2,2,2,2"
BorderBrush="#FF000000" />
<Button
Content="Show"
x:Name="showCustomBalloon"
Click="showCustomBalloon_Click"
HorizontalAlignment="Right"
Margin="0,0,91.377,10"
Width="71.623"
Height="23"
VerticalAlignment="Bottom" />
<TextBox
VerticalAlignment="Bottom"
Height="23"
Text="WPF Balloon"
TextWrapping="Wrap"
Margin="10,0,173,10"
x:Name="customBalloonTitle" />
<TextBlock
Margin="10,35,24.377,0"
VerticalAlignment="Top"
TextWrapping="Wrap"
Height="119.68"><Run
Text="Custom " /><Run
Text="Balloons are much " /><Run
Text="ore flexible then standard balloons " /><Run
Text="tips " /><Run
Text="when it comes to styling." /><Run
Text=" You can display arbitrary UI Elements (e.g. User Controls) as custom balloons." /><LineBreak /><Run
Text="Apart from the richer UI, custom balloons also provide a" /><Run
Text="ttached properties and events that can be used to control behavior." /><Run
Text=" Custom balloons also work if the NotifyIcon is not visible." /><LineBreak /><Run
Text="(Hint: Hover over the " /><Run
Text="balloon " /><Run
Text="to suspend the fade-out.)" /></TextBlock>
<Button
Content="Close"
x:Name="hideCustomBalloon"
Click="hideCustomBalloon_Click"
HorizontalAlignment="Right"
Margin="0,0,9.754,10.52"
Width="71.623"
Height="23"
VerticalAlignment="Bottom" />
<TextBlock
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="Auto"
Height="Auto"
FontWeight="Bold"
TextDecorations="Underline"
TextWrapping="Wrap"><Run
Text="Custom Balloons" /></TextBlock>
</Grid>
<Grid
Margin="10,153.5,0,0"
VerticalAlignment="Top"
Height="138.38"
x:Name="Common"
Width="447.5"
HorizontalAlignment="Left">
<Border
BorderThickness="2,2,2,2"
BorderBrush="#FF000000"
d:IsLocked="True" />
<CheckBox
Margin="10,35.96,0,0"
VerticalAlignment="Top"
Content="NotifyIcon Visible"
x:Name="iconVisibility"
IsChecked="True"
HorizontalAlignment="Left"
Width="155.42"
d:LayoutOverrides="Height" />
<ListBox
Margin="10,77.38,0,0"
IsSynchronizedWithCurrentItem="True"
x:Name="iconList"
SelectedIndex="0"
Width="123"
HorizontalAlignment="Left"
Height="51"
VerticalAlignment="Top">
<Image
Width="16"
Margin="0,4,0,0"
Height="16"
Source="Icons\Inactive.ico" />
<Image
Width="16"
Margin="0,4,0,0"
Height="16"
Source="Icons\Error.ico" />
</ListBox>
<TextBlock
Margin="10,55.38,0,61"
Width="Auto"
TextWrapping="Wrap"
d:LayoutOverrides="Width"
HorizontalAlignment="Left"><Run
Text="Select an image to switch icons:" /></TextBlock>
<TextBlock
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="Auto"
Height="Auto"
FontWeight="Bold"
TextDecorations="Underline"
TextWrapping="Wrap"><Run
Text="Icon / Visibility" /></TextBlock>
</Grid>
</Grid>
</Window>