Windows 7 任务栏 C# 快速参考






4.97/5 (17投票s)
以下是一些列表,用作使用 Windows API Code Pack 的常见 Windows 7 任务栏功能的快速参考。
以下是一些列表,用作使用 Windows API Code Pack 的常见 Windows 7 任务栏功能的快速参考。
此帖子中的代码基于 Yochay 和 Sasha 的先前工作。
特点
设置进程的应用程序 ID
TaskbarManager.Instance.ApplicationId = "TaskbarManaged";
注意:这应该在显示窗口之前完成,一个好的位置是在窗体加载时。
设置窗口的应用程序 ID
TaskbarManager.Instance.SetApplicationIdForSpecificWindow(form.Handle, "SomethingElse");
或者在 WPF 中
TaskbarManager.Instance.SetApplicationIdForSpecificWindow(wpfWindow, "SomethingElse");
将文件添加到 JumpList 的“最近”类别
_jumpList = JumpList.CreateJumpList();
...
_jumpList.AddToRecent(filePath);
注意:您必须将文件类型与您的应用程序关联(注册)才能使其工作。注意 2:如果您使用常用对话框打开文件,它将自动添加到最近列表。
将任务添加到 JumpList
IJumpListTask task1 = new JumpListLink(filePath, taskTitle)
{
Arguments = arguments,
WorkingDirectory = workingDirectory
};
IJumpListTask separator = new JumpListSeparator();
IJumpListTask task2 = ...
_jumpList.AddUserTasks(task1, separator, task2);
_jumpList.Refresh();
添加自定义类别
JumpListCustomCategory category = new JumpListCustomCategory("My Category");
_jumpList.AddCustomCategories(category);
IJumpListItem item1 = new JumpListLink(path1, title1);
IJumpListItem item2 = new JumpListLink(path2, title2);
category.AddJumpListItems(item1, item2);
创建缩略图工具栏
ThumbnailToolbarButton button1 = new ThumbnailToolbarButton(icon, tooltip);
button1.Click += delegate
{
MessageBox.Show("button1 clicked");
};
TaskbarManager.Instance.ThumbnailToolbars.AddButtons(form.Handle, button1);
设置叠加图标
TaskbarManager.Instance.SetOverlayIcon(icon, accessibilityText);
设置进度状态和值
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal, form.Handle);
TaskbarManager.Instance.SetProgressValue(currentValue, maximumValue, form.Handle);
自定义实时缩略图
TabbedThumbnail preview = new TabbedThumbnail(parentForm.Handle, childForm.Handle);
TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(preview);
preview.TabbedThumbnailBitmapRequested += (o, e) =>
{
Bitmap bmp = new Bitmap(width, height);
// draw custom bitmap...
e.SetImage(bmp);
e.Handled = true;
};
附加说明
Windows API Code Pack v1.0.1 中的一个缺点
当使用 Windows API Code Pack 库将任务栏功能添加到您的 WinForms 应用程序时,您必须添加对以下 WPF DLL 的引用:PresentationCore.dll、PresentationFramework 和 WindowsBase.dll
原因是库中每个在参数中具有句柄的任务栏函数都成对出现,第一个期望一个本机 IntPtr(与 myWinForm.Handle 属性一起使用),第二个期望一个 WPF Window 对象。由于我们使用一个在其参数中期望 WPF 类型的类(Taskbar),因此我们必须将 WPF DLL 添加到我们的 WinForms 项目。
Windows API Code Pack v1.0.1 中的一个错误
当进程以提升权限(即作为管理员)运行时,缩略图工具栏按钮的单击事件不会触发。
问题在于某些任务栏消息无法通过 Windows UIPI 机制传递。一个快速解决方法是在加载事件中添加以下行
private void Form_Load(object sender, EventArgs eventargs)
{
AllowTaskbarWindowMessagesThroughUIPI();
}
此方法的定义在此处提供
#region Fix bug in Windows API Code Pack
[DllImport("user32.dll", EntryPoint = "RegisterWindowMessage", SetLastError = true,
CharSet = CharSet.Unicode)]
private static extern uint RegisterWindowMessage([MarshalAs(
UnmanagedType.LPWStr)] string lpString);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr ChangeWindowMessageFilter(uint message, uint dwFlag);
private const uint MSGFLT_ADD = 1;
private const uint WM_COMMAND = 0x0111;
private const uint WM_SYSCOMMAND = 0x112;
private const uint WM_ACTIVATE = 0x0006;
/// <summary>
/// Specifies that the taskbar-related windows messages should
/// pass through the Windows UIPI mechanism even if the process is
/// running elevated. Calling this method is not required unless the
/// process is running elevated.
/// </summary>
private static void AllowTaskbarWindowMessagesThroughUIPI()
{
uint WM_TaskbarButtonCreated = RegisterWindowMessage("TaskbarButtonCreated");
ChangeWindowMessageFilter(WM_TaskbarButtonCreated, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_COMMAND, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_SYSCOMMAND, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_ACTIVATE, MSGFLT_ADD);
}
#endregion Fix bug in Windows API Code Pack
暂时就到这里,
Arik Poznanski。