利用VB完全控制你的桌面圖示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 hWindowEnd SubSub 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
SubPrivate 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下運行通過。