NEWS

[C#] Hướng dẫn sử dụng JumpList trong Winform

[C#] Hướng dẫn sử dụng JumpList trong Winform
Đăng bởi: Thảo Meo - Lượt xem: 3789 08:45:54, 14/07/2020ANDROID

Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách sử dụng JumpList TaskBar của Windows trong lập trình C# Winform.

[C#] Using JumpList Taskbar Windows 

Vậy JumpList là gì?

Từ phiên bản hệ điều hành Windows 7, windows cung cấp một tính năng mới trên thanh tác vụ cho các ứng dụng được gọi là Jumplists.

Chúng xuất hiện khi bạn nhấn chuột phải vào biểu tượng của ứng dụng trên thanh taskbar.

Theo mặc định bạn sẽ thấy một danh sách các file vừa mở ra và hai mục để khởi động và tách các ứng dụng.

Hình ảnh jumplist bên dưới, khi bạn Click phải chuột vào icon Chrome dưới thanh Taskbar.

jump_list_chrome

Trong bài viết này, mình sẽ sử dụng Winform C#, để tạo một list danh sách các ứng dụng vừa mở lưu vào JumpList: Notepad, Mspaint, Calculator..

Giao diện demo ứng dụng Jumplist C#:

jump_list_demo_csharp

Source code Jumplist C#:

Đầu tiên các bạn tạo 1 class MyJumpList.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAPICodePack.Taskbar;
using System.IO;
using System.Reflection;
using Microsoft.WindowsAPICodePack.Shell;

namespace JumplistDemo
{
    public class MyJumplist
    {
        private JumpList list;
      
        public MyJumplist(IntPtr windowHandle)
        {
            list = JumpList.CreateJumpListForIndividualWindow(TaskbarManager.Instance.ApplicationId, windowHandle);
            list.KnownCategoryToDisplay = JumpListKnownCategoryType.Recent;
            BuildList();
        }

        public void AddToRecent(string destination)
        {
            list.AddToRecent(destination);
            list.Refresh();
        }

        /// <summary>
        /// Builds the Jumplist
        /// </summary>
        private void BuildList()
        {
            JumpListCustomCategory userActionsCategory = new JumpListCustomCategory("Actions");
            JumpListLink userActionLink = new JumpListLink(Assembly.GetEntryAssembly().Location, "Clear History");
            userActionLink.Arguments = "-1";

            userActionsCategory.AddJumpListItems(userActionLink);
            list.AddCustomCategories(userActionsCategory);

            string notepadPath = Path.Combine(Environment.SystemDirectory, "notepad.exe");
            JumpListLink jlNotepad = new JumpListLink(notepadPath, "Notepad");
            jlNotepad.IconReference = new IconReference(notepadPath, 0);

            string calcPath = Path.Combine(Environment.SystemDirectory, "calc.exe");
            JumpListLink jlCalculator = new JumpListLink(calcPath, "Calculator");
            jlCalculator.IconReference = new IconReference(calcPath, 0);

            string mspaintPath = Path.Combine(Environment.SystemDirectory, "mspaint.exe");
            JumpListLink jlPaint = new JumpListLink(mspaintPath, "Paint");
            jlPaint.IconReference = new IconReference(mspaintPath, 0);

            list.AddUserTasks(jlNotepad);
            list.AddUserTasks(jlPaint);
            list.AddUserTasks(new JumpListSeparator());
            list.AddUserTasks(jlCalculator);

            list.Refresh();
        }
    }
}

Tiếp theo là tạo tiếp 1 file WindowsMessageHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Windows7Jumplist
{
    class WindowsMessageHelper
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        public static extern int RegisterWindowMessage(string msgName);

        public static int ClearHistoryArg;

        static WindowsMessageHelper()
        {
            ClearHistoryArg = WindowsMessageHelper.RegisterWindowMessage("Jumplist.demo.ClearHistoryArg");
        }

        public static int RegisterMessage(string msgName)
        {
            return RegisterWindowMessage(msgName);
        }

        public static void SendMessage(string windowTitle, int msgId)
        {
            SendMessage(windowTitle, msgId, IntPtr.Zero, IntPtr.Zero);
        }

        public static bool SendMessage(string windowTitle, int msgId, IntPtr wParam, IntPtr lParam)
        {
            IntPtr WindowToFind = FindWindow(null, windowTitle);
            if (WindowToFind == IntPtr.Zero) return false;

            long result = SendMessage(WindowToFind, msgId, wParam, lParam);

            if (result == 0) return true;
            else return false;
        }
    }
}

Và cuối cùng là source code cho File Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using Windows7Jumplist;

namespace JumplistDemo
{
    public partial class Form1 : Form
    {
        private MyJumplist list;
        public Form1()
        {
            InitializeComponent();          
            list = new MyJumplist(this.Handle);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WindowsMessageHelper.ClearHistoryArg)
            {
            
                ClearHistory();
                UpdateRecentAction(RecentActions.ClearHistory);
            }
            else
            {
                base.WndProc(ref m);
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {              
                txtFilePath.Text = dlg.FileName;             
                lbRecentFiles.Items.Add(txtFilePath.Text);
                list.AddToRecent(txtFilePath.Text);               
                UpdateRecentAction(RecentActions.OpenFile);
            }
        }

        private void btnNotepad_Click(object sender, EventArgs e)
        {
            OpenNotepad();
        }

        private void btnCalculator_Click(object sender, EventArgs e)
        {
            OpenCalculator();
        }

        private void btnClearHistory_Click(object sender, EventArgs e)
        {
            ClearHistory();
        }

        private void btnPaint_Click(object sender, EventArgs e)
        {
            OpenPaint();
        }

        private void ClearHistory()
        {         
            lbRecentFiles.Items.Clear();
            txtFilePath.Text = "";          
            UpdateRecentAction(RecentActions.ClearHistory);
        }

        private void OpenNotepad()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
            Process.Start(Path.Combine(path, "notepad.exe"));

            UpdateRecentAction(RecentActions.OpenNotepad);
        }

        private void OpenCalculator()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
            Process.Start(Path.Combine(path, "calc.exe"));

            UpdateRecentAction(RecentActions.OpenCalculator);
        }

        private void OpenPaint()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
            Process.Start(Path.Combine(path, "mspaint.exe"));

            UpdateRecentAction(RecentActions.OpenPaint);
        }

        private void UpdateRecentAction(RecentActions recentAction)
        {
            switch (recentAction)
            {
                case RecentActions.ClearHistory:
                    lblRecentAction.Text = "Clear History";
                    break;

                case RecentActions.OpenCalculator:
                    lblRecentAction.Text = "Open Calculator";
                    break;

                case RecentActions.OpenFile:
                    lblRecentAction.Text = "Open file";
                    break;

                case RecentActions.OpenNotepad:
                    lblRecentAction.Text = "Open Notepad";
                    break;

                case RecentActions.OpenPaint:
                    lblRecentAction.Text = "Open Paint";
                    break;
            }
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

Tags: jumplist c# winformjumplist c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn sử dụng JumpList trong Winform
Đăng bởi: Thảo Meo - Lượt xem: 3789 08:45:54, 14/07/2020ANDROID

CÁC BÀI CÙNG CHỦ ĐỀ

.