Use VB to fully control your desktop icons. Are you tired of the arrangement of desktop icons in Windows and want to arrange the icons according to your own ideas? Do you want to change the background of the desktop icon text without causing ugly color blocks to appear under the icon text? Here I want to introduce how to completely change the desktop icons through VB.
In fact, the desktop and taskbar under Windows are all window objects. We can use the Windows API functions FindWindow and FindWindowEx to obtain their handles, and then call other corresponding API functions to control them. The window where the desktop icon is placed is a ListView object. Use the SendMessage function to send the corresponding message to the window, and the icon can be modified. The following is the specific program implementation.
First, create a new project in VB, add three CommandButton controls to Form1, and then add the following code to the code window of Form1:
Option Explicit
Private Type POINTAPI x As Long, y As Long
End Type
Private Declare Function FindWindow Lib user32 Alias FindWindowA _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib user32 Alias FindWindowExA _ (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _ ByVal lpsz2 As String) As Long
Private Declare Function UpdateWindow Lib user32 (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib user32 Alias SendMessageA (ByVal _ hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) _ As Long
Private Declare Function SendMessageP Lib user32 Alias SendMessageA (ByVal _ hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) _ As Long
Private Declare Function GetSysColor Lib user32 (ByVal nIndex As Long) As Long
Private Declare Function SetSysColors Lib user32 (ByVal nChanges As Long, _ lpSysColor As Long, lpColorValues As Long) As Long
Const LVM_FIRST = &H1000
Const LVM_GETITEMCOUNT = LVM_FIRST + 4
Const LVM_SETTEXTCOLOR = LVM_FIRST + 36
Const LVM_REDRAWITEMS = LVM_FIRST + 21
Const LVM_SETTEXTBKCOLOR = LVM_FIRST + 38
Const LVM_SETITEMPOSITION = LVM_FIRST + 15
Const COLOR_DESKTOP = 1'RestoreColor function restores the default icon text color and background
SubRestoreColor()
Dim lColor As Long
lColor = GetSysColor(COLOR_DESKTOP)
SetSysColors 1, COLOR_DESKTOP, lColor
End Sub
Sub SetIconText(clFore, clBack As Long, bTrans As Boolean)
Dim hWindow As Long
Dim lItemCount As Long 'Find the window where the desktop chart is placed in three steps
hWindow = FindWindow(Progman, Program Manager)
hWindow = FindWindowEx(hWindow, 0, SHELLDLL_DefView, )
hWindow = FindWindowEx(hWindow, 0, SysListView32, )
If bTrans Then 'Transparent background
SendMessage hWindow, LVM_SETTEXTBKCOLOR, 0, &HFFFFFFFF
Else 'Non-transparent background
SendMessage hWindow, LVM_SETTEXTBKCOLOR, 0, clBack
End If
'Set the color of the icon text
SendMessage hWindow, LVM_SETTEXTCOLOR, 0, clFore 'Redraw all icons
lItemCount = SendMessage(hWindow, LVM_GETITEMCOUNT, 0, 0)
SendMessage hWindow, LVM_REDRAWITEMS, 0, lItemCount - 1
'Update window
UpdateWindow hWindowEnd SubSub ArrangeDesktopIcon(iWidth As Integer, iHeight As Integer)
Dim hWindow As Long Dim i1, i2, i, iCount As Integer
Dim po As POINTAPI
'Find the window where the desktop chart is placed in three steps
hWindow = FindWindow(Progman, Program Manager)
hWindow = FindWindowEx(hWindow, 0, SHELLDLL_DefView, )
hWindow = FindWindowEx(hWindow, 0, SysListView32, )
i1 = 20: i2 = 20
iCount = SendMessage(hWindow, LVM_GETITEMCOUNT, 0, 0)
For i = 0 To iCount - 1
po.x = i1: po.y = i2
'Send LVM_SETITEMPOSITION message to arrange icons
Call SendMessage(hWindow, LVM_SETITEMPOSITION, i, i2 * 65536 + i1)
i1 = i1 + iWidth
If i1 > ((Screen.Width / 15) - 32) Then
i1 = 20
i2 = i2 + iHeight
End If
Next i
SendMessage hWindow, LVM_REDRAWITEMS, 0, iCount - 1
'Update window
UpdateWindow hWindow
End Sub
Private Sub Command1_Click()
'Set the icon text color to blue, the background color to black, and the background to transparent
SetIconText vbBlue, vbBlack, True
End
SubPrivate Sub Command2_Click()
RestoreColor
End Sub
Private Sub Command3_Click()
'Arrange icons in units of 100x100 pixels
ArrangeDesktopIcon 100, 100
End Sub
Private Sub Form_Load()
Command1.Caption = Set text background
Command2.Caption = Restore text background
Command3.Caption = Arrange desktop icons
End Sub
Run the program and click Command1. You can see that the text background of the desktop icon turns blue. If you set a desktop picture, you can also see that the background of the text becomes transparent instead of an ugly color block below. Click Command2 to restore the default settings of Windows. Click Command3 to arrange your desktop icons horizontally, but only if the automatic arrangement property of desktop icons is set to False. The above program runs successfully under VB6, Windows98, and Windows2000.