Tuesday, May 29, 2012

BCM_SETIMAGELIST CommandButton Standar Tampil Indah Menawan

CommandButton Standar dengan Icon 32-bit. Melanjutkan project mengenai tampilan yang telah saya tulis, sekarang kita akan bereksperimen dengan konstanta API BCM_SETIMAGELIST (konstanta yang diperkenalkan Microsoft sekitar tahun 2004). Apakah kegunaan dari BCM_SETIMAGELIST ini? kegunaannya ialah untuk meng-assign serangkaian icon (tepatnya 5 atau 6 icon) ke dalam CommandButton. Adapun kelima icon tersebut secara berurutan: NORMAL, HOT, PRESSED, DISABLED, DEFAULTED. Icon-icon tersebut harus diurutkan seperti demikian, agar menghasilkan effect yang baik. Perhatikan gambar di bawah:

Karena icon yang digunakan disini memiliki color depth 32 bit, maka penggunaan ImageList standar yang terdapat pada file MSCOMCTL.OCX akan menuai masalah, oleh karenanya kita ganti dengan ImageList yang dibuat oleh vbaccelelator. Alternatif lainnya adalah membuat sendiri ImageList melalui Fungsi API.

Option Explicit 

Public Declare Sub
InitCommonControls Lib "comctl32" ) 'For XP style
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function
SetWindowLong Lib "user32" Alias "SetWindowLongA" ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function
SendMessage Lib "user32" Alias "SendMessageA" ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const
GWL_STYLE As Long = -16&
Private Const BM_SETIMAGE As Long = &HF7&
Private Const BCM_SETIMAGELIST = &H1602&

Private Type
RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Type
BUTTON_IMAGELIST
hIml As Long
rc As RECT
uAlign As Long
End Type

Public Sub
SetButtonXPIcon(btn As CommandButton, il As vbalImageList, Optional align As Long = 4, _
Optional leftMargin As Long, Optional topMargin As Long, _
Optional rightMargin As Long, Optional bottomMargin As Long)

Dim
bi As BUTTON_IMAGELIST
Dim sPic As StdPicture
Dim hicon As Long

With
bi
.uAlign = align
.rc.Left = leftMargin
.rc.Top = topMargin
.rc.Right = rightMargin
.rc.Bottom = bottomMargin
.hIml = il.hIml
End With

SendMessage btn.hwnd, BCM_SETIMAGELIST, 0, bi

End Sub