網上討論編程實現XP風ge已經很久了,但對于VB編程實現XP風ge,卻終沒有一個完美的解決方案。筆者通過N個日夜的刻苦鉆研終于揭開其中奧秘。下面分為三個方面與大家共享之。
點擊下載源代碼文件
一.用manifest文件實現XP風ge
正常情況下,在Windows XP系統中,用VB6開發的應用程序只有窗口標題條具備XP風ge,窗體上的按鈕、文本框等控件仍然顯示Windows傳統風ge。如圖1所示:
通過查閱MSDN里的Visual Style章節知道,Windows XP通過Comctl32.dll(版本6)來加載具備XP風ge的組件,應用程序則通過一個XML資源文件來通知系統來做這些。XML文件的內容如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
name="XP style manifest"
processorArchitecture="x86"
version="1.0.0.0"
type="win32"/>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="x86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
假設你最終編譯的程序名是abc.exe,工作目錄是d:/vbxp。復制上述XML內容并保存為文本文件。然后將該文件改名為abc.exe.manifest(注意.txt擴展名要去掉)。在VB程序中,我們要在所有窗體加載之前調用InitCommonControlsEx函數從comctl32.dll(版本6)中對組件類進行初始化。API函數InitCommonControlsEx及相關常數、數據類型的聲明如下:
Private Declare Function InitCommonControlsEx Lib "comctl32.dll" _
(iccex As tagInitCommonControlsEx) As Boolean
Private Type tagInitCommonControlsEx
lngSize As Long
lngICC As Long
End Type
Private Const ICC_USEREX_CLASSES = &H200
這里我們編寫一個函數封裝初始化操作:
Public Function InitCommonControlsVB() As Boolean
On Error Resume Next
Dim iccex As tagInitCommonControlsEx
With iccex
.lngSize = LenB(iccex)
.lngICC = ICC_USEREX_CLASSES
End With
InitCommonControlsEx iccex
InitCommonControlsVB = (Err.Number = 0)
On Error Goto 0
End Function
注意初始化動作必須在所有窗體加載前完成,所以要把相關語句放到Sub Main()中,并設置工程從Sub Main()啟動。代碼如下:
Sub Main()
InitCommonControlsVB
Form1.Show
End Sub
至此,你編譯后的abc.exe將具備XP風ge,如圖2所示:
新聞熱點
疑難解答