Windows中的桌面图标的排列方式是否让你感到厌倦而想按照自己的想法排列图标。是否想改变桌面图标文字的背景而不
使在图标文字下出现一个个难看的色块。这里我要介绍如何通过VB来对桌面的图标进行彻底的改变。
其实在Windows下的桌面以及任务栏等都是窗口对象,我们可以利用Windows API函数FindWindow和FindWindowEx来获得
它们的句柄,然后再调用其它相应的API函数来控制它们。而放置桌面图标的窗口是一个ListView对象,利用SendMessage函数
向该窗口发送相应的消息,就可以对图标进行修改了。下面是具体的程序实现。
首先在VB中建立一个新的工程,在Form1中加入三个CommandButton控件,然后在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函数回复默认的图标文字颜色和背景
Sub RestoreColor()
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
'通过三步查找到放置桌面图表的窗口
hWindow = FindWindow("Progman", "Program Manager")
hWindow = FindWindowEx(hWindow, 0, "SHELLDLL_DefView", "")
hWindow = FindWindowEx(hWindow, 0, "SysListView32", "")
If bTrans Then '透明背景
SendMessage hWindow, LVM_SETTEXTBKCOLOR, 0, &HFFFFFFFF
Else '非透明背景
SendMessage hWindow, LVM_SETTEXTBKCOLOR, 0, clBack
End If
'设置图标文字的颜色
SendMessage hWindow, LVM_SETTEXTCOLOR, 0, clFore
'重新绘制所有的图标
lItemCount = SendMessage(hWindow, LVM_GETITEMCOUNT, 0, 0)
SendMessage hWindow, LVM_REDRAWITEMS, 0, lItemCount - 1
'更新窗口
UpdateWindow hWindow
End Sub
Sub ArrangeDesktopIcon(iWidth As Integer, iHeight As Integer)
Dim hWindow As Long
Dim i1, i2, i, iCount As Integer
Dim po As POINTAPI
'通过三步查找到放置桌面图表的窗口
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
'发送LVM_SETITEMPOSITION消息排列图标
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
'更新窗口
UpdateWindow hWindow
End Sub
Private Sub Command1_Click()
'设置图标文字的颜色为蓝色,背景色为黑色,背景为透明
SetIconText vbBlue, vbBlack, True
End Sub
Private Sub Command2_Click()
RestoreColor
End Sub
Private Sub Command3_Click()
'以100x100像素为单位排列图标
ArrangeDesktopIcon 100, 100
End Sub
Private Sub Form_Load()
Command1.Caption = "设置文字背景"
Command2.Caption = "恢复文字背景"
Command3.Caption = "排列桌面图标"
End Sub
运行程序,点击Command1,可以看到桌面图标的文本景色变成了蓝色,如果你设置了桌面图片,还可以看到文字
的背景变成了透明的而不是在下面有一个难看的色块,点击Command2可以恢复Windows的默认设置,点击Command3可以
使你的桌面图标以横排的方式排列,不过前提是要将桌面图标的自动排列属性设置为False。
以上程序在VB6,Windows98,Windows2000下运行通过。