Click here to Skip to main content
15,879,184 members
Articles / Desktop Programming / Win32

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

Rate me:
Please Sign up or sign in to vote.
4.56/5 (8 votes)
20 Jan 2010CPOL3 min read 55.8K   2K   26   7
Experience small and compact desktop icons view in Vista and Windows 7.

Image 1

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:

C++
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:

C++
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:

C++
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:

C++
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)


Written By
Software Developer (Senior)
Australia Australia
A developer for Security/Antivirus/AntiSpyware solutions.

Comments and Discussions

 
QuestionVery useful in Windows 10 desktop since they broke the CTRL-0 zoom reset. Pin
studleylee1-May-18 11:16
studleylee1-May-18 11:16 
QuestionExcellent Pin
Member 1005829916-May-13 16:39
Member 1005829916-May-13 16:39 
GeneralThanks! Works in Win8! Pin
geort4514-Dec-12 6:38
geort4514-Dec-12 6:38 
GeneralRe: Thanks! Works in Win8! Pin
krystian3w29-Aug-19 9:58
krystian3w29-Aug-19 9:58 
QuestionThank you! Pin
Member 802991923-Jun-11 2:22
Member 802991923-Jun-11 2:22 
GeneralThanks for some very handy code... Pin
Jamie Morrison10-Aug-10 3:52
Jamie Morrison10-Aug-10 3:52 
GeneralColumn width Pin
Jake Wharton18-Mar-10 11:01
Jake Wharton18-Mar-10 11:01 

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

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