在 Windows 11 中,使用 Win32 或 UWP API 在任务栏中制作图标动画可以通过几种不同的方法实现。以下是一些常见的方法:
使用 Win32 API,你可以通过 Shell 通知图标(System Tray Icon)来实现图标动画。你可以定期更新图标以创建动画效果。
以下是一个使用 Win32 API 创建任务栏图标动画的示例:
#include <windows.h>
#include <shellapi.h>
#include <thread>
#include <vector>
#define ID_TRAY_APP_ICON 1001
#define WM_TRAYICON (WM_USER + 1)
HINSTANCE hInst;
NOTIFYICONDATA nid;
std::vector<HICON> icons;
bool running = true;
void LoadIcons() {
icons.push_back(LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1)));
icons.push_back(LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON2)));
icons.push_back(LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON3)));
// Add more icons as needed
}
void AnimateIcon() {
int index = 0;
while (running) {
nid.hIcon = icons[index];
Shell_NotifyIcon(NIM_MODIFY, &nid);
index = (index + 1) % icons.size();
std::this_thread::sleep_for(std::chrono::milliseconds(500)); // Change delay as needed
}
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_TRAYICON:
if (lParam == WM_LBUTTONUP) {
MessageBox(hwnd, L"Tray icon clicked!", L"Info", MB_OK);
}
break;
case WM_DESTROY:
running = false;
Shell_NotifyIcon(NIM_DELETE, &nid);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {
hInst = hInstance;
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hInstance, NULL, NULL, NULL, NULL, L"TrayIconApp", NULL };
RegisterClassEx(&wc);
HWND hwnd = CreateWindow(L"TrayIconApp", L"Tray Icon Animation", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
LoadIcons();
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hwnd;
nid.uID = ID_TRAY_APP_ICON;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_TRAYICON;
wcscpy_s(nid.szTip, L"Tray Icon Animation");
Shell_NotifyIcon(NIM_ADD, &nid);
std::thread animationThread(AnimateIcon);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
animationThread.join();
return (int)msg.wParam;
}
在 UWP 应用中,你可以使用 Taskbar Extensions 来实现任务栏图标动画。UWP 提供了一些 API 来与任务栏交互。
以下是一个使用 UWP API 创建任务栏图标动画的示例:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
namespace TaskbarIconAnimation
{
public sealed partial class MainPage : Page
{
private List<BitmapImage> _icons;
private int _currentIconIndex = 0;
private DispatcherTimer _timer;
public MainPage()
{
this.InitializeComponent();
LoadIcons();
StartAnimation();
}
private void LoadIcons()
{
_icons = new List<BitmapImage>
{
new BitmapImage(new Uri("ms-appx:///Assets/Icon1.png")),
new BitmapImage(new Uri("ms-appx:///Assets/Icon2.png")),
new BitmapImage(new Uri("ms-appx:///Assets/Icon3.png"))
// Add more icons as needed
};
}
private void StartAnimation()
{
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(500); // Change delay as needed
_timer.Tick += (sender, e) => UpdateIcon();
_timer.Start();
}
private async void UpdateIcon()
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
var taskbarIcon = new Windows.UI.Shell.TaskbarManager();
taskbarIcon.SetIcon(_icons[_currentIconIndex]);
_currentIconIndex = (_currentIconIndex + 1) % _icons.Count;
});
}
}
}
领取专属 10元无门槛券
手把手带您无忧上云