List Box Warna Module

Kamis, 20 Januari 2011
List Box warna tutorial-vb.com

DESAIN MODULE LIST BOX

Tutorial VB berikut adalah postingan  lanjutan dari List box warna. Berikut ini adalah isi tentang module dan penjelasannya:


Option Explicit

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

Public Type DRAWITEMSTRUCT
CtlType As Long
CtlID As Long
itemID As Long
itemAction As Long
itemState As Long
hwndItem As Long
hdc As Long
rcItem As RECT
ItemData As Long
End Type

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Public Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Public Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Public Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Public Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
Public Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long

Public Const COLOR_HIGHLIGHT = 13
Public Const COLOR_HIGHLIGHTTEXT = 14
Public Const COLOR_WINDOW = 5
Public Const COLOR_WINDOWTEXT = 8
Public Const LB_GETTEXT = &H189
Public Const WM_DRAWITEM = &H2B
Public Const GWL_WNDPROC = (-4)
Public Const ODS_FOCUS = &H10
Public Const ODT_LISTBOX = 2

Public lPrevWndProc As Long

Public Function SubClassedList(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim tItem As DRAWITEMSTRUCT
Dim sBuff As String * 255
Dim sItem As String
Dim lBack As Long

If Msg = WM_DRAWITEM Then
'Gambar ulang listboxnya
'Fungsi ini hanya akan melewati item Draw, jadi kita memerlukannya
Call CopyMemory(tItem, ByVal lParam, Len(tItem))
'Untuk memastikan bahwa kita sedang mengolah list box tersebut
If tItem.CtlType = ODT_LISTBOX Then
'mendapatkan item pada list
Call SendMessage(tItem.hwndItem, LB_GETTEXT, tItem.itemID, ByVal sBuff)
sItem = Left(sBuff, InStr(sBuff, Chr(0)) - 1)
If (tItem.itemState And ODS_FOCUS) Then
'Item akan tetap fokus,di sini saya menggunakan warna dasar
'contoh warnanya
lBack = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT))
Call FillRect(tItem.hdc, tItem.rcItem, lBack)
Call SetBkColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHT))
Call SetTextColor(tItem.hdc, GetSysColor(COLOR_HIGHLIGHTTEXT))
TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
DrawFocusRect tItem.hdc, tItem.rcItem
Else
'Jika Item tidak fokus, maka hanya akan menggambar backgroundnya

lBack = CreateSolidBrush(tItem.ItemData)
'Warnai area pada list
Call FillRect(tItem.hdc, tItem.rcItem, lBack)
'menset warna text pada listbox
Call SetBkColor(tItem.hdc, tItem.ItemData)
Call SetTextColor(tItem.hdc, IIf(tItem.ItemData = vbBlack, vbWhite, vbBlack))
'Menampilkan text tersebut
TextOut tItem.hdc, tItem.rcItem.Left, tItem.rcItem.Top, ByVal sItem, Len(sItem)
End If
Call DeleteObject(lBack)
'jangan mengganti valuenya.
SubClassedList = 0
Exit Function

End If

End If
SubClassedList = CallWindowProc(lPrevWndProc, hwnd, Msg, wParam, lParam)
End Function



Bagi kalian yang ingin mencobanya, bisa mendownloadnya di

Sample List Box warna



Pada download tersebut saya sengaja mengkonversikan bahasa ke dalam English..jadi kalo mau lebih memahaminya, silahkan simpan page Tutorial VB ini

Movie Category 1