- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
- [C#] Thiết lập dấu (,) hay dấu (.) ở định dạng số đúng với định dạng số Việt Nam
- [C#] Chia sẻ source code Game Spin Lucky Wheel
- [C#] Hướng dẫn Encode and Decode HTML
- Danh sách tài khoản ChatGPT miễn phí - Hướng dẫn tạo tài khoản Chat Open AI GPT tại Việt Nam
- [C#] Hướng dẫn thay đổi giao diện ứng dụng Winform theo giao diện của Windows
- [VB.NET] Hiệu ứng Acrylic, Mica, Tabbed Blur Effect trên Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên Combobox Edit
- [C#] Chia sẻ source code Orange Rain in Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên XtraMessageBox Winform Devexpress 22.2.3
- [DEVEXPRESS] Hướng dẫn sử dụng HTML and CSS Code Viewer trên Winform
- [C#] Number Effect Counter up and down in winform
- [C#] Hướng dẫn Supend and Resume Process ID in Winform
- [C#] Hiển thị line number trên Richtextbox Winform
- [C#] Fake Blue Screen BSOD in winform
- [C#] Chia sẽ code demo sử dụng Async Parallel Foreach and For in Winform
- [C#] Sử dụng ActionBlock run X task at time winform
- [C#] Hướng dẫn sử dụng Property Grid để lưu và tải lại thông tin cấu hình user trên winform
[C#] Hướng dẫn sử dụng JumpList trong Winform
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.
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#:
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!