打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Jump List with Windows Forms in C# for Visual...

Jump List with Windows Forms

Jump List Helpers is a library that allows to add Jump List to Windows Forms Applications and to be notified when a command is selected (instead of starting a new application instance, that is the default behaviour).

C# (1.4 MB)

Jump List Helpers: create and manage Jump List with Windows Forms

Jump List Helpers is a library that allows to add Jump List to Windows Forms Applications and to be notified when a command is selected (instead of starting a new application instance, that is the default behaviour).

The library is available on NuGet too.

For more information, you can refer to the article Jump List con Windows Forms (in Italian) that I have written forDotNetToscana.

Building the Sample

The code you can download from this page contains a Class Library, named JumpListHelpers, that provides all you need to add and manage Jump List to your applications. It also includes a Windows Forms Application,JumpListManagerExample, that shows some examples of its usage:

 

 

Description

WPF 4 provides a native support for Jump List. Instead, if we use Windows Forms, we must adopt the Windows API Code Pack, a managed wrapper that allows to access to the Windows API functions. Available on NuGet too, it provides all you need to manage Jump Lists within our Windows Forms Applications.

Unfortunately, a limit of Jump Lists (no matter the technology we use) is that the commands shown are able to start programs, URLs and files only: it isn't possible to send command directly to the application instance that owns the Jump List.

So, I developed Jump List Helpers, a library built on top of Windows API Code Pack that is able to overcome this limitation. It allows to add commands to the Jump List that start external programs and commands that can be received by the application itself via a normal event.

Its usage is very simple. For example, the following code creates the Jump List shown in the previous screenshot:
C#
private void MainForm_Shown(object sender, EventArgs e) {     JumpListManager.AddCategorySelfLink("Commands""First command""Command1");     JumpListManager.AddCategorySelfLink("Commands""Second command""Command2");      JumpListManager.AddCategorySelfLink("Commands""Third command""Command3");           JumpListManager.AddCategoryLink("Links""Around and About .NET World""http://blogs.ugidotnet.org/marcom""shell32.dll"220);           JumpListManager.AddTaskLink("Start Notepad""notepad.exe""notepad.exe");     JumpListManager.AddTaskLink("Start Calculator""calc.exe""calc.exe");      JumpListManager.AddTaskLink("Start Paint""mspaint.exe""mspaint.exe");           JumpListManager.Refresh(); }
Note that Jump Lists require a visible form when they are created, otherwise we'll get an exception. So, we can define then inside the form Shown event handler. The first threed methods include the term Self in the name, so they define commands that belong to our application. The others are normal command that starts external program or open an Internet address.

The next step is to modify our program so that it receives the selected command (Command1Command2 andCommand3 in the example), instead of starting a new instance. So, when the program starts, we must check if a previous instance is already running and, in such a situation, send it the selected command.

Jump List Helpers provides a series of methods and events that makes these checks really simple.

First of all, we must open the Program.cs file and modify the Main method so it looks like the following:
C#
[STAThread] static void Main() {     ProgramManager.Run(typeof(MainForm), "JumpListManager Example"); }
The ProgramManager.Run method requires the type of the application main form and its title. Internally, it verifies whether we are starting the program for the first time: in this case, it behaves like the standard Mainmethod. Otherwise, if retrieves the existing instance and pass to it the command line arguments: if the application has been started through the Jump List, they are the commands that we have set when we have created it (Command1Command2 or Command3).

Now, we need to modify our application main form so that it is able to receive Jump List command. Let's change its declaration so that it inherits from JumpListMainFormBase (contained in Jump List Helpers) and then add an handler for the JumpListCommandReceived event:
C#
public partial class MainForm : JumpListMainFormBase {     public MainForm()         {         InitializeComponent();                 this.JumpListCommandReceived += new EventHandler(MainForm_JumpListCommandReceived);         }       void MainForm_JumpListCommandReceived(object sender, CommandEventArgs e)         {         MessageBox.Show("Comando ricevuto: " + e.CommandName,              "JumpListManager Example", MessageBoxButtons.OK, MessageBoxIcon.Information);      }      // ... }
That's it: now, when we select one of the "self" command in the Jump List, the JumpListCommandReceived event handler will be called with the right parameter.

You can find a more complex example in the Windows Forms Application that is available in the ZIP file.

 

More Information

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Office2010 Win7 有效一起运行办公
常用英语课堂教学游戏
How to debug windows service
Xcode 9 快速跳转到定义新姿势(Jump to Definition)
“显示桌面”快捷方式图标不见了的解决方法
英特尔图形指挥中心应用程序 | 驱动程序详情 | Dell 中国
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服