技術(shù) 點(diǎn)
- 技術(shù)
- 點(diǎn)
- V幣
- 點(diǎn)
- 積分
- 12861

|
本帖最后由 t小寶 于 2013-7-16 17:04 編輯
ImageList非Access自帶,是Activex控件,用于保存多個(gè)圖像,為TreeView、ListView等兄弟控件提供圖像引用。
有時(shí)候我們添加到ImageList控件的原始圖片找不到了,但I(xiàn)mageList中還有,怎么把它們導(dǎo)出來另作它用呢?
王站已經(jīng)開了一個(gè)好頭,在這里:http://m.mzhfr.cn/access/20130715/8063.html
ImageList控件允許兩種圖像格式:圖標(biāo)和位圖。用常規(guī)的SavePicture方法可以正常導(dǎo)出位圖,但導(dǎo)出圖標(biāo)會(huì)失真(因?yàn)樯钭兂?位)。
于是只能自己動(dòng)手豐衣足食,寫了一個(gè)模塊,實(shí)現(xiàn)正常導(dǎo)出圖標(biāo),還能夠自動(dòng)區(qū)分圖標(biāo)和位圖,分別導(dǎo)出。
要注意的是,從ImageList控件中獲取的圖標(biāo)丟失了原始色深(位深)信息,所以在代碼中設(shè)了一個(gè)參數(shù),可以手動(dòng)指定導(dǎo)出圖標(biāo)的色深,一般指定為24位(真彩色)即可。
模塊中的代碼:
- Private Type icondirentry
- bwidth As Byte
- bheight As Byte
- bcolorcount As Byte
- breserved As Byte
- wplanes As Integer
- wbitcount As Integer
- dwbytesinres As Long
- dwimageoffset As Long
- End Type
- Private Type icondir
- idreserved As Integer
- idtype As Integer
- idcount As Integer
- identries() As icondirentry
- End Type
- Private Type bitmap
- bmType As Long
- bmWidth As Long
- bmHeight As Long
- bmWidthBytes As Long
- bmPlanes As Integer
- bmBitsPixel As Integer
- bmBits As Long
- End Type
- Private Type BITMAPINFOHEADER
- biSize As Long
- biWidth As Long
- biHeight As Long
- biPlanes As Integer
- biBitCount As Integer
- biCompression As Long
- biSizeImage As Long
- biXPelsPerMeter As Long
- biYPelsPerMeter As Long
- biClrUsed As Long
- biClrImportant As Long
- End Type
- Private Type RGBQUAD
- b As Byte
- G As Byte
- r As Byte
- a As Byte
- End Type
- Private Type BITMAPINFO
- bmiHeader As BITMAPINFOHEADER
- bmiColors(255) As RGBQUAD
- End Type
- Private Type ICONINFO
- fIcon As Long
- xHotspot As Long
- yHotspot As Long
- hbmMask As Long
- hBMColor As Long
- End Type
- Private Declare Function GetIconInfo Lib "user32" (ByVal hIcon As Long, piconinfo As ICONINFO) As Long
- Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
- Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
- Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDc As Long) As Long
- Private Declare Function DeleteDC Lib "gdi32" (ByVal hDc As Long) As Long
- Private Const DIB_RGB_COLORS = 0
- Private Const BI_RGB = 0&
- '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
- ' 函數(shù)名稱: IcoToFile
- ' 功能描述: 從圖標(biāo)句柄創(chuàng)建圖標(biāo)文件
- ' 輸入?yún)?shù): hIcon .......... 必選,圖標(biāo)句柄
- ' sFileName ...... 必選,輸出文件名
- ' iBitsPixel ..... 可選,圖標(biāo)的色深,2、4、8、16、24、32等值。如果不指定,自動(dòng)獲取色深
- ' 返回參數(shù): 成功返回 True
- ' 使用示例: IcoToFile hIcon, "C:\MyIcon.ico", 24 '從圖標(biāo)句柄hIcon創(chuàng)建圖標(biāo)文件,指定色深為24位,輸出到C:\MyIcon.ico
- ' 作 者: t小寶 (QQ:377922812)
- ' 創(chuàng)建日期: 20013-07-16
- '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
- Public Function IcoToFile(ByVal hIcon As Long, ByVal sFileName As String, Optional iBitsPixel As Integer) As Boolean
- On Error GoTo Err_Handler
- Dim tIconInfo As ICONINFO
- Dim hBMColor As Long
- Dim hbmMask As Long
- Dim hMemDC As Long
- Dim bm As bitmap
- Dim tBitInfoAND As BITMAPINFO
- Dim tBitInfoXOR As BITMAPINFO
- Dim lBmiHeaderLen As Long
- Dim lColorsLen As Long
- Dim bytDataAND() As Byte
- Dim bytDataXOR() As Byte
- Dim tIconDir As icondir ' 圖標(biāo)目錄結(jié)構(gòu)
- Dim iFileNum As Integer
-
- Dim bytAnd() As Byte
- Dim i As Long, j As Long, k As Long
- Dim bBlank As Boolean
- Dim byt0 As Byte, byt1 As Byte, byt2 As Byte, byt3 As Byte ' 檢查所有像素是否相同
-
- If Len(sFileName) = 0 Then Exit Function
-
- ' 從圖標(biāo)句柄獲取圖標(biāo)XOR位圖和AND位圖
- GetIconInfo hIcon, tIconInfo
- hBMColor = tIconInfo.hBMColor
- hbmMask = tIconInfo.hbmMask
- ' 獲取XOR位圖數(shù)據(jù)
- lBmiHeaderLen = Len(tBitInfoXOR.bmiHeader) '40
- GetObject hBMColor, Len(bm), bm
- If iBitsPixel <> 1 And iBitsPixel <> 4 And iBitsPixel <> 8 And iBitsPixel <> 16 _
- And iBitsPixel <> 24 And iBitsPixel <> 32 Then iBitsPixel = bm.bmBitsPixel
- If bm.bmWidth > 255 Or bm.bmHeight > 255 Then Exit Function ' 圖標(biāo)尺寸不能大于256*256
- With tBitInfoXOR.bmiHeader
- .biWidth = bm.bmWidth
- .biHeight = bm.bmHeight
- .biBitCount = iBitsPixel
- .biCompression = BI_RGB
- .biPlanes = 1
- .biSize = lBmiHeaderLen
- If .biBitCount = 8 Then
- lColorsLen = 256 * 4
- ElseIf .biBitCount = 4 Then
- lColorsLen = 16 * 4
- ElseIf .biBitCount = 1 Then
- lColorsLen = 2 * 4
- End If
- ReDim bytDataXOR(((.biWidth * .biBitCount / 8 + 3) \ 4) * 4 * .biHeight - 1) As Byte ' 設(shè)置數(shù)組大小與位圖數(shù)據(jù)一致
- End With
-
- hMemDC = CreateCompatibleDC(0) ' 創(chuàng)建內(nèi)存設(shè)備場景
- GetDIBits hMemDC, hBMColor, 0, bm.bmHeight, bytDataXOR(0), tBitInfoXOR, DIB_RGB_COLORS ' 獲得位圖數(shù)據(jù)
-
- ' 獲取AND位圖數(shù)據(jù)
- If hbmMask = 0 Then
- tBitInfoAND.bmiHeader.biSizeImage = ((bm.bmWidth * 1 / 8 + 3) \ 4) * 4 * bm.bmHeight
- ReDim bytDataAND(tBitInfoAND.bmiHeader.biSizeImage - 1) As Byte ' 設(shè)置數(shù)組大小與位圖數(shù)據(jù)一致
- Else
- GetObject hbmMask, Len(bm), bm
- With tBitInfoAND.bmiHeader
- .biWidth = bm.bmWidth
- .biHeight = bm.bmHeight
- .biBitCount = 1
- .biCompression = BI_RGB
- .biPlanes = 1
- .biSize = lBmiHeaderLen
- End With
- ReDim bytDataAND(((bm.bmWidth * 1 / 8 + 3) \ 4) * 4 * bm.bmHeight - 1) As Byte ' 設(shè)置數(shù)組大小與位圖數(shù)據(jù)一致
- GetDIBits hMemDC, hbmMask, 0, bm.bmHeight, bytDataAND(0), tBitInfoAND, DIB_RGB_COLORS ' 獲得位圖數(shù)據(jù)
- End If
- DeleteDC hMemDC
- '處理圖標(biāo)目錄
- ReDim tIconDir.identries(0)
- tIconDir.idreserved = 0 ' 保留字,必須為0
- tIconDir.idtype = 1 ' 1為圖標(biāo),0為光標(biāo)
- tIconDir.idcount = 1 ' 圖像個(gè)數(shù)
- With tIconDir.identries(0)
- .bwidth = tBitInfoXOR.bmiHeader.biWidth
- .bheight = tBitInfoXOR.bmiHeader.biHeight
- .bcolorcount = 0
- .breserved = 0
- .wplanes = 1 ' 不設(shè)也沒有影響
- .wbitcount = tBitInfoXOR.bmiHeader.biBitCount ' 每個(gè)像素的位數(shù),不設(shè)也沒有影響
- .dwbytesinres = lBmiHeaderLen + lColorsLen + _
- tBitInfoXOR.bmiHeader.biSizeImage + tBitInfoAND.bmiHeader.biSizeImage '
- .dwimageoffset = 22 ' 圖像數(shù)據(jù)偏移起點(diǎn),第1個(gè)圖像是22
- End With
-
- 'XOR位圖信息頭兩個(gè)成員須要調(diào)整
- With tBitInfoXOR.bmiHeader
- .biSizeImage = .biSizeImage + tBitInfoAND.bmiHeader.biSizeImage
- .biHeight = .biHeight * 2
- End With
- '創(chuàng)建文件 寫入圖標(biāo)數(shù)據(jù)
- iFileNum = FreeFile
- Open sFileName For Output As #iFileNum
- Close #iFileNum
- Open sFileName For Binary As #iFileNum
- Put #iFileNum, , tIconDir.idreserved
- Put #iFileNum, , tIconDir.idtype
- Put #iFileNum, , tIconDir.idcount
- Put #iFileNum, , tIconDir.identries(0) ' icondirentry 圖標(biāo)目錄
- Put #iFileNum, , tBitInfoXOR.bmiHeader ' XOR位圖頭
- If lColorsLen > 0 Then Put #iFileNum, , tBitInfoXOR.bmiColors ' XOR位圖顏色表
- Put #iFileNum, , bytDataXOR ' XOR位圖數(shù)據(jù)
- Put #iFileNum, , bytDataAND ' AND位圖數(shù)據(jù)
- Close #iFileNum
- IcoToFile = True
-
- Err_Handler:
- Exit Function
- End Function
復(fù)制代碼 在窗體上添加一個(gè)ImageList控件,插入一些圖標(biāo)和位圖,添加一個(gè)命令按鈕,窗體模塊中添加以下代碼:- Private Sub Command1_Click()
- Dim pic As IPictureDisp
- Dim i As Integer
-
- For i = 1 To Me.ImageList0.ListImages.Count
- '.ListImages(i).ExtractIcon:返回的總是圖標(biāo) '.Overlay(i,i):返回的總是位圖
- Set pic = Me.ImageList0.ListImages(i).Picture
-
- ' 3是圖標(biāo),1是位圖
- If pic.Type = 3 Then
- '這里圖標(biāo)的色深是當(dāng)前屏幕色深,不是原始圖標(biāo)的色深,所以在最后一個(gè)參數(shù)指定色深。用LoadPicture加載的圖片可以取得原始色深。
- IcoToFile pic.handle, "C:\ImageList" & i & ".ico", 24
- Else
- SavePicture pic, "C:\ImageList" & i & ".bmp"
- End If
- Set pic = Nothing
- Next
- MsgBox "已導(dǎo)出到c盤根目錄。"
-
- End Sub
復(fù)制代碼
示例如下:
|
本帖子中包含更多資源
您需要 登錄 才可以下載或查看,沒有帳號?注冊
x
評分
-
查看全部評分
|