当前位置:网站首页 / WPF / 正文

[AY博客] 写给自己的WPF4.5 [29] NotifyIcon

时间:2015年08月03日 | 作者 : aaronyang | 分类 : WPF | 浏览: 3294次 | 评论 0

======================================== AYUI       www.ayjs.net      AY         杨洋原创编写,请不要转载谢谢===============

        private System.Windows.Forms.NotifyIcon notifyIcon;
  public MainView()
		{
			this.InitializeComponent();
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(OnDataContextChanged);

            trayContextMenu = (ContextMenu)FindResource("TrayContextMenu");

            notifyIcon = new System.Windows.Forms.NotifyIcon();
            notifyIcon.Icon = new System.Drawing.Icon(typeof(App).Module.Assembly.GetManifestResourceStream("Messenger.Demo.Resources.Application.ico"));
            notifyIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(OnTrayIconMouseDoubleClick);
            notifyIcon.MouseDown += new System.Windows.Forms.MouseEventHandler(OnTrayIconMouseDown);
            notifyIcon.Visible = true;
		}
        private void OnTrayIconMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // Workaround to make sure that the context menu isn't already showing somewhere else
            trayContextMenu.DataContext = DataContext;
            trayContextMenu.IsOpen = false; 

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
                trayContextMenu.IsOpen = true;            
        }
 <ContextMenu x:Key="TrayContextMenu" Placement="MousePoint">
            <ContextMenu.InputBindings>
                <KeyBinding Modifiers="Control" Key="C" Command="{StaticResource ConnectCommandReference}" />
                <KeyBinding Modifiers="Control" Key="D" Command="{StaticResource DisconnectCommandReference}" />
                <KeyBinding Modifiers="Control" Key="X" Command="{StaticResource ExitCommandReference}" />
                <KeyBinding Modifiers="Control" Key="S" Command="{StaticResource StartConversationCommandReference}" />
            </ContextMenu.InputBindings>
            <MenuItem Header="_Set Status" ItemsSource="{Binding Source={StaticResource PresenceStatusValues}}" Click="OnTrayMenuStatusClick" Visibility="{Binding Connected, Converter={StaticResource BooleanToVisibilityConverter}}">
                <MenuItem.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" />
                    </DataTemplate>
                </MenuItem.ItemTemplate>
            </MenuItem>
            <MenuItem Header="_Connection" Visibility="{Binding Connected, Converter={StaticResource BooleanToVisibilityConverter}}">
                <MenuItem Command="{Binding DisconnectCommand}" Header="_Disconnect" InputGestureText="Ctrl-D" />
            </MenuItem>
            <Separator Visibility="{Binding Connected, Converter={StaticResource BooleanToVisibilityConverter}}" />

            <!-- Menu items are kept in sync with the active conversations -->
            <MenuItem Header="Conversations" ItemsSource="{Binding ConversationCollection}" Click="OnTrayMenuConversationsClick" Visibility="{Binding ConversationCollection.Count, Converter={StaticResource CountVisibilityConverter}}">
                <MenuItem.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Title}" />
                    </DataTemplate>
                </MenuItem.ItemTemplate>
            </MenuItem>
            <Separator Visibility="{Binding ConversationCollection.Count, Converter={StaticResource CountVisibilityConverter}}" />
            <MenuItem Header="_Show" Click="OnTrayMenuShowClick"/>
            <MenuItem Command="{Binding ExitCommand}" Header="E_xit Messenger" InputGestureText="Ctrl-X" />
        </ContextMenu>
    private void OnTrayMenuConversationsClick(object sender, RoutedEventArgs e)
        {
            ConversationViewModel conversationViewModel = ((MenuItem)e.OriginalSource).Header as ConversationViewModel;
            if (conversationViewModel != null)
            {
                conversationViewModel.IsVisible = true;
                conversationViewModel.IsActive = true;
            }
        }

        private void OnTrayMenuShowClick(object sender, RoutedEventArgs e)
        {
            MainViewModel mainViewModel = DataContext as MainViewModel;
            if (mainViewModel != null)
            {
                mainViewModel.IsVisible = true;
                mainViewModel.IsActive = true;
            }
        }

        private void OnTrayIconMouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            MainViewModel mainViewModel = DataContext as MainViewModel;
            if (mainViewModel != null)
            {
                mainViewModel.ChangeVisibilityCommand.Execute("show");
            }
        }

        private void OnClosed(object sender, EventArgs e)
        {
            // Disconnect from the server before exiting
            MainViewModel mainViewModel = DataContext as MainViewModel;
            if (mainViewModel.DisconnectCommand.CanExecute(null))
                mainViewModel.DisconnectCommand.Execute(null);

            // Ensure that we remove the tray icon after we exit
            notifyIcon.Dispose();
        }


推荐您阅读更多有关于“C#WPF4.5,”的文章

猜你喜欢

额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

  查看权限

抖音:wpfui 工作wpf,目前主maui

招聘合肥一枚WPF工程师,跟我一个开发组,10-15K,欢迎打扰

目前在合肥市企迈科技就职

AYUI8全源码 Github地址:前往获取

杨洋(AaronYang简称AY,安徽六安人)AY唯一QQ:875556003和AY交流

高中学历,2010年开始web开发,2015年1月17日开始学习WPF

声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费

不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo

查看捐赠

AYUI7.X MVC教程 更新如下:

第一课 第二课 程序加密教程

标签列表