SystemTray.cs :  » GUI » LongBar » Slate » General » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » LongBar 
LongBar » Slate » General » SystemTray.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows;
using System.Drawing;

namespace Slate.General{
    public class SystemTray
    {
        public delegate void SidebarvisibleChangedEventHandler(bool value);
        public static event SidebarvisibleChangedEventHandler SidebarvisibleChanged;
        private static bool _SidebarVisible = true;
        public static bool isRunning = false;
        protected static void OnSidebarVisibleChanged(bool value)
        {
            if (SidebarvisibleChanged != null)
                SidebarvisibleChanged(value);
        }

        [DllImport("shell32.dll")]
        private static extern IntPtr ExtractIcon(IntPtr hInstance, string path, int iconIndex);
        [DllImport("user32.dll")]
        private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern int SetForeGroundWindow(ref IntPtr hWnd);

        private static string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

        private static NotifyIcon trayIcon;
        private static System.Windows.Controls.ContextMenu trayMenu;
        private static Window window;
        private static System.Windows.Controls.MenuItem closeMenuItem;
        private static System.Windows.Controls.MenuItem showHideMenuItem;

        public static void AddIcon(Window wnd)
        {
            if (isRunning)
                return;
            trayMenu = new System.Windows.Controls.ContextMenu();

            closeMenuItem = new System.Windows.Controls.MenuItem();
            closeMenuItem.AddHandler(System.Windows.Controls.MenuItem.ClickEvent, new RoutedEventHandler(CloseMenuItem_Click));

            showHideMenuItem = new System.Windows.Controls.MenuItem();
            showHideMenuItem.AddHandler(System.Windows.Controls.MenuItem.ClickEvent, new RoutedEventHandler(ShowHideMenuItem_Click));

            SetLocale();

            trayMenu.Items.Add(showHideMenuItem);
            trayMenu.Items.Add(closeMenuItem);

            trayIcon = new NotifyIcon();
            trayIcon.Icon = Icon.FromHandle(ExtractIcon(IntPtr.Zero, path + @"\LongBar.exe", 0));
            trayIcon.Text = "LongBar 2.1 Beta";
            trayIcon.MouseClick += new MouseEventHandler(trayIcon_MouseClick);
            trayIcon.MouseDoubleClick += new MouseEventHandler(trayIcon_MouseDoubleClick);
            trayIcon.Visible = true;
            window = wnd;
        }

        private static void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                if (!SidebarVisible)
                    SystemTray.SidebarVisible = true;
                else SystemTray.SidebarVisible = false;
        }

        private static void trayIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                window.Activate();
            else
                trayMenu.IsOpen = true;
        }

        public static void SetLocale()
        {
            closeMenuItem.Header = System.Windows.Application.Current.TryFindResource("Close");
            showHideMenuItem.Header = System.Windows.Application.Current.TryFindResource("ShowHide");
        }

        public static void RemoveIcon()
        {
            if (isRunning)
                return;
            trayIcon.MouseClick -= new MouseEventHandler(trayIcon_MouseClick);
            trayIcon.Visible = false;
            trayIcon.Dispose();
        }

        private static void CloseMenuItem_Click(object sender, RoutedEventArgs e)
        {
            window.Close();
        }

        private static void ShowHideMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (!SidebarVisible)
                SystemTray.SidebarVisible = true;
            else SystemTray.SidebarVisible = false;
        }

        private static bool overlapTaskbar = false;

        public static bool SidebarVisible
        {
            get
            {
                return _SidebarVisible;
            }
            set
            {
                if (value)
                {
                    ShowWindow(Sidebar.Handle, 5);
                    if (Sidebar.AlwaysTop)
                    {
                        Sidebar.AppbarRemove();
                        Sidebar.AppbarNew();
                        if (!Sidebar.Overlapped && overlapTaskbar)
                            Sidebar.OverlapTaskbar();
                        Sidebar.SizeAppbar();
                    }
                    _SidebarVisible = true;
                    OnSidebarVisibleChanged(true);
                }
                else
                {
                    ShowWindow(Sidebar.Handle, 0);
                    if (Sidebar.AlwaysTop)
                    {
                        Sidebar.AppbarRemove();
                        if (Sidebar.Overlapped)
                        {
                            Sidebar.UnOverlapTaskbar();
                            overlapTaskbar = true;
                        }
                        else
                            overlapTaskbar = true;
                    }
                    _SidebarVisible = false;
                    OnSidebarVisibleChanged(false);
                }
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.