home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / code_lib / objlibr / objlib12 / sample3 / module1.bas < prev    next >
Encoding:
BASIC Source File  |  1994-10-10  |  8.1 KB  |  261 lines

  1. Option Explicit
  2.  
  3. '
  4. 'prevent needless paints
  5. Dim resizing%
  6. Global nl$
  7.  
  8. 'types
  9. Type rect
  10.     left As Integer
  11.     top As Integer
  12.     right As Integer
  13.     bottom As Integer
  14. End Type
  15.  
  16. 'each list needs a caption and bitmap, so declare a simple structure
  17. Type ITEMDATA
  18.     text As String
  19.     pic As Integer
  20. End Type
  21.  
  22. 'variable data for each window - each instance of the list is created
  23. 'by declaring a listdata structure
  24. Type LISTDATA
  25.     cellwidth As Integer        'w,h of each item
  26.     cellheight As Integer
  27.     picx As Integer         'x,y offset of bmp
  28.     picy As Integer
  29.     picwidth As Integer
  30.     picheight As Integer
  31.     textrect As rect        'x,y offset,r,b offset of caption
  32.     bcolor As Long             'window background color
  33.     fcolor As Long             'window text
  34.     hilitebcolor As Long          '
  35.     hilitefcolor As Long      '
  36.     toprow As Integer           'client area's top
  37.     itemcount As Integer        'total items
  38.     active As Integer           'active item
  39.     cols As Integer
  40.     rows As Integer
  41.     visrows As Integer
  42.     Width As Integer
  43.     tx As Integer
  44.     ty As Integer
  45. End Type
  46.  
  47. 'API constants and types====================
  48. Global Const black = &H0
  49. Global Const white = &HFFFFFF
  50. Global Const lgrey = &HC0C0C0
  51. Global Const PATPAINT = &HFB0A09
  52. Global Const PATCOPY = &HF00021
  53. Global Const SRCCOPY = &HCC0020
  54. Global Const GWW_HINSTANCE = (-6)
  55. Global Const WM_USER = &H400
  56. Global Const GWL_STYLE = (-16)
  57. 'draw text
  58. Global Const DT_CALCRECT = &H400
  59. Global Const DT_CENTER = &H1
  60. Global Const DT_NOPREFIX = &H800
  61. Global Const DT_VCENTER = &H4
  62. Global Const DT_WORDBREAK = &H10
  63. Global Const DT_INTERNAL = &H1000
  64. Global Const DT_SINGLELINE = &H20
  65. Global Const DT_LEFT = &H0
  66. Global Const DT_GETRECT = DT_CALCRECT Or DT_NOPREFIX Or DT_CENTER Or DT_WORDBREAK
  67. Global Const DT_ICONCAP = DT_NOPREFIX Or DT_WORDBREAK Or DT_CENTER
  68. Global Const DT_LISTCAP = DT_NOPREFIX Or DT_LEFT  ' Or DT_WORDBREAK Or DT_SINGLELINE
  69. Global Const DT_ICONTITLE = DT_NOPREFIX Or DT_CENTER Or DT_WORDBREAK 'Or DT_VCENTER
  70. Declare Function bitblt% Lib "GDI" (ByVal hDestDC%, ByVal x%, ByVal y%, ByVal nWidth%, ByVal nHeight%, ByVal hSrcDC%, ByVal XSrc%, ByVal YSrc%, ByVal dwRop&)
  71. Declare Function CreateDC Lib "GDI" (ByVal lpDriverName As String, ByVal lpDeviceName As Any, ByVal lpOutput As Any, ByVal lpInitData As Any) As Integer
  72. Declare Function CreateSolidBrush% Lib "GDI" (ByVal crColor&)
  73. Declare Function DeleteDC Lib "GDI" (ByVal hDC As Integer) As Integer
  74. Declare Function DeleteObject% Lib "GDI" (ByVal hObject%)
  75. Declare Function DrawText% Lib "User" (ByVal hDC%, ByVal lpStr$, ByVal nCount%, lpRect As rect, ByVal wFormat%)
  76. Declare Function DrawIcon Lib "USER" (ByVal lpHandle As Integer, ByVal xcoord As Integer, ByVal ycoord As Integer, ByVal hicon As Integer) As Integer
  77. Declare Function ExtractIcon Lib "shell" (ByVal lpHandle As Integer, ByVal lpExe As String, ByVal lpiconindex As Integer) As Integer
  78. Declare Function GetSysColor& Lib "User" (ByVal nIndex%)
  79. Declare Function GetWindowWord Lib "User" (ByVal hWnd As Integer, ByVal nIndex As Integer) As Integer
  80. Declare Function SetTextColor& Lib "GDI" (ByVal hDC%, ByVal crColor&)
  81. Declare Function PatBlt% Lib "GDI" (ByVal hDC%, ByVal x%, ByVal y%, ByVal nWidth%, ByVal nHeight%, ByVal dwRop&)
  82. Declare Function SelectObject% Lib "GDI" (ByVal hDC%, ByVal hObject%)
  83.  
  84. Sub InitializeList (ld As LISTDATA, L As PictureBox)
  85. Dim i%, s$
  86. ld.bcolor = GetSysColor(5)
  87. ld.fcolor = GetSysColor(8)
  88. ld.hilitebcolor = GetSysColor(13)
  89. ld.hilitefcolor = GetSysColor(14)
  90. ld.tx = screen.TwipsPerPixelX
  91. ld.ty = screen.TwipsPerPixelY
  92.  
  93. ld.toprow = 0
  94. ld.active = 1
  95. ld.textrect.right = ld.cellwidth - 2 * ld.textrect.left
  96. ld.textrect.bottom = ld.cellheight - ld.textrect.top
  97. End Sub
  98.  
  99. Sub ItemClick (F As Form, ld As LISTDATA, txt() As ITEMDATA, x!, y!, L As PictureBox)
  100. Dim n%, old%
  101. Dim tr  As rect, hr As rect
  102.  
  103. '===set focus to clicked item=====================
  104. y = y \ ld.cellheight:  'Debug.Print x, y
  105. 'determine relative item #
  106. n = y + 1'Debug.Print n
  107. 'determine absolute item #
  108. n = n + ld.toprow'Debug.Print n
  109. 'set active item
  110. If n <= ld.itemcount Then
  111.     'old is a 1-based index; the draw routine uses a 0-base
  112.     old% = ld.active - 1
  113.     ld.active = n
  114. End If
  115.  
  116. 'erase old hilite
  117. hr.left = ld.picx + ld.picwidth
  118. hr.right = ld.Width
  119. tr.left = ld.picx + ld.picwidth + ld.textrect.left
  120. tr.right = ld.Width - ld.textrect.left
  121. 'valid index?
  122. If old >= 0 And old < ld.itemcount Then
  123.     'is it still visible?
  124.     n = old - ld.toprow
  125.     If n >= 0 And n < ld.visrows Then
  126.     
  127.     'size of text rect:
  128.     tr.top = n * ld.cellheight + ld.textrect.top
  129.     tr.bottom = (n + 1) * ld.cellheight
  130.     '
  131.     'size of hilite rect
  132.     hr.top = n * ld.cellheight
  133.     hr.bottom = tr.bottom + 2
  134.     PaintHilite 0, txt(old + 1).text, tr, hr, ld, L
  135.     End If
  136. End If
  137.  
  138. 'draw new hilite
  139. n = ld.active - 1 - ld.toprow:  'Debug.Print "rel" & n
  140.     'check if its visible:'Debug.Print "total" & ld.visrows * ld.cols
  141.     If n < 0 Or n > ld.visrows - 1 Then Exit Sub
  142.     
  143.     'size of text rect:
  144.     tr.top = n * ld.cellheight + 8
  145.     tr.bottom = (n + 1) * ld.cellheight 'Debug.Print hr.left, hr.top, hr.right, hr.bottom
  146.     'size of hilite rect
  147.     hr.top = tr.top - 8
  148.     hr.bottom = tr.bottom + 2
  149.     PaintHilite -1, txt(ld.active).text, tr, hr, ld, L
  150.  
  151. End Sub
  152.  
  153. Sub PaintHilite (op%, s$, tr As rect, hr As rect, ld As LISTDATA, L As PictureBox)
  154. Dim bkgcolor&, txtcolor&, r%
  155. Dim offset%'offset of icon caption
  156. Dim hbrOld%, hbr%, cOld& 'api stuff
  157. '
  158. 'n = 0 erase hilite; n = -1 paint hilite
  159. If op Then
  160.     bkgcolor& = ld.hilitebcolor
  161.     txtcolor& = ld.hilitefcolor
  162. Else
  163.     bkgcolor& = ld.bcolor
  164.     txtcolor = ld.fcolor
  165. End If
  166.  
  167. 'paint a hilite rectangle:
  168. hbr = CreateSolidBrush(bkgcolor&)
  169. hbrOld = SelectObject(L.hDC, hbr)
  170. r = PatBlt(L.hDC, hr.left, hr.top, hr.right - hr.left, hr.bottom - hr.top, PATCOPY)
  171. L.Line (0, hr.top)-(ld.picwidth + 1, hr.top + ld.cellheight), bkgcolor&, B
  172.  
  173. 'paint hilite text:
  174. cOld = SetTextColor(L.hDC, txtcolor&)
  175. r = DrawText(L.hDC, s, Len(s), tr, DT_LISTCAP)
  176.  
  177. 'cleanup
  178. cOld = SetTextColor(L.hDC, cOld)
  179. hbr = SelectObject(L.hDC, hbrOld)
  180. r = DeleteObject(hbr)
  181. End Sub
  182.  
  183. Sub PaintList (ld As LISTDATA, txt() As ITEMDATA, p As PictureBox, L As PictureBox)
  184. Dim i%, r%
  185. Dim y% 'y pos to draw icon
  186. Dim ypos% 'y pos of item
  187. Dim pstart%, pend% 'indexes of first and last visible icons
  188. Dim hr As rect, tr  As rect 'for drawing text
  189.  
  190. 'calculate which icons to show:
  191. pstart% = ld.toprow + 1': Debug.Print pstart
  192. pend% = pstart% + ld.visrows - 1
  193. If pend% > ld.itemcount Then pend% = ld.itemcount: Debug.Print pend
  194. '
  195. L.Cls
  196. 'draw the icons:
  197. y = -ld.cellheight + 2
  198. For i = pstart% To pend%
  199.     y = y + ld.cellheight'(new row)
  200.     r = bitblt(L.hDC, ld.picx, y + ld.picy, ld.picwidth, ld.picheight, p.hDC, txt(i).pic * ld.picwidth, 0, SRCCOPY)
  201. Next
  202.  
  203. y = -ld.cellheight
  204. tr.left = ld.picx + ld.picwidth + ld.textrect.left
  205. tr.right = ld.Width' - tr.left
  206. For i = pstart% To pend%
  207.     y = y + ld.cellheight'(new row)
  208.     'define the rect to draw text in:
  209.     tr.top = y + ld.textrect.top
  210.     tr.bottom = y + ld.cellheight
  211.     '
  212.     If i = ld.active Then
  213.     hr.left = ld.picx + ld.picwidth
  214.     hr.top = y
  215.     hr.bottom = y + ld.cellheight
  216.     hr.right = L.ScaleWidth
  217.     Debug.Print txt(i).text
  218.     PaintHilite -1, txt(i).text, tr, hr, ld, L
  219.     Else
  220.     Debug.Print txt(i).text
  221.     r = DrawText(L.hDC, txt(i).text, Len(txt(i).text), tr, DT_LISTCAP)
  222.     End If
  223. Next
  224. Exit Sub
  225. '
  226. paintlisterr:
  227. MsgBox "Err: " & Err & nl & Error(Err), , "UNABLE TO PAINT WINDOW"
  228. Exit Sub
  229.  
  230. End Sub
  231.  
  232. Sub ResizeList (F As Form, ld As LISTDATA, L As PictureBox)
  233. 'Dim x%, y%
  234. 'Dim r As rect
  235. Debug.Print "Resizing"
  236. resizing = -1
  237. '
  238. ld.rows = ld.itemcount
  239. If ld.rows < 1 Then ld.rows = 1
  240. ld.cols = 1
  241. ld.visrows = L.ScaleHeight \ ld.cellheight + 1
  242. Debug.Print ld.rows, ld.visrows
  243.  
  244. 'F.vs.Enabled = 0
  245. '
  246. If ld.rows > ld.visrows Then
  247.     'F.vs.Move L.ScaleWidth - F.vs.Width, 0, F.vs.Width, F.ScaleHeight
  248.     F.vs.Enabled = -1
  249.     F.vs.Max = ld.rows - ld.visrows
  250. Else
  251.     ld.toprow = 0
  252.     F.vs.Enabled = 0
  253. End If
  254. ld.Width = L.ScaleWidth
  255. '
  256. resizing = 0
  257. ld.textrect.right = L.Width - (ld.picx + ld.picwidth + ld.textrect.left)
  258.  
  259. End Sub
  260.  
  261.