{"id":2303,"date":"2013-12-23T07:00:00","date_gmt":"2013-12-23T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/12\/23\/creating-custom-tasks-on-a-jump-list\/"},"modified":"2013-12-23T07:00:00","modified_gmt":"2013-12-23T07:00:00","slug":"creating-custom-tasks-on-a-jump-list","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20131223-00\/?p=2303","title":{"rendered":"Creating custom tasks on a jump list"},"content":{"rendered":"<p>\nToday&#8217;s Little Program adds a custom task to the application&#8217;s\njump list.\nTake the\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2003\/07\/23\/54576.aspx\">\nscratch program<\/a>\nand make the following changes.\n(Remember, Little Programs do very little error checking\nbecause that&#8217;s how they roll.)\n<\/p>\n<pre>\n<font COLOR=\"blue\">#include &lt;shlobj.h&gt;\n#include &lt;propkey.h&gt;\n#include &lt;wrl\/client.h&gt;\nusing namespace Microsoft::WRL;\nComPtr&lt;IShellLink&gt;\nCreateShellLinkForTask(\n    PCWSTR pszTitle,\n    PCTSTR pszArgs,\n    int idIcon)\n{\n    ComPtr&lt;IShellLink&gt; spsl;\n    CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&amp;spsl));\n    wchar_t szBuf[MAX_PATH];\n    GetModuleFileName(g_hinst, szBuf, ARRAYSIZE(szBuf));\n    spsl-&gt;SetPath(szBuf);\n    spsl-&gt;SetArguments(pszArgs);\n    spsl-&gt;SetIconLocation(szBuf, idIcon);\n    PROPVARIANT pvar;\n    pvar.vt = VT_LPWSTR;\n    pvar.pwszVal = const_cast&lt;PWSTR&gt;(pszTitle);\n    ComPtr&lt;IPropertyStore&gt; spps;\n    spsl.As(&amp;spps);\n    spps-&gt;SetValue(PKEY_Title, pvar);\n    spps-&gt;Commit();\n    return spsl;\n}<\/font>\n<\/pre>\n<p>\nThis helper function creates an\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/02\/24\/10133280.aspx\">\nin-memory shell link object<\/a>\nwith the specified title, command line arguments, and icon.\nThe underlying executable is assumed to be the running executable.\n<\/p>\n<pre>\nBOOL\nOnCreate(HWND hwnd, LPCREATESTRUCT lpcs)\n{\n  <font COLOR=\"blue\">ComPtr&lt;ICustomDestinationList&gt; spcdl;\n  CoCreateInstance(CLSID_DestinationList, nullptr, CLSCTX_ALL,\n                   IID_PPV_ARGS(&amp;spcdl));\n  ComPtr&lt;IObjectCollection&gt; spoc;\n  UINT cMinSlots;\n  spcdl-&gt;BeginList(&amp;cMinSlots, IID_PPV_ARGS(&amp;spoc));\n  spoc-&gt;Clear();\n  spoc-&gt;AddObject(CreateShellLinkForTask(L\"New frob\",\n                   TEXT(\"\/frob\"), -2).Get());\n  spcdl-&gt;AddUserTasks(spoc.Get());\n  spcdl-&gt;CommitList();<\/font>\n  return TRUE;\n}\n<\/pre>\n<p>\nWhen our window is created,\nwe get the destination list for our application\nand ask it for an object collection so we can fill it with tasks.\nWe empty the existing collection and add a single shortcut\ncalled &#8220;New frob&#8221; and which passes the <code>\/frob<\/code> command line\nargument.\nThe icon here is given as a negative number to indicate\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/05\/05\/10007461.aspx\">\nthat it is an icon ID rather than an icon index<\/a>.\nWe then tell the destination list that this is our new task collection.\n<\/p>\n<p>\nBefore we forget, let&#8217;s add the icon to our resource file.\n<\/p>\n<pre>\n\/\/ scratch.rc\n1 ICON icon1.ico\n2 ICON icon2.ico\n<\/pre>\n<p>\nI&#8217;ll leave you to find some icons to use.\nIcon number 2 is the one that will be used for the jump list.\n(Icon number 1 I left to represent the application itself.)\n<\/p>\n<p>\nFinally, we respond to the command line switch.\n<\/p>\n<pre>\nint WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev,\n                   LPSTR lpCmdLine, int nShowCmd)\n{\n  <font COLOR=\"blue\">if (strcmp(lpCmdLine, \"\/frob\") == 0) {\n    MessageBox(nullptr, L\"Frob!\", L\"Title\", MB_OK);\n    return 0;\n  }<\/font>\n  ...\n}\n<\/pre>\n<p>\nIf the command line switch <code>\/frob<\/code> is passed,\nthen we say something silly.\nIn real life, we would create a new frob,\npossibly by looking for an existing running copy of the program\nand asking it to do the creation.\n<\/p>\n<p>\nOkay, run this program and then right-click on the taskbar icon.\nObserve that there is now a <i>New frob<\/i> task,\nand if you select it,\nyou get the silly message.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little Program adds a custom task to the application&#8217;s jump list. Take the scratch program and make the following changes. (Remember, Little Programs do very little error checking because that&#8217;s how they roll.) #include &lt;shlobj.h&gt; #include &lt;propkey.h&gt; #include &lt;wrl\/client.h&gt; using namespace Microsoft::WRL; ComPtr&lt;IShellLink&gt; CreateShellLinkForTask( PCWSTR pszTitle, PCTSTR pszArgs, int idIcon) { ComPtr&lt;IShellLink&gt; spsl; CoCreateInstance(CLSID_ShellLink, [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-2303","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Today&#8217;s Little Program adds a custom task to the application&#8217;s jump list. Take the scratch program and make the following changes. (Remember, Little Programs do very little error checking because that&#8217;s how they roll.) #include &lt;shlobj.h&gt; #include &lt;propkey.h&gt; #include &lt;wrl\/client.h&gt; using namespace Microsoft::WRL; ComPtr&lt;IShellLink&gt; CreateShellLinkForTask( PCWSTR pszTitle, PCTSTR pszArgs, int idIcon) { ComPtr&lt;IShellLink&gt; spsl; CoCreateInstance(CLSID_ShellLink, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=2303"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2303\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=2303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=2303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=2303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}