任务管理器






4.90/5 (91投票s)
使用 Windows 7 的一些令人兴奋的功能来管理您的日常任务和待办事项列表。
引言
任务管理器是一个用于管理日常任务/待办事项列表的工具。您可以利用 Windows 7 的一些令人兴奋的功能轻松管理您的任务。任务管理器提供了设置任务标题、详细信息、开始日期、结束日期、优先级、颜色标签和标志等功能。
与之前的 Windows 版本相比,Windows 7 提供了一个非常漂亮且易于管理的任务栏。因此,我在此应用程序中使用了 Windows 7 引入的一些新功能,如 JumpList、TabbedThumbnail 等。借助这些新功能,任务管理器成为一个更具交互性的应用程序。本文将解释如何在应用程序中使用这些功能,并将教您以高级方式使用 ComboBox
和 DataGridView
。
任务管理器还有一个独立版本,供没有 Windows 7 的用户使用。
跳转列表
任务管理器提供了一种通过 Windows 7 的跳转列表轻松查看/编辑任务的方式。您可以通过任务管理器的“设置”窗口轻松设置跳转列表的视图。
“设置”窗口提供了三种方式来对跳转列表进行分组:按优先级、颜色类别或标志。
使用 Windows API Code Pack 管理跳转列表非常容易。调用 JumpList.CreateJumpList()
方法来为您的应用程序创建跳转列表。
private JumpList _jumpList;
private void InitTaskJumpList()
{
_jumpList = JumpList.CreateJumpList();
}
这是创建按颜色类别分组的跳转列表的示例。创建 JumpListCustomCategory
对象并将其添加到跳转列表对象中。
private void ShowJumpListByColorCategory(IQueryable<task> tasks)
{
string[] colorCategories = Enum.GetNames(typeof(TaskCategory));
foreach (string catName in colorCategories)
{
// Get task by color category
IQueryable<Task> colorTask = tasks.Where(t => t.ColorCategory == catName);
if (colorTask.Count() > 0)
{
// Create jump list custom category
JumpListCustomCategory customList = new JumpListCustomCategory(catName);
// Add jump list category in list
_jumpList.AddCustomCategories(customList);
// Add tasks in custom category
foreach (Task task in colorTask)
{
string imagePath = "Resources\\" + catName + "_Category.ico";
AddTask(customList, task.Title, task.TaskID.ToString(), imagePath);
}
}
}
}
使用 AddJumpListItems()
方法将 JumpListLink
添加到 JumpListCustomCategory
集合中,以创建跳转列表链接。
private void AddTask(JumpListCustomCategory customCategory,
string text, string argument, string imagePath)
{
// Get executable file path
string path = Path.GetDirectoryName(Application.ExecutablePath);
// Creat jump list link
JumpListLink jumpListLink =
new JumpListLink(Assembly.GetExecutingAssembly().Location, text);
jumpListLink.Arguments = argument;
jumpListLink.IconReference =
new IconReference(Path.Combine(path, imagePath), 0);
// Add link in category
customCategory.AddJumpListItems(jumpListLink);
}
JumpListLink
在每次点击时都会获取应用程序可执行文件的路径及其打开的窗口。但是,任务管理器在链接点击时会打开同一应用程序的编辑任务窗口。您可以使用 Mutex
类来实现此功能。Mutex
用于多线程环境中,用于同步机制,以确保一次只有一个线程使用资源。SingleInstanceChecker
类用于检查应用程序的运行实例。
using (SingleInstanceChecker singleInstance =
new SingleInstanceChecker("TaskManager"))
{
if (!singleInstance.IsSingle)
{
HandleJumpListCommand();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
TaskManagerMainForm taskManagerMainForm = new TaskManagerMainForm();
Application.Run(taskManagerMainForm);
}
}
如果 SingleInstanceChecker
将 IsSingle
设置为 false
,则表示应用程序已在运行;使用 HandleJumpListCommand()
方法进行处理。如果此事件是由于跳转列表项引起的,则此方法会将一个窗口消息发送到 TaskManagerMainForm
。TaskManagerMainForm
通过重写 WndProc
方法来处理窗口消息,从而处理此消息。
protected override void WndProc(ref Message m)
{
if (m.Msg == WindowMessageHelper.JumplistEvent)
{
int taskID = m.WParam.ToInt32();
OpenAddNewTaskForm(taskID);
}
base.WndProc(ref m);
}
选项卡式缩略图
您可以使用 Windows 7 的选项卡式缩略图轻松导航未完成的任务。任务管理器允许您通过缩略图向前和向后移动您的任务,并且您还可以通过“编辑”按钮来编辑/查看您的任务详细信息。
Windows 7 API Code Pack 还提供了一种轻松添加选项卡式缩略图预览的方法。只需创建一个 TabbedThumbnail
对象,然后使用 TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview()
方法将其添加。您还可以使用 ThumbnailToolbarButton
类在其上添加按钮。创建该类的对象,并使用 TaskbarManager.Instance.ThumbnailToolbars.AddButtons()
方法将其添加到缩略图中。注册按钮的点击事件以在按钮点击时执行相应的操作。
private void InitializeThumbnail()
{
if (!HasThumbnailControl(_controlToShow))
{
// Back Button
_btnBack = new ThumbnailToolbarButton(Properties.Resources.Back,
"Previous Task");
_btnBack.Click +=
new EventHandler<thumbnailbuttonclickedeventargs>(_btnBack_Click);
// Forwaard
_btnForward = new ThumbnailToolbarButton(Properties.Resources.Forward,
"Next Task");
_btnForward.Click += new
EventHandler<ThumbnailButtonClickedEventArgs>(_btnForward_Click);
// Edit
_btnEdit = new ThumbnailToolbarButton(
Properties.Resources.edit_Icon, "Edit Task");
_btnEdit.Click += new
EventHandler<ThumbnailButtonClickedEventArgs>(_btnEdit_Click);
TabbedThumbnail thumbnail =
new TabbedThumbnail(_parentHandle, _controlToShow);
thumbnail.DisplayFrameAroundBitmap = true;
thumbnail.SetWindowIcon(Properties.Resources.Task_manager);
thumbnail.Title = "Task Manager";
thumbnail.Tooltip = "Task Manager";
thumbnail.TabbedThumbnailClosed += new
EventHandler<TabbedThumbnailEventArgs>(
thumbnail_TabbedThumbnailClosed);
thumbnail.TabbedThumbnailActivated +=
new EventHandler<TabbedThumbnailEventArgs>(
thumbnail_TabbedThumbnailActivated);
// Add Buttons in the tool bar
TaskbarManager.Instance.ThumbnailToolbars.AddButtons(
_controlToShow.Handle, _btnBack, _btnForward, _btnEdit);
// Add Tabbed preview
TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(thumbnail);
// Set active tab
TaskbarManager.Instance.TabbedThumbnail.SetActiveTab(_controlToShow);
// Set Thumnail preview
SetThumbnail();
}
}
SetThumbnail()
方法设置当前任务的预览图像。它使用 SetImage()
方法设置图像。
private void SetThumbnail()
{
TabbedThumbnail preview =
TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(_controlToShow);
if (preview != null)
{
Task task = null;
string toolTip = "Task Manager";
if (_taskList.Count > _selectedIndex)
{
task = _taskList[_selectedIndex];
toolTip = task.Title;
}
_controlToShow.LoadTaskData(task);
preview.Tooltip = toolTip;
Bitmap bitmap = new Bitmap(_controlToShow.Width, _controlToShow.Height);
bitmap = _controlToShow.Image;
preview.SetImage(bitmap);
TaskbarManager.Instance.TabbedThumbnail.SetActiveTab(_controlToShow);
}
}
Progress Bar
任务管理器还使用 Windows 7 Code Pack 功能在进度条中显示任务完成的百分比。您可以轻松设置进度条的值。
public static void SetProgressValue(int value)
{
TaskbarProgressBarState state = TaskbarProgressBarState.Normal;
TaskbarManager.Instance.SetProgressState(state);
TaskbarManager.Instance.SetProgressValue(value, 100);
}
覆盖图标
图标覆盖是 Windows 7 的最佳功能。您只需看一眼任务栏即可轻松了解应用程序的状态。任务管理器还在任务栏上显示两个图标:一个用于错误,一个用于确认/问题。
|
|
public static void SetTaskBarIcon(Icon icon)
{
TaskbarManager.Instance.SetOverlayIcon(icon, "icon");
}
甘特图
您还可以在任务管理器中查看任务的甘特视图。此甘特图仅显示日视图。甘特图网格将周末(周六、周日)的单元格显示为灰色,并将当前天的单元格显示为绿色线条。您可以看到繁忙的日子以红色条显示,并且在鼠标悬停在繁忙的单元格上时,还会显示带有任务详细信息的工具提示。
GanttChart
类继承自 DataGridView
,因为甘特图视图类似于网格。GanttChart
类重写了 DataGridview
的一些方法以更改默认行为。
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
// Draw row selector header
if (e.ColumnIndex < 0)
{
DrawCellColor(e.Graphics, e.CellBounds, Color.SteelBlue, Color.Black);
e.PaintContent(e.CellBounds);
}
/// Draw Header Cell
else if (e.RowIndex < 0 && e.ColumnIndex >= 0)
{
DrawDayHeaderCell(e);
}
/// Draw Cell
else
{
DrawDataCell(e);
}
// set true to set it is handled
e.Handled = true;
}
OnCellPainting
方法在每个单元格绘制时执行,并根据其数据和类型重写单元格绘制方法。如果单元格是标题,则绘制日期标题;如果单元格是数据,则根据其数据绘制单元格。OnPaint
方法处理年份标题的绘制。
protected override void OnPaint(PaintEventArgs e)
{
DrawYearTopHeader(e.Graphics);
base.OnPaint(e);
}
OnPaint
方法仅绘制年份标题并调用基类的 OnPaint(e)
方法。
组合框中的图像
任务管理器在组合框中显示优先级、颜色类别和标志的图像。.NET 提供了一种在组合框中添加图像的简便方法。下面是一个优先级组合框的代码示例。
public static void BindTaskPriorityCombo(ComboBox priorityCombo, bool addEmpty)
{
priorityCombo.DrawMode = DrawMode.OwnerDrawVariable;
priorityCombo.DrawItem += new DrawItemEventHandler(priorityCombo_DrawItem);
priorityCombo.Items.Clear();
if (addEmpty)
{
priorityCombo.Items.Add("");
}
foreach (TaskPriority priority in Enum.GetValues(typeof(TaskPriority)))
{
priorityCombo.Items.Add(priority);
}
}
static void priorityCombo_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index >= 0)
{
ComboBox cmbPriority = sender as ComboBox;
string text = cmbPriority.Items[e.Index].ToString();
e.DrawBackground();
if (text.Length > 0)
{
TaskPriority priority =
(TaskPriority)Enum.Parse(typeof(TaskPriority), text);
Image img = GetTaskPriorityImage(priority);
if (img != null)
{
e.Graphics.DrawImage(img, e.Bounds.X, e.Bounds.Y, 15, 15);
}
}
e.Graphics.DrawString(text, cmbPriority.Font,
System.Drawing.Brushes.Black,
new RectangleF(e.Bounds.X + 15, e.Bounds.Y,
e.Bounds.Width, e.Bounds.Height));
e.DrawFocusRectangle();
}
}
要在组合框中添加图像,您需要设置组合框的 DrawMode
:DrawMode = DrawMode.OwnerDrawVariable
。然后,您需要重写组合框的 DrawItem
事件,您可以在其中按照您想要的方式绘制项目。