wpf jumplist demo

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int RegisterWindowMessage(string msg);

        private HwndSource _HwndSource = null;
        private IntPtr _Handler = IntPtr.Zero;

        private readonly int Message1 = RegisterWindowMessage("Message_1");
        private readonly int Message2 = RegisterWindowMessage("Message_2");
        private readonly int Message3 = RegisterWindowMessage("Message_3");

        public MainWindow()
        {
            InitializeComponent();

            this.SourceInitialized += new EventHandler(MainWindow_SourceInitialized);
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.LoadJumpList(); 
        }

        void MainWindow_SourceInitialized(object sender, EventArgs e)
        {
            this._HwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
            this._Handler = this._HwndSource.Handle;
            this._HwndSource.AddHook(this.WindowProc);
        }

        public IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == Message1)
            {
                MessageBox.Show("Message_1");
            }
            else if (msg == Message2)
            {
                MessageBox.Show("Message_2");
            }
            else if (msg == Message3)
            {
                MessageBox.Show("Message_3");
            }
            return IntPtr.Zero;
        }

        private void LoadJumpList()
        {
            JumpList jumpList = JumpList.GetJumpList(Application.Current);
            if (jumpList != null
                   && jumpList.JumpItems != null
                   && jumpList.JumpItems.Count > 0)
                jumpList.JumpItems.Clear();

            JumpTask jtNotpad = new JumpTask()
            {
                ApplicationPath = "notepad.exe",
                CustomCategory = "工具",
                Description = "记事本",
                Title = "记事本",
                IconResourcePath = "notepad.exe",
                IconResourceIndex = 0
            };
            JumpTask jtMsPaint = new JumpTask()
            {
                ApplicationPath = "mspaint.exe",
                CustomCategory = "工具",
                Description = "画图",
                Title = "画图",
                IconResourcePath = "mspaint.exe",
                IconResourceIndex = 0
            };
            JumpTask jtCalc = new JumpTask()
            {
                ApplicationPath = "calc.exe",
                CustomCategory = "工具",
                Description = "计算器",
                Title = "计算器",
                IconResourcePath = "calc.exe",
                IconResourceIndex = 0
            };

            int windowInt = this._Handler.ToInt32();
            JumpTask jtMessage1 = new JumpTask()
            {
                CustomCategory = "Message",
                Description = "Message1",
                Title = "Message1",
                ApplicationPath = Assembly.GetEntryAssembly().CodeBase,
                Arguments = windowInt + " " + Message1
            };
            JumpTask jtMessage2 = new JumpTask()
            {
                CustomCategory = "Message",
                Description = "Message2",
                Title = "Message2",
                ApplicationPath = Assembly.GetEntryAssembly().CodeBase,
                Arguments = windowInt + " " + Message2
            };
            JumpTask jtMessage3 = new JumpTask()
            {
                CustomCategory = "Message",
                Description = "Message3",
                Title = "Message3",
                ApplicationPath = Assembly.GetEntryAssembly().CodeBase,
                Arguments = windowInt + " " + Message3
            };

            if (jumpList == null)
                jumpList = new JumpList();
            jumpList.JumpItems.Add(jtNotpad);
            jumpList.JumpItems.Add(jtMsPaint);
            jumpList.JumpItems.Add(jtCalc);
            jumpList.JumpItems.Add(jtMessage1);
            jumpList.JumpItems.Add(jtMessage2);
            jumpList.JumpItems.Add(jtMessage3);

            jumpList.ShowFrequentCategory = true;
            jumpList.ShowRecentCategory = true;
            jumpList.Apply();
            this.ShowInTaskbar = true;
        }
    }

 

    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        public App()
        {
            string[] args = Environment.GetCommandLineArgs();

            if (args.Length == 3)
            {
                //处理JumpList自定义按钮操作
                int handler = Int32.Parse(args[1]);
                uint msg = UInt32.Parse(args[2]);
                if (handler != 0 && msg != 0)
                {
                    SendMessage((IntPtr)handler, msg, IntPtr.Zero, IntPtr.Zero);
                }
                Application.Current.Shutdown();
                return;
            }
        }
    }

 

posted @ 2013-10-16 10:16  Xsi64  阅读(198)  评论(0编辑  收藏  举报