[C#] Thêm ứng dụng vào Taskbar sử dụng SharpShell DeskBand
Xin chào các bạn bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn, cách tích hợp ứng dụng của mình vào TaskBar trên Windows hay còn gọi là DeskBand C#, Winform.
[C#] Network Monitor Deskband with Sharpshell Winform
Vậy DeskBand C# là gì? các bạn có thể xem mấy hình ảnh dưới đây.
1. Ứng dụng Network monitor
2. Ứng dụng xem dung lượng Pin Laptop
Ở bài viết này mình sẽ demo ứng dụng Network Monitor.
Từ Taskbar các bạn Right Click chuột phải => Toolbar => Network Monitor vb.net
Kết quả ta sẽ được ứng dụng Deskband Network Monitor example như hình dưới đây:
Đầu tiên các bạn tạo cho mình một project Windows Form Control Library
Tiếp đến các bạn import cho mình thư viện SharpShell
vào từ Nuget Console
PM> Install-Package SharpShell -Version 2.7.2
Các bạn tạo cho mình một Usercontrol với tên DeskBandUI.cs
như code bên dưới:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Net.NetworkInformation;
namespace WebSearchDeskBand
{
public partial class DeskBandUI : UserControl
{
private NetworkInterface[] nicArr;
private Timer timer;
NetworkInterface nic;
public DeskBandUI()
{
InitializeComponent();
nicArr = NetworkInterface.GetAllNetworkInterfaces();
nic = nicArr[0];
InitializeTimer();
}
private void InitializeTimer()
{
timer = new Timer();
timer.Enabled = true;
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
string bytesend = "0";
string byteReceived = "0";
private void UpdateNetworkInterface()
{
IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics();
long bytesSentSpeed = (long)(interfaceStats.BytesSent - double.Parse(bytesend)) / 1024;
long bytesReceivedSpeed = (long)(interfaceStats.BytesReceived - double.Parse(byteReceived)) / 1024;
lblUpload.Text = bytesSentSpeed.ToString() + " KB/s";
lblDownload.Text = bytesReceivedSpeed.ToString() + " KB/s";
bytesend = interfaceStats.BytesSent.ToString("N0");
byteReceived = interfaceStats.BytesReceived.ToString("N0");
// lblUpload.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
}
void timer_Tick(object sender, EventArgs e)
{
UpdateNetworkInterface();
}
}
}
Tiếp tục viết một class WebSearchDeskBand.cs
using SharpShell.Attributes;
using SharpShell.SharpDeskBand;
using System.Runtime.InteropServices;
namespace WebSearchDeskBand
{
[ComVisible(true)]
[DisplayName("Network Monitor - vb.net")] // tên hiển thị ở thanh toolbar
public class WebSearchDeskBand : SharpDeskBand
{
protected override System.Windows.Forms.UserControl CreateDeskBand()
{
return new DeskBandUI();
}
protected override BandOptions GetBandOptions()
{
return new BandOptions
{
HasVariableHeight = false,
IsSunken = false,
ShowTitle = false,
Title = "Network Monitor",
UseBackgroundColour = false,
AlwaysShowGripper = true
};
}
}
}
[DisplayName("Network Monitor - vb.net")] => tên hiển thị ở thanh toolbar
Xong khi viết xong nó sẽ build ra file Dll, các bạn sẽ tiến hành chạy lệnh command dos để đăng ký nó vào Windows Explorer.
Đầu tiên, các bạn vào thư mục Bin/Debug hoặc Release
Sau đó bạn thực hiện 2 lệnh sau để Đăng ký và Hủy đăng ký.
Lênh command regasm.exe các bạn cần thêm vào Path Environment như hình mình bên dưới.
Khi đăng ký hoặc hủy đăng ký các bạn nhớ Restart lại Windows Explorer từ TaskManager nhé.
Vì ứng dụng Deskband sẽ đăng ký toolbar vào Windows Explorer.
Thanks for waching!