How to hide Desktop icons with Windows API in C++?
The answers I've found link to fHideIcon, but I get a 404 error on Microsoft's page for those links.
I've also tried:
SHELLSTATE ss;
SecureZeroMemory(&ss, sizeof(ss));
SHGetSetSettings(&ss, SSF_HIDEICONS, TRUE);
But that didn't work.
Does anyone know how to do this?
c++ windows winapi
|
show 1 more comment
The answers I've found link to fHideIcon, but I get a 404 error on Microsoft's page for those links.
I've also tried:
SHELLSTATE ss;
SecureZeroMemory(&ss, sizeof(ss));
SHGetSetSettings(&ss, SSF_HIDEICONS, TRUE);
But that didn't work.
Does anyone know how to do this?
c++ windows winapi
MS sure doesn't maintain its winapi C++ documentation like it used to.
– Christopher Pisz
Nov 14 '18 at 23:38
@Christopher Was it good before?
– Adam
Nov 14 '18 at 23:43
It was 'better' from 1998-2003. At least the links worked.
– Christopher Pisz
Nov 14 '18 at 23:49
MSDN have been migrating its documentation frommsdn.microsoft.comto a new format ondocs.microsoft.comfor some time now, and a lot of things are falling through the cracks (broken links, incomplete text, etc). It is horrible. IMHO, there was nothing wrong with the old documentation, they should have just left it alone.
– Remy Lebeau
Nov 15 '18 at 1:58
@rem: While I agree that the current state of the documentation is a pretty significant regression, there is actually a reason for moving it. Once complete, we will finally have a way to submit and publicly track documentation defects.
– IInspectable
Nov 15 '18 at 9:27
|
show 1 more comment
The answers I've found link to fHideIcon, but I get a 404 error on Microsoft's page for those links.
I've also tried:
SHELLSTATE ss;
SecureZeroMemory(&ss, sizeof(ss));
SHGetSetSettings(&ss, SSF_HIDEICONS, TRUE);
But that didn't work.
Does anyone know how to do this?
c++ windows winapi
The answers I've found link to fHideIcon, but I get a 404 error on Microsoft's page for those links.
I've also tried:
SHELLSTATE ss;
SecureZeroMemory(&ss, sizeof(ss));
SHGetSetSettings(&ss, SSF_HIDEICONS, TRUE);
But that didn't work.
Does anyone know how to do this?
c++ windows winapi
c++ windows winapi
edited Nov 14 '18 at 23:41
zx485
15.1k133048
15.1k133048
asked Nov 14 '18 at 23:37
AdamAdam
167
167
MS sure doesn't maintain its winapi C++ documentation like it used to.
– Christopher Pisz
Nov 14 '18 at 23:38
@Christopher Was it good before?
– Adam
Nov 14 '18 at 23:43
It was 'better' from 1998-2003. At least the links worked.
– Christopher Pisz
Nov 14 '18 at 23:49
MSDN have been migrating its documentation frommsdn.microsoft.comto a new format ondocs.microsoft.comfor some time now, and a lot of things are falling through the cracks (broken links, incomplete text, etc). It is horrible. IMHO, there was nothing wrong with the old documentation, they should have just left it alone.
– Remy Lebeau
Nov 15 '18 at 1:58
@rem: While I agree that the current state of the documentation is a pretty significant regression, there is actually a reason for moving it. Once complete, we will finally have a way to submit and publicly track documentation defects.
– IInspectable
Nov 15 '18 at 9:27
|
show 1 more comment
MS sure doesn't maintain its winapi C++ documentation like it used to.
– Christopher Pisz
Nov 14 '18 at 23:38
@Christopher Was it good before?
– Adam
Nov 14 '18 at 23:43
It was 'better' from 1998-2003. At least the links worked.
– Christopher Pisz
Nov 14 '18 at 23:49
MSDN have been migrating its documentation frommsdn.microsoft.comto a new format ondocs.microsoft.comfor some time now, and a lot of things are falling through the cracks (broken links, incomplete text, etc). It is horrible. IMHO, there was nothing wrong with the old documentation, they should have just left it alone.
– Remy Lebeau
Nov 15 '18 at 1:58
@rem: While I agree that the current state of the documentation is a pretty significant regression, there is actually a reason for moving it. Once complete, we will finally have a way to submit and publicly track documentation defects.
– IInspectable
Nov 15 '18 at 9:27
MS sure doesn't maintain its winapi C++ documentation like it used to.
– Christopher Pisz
Nov 14 '18 at 23:38
MS sure doesn't maintain its winapi C++ documentation like it used to.
– Christopher Pisz
Nov 14 '18 at 23:38
@Christopher Was it good before?
– Adam
Nov 14 '18 at 23:43
@Christopher Was it good before?
– Adam
Nov 14 '18 at 23:43
It was 'better' from 1998-2003. At least the links worked.
– Christopher Pisz
Nov 14 '18 at 23:49
It was 'better' from 1998-2003. At least the links worked.
– Christopher Pisz
Nov 14 '18 at 23:49
MSDN have been migrating its documentation from
msdn.microsoft.com to a new format on docs.microsoft.com for some time now, and a lot of things are falling through the cracks (broken links, incomplete text, etc). It is horrible. IMHO, there was nothing wrong with the old documentation, they should have just left it alone.– Remy Lebeau
Nov 15 '18 at 1:58
MSDN have been migrating its documentation from
msdn.microsoft.com to a new format on docs.microsoft.com for some time now, and a lot of things are falling through the cracks (broken links, incomplete text, etc). It is horrible. IMHO, there was nothing wrong with the old documentation, they should have just left it alone.– Remy Lebeau
Nov 15 '18 at 1:58
@rem: While I agree that the current state of the documentation is a pretty significant regression, there is actually a reason for moving it. Once complete, we will finally have a way to submit and publicly track documentation defects.
– IInspectable
Nov 15 '18 at 9:27
@rem: While I agree that the current state of the documentation is a pretty significant regression, there is actually a reason for moving it. Once complete, we will finally have a way to submit and publicly track documentation defects.
– IInspectable
Nov 15 '18 at 9:27
|
show 1 more comment
3 Answers
3
active
oldest
votes
The third parameter isn't about changing the setting, it's to select the SHGetSetSettings() behavior.
FALSE will get the value of the current setting and store it in ss, TRUE will set the value of the setting to what is in ss.
So basically you have to do ss.fHideIcons = TRUE and then call SHGetSetSettings(&ss, SSF_HIDEICONS, TRUE) to set it.
I know, it's weird, but on the other hand it allows you to change multiple settings simultaneously because SSF_* is a bitmask.
Except that doesn't work... I thinkss.fHideIconshas a different meaning to what the OP is hoping for
– Paul Sanders
Nov 14 '18 at 23:50
@PaulSanders Well hopefully you're doingss.fHideIcons = TRUEafter zeroing the memory, otherwise it's work undone.
– Havenard
Nov 14 '18 at 23:52
Well of course,
– Paul Sanders
Nov 14 '18 at 23:54
@PaulSanders Well... yeah, I just tested here and, my icons blink but that's about it. Funny thing they only blink when you actually change the setting to a value different to what it was before, regardless if I'm trying to hide or show the icons. It won't show icons either if I manually hide them on the Windows Explorer context menu. I don't know, maybe it's a Windows 10 bug? What Windows version are you using?
– Havenard
Nov 15 '18 at 0:09
1
@Havenard Oh well, you know MS: backwards compatibility is king.
– Paul Sanders
Nov 15 '18 at 0:20
|
show 12 more comments
The following approach which uses the official shell API is a little bit involved, but works even under Windows 10.
Steps:
- Get the
IFolderView2interface of the desktop (supported since Windows Vista). - Call
IFolderView2::SetCurrentFolderFlags()withFWF_NOICONSfor both thedwMaskanddwFlagsparameters.
The effect of the flag is visible immediately. There is no need to restart the computer nor "explorer.exe". The flag also persists after logoff or reboot.
The tricky thing is step 1). Raymond Chen shows C++ code for that in his article "Manipulating the positions of desktop icons", specifically in his FindDesktopFolderView() function.
Here is a full example in form of a console application. It is based on Raymond Chen's code. The program toggles the visibility of the desktop icons each time it is run.
The code has been tested under Windows 10 Version 1803.
"Library" code:
#include <ShlObj.h> // Shell API
#include <atlcomcli.h> // CComPtr & Co.
#include <string>
#include <iostream>
#include <system_error>
// Throw a std::system_error if the HRESULT indicates failure.
template< typename T >
void ThrowIfFailed( HRESULT hr, T&& msg )
{
if( FAILED( hr ) )
throw std::system_error{ hr, std::system_category(), std::forward<T>( msg ) };
}
// RAII wrapper to initialize/uninitialize COM
struct CComInit
{
CComInit() { ThrowIfFailed( ::CoInitialize( nullptr ), "CoInitialize failed" ); }
~CComInit() { ::CoUninitialize(); }
CComInit( CComInit const& ) = delete;
CComInit& operator=( CComInit const& ) = delete;
};
// Query an interface from the desktop shell view.
void FindDesktopFolderView( REFIID riid, void **ppv, std::string const& interfaceName )
{
CComPtr<IShellWindows> spShellWindows;
ThrowIfFailed(
spShellWindows.CoCreateInstance( CLSID_ShellWindows ),
"Failed to create IShellWindows instance" );
CComVariant vtLoc( CSIDL_DESKTOP );
CComVariant vtEmpty;
long lhwnd;
CComPtr<IDispatch> spdisp;
ThrowIfFailed(
spShellWindows->FindWindowSW(
&vtLoc, &vtEmpty, SWC_DESKTOP, &lhwnd, SWFO_NEEDDISPATCH, &spdisp ),
"Failed to find desktop window" );
CComQIPtr<IServiceProvider> spProv( spdisp );
if( ! spProv )
ThrowIfFailed( E_NOINTERFACE, "Failed to get IServiceProvider interface for desktop" );
CComPtr<IShellBrowser> spBrowser;
ThrowIfFailed(
spProv->QueryService( SID_STopLevelBrowser, IID_PPV_ARGS( &spBrowser ) ),
"Failed to get IShellBrowser for desktop" );
CComPtr<IShellView> spView;
ThrowIfFailed(
spBrowser->QueryActiveShellView( &spView ),
"Failed to query IShellView for desktop" );
ThrowIfFailed(
spView->QueryInterface( riid, ppv ),
"Could not query desktop IShellView for interface " + interfaceName );
}
Example to toggle desktop icons using the above code:
void ToggleDesktopIcons()
{
CComPtr<IFolderView2> spView;
FindDesktopFolderView( IID_PPV_ARGS(&spView), "IFolderView2" );
DWORD flags = 0;
ThrowIfFailed(
spView->GetCurrentFolderFlags( &flags ),
"GetCurrentFolderFlags failed" );
ThrowIfFailed(
spView->SetCurrentFolderFlags( FWF_NOICONS, flags ^ FWF_NOICONS ),
"SetCurrentFolderFlags failed" );
}
int wmain(int argc, wchar_t **argv)
{
try
{
CComInit init;
ToggleDesktopIcons();
std::cout << "Desktop icons have been toggled.n";
}
catch( std::system_error const& e )
{
std::cout << "ERROR: " << e.what() << ", error code: " << e.code() << "n";
return 1;
}
return 0;
}
add a comment |
The following seems to work (adapted from https://stackoverflow.com/a/6403014/5743288):
#include <windows.h>
int main ()
{
HWND hProgman = FindWindowW (L"Progman", L"Program Manager");
HWND hChild = GetWindow (hProgman, GW_CHILD);
ShowWindow (hChild, SW_HIDE);
Sleep (2000);
ShowWindow (hChild, SW_SHOW);
}
Please note: this approach is not supported by Microsoft and disables right-clicking on ths desktop.
@peter Thanks! Yeah I found that link, but I couldn't figure out how to make it work in C++. Can you possibly explain how this works? I don't understand it.
– Adam
Nov 15 '18 at 0:12
@Adam Yeah that's precisely the method I was talking about, I was just unsure if it worked across different versions of Windows. Geez I remember doing this on Windows 98...
– Havenard
Nov 15 '18 at 0:13
@adam As the post I linked to explains, it hides the parent window that hosts all the desktop icons. Hopefully, MS won't break this.
– Paul Sanders
Nov 15 '18 at 0:18
1
The Shell has a public API. Don't go about handing out solutions based on implementation details, without clearly marking them as unsupported.
– IInspectable
Nov 15 '18 at 9:22
1
May also want to note, that this loses the ability to right-click on the desktop.
– IInspectable
Nov 15 '18 at 20:58
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310374%2fhow-to-hide-desktop-icons-with-windows-api-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The third parameter isn't about changing the setting, it's to select the SHGetSetSettings() behavior.
FALSE will get the value of the current setting and store it in ss, TRUE will set the value of the setting to what is in ss.
So basically you have to do ss.fHideIcons = TRUE and then call SHGetSetSettings(&ss, SSF_HIDEICONS, TRUE) to set it.
I know, it's weird, but on the other hand it allows you to change multiple settings simultaneously because SSF_* is a bitmask.
Except that doesn't work... I thinkss.fHideIconshas a different meaning to what the OP is hoping for
– Paul Sanders
Nov 14 '18 at 23:50
@PaulSanders Well hopefully you're doingss.fHideIcons = TRUEafter zeroing the memory, otherwise it's work undone.
– Havenard
Nov 14 '18 at 23:52
Well of course,
– Paul Sanders
Nov 14 '18 at 23:54
@PaulSanders Well... yeah, I just tested here and, my icons blink but that's about it. Funny thing they only blink when you actually change the setting to a value different to what it was before, regardless if I'm trying to hide or show the icons. It won't show icons either if I manually hide them on the Windows Explorer context menu. I don't know, maybe it's a Windows 10 bug? What Windows version are you using?
– Havenard
Nov 15 '18 at 0:09
1
@Havenard Oh well, you know MS: backwards compatibility is king.
– Paul Sanders
Nov 15 '18 at 0:20
|
show 12 more comments
The third parameter isn't about changing the setting, it's to select the SHGetSetSettings() behavior.
FALSE will get the value of the current setting and store it in ss, TRUE will set the value of the setting to what is in ss.
So basically you have to do ss.fHideIcons = TRUE and then call SHGetSetSettings(&ss, SSF_HIDEICONS, TRUE) to set it.
I know, it's weird, but on the other hand it allows you to change multiple settings simultaneously because SSF_* is a bitmask.
Except that doesn't work... I thinkss.fHideIconshas a different meaning to what the OP is hoping for
– Paul Sanders
Nov 14 '18 at 23:50
@PaulSanders Well hopefully you're doingss.fHideIcons = TRUEafter zeroing the memory, otherwise it's work undone.
– Havenard
Nov 14 '18 at 23:52
Well of course,
– Paul Sanders
Nov 14 '18 at 23:54
@PaulSanders Well... yeah, I just tested here and, my icons blink but that's about it. Funny thing they only blink when you actually change the setting to a value different to what it was before, regardless if I'm trying to hide or show the icons. It won't show icons either if I manually hide them on the Windows Explorer context menu. I don't know, maybe it's a Windows 10 bug? What Windows version are you using?
– Havenard
Nov 15 '18 at 0:09
1
@Havenard Oh well, you know MS: backwards compatibility is king.
– Paul Sanders
Nov 15 '18 at 0:20
|
show 12 more comments
The third parameter isn't about changing the setting, it's to select the SHGetSetSettings() behavior.
FALSE will get the value of the current setting and store it in ss, TRUE will set the value of the setting to what is in ss.
So basically you have to do ss.fHideIcons = TRUE and then call SHGetSetSettings(&ss, SSF_HIDEICONS, TRUE) to set it.
I know, it's weird, but on the other hand it allows you to change multiple settings simultaneously because SSF_* is a bitmask.
The third parameter isn't about changing the setting, it's to select the SHGetSetSettings() behavior.
FALSE will get the value of the current setting and store it in ss, TRUE will set the value of the setting to what is in ss.
So basically you have to do ss.fHideIcons = TRUE and then call SHGetSetSettings(&ss, SSF_HIDEICONS, TRUE) to set it.
I know, it's weird, but on the other hand it allows you to change multiple settings simultaneously because SSF_* is a bitmask.
edited Nov 17 '18 at 0:01
answered Nov 14 '18 at 23:44
HavenardHavenard
18.3k22646
18.3k22646
Except that doesn't work... I thinkss.fHideIconshas a different meaning to what the OP is hoping for
– Paul Sanders
Nov 14 '18 at 23:50
@PaulSanders Well hopefully you're doingss.fHideIcons = TRUEafter zeroing the memory, otherwise it's work undone.
– Havenard
Nov 14 '18 at 23:52
Well of course,
– Paul Sanders
Nov 14 '18 at 23:54
@PaulSanders Well... yeah, I just tested here and, my icons blink but that's about it. Funny thing they only blink when you actually change the setting to a value different to what it was before, regardless if I'm trying to hide or show the icons. It won't show icons either if I manually hide them on the Windows Explorer context menu. I don't know, maybe it's a Windows 10 bug? What Windows version are you using?
– Havenard
Nov 15 '18 at 0:09
1
@Havenard Oh well, you know MS: backwards compatibility is king.
– Paul Sanders
Nov 15 '18 at 0:20
|
show 12 more comments
Except that doesn't work... I thinkss.fHideIconshas a different meaning to what the OP is hoping for
– Paul Sanders
Nov 14 '18 at 23:50
@PaulSanders Well hopefully you're doingss.fHideIcons = TRUEafter zeroing the memory, otherwise it's work undone.
– Havenard
Nov 14 '18 at 23:52
Well of course,
– Paul Sanders
Nov 14 '18 at 23:54
@PaulSanders Well... yeah, I just tested here and, my icons blink but that's about it. Funny thing they only blink when you actually change the setting to a value different to what it was before, regardless if I'm trying to hide or show the icons. It won't show icons either if I manually hide them on the Windows Explorer context menu. I don't know, maybe it's a Windows 10 bug? What Windows version are you using?
– Havenard
Nov 15 '18 at 0:09
1
@Havenard Oh well, you know MS: backwards compatibility is king.
– Paul Sanders
Nov 15 '18 at 0:20
Except that doesn't work... I think
ss.fHideIcons has a different meaning to what the OP is hoping for– Paul Sanders
Nov 14 '18 at 23:50
Except that doesn't work... I think
ss.fHideIcons has a different meaning to what the OP is hoping for– Paul Sanders
Nov 14 '18 at 23:50
@PaulSanders Well hopefully you're doing
ss.fHideIcons = TRUE after zeroing the memory, otherwise it's work undone.– Havenard
Nov 14 '18 at 23:52
@PaulSanders Well hopefully you're doing
ss.fHideIcons = TRUE after zeroing the memory, otherwise it's work undone.– Havenard
Nov 14 '18 at 23:52
Well of course,
– Paul Sanders
Nov 14 '18 at 23:54
Well of course,
– Paul Sanders
Nov 14 '18 at 23:54
@PaulSanders Well... yeah, I just tested here and, my icons blink but that's about it. Funny thing they only blink when you actually change the setting to a value different to what it was before, regardless if I'm trying to hide or show the icons. It won't show icons either if I manually hide them on the Windows Explorer context menu. I don't know, maybe it's a Windows 10 bug? What Windows version are you using?
– Havenard
Nov 15 '18 at 0:09
@PaulSanders Well... yeah, I just tested here and, my icons blink but that's about it. Funny thing they only blink when you actually change the setting to a value different to what it was before, regardless if I'm trying to hide or show the icons. It won't show icons either if I manually hide them on the Windows Explorer context menu. I don't know, maybe it's a Windows 10 bug? What Windows version are you using?
– Havenard
Nov 15 '18 at 0:09
1
1
@Havenard Oh well, you know MS: backwards compatibility is king.
– Paul Sanders
Nov 15 '18 at 0:20
@Havenard Oh well, you know MS: backwards compatibility is king.
– Paul Sanders
Nov 15 '18 at 0:20
|
show 12 more comments
The following approach which uses the official shell API is a little bit involved, but works even under Windows 10.
Steps:
- Get the
IFolderView2interface of the desktop (supported since Windows Vista). - Call
IFolderView2::SetCurrentFolderFlags()withFWF_NOICONSfor both thedwMaskanddwFlagsparameters.
The effect of the flag is visible immediately. There is no need to restart the computer nor "explorer.exe". The flag also persists after logoff or reboot.
The tricky thing is step 1). Raymond Chen shows C++ code for that in his article "Manipulating the positions of desktop icons", specifically in his FindDesktopFolderView() function.
Here is a full example in form of a console application. It is based on Raymond Chen's code. The program toggles the visibility of the desktop icons each time it is run.
The code has been tested under Windows 10 Version 1803.
"Library" code:
#include <ShlObj.h> // Shell API
#include <atlcomcli.h> // CComPtr & Co.
#include <string>
#include <iostream>
#include <system_error>
// Throw a std::system_error if the HRESULT indicates failure.
template< typename T >
void ThrowIfFailed( HRESULT hr, T&& msg )
{
if( FAILED( hr ) )
throw std::system_error{ hr, std::system_category(), std::forward<T>( msg ) };
}
// RAII wrapper to initialize/uninitialize COM
struct CComInit
{
CComInit() { ThrowIfFailed( ::CoInitialize( nullptr ), "CoInitialize failed" ); }
~CComInit() { ::CoUninitialize(); }
CComInit( CComInit const& ) = delete;
CComInit& operator=( CComInit const& ) = delete;
};
// Query an interface from the desktop shell view.
void FindDesktopFolderView( REFIID riid, void **ppv, std::string const& interfaceName )
{
CComPtr<IShellWindows> spShellWindows;
ThrowIfFailed(
spShellWindows.CoCreateInstance( CLSID_ShellWindows ),
"Failed to create IShellWindows instance" );
CComVariant vtLoc( CSIDL_DESKTOP );
CComVariant vtEmpty;
long lhwnd;
CComPtr<IDispatch> spdisp;
ThrowIfFailed(
spShellWindows->FindWindowSW(
&vtLoc, &vtEmpty, SWC_DESKTOP, &lhwnd, SWFO_NEEDDISPATCH, &spdisp ),
"Failed to find desktop window" );
CComQIPtr<IServiceProvider> spProv( spdisp );
if( ! spProv )
ThrowIfFailed( E_NOINTERFACE, "Failed to get IServiceProvider interface for desktop" );
CComPtr<IShellBrowser> spBrowser;
ThrowIfFailed(
spProv->QueryService( SID_STopLevelBrowser, IID_PPV_ARGS( &spBrowser ) ),
"Failed to get IShellBrowser for desktop" );
CComPtr<IShellView> spView;
ThrowIfFailed(
spBrowser->QueryActiveShellView( &spView ),
"Failed to query IShellView for desktop" );
ThrowIfFailed(
spView->QueryInterface( riid, ppv ),
"Could not query desktop IShellView for interface " + interfaceName );
}
Example to toggle desktop icons using the above code:
void ToggleDesktopIcons()
{
CComPtr<IFolderView2> spView;
FindDesktopFolderView( IID_PPV_ARGS(&spView), "IFolderView2" );
DWORD flags = 0;
ThrowIfFailed(
spView->GetCurrentFolderFlags( &flags ),
"GetCurrentFolderFlags failed" );
ThrowIfFailed(
spView->SetCurrentFolderFlags( FWF_NOICONS, flags ^ FWF_NOICONS ),
"SetCurrentFolderFlags failed" );
}
int wmain(int argc, wchar_t **argv)
{
try
{
CComInit init;
ToggleDesktopIcons();
std::cout << "Desktop icons have been toggled.n";
}
catch( std::system_error const& e )
{
std::cout << "ERROR: " << e.what() << ", error code: " << e.code() << "n";
return 1;
}
return 0;
}
add a comment |
The following approach which uses the official shell API is a little bit involved, but works even under Windows 10.
Steps:
- Get the
IFolderView2interface of the desktop (supported since Windows Vista). - Call
IFolderView2::SetCurrentFolderFlags()withFWF_NOICONSfor both thedwMaskanddwFlagsparameters.
The effect of the flag is visible immediately. There is no need to restart the computer nor "explorer.exe". The flag also persists after logoff or reboot.
The tricky thing is step 1). Raymond Chen shows C++ code for that in his article "Manipulating the positions of desktop icons", specifically in his FindDesktopFolderView() function.
Here is a full example in form of a console application. It is based on Raymond Chen's code. The program toggles the visibility of the desktop icons each time it is run.
The code has been tested under Windows 10 Version 1803.
"Library" code:
#include <ShlObj.h> // Shell API
#include <atlcomcli.h> // CComPtr & Co.
#include <string>
#include <iostream>
#include <system_error>
// Throw a std::system_error if the HRESULT indicates failure.
template< typename T >
void ThrowIfFailed( HRESULT hr, T&& msg )
{
if( FAILED( hr ) )
throw std::system_error{ hr, std::system_category(), std::forward<T>( msg ) };
}
// RAII wrapper to initialize/uninitialize COM
struct CComInit
{
CComInit() { ThrowIfFailed( ::CoInitialize( nullptr ), "CoInitialize failed" ); }
~CComInit() { ::CoUninitialize(); }
CComInit( CComInit const& ) = delete;
CComInit& operator=( CComInit const& ) = delete;
};
// Query an interface from the desktop shell view.
void FindDesktopFolderView( REFIID riid, void **ppv, std::string const& interfaceName )
{
CComPtr<IShellWindows> spShellWindows;
ThrowIfFailed(
spShellWindows.CoCreateInstance( CLSID_ShellWindows ),
"Failed to create IShellWindows instance" );
CComVariant vtLoc( CSIDL_DESKTOP );
CComVariant vtEmpty;
long lhwnd;
CComPtr<IDispatch> spdisp;
ThrowIfFailed(
spShellWindows->FindWindowSW(
&vtLoc, &vtEmpty, SWC_DESKTOP, &lhwnd, SWFO_NEEDDISPATCH, &spdisp ),
"Failed to find desktop window" );
CComQIPtr<IServiceProvider> spProv( spdisp );
if( ! spProv )
ThrowIfFailed( E_NOINTERFACE, "Failed to get IServiceProvider interface for desktop" );
CComPtr<IShellBrowser> spBrowser;
ThrowIfFailed(
spProv->QueryService( SID_STopLevelBrowser, IID_PPV_ARGS( &spBrowser ) ),
"Failed to get IShellBrowser for desktop" );
CComPtr<IShellView> spView;
ThrowIfFailed(
spBrowser->QueryActiveShellView( &spView ),
"Failed to query IShellView for desktop" );
ThrowIfFailed(
spView->QueryInterface( riid, ppv ),
"Could not query desktop IShellView for interface " + interfaceName );
}
Example to toggle desktop icons using the above code:
void ToggleDesktopIcons()
{
CComPtr<IFolderView2> spView;
FindDesktopFolderView( IID_PPV_ARGS(&spView), "IFolderView2" );
DWORD flags = 0;
ThrowIfFailed(
spView->GetCurrentFolderFlags( &flags ),
"GetCurrentFolderFlags failed" );
ThrowIfFailed(
spView->SetCurrentFolderFlags( FWF_NOICONS, flags ^ FWF_NOICONS ),
"SetCurrentFolderFlags failed" );
}
int wmain(int argc, wchar_t **argv)
{
try
{
CComInit init;
ToggleDesktopIcons();
std::cout << "Desktop icons have been toggled.n";
}
catch( std::system_error const& e )
{
std::cout << "ERROR: " << e.what() << ", error code: " << e.code() << "n";
return 1;
}
return 0;
}
add a comment |
The following approach which uses the official shell API is a little bit involved, but works even under Windows 10.
Steps:
- Get the
IFolderView2interface of the desktop (supported since Windows Vista). - Call
IFolderView2::SetCurrentFolderFlags()withFWF_NOICONSfor both thedwMaskanddwFlagsparameters.
The effect of the flag is visible immediately. There is no need to restart the computer nor "explorer.exe". The flag also persists after logoff or reboot.
The tricky thing is step 1). Raymond Chen shows C++ code for that in his article "Manipulating the positions of desktop icons", specifically in his FindDesktopFolderView() function.
Here is a full example in form of a console application. It is based on Raymond Chen's code. The program toggles the visibility of the desktop icons each time it is run.
The code has been tested under Windows 10 Version 1803.
"Library" code:
#include <ShlObj.h> // Shell API
#include <atlcomcli.h> // CComPtr & Co.
#include <string>
#include <iostream>
#include <system_error>
// Throw a std::system_error if the HRESULT indicates failure.
template< typename T >
void ThrowIfFailed( HRESULT hr, T&& msg )
{
if( FAILED( hr ) )
throw std::system_error{ hr, std::system_category(), std::forward<T>( msg ) };
}
// RAII wrapper to initialize/uninitialize COM
struct CComInit
{
CComInit() { ThrowIfFailed( ::CoInitialize( nullptr ), "CoInitialize failed" ); }
~CComInit() { ::CoUninitialize(); }
CComInit( CComInit const& ) = delete;
CComInit& operator=( CComInit const& ) = delete;
};
// Query an interface from the desktop shell view.
void FindDesktopFolderView( REFIID riid, void **ppv, std::string const& interfaceName )
{
CComPtr<IShellWindows> spShellWindows;
ThrowIfFailed(
spShellWindows.CoCreateInstance( CLSID_ShellWindows ),
"Failed to create IShellWindows instance" );
CComVariant vtLoc( CSIDL_DESKTOP );
CComVariant vtEmpty;
long lhwnd;
CComPtr<IDispatch> spdisp;
ThrowIfFailed(
spShellWindows->FindWindowSW(
&vtLoc, &vtEmpty, SWC_DESKTOP, &lhwnd, SWFO_NEEDDISPATCH, &spdisp ),
"Failed to find desktop window" );
CComQIPtr<IServiceProvider> spProv( spdisp );
if( ! spProv )
ThrowIfFailed( E_NOINTERFACE, "Failed to get IServiceProvider interface for desktop" );
CComPtr<IShellBrowser> spBrowser;
ThrowIfFailed(
spProv->QueryService( SID_STopLevelBrowser, IID_PPV_ARGS( &spBrowser ) ),
"Failed to get IShellBrowser for desktop" );
CComPtr<IShellView> spView;
ThrowIfFailed(
spBrowser->QueryActiveShellView( &spView ),
"Failed to query IShellView for desktop" );
ThrowIfFailed(
spView->QueryInterface( riid, ppv ),
"Could not query desktop IShellView for interface " + interfaceName );
}
Example to toggle desktop icons using the above code:
void ToggleDesktopIcons()
{
CComPtr<IFolderView2> spView;
FindDesktopFolderView( IID_PPV_ARGS(&spView), "IFolderView2" );
DWORD flags = 0;
ThrowIfFailed(
spView->GetCurrentFolderFlags( &flags ),
"GetCurrentFolderFlags failed" );
ThrowIfFailed(
spView->SetCurrentFolderFlags( FWF_NOICONS, flags ^ FWF_NOICONS ),
"SetCurrentFolderFlags failed" );
}
int wmain(int argc, wchar_t **argv)
{
try
{
CComInit init;
ToggleDesktopIcons();
std::cout << "Desktop icons have been toggled.n";
}
catch( std::system_error const& e )
{
std::cout << "ERROR: " << e.what() << ", error code: " << e.code() << "n";
return 1;
}
return 0;
}
The following approach which uses the official shell API is a little bit involved, but works even under Windows 10.
Steps:
- Get the
IFolderView2interface of the desktop (supported since Windows Vista). - Call
IFolderView2::SetCurrentFolderFlags()withFWF_NOICONSfor both thedwMaskanddwFlagsparameters.
The effect of the flag is visible immediately. There is no need to restart the computer nor "explorer.exe". The flag also persists after logoff or reboot.
The tricky thing is step 1). Raymond Chen shows C++ code for that in his article "Manipulating the positions of desktop icons", specifically in his FindDesktopFolderView() function.
Here is a full example in form of a console application. It is based on Raymond Chen's code. The program toggles the visibility of the desktop icons each time it is run.
The code has been tested under Windows 10 Version 1803.
"Library" code:
#include <ShlObj.h> // Shell API
#include <atlcomcli.h> // CComPtr & Co.
#include <string>
#include <iostream>
#include <system_error>
// Throw a std::system_error if the HRESULT indicates failure.
template< typename T >
void ThrowIfFailed( HRESULT hr, T&& msg )
{
if( FAILED( hr ) )
throw std::system_error{ hr, std::system_category(), std::forward<T>( msg ) };
}
// RAII wrapper to initialize/uninitialize COM
struct CComInit
{
CComInit() { ThrowIfFailed( ::CoInitialize( nullptr ), "CoInitialize failed" ); }
~CComInit() { ::CoUninitialize(); }
CComInit( CComInit const& ) = delete;
CComInit& operator=( CComInit const& ) = delete;
};
// Query an interface from the desktop shell view.
void FindDesktopFolderView( REFIID riid, void **ppv, std::string const& interfaceName )
{
CComPtr<IShellWindows> spShellWindows;
ThrowIfFailed(
spShellWindows.CoCreateInstance( CLSID_ShellWindows ),
"Failed to create IShellWindows instance" );
CComVariant vtLoc( CSIDL_DESKTOP );
CComVariant vtEmpty;
long lhwnd;
CComPtr<IDispatch> spdisp;
ThrowIfFailed(
spShellWindows->FindWindowSW(
&vtLoc, &vtEmpty, SWC_DESKTOP, &lhwnd, SWFO_NEEDDISPATCH, &spdisp ),
"Failed to find desktop window" );
CComQIPtr<IServiceProvider> spProv( spdisp );
if( ! spProv )
ThrowIfFailed( E_NOINTERFACE, "Failed to get IServiceProvider interface for desktop" );
CComPtr<IShellBrowser> spBrowser;
ThrowIfFailed(
spProv->QueryService( SID_STopLevelBrowser, IID_PPV_ARGS( &spBrowser ) ),
"Failed to get IShellBrowser for desktop" );
CComPtr<IShellView> spView;
ThrowIfFailed(
spBrowser->QueryActiveShellView( &spView ),
"Failed to query IShellView for desktop" );
ThrowIfFailed(
spView->QueryInterface( riid, ppv ),
"Could not query desktop IShellView for interface " + interfaceName );
}
Example to toggle desktop icons using the above code:
void ToggleDesktopIcons()
{
CComPtr<IFolderView2> spView;
FindDesktopFolderView( IID_PPV_ARGS(&spView), "IFolderView2" );
DWORD flags = 0;
ThrowIfFailed(
spView->GetCurrentFolderFlags( &flags ),
"GetCurrentFolderFlags failed" );
ThrowIfFailed(
spView->SetCurrentFolderFlags( FWF_NOICONS, flags ^ FWF_NOICONS ),
"SetCurrentFolderFlags failed" );
}
int wmain(int argc, wchar_t **argv)
{
try
{
CComInit init;
ToggleDesktopIcons();
std::cout << "Desktop icons have been toggled.n";
}
catch( std::system_error const& e )
{
std::cout << "ERROR: " << e.what() << ", error code: " << e.code() << "n";
return 1;
}
return 0;
}
answered Nov 17 '18 at 1:09
zett42zett42
7,4393741
7,4393741
add a comment |
add a comment |
The following seems to work (adapted from https://stackoverflow.com/a/6403014/5743288):
#include <windows.h>
int main ()
{
HWND hProgman = FindWindowW (L"Progman", L"Program Manager");
HWND hChild = GetWindow (hProgman, GW_CHILD);
ShowWindow (hChild, SW_HIDE);
Sleep (2000);
ShowWindow (hChild, SW_SHOW);
}
Please note: this approach is not supported by Microsoft and disables right-clicking on ths desktop.
@peter Thanks! Yeah I found that link, but I couldn't figure out how to make it work in C++. Can you possibly explain how this works? I don't understand it.
– Adam
Nov 15 '18 at 0:12
@Adam Yeah that's precisely the method I was talking about, I was just unsure if it worked across different versions of Windows. Geez I remember doing this on Windows 98...
– Havenard
Nov 15 '18 at 0:13
@adam As the post I linked to explains, it hides the parent window that hosts all the desktop icons. Hopefully, MS won't break this.
– Paul Sanders
Nov 15 '18 at 0:18
1
The Shell has a public API. Don't go about handing out solutions based on implementation details, without clearly marking them as unsupported.
– IInspectable
Nov 15 '18 at 9:22
1
May also want to note, that this loses the ability to right-click on the desktop.
– IInspectable
Nov 15 '18 at 20:58
|
show 4 more comments
The following seems to work (adapted from https://stackoverflow.com/a/6403014/5743288):
#include <windows.h>
int main ()
{
HWND hProgman = FindWindowW (L"Progman", L"Program Manager");
HWND hChild = GetWindow (hProgman, GW_CHILD);
ShowWindow (hChild, SW_HIDE);
Sleep (2000);
ShowWindow (hChild, SW_SHOW);
}
Please note: this approach is not supported by Microsoft and disables right-clicking on ths desktop.
@peter Thanks! Yeah I found that link, but I couldn't figure out how to make it work in C++. Can you possibly explain how this works? I don't understand it.
– Adam
Nov 15 '18 at 0:12
@Adam Yeah that's precisely the method I was talking about, I was just unsure if it worked across different versions of Windows. Geez I remember doing this on Windows 98...
– Havenard
Nov 15 '18 at 0:13
@adam As the post I linked to explains, it hides the parent window that hosts all the desktop icons. Hopefully, MS won't break this.
– Paul Sanders
Nov 15 '18 at 0:18
1
The Shell has a public API. Don't go about handing out solutions based on implementation details, without clearly marking them as unsupported.
– IInspectable
Nov 15 '18 at 9:22
1
May also want to note, that this loses the ability to right-click on the desktop.
– IInspectable
Nov 15 '18 at 20:58
|
show 4 more comments
The following seems to work (adapted from https://stackoverflow.com/a/6403014/5743288):
#include <windows.h>
int main ()
{
HWND hProgman = FindWindowW (L"Progman", L"Program Manager");
HWND hChild = GetWindow (hProgman, GW_CHILD);
ShowWindow (hChild, SW_HIDE);
Sleep (2000);
ShowWindow (hChild, SW_SHOW);
}
Please note: this approach is not supported by Microsoft and disables right-clicking on ths desktop.
The following seems to work (adapted from https://stackoverflow.com/a/6403014/5743288):
#include <windows.h>
int main ()
{
HWND hProgman = FindWindowW (L"Progman", L"Program Manager");
HWND hChild = GetWindow (hProgman, GW_CHILD);
ShowWindow (hChild, SW_HIDE);
Sleep (2000);
ShowWindow (hChild, SW_SHOW);
}
Please note: this approach is not supported by Microsoft and disables right-clicking on ths desktop.
edited Nov 15 '18 at 21:11
answered Nov 15 '18 at 0:02
Paul SandersPaul Sanders
5,3262621
5,3262621
@peter Thanks! Yeah I found that link, but I couldn't figure out how to make it work in C++. Can you possibly explain how this works? I don't understand it.
– Adam
Nov 15 '18 at 0:12
@Adam Yeah that's precisely the method I was talking about, I was just unsure if it worked across different versions of Windows. Geez I remember doing this on Windows 98...
– Havenard
Nov 15 '18 at 0:13
@adam As the post I linked to explains, it hides the parent window that hosts all the desktop icons. Hopefully, MS won't break this.
– Paul Sanders
Nov 15 '18 at 0:18
1
The Shell has a public API. Don't go about handing out solutions based on implementation details, without clearly marking them as unsupported.
– IInspectable
Nov 15 '18 at 9:22
1
May also want to note, that this loses the ability to right-click on the desktop.
– IInspectable
Nov 15 '18 at 20:58
|
show 4 more comments
@peter Thanks! Yeah I found that link, but I couldn't figure out how to make it work in C++. Can you possibly explain how this works? I don't understand it.
– Adam
Nov 15 '18 at 0:12
@Adam Yeah that's precisely the method I was talking about, I was just unsure if it worked across different versions of Windows. Geez I remember doing this on Windows 98...
– Havenard
Nov 15 '18 at 0:13
@adam As the post I linked to explains, it hides the parent window that hosts all the desktop icons. Hopefully, MS won't break this.
– Paul Sanders
Nov 15 '18 at 0:18
1
The Shell has a public API. Don't go about handing out solutions based on implementation details, without clearly marking them as unsupported.
– IInspectable
Nov 15 '18 at 9:22
1
May also want to note, that this loses the ability to right-click on the desktop.
– IInspectable
Nov 15 '18 at 20:58
@peter Thanks! Yeah I found that link, but I couldn't figure out how to make it work in C++. Can you possibly explain how this works? I don't understand it.
– Adam
Nov 15 '18 at 0:12
@peter Thanks! Yeah I found that link, but I couldn't figure out how to make it work in C++. Can you possibly explain how this works? I don't understand it.
– Adam
Nov 15 '18 at 0:12
@Adam Yeah that's precisely the method I was talking about, I was just unsure if it worked across different versions of Windows. Geez I remember doing this on Windows 98...
– Havenard
Nov 15 '18 at 0:13
@Adam Yeah that's precisely the method I was talking about, I was just unsure if it worked across different versions of Windows. Geez I remember doing this on Windows 98...
– Havenard
Nov 15 '18 at 0:13
@adam As the post I linked to explains, it hides the parent window that hosts all the desktop icons. Hopefully, MS won't break this.
– Paul Sanders
Nov 15 '18 at 0:18
@adam As the post I linked to explains, it hides the parent window that hosts all the desktop icons. Hopefully, MS won't break this.
– Paul Sanders
Nov 15 '18 at 0:18
1
1
The Shell has a public API. Don't go about handing out solutions based on implementation details, without clearly marking them as unsupported.
– IInspectable
Nov 15 '18 at 9:22
The Shell has a public API. Don't go about handing out solutions based on implementation details, without clearly marking them as unsupported.
– IInspectable
Nov 15 '18 at 9:22
1
1
May also want to note, that this loses the ability to right-click on the desktop.
– IInspectable
Nov 15 '18 at 20:58
May also want to note, that this loses the ability to right-click on the desktop.
– IInspectable
Nov 15 '18 at 20:58
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310374%2fhow-to-hide-desktop-icons-with-windows-api-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
MS sure doesn't maintain its winapi C++ documentation like it used to.
– Christopher Pisz
Nov 14 '18 at 23:38
@Christopher Was it good before?
– Adam
Nov 14 '18 at 23:43
It was 'better' from 1998-2003. At least the links worked.
– Christopher Pisz
Nov 14 '18 at 23:49
MSDN have been migrating its documentation from
msdn.microsoft.comto a new format ondocs.microsoft.comfor some time now, and a lot of things are falling through the cracks (broken links, incomplete text, etc). It is horrible. IMHO, there was nothing wrong with the old documentation, they should have just left it alone.– Remy Lebeau
Nov 15 '18 at 1:58
@rem: While I agree that the current state of the documentation is a pretty significant regression, there is actually a reason for moving it. Once complete, we will finally have a way to submit and publicly track documentation defects.
– IInspectable
Nov 15 '18 at 9:27