{"id":93535,"date":"2016-05-30T07:00:00","date_gmt":"2016-05-30T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=93535"},"modified":"2019-03-13T11:03:40","modified_gmt":"2019-03-13T18:03:40","slug":"20160530-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20160530-00\/?p=93535","title":{"rendered":"Printing the name and position of the focused item on the desktop"},"content":{"rendered":"<p>Today&#8217;s Little Program prints the name and position of the focused item on the desktop. Remember, Little Programs do little to no error checking. <\/p>\n<pre>\n#define UNICODE\n#define <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/02\/12\/71851.aspx\">_UNICODE<\/a>\n#include \"stdafx.h\"\n#include &lt;windows.h&gt;\n#include &lt;shlobj.h&gt;\n#include &lt;exdisp.h&gt;\n#include &lt;shlwapi.h&gt;\n#include &lt;atlbase.h&gt;\n#include &lt;atlalloc.h&gt;\n#include &lt;stdio.h&gt;\n\nint __cdecl wmain(int argc, wchar_t **argv)\n{\n  <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">CCoInitialize<\/a> init;\n  CComPtr&lt;IFolderView&gt; spView;\n  <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20130318-00\/?p=4933\">FindDesktopFolderView<\/a>(IID_PPV_ARGS(&amp;spView));\n  CComPtr&lt;IShellFolder&gt; spFolder;\n  spView-&gt;GetFolder(IID_PPV_ARGS(&amp;spFolder));\n\n  int iItem;\n  if (FAILED(spView-&gt;GetFocusedItem(&amp;iItem))) {\n    wprintf(L\"Sorry, no focused item.\\n\");\n    return 0;\n  }\n\n  CComHeapPtr&lt;ITEMID_CHILD&gt; spidl;\n  spView-&gt;Item(iItem, &amp;spidl);\n\n  STRRET str;\n  spFolder-&gt;GetDisplayNameOf(spidl, SHGDN_NORMAL, &amp;str);\n  CComHeapPtr&lt;wchar_t&gt; spszName;\n  StrRetToStr(&amp;str, spidl, &amp;spszName);\n\n  wprintf(L\"Focused item is %ls\\n\", static_cast&lt;LPWSTR&gt;(spszName));\n  spszName.Free();\n\n  spFolder-&gt;GetDisplayNameOf(spidl, SHGDN_FORPARSING, &amp;str);\n  StrRetToStr(&amp;str, spidl, &amp;spszName);\n  wprintf(L\"Parsing name is %ls\\n\", static_cast&lt;LPWSTR&gt;(spszName));\n\n  POINT pt;\n  spView-&gt;GetItemPosition(spidl, &amp;pt);\n  wprintf(L\"Position is %d, %d\\n\", pt.x, pt.y);\n\n  return 0;\n}\n<\/pre>\n<p>We actually have most of the necessary pieces lying around already. <\/p>\n<p>After initializing COM and getting the desktop folder view, we get the underlying <code>IShell&shy;Folder<\/code> because we&#8217;re going to need it in order to interpret the pidls that come out later. <\/p>\n<p>We ask the view for the index of the focused item. If it can&#8217;t cough one up, then we apologize and exit. <\/p>\n<p>Otherwise, we use that index to get the corresponding pidl and then ask the folder to convert it into a normal name (which is the name shown under the icon) and a parsing name (which for files is the full path name). <\/p>\n<p>Finally, we ask for the item position. <\/p>\n<p>There you have it, a little program that identifies the current focused item on the desktop. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>You already know how to get the view, so ask the view.<\/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-93535","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You already know how to get the view, so ask the view.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/93535","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=93535"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/93535\/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=93535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=93535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=93535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}