Click here to Skip to main content
Click here to Skip to main content

Programmatically resize Desktop icons to a small list in Windows Vista and Windows 7

, 20 Jan 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Experience small and compact desktop icons view in Vista and Windows 7.

Introduction

The article shows a simple trick to automatically resize large desktop icons in Vista and Windows 7, and also display them as a list.

Background

Sometimes we need to resize our desktop icons to smaller ones for a cleaner or comfortable look. In Windows Vista and Windows 7, there is a method to do this - set the desktop on focus by clicking anywhere on the desktop area, then press the Control key while scrolling the mouse wheel up or down. You will see that the icons will resize as you scroll. However, this method requires user intervention, and another problem is, it just resizes them but does not merge them together to save more desktop space.

Solution

The desktop area is actually a ListView control, so the main trick is to get the window handle of that control, then send appropriate window messages to apply the changes.

Step one: Search the handle of the ListView control

Use the EnumWindows and FindWindowEx APIs:

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    HWND hChild = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
    if( hChild )
    {
        HWND hDesk = FindWindowEx( hChild, NULL, _T("SysListView32"), NULL );
        if( hDesk )
        {
            *(reinterpret_cast<HWND*>(lParam)) = hDesk;
            return FALSE;
        }
    }
    return TRUE;
}
HWND hWnd = NULL;
EnumWindows( EnumWindowsProc, (LPARAM)&hWnd );

if( hWnd )
{
    return SetDeskIcon( hWnd, size );
}

The code above will first enumerate all top-level windows in the screen using the EnumWindows API. For each window, it will get the handle and pass it to the specified callback. Under the callback, we use the handle to search the child windows or controls.

Using the FindWindowEx API, we search the window that we are looking for by window name or class name. But before we can search, we need to know the names to search.

There is a tool installed with Visual Studio called Spy++ which has the capability to display information of all the windows currently running. Using its "Search Window" feature, we can easily access the properties of the window or control we want by dragging its Finder Tool on to it and then pressing OK. After doing that, you will see that the ListView control is under a child window with the class name, SHELLDLL_DefView, and the ListView control's class name will be SysListView32.

Step 2: Do the resizing

As mentioned above, one method to resize the desktop icons is using the Control key and mouse wheel combination. We can simulate this automatically by sending a window message to the list view. By using the window handle we retrieve from the search, we send the following messages:

Resize the icons to be smaller. We call this repeatedly until we get the desired size:

SendMessage( hWnd, WM_MOUSEWHEEL, MAKEWPARAM(MK_CONTROL, -WHEEL_DELTA), MAKELPARAM(0, 0) );

Resize the icons to be bigger. We call this repeatedly until we get the desired size:

SendMessage( hWnd, WM_MOUSEWHEEL, MAKEWPARAM(MK_CONTROL, WHEEL_DELTA), MAKELPARAM(0, 0) );

Step 3: Set the display style to Small Icon view

By applying this style, the icons will be merged together and will be displayed as a list. With this step, we will gain more desktop space. Again, by using the same handle:

STYLESTRUCT Styles;
Styles.styleOld = GetWindowLong( hWnd, GWL_STYLE );
Styles.styleNew = Styles.styleOld & (~LVS_TYPEMASK); 
Styles.styleOld = Styles.styleNew;
if ( size == DeskIconSmall ) 
{
    Styles.styleNew |= LVS_SMALLICON;
    Styles.styleOld |= LVS_ICON;  
}
else
{
    Styles.styleNew |= LVS_ICON;
    Styles.styleOld |= LVS_SMALLICON;
}

// Set the new style
SetWindowLong( hWnd, GWL_STYLE, Styles.styleNew );
SendMessage( hWnd, WM_STYLECHANGED, GWL_STYLE, (LONG)(&Styles) );
SendMessage( hWnd, WM_KILLFOCUS, 0, 0 );

// Refresh control
UpdateWindow( hWnd );
ShowWindow( hWnd, SW_HIDE );
ShowWindow( hWnd, SW_SHOWNORMAL );

That's it! Please have a look at the sample source code provided. The sample is a console application that accepts these parameters: /SMALL - to set the icon sizes to smallest and display as a list, or /BIG - to return them to normal sizes.

Basically, the above code will also work in Windows XP. But, we do not need to do the Step 2 anymore since it is for Vista and Windows 7 only.

You may also want to try the sample application (attached as DeskIconSetup.zip) that I created that does the same functionality. There is also an installer, and it provides an option to make your desktop icons smaller automatically on startup. Also, it will create Start Menu shortcuts for you to be able to return them to normal sizes or vice versa.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rei Resurreccion
Software Developer (Senior)
Australia Australia
A developer for Security/Antivirus/AntiSpyware solutions.

Comments and Discussions

 
QuestionExcellent PinmemberMember 1005829916-May-13 17:39 
GeneralThanks! Works in Win8! Pinmembergeort4514-Dec-12 7:38 
QuestionThank you! PinmemberMember 802991923-Jun-11 3:22 
GeneralThanks for some very handy code... PinmemberJamie Morrison10-Aug-10 4:52 
GeneralColumn width PinmemberJake Wharton18-Mar-10 12:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 20 Jan 2010
Article Copyright 2010 by Rei Resurreccion
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid