📄 treebkpicmdu.bas
字号:
'because the source bitmap may be
'a DIB section, which behaves differently than a bitmap.
'(Specifically, copying from a DIB section to a monochrome bitmap
'does a nearest-color selection rather than painting based on the
'backcolor and forecolor.
hdcColor = CreateCompatibleDC(hdcScreen)
hbmColorOld = SelectObject(hdcColor, hbmColor)
hPalOld = SelectPalette(hdcColor, hPal, True)
RealizePalette hdcColor
'In case hdcSrc contains a monochrome bitmap, we must set the destination
'foreground/background colors according to those currently set in hdcSrc
'(because Windows will associate these colors with the two monochrome colors)
SetBkColor hdcColor, GetBkColor(hdcSrc)
SetTextColor hdcColor, GetTextColor(hdcSrc)
BitBlt hdcColor, 0, 0, width, Height, hdcSrc, xSrc, ySrc, vbSrcCopy
'Paint the mask. What we want is white at the transparent color
'from the source, and black everywhere else.
hdcMask = CreateCompatibleDC(hdcScreen)
hbmMaskOld = SelectObject(hdcMask, hbmMask)
'When bitblt'ing from color to monochrome, Windows sets to 1
'all pixels that match the background color of the source DC. All
'other bits are set to 0.
SetBkColor hdcColor, lMaskColor
SetTextColor hdcColor, vbWhite
BitBlt hdcMask, 0, 0, width, Height, hdcColor, 0, 0, vbSrcCopy
'Paint the rest of the cover bitmap.
'
'What we want here is black at the transparent color, and
'the original colors everywhere else. To do this, we first
'paint the original onto the cover (which we already did), then we
'AND the inverse of the mask onto that using the DSna ternary raster
'operation (0x00220326 - see Win32 SDK reference, Appendix, "Raster
'Operation Codes", "Ternary Raster Operations", or search in MSDN
'for 00220326). DSna [reverse polish] means "(not SRC) and DEST".
'
'When bitblt'ing from monochrome to color, Windows transforms all white
'bits (1) to the background color of the destination hdc. All black (0)
'bits are transformed to the foreground color.
SetTextColor hdcColor, vbBlack
SetBkColor hdcColor, vbWhite
BitBlt hdcColor, 0, 0, width, Height, hdcMask, 0, 0, DSna
'Paint the Mask to the Screen buffer
BitBlt hdcScnBuffer, 0, 0, width, Height, hdcMask, 0, 0, vbSrcAnd
'Paint the Color to the Screen buffer
BitBlt hdcScnBuffer, 0, 0, width, Height, hdcColor, 0, 0, vbSrcPaint
'Copy the screen buffer to the screen
BitBlt hdcDest, xDest, yDest, width, Height, hdcScnBuffer, 0, 0, vbSrcCopy
'All done!
DeleteObject SelectObject(hdcColor, hbmColorOld)
SelectPalette hdcColor, hPalOld, True
RealizePalette hdcColor
DeleteDC hdcColor
DeleteObject SelectObject(hdcScnBuffer, hbmScnBufferOld)
SelectPalette hdcScnBuffer, hPalBufferOld, True
RealizePalette hdcScnBuffer
DeleteDC hdcScnBuffer
DeleteObject SelectObject(hdcMask, hbmMaskOld)
DeleteDC hdcMask
ReleaseDC 0&, hdcScreen
End Sub
Public Sub PaintTransparentStdPic(ByVal hdcDest As Long, _
ByVal xDest As Long, _
ByVal yDest As Long, _
ByVal width As Long, _
ByVal Height As Long, _
ByVal picSource As Picture, _
ByVal xSrc As Long, _
ByVal ySrc As Long, _
ByVal clrMask As OLE_COLOR, _
Optional ByVal hPal As Long = 0)
Dim hdcSrc As Long 'HDC that the source bitmap is selected into
Dim hbmMemSrcOld As Long
Dim hbmMemSrc As Long
Dim udtRect As RECT
Dim hbrMask As Long
Dim lMaskColor As Long
Dim hdcScreen As Long
Dim hPalOld As Long
'Verify that the passed picture is a Bitmap
If picSource Is Nothing Then
GoTo PaintTransparentStdPic_InvalidParam
End If
Select Case picSource.Type
Case vbPicTypeBitmap
hdcScreen = GetDC(0&)
'Validate palette
If hPal = 0 Then
hPal = m_hpalHalftone
End If
'Select passed picture into an HDC
hdcSrc = CreateCompatibleDC(hdcScreen)
hbmMemSrcOld = SelectObject(hdcSrc, picSource.Handle)
hPalOld = SelectPalette(hdcSrc, hPal, True)
RealizePalette hdcSrc
'Draw the bitmap
PaintTransparentDC hdcDest, xDest, yDest, width, Height, hdcSrc, xSrc, ySrc, clrMask, hPal
SelectObject hdcSrc, hbmMemSrcOld
SelectPalette hdcSrc, hPalOld, True
RealizePalette hdcSrc
DeleteDC hdcSrc
ReleaseDC 0&, hdcScreen
Case vbPicTypeIcon
'Create a bitmap and select it into an DC
hdcScreen = GetDC(0&)
'Validate palette
If hPal = 0 Then
hPal = m_hpalHalftone
End If
hdcSrc = CreateCompatibleDC(hdcScreen)
hbmMemSrc = CreateCompatibleBitmap(hdcScreen, width, Height)
hbmMemSrcOld = SelectObject(hdcSrc, hbmMemSrc)
hPalOld = SelectPalette(hdcSrc, hPal, True)
RealizePalette hdcSrc
'Draw Icon onto DC
udtRect.Bottom = Height
udtRect.Right = width
OleTranslateColor clrMask, 0&, lMaskColor
hbrMask = CreateSolidBrush(lMaskColor)
FillRect hdcSrc, udtRect, hbrMask
DeleteObject hbrMask
DrawIconEx hdcSrc, 0, 0, picSource.Handle, 0, 0, 0, 0, DI_NORMAL
'Draw Transparent image
PaintTransparentDC hdcDest, xDest, yDest, width, Height, hdcSrc, 0, 0, lMaskColor, hPal
'Clean up
DeleteObject SelectObject(hdcSrc, hbmMemSrcOld)
SelectPalette hdcSrc, hPalOld, True
RealizePalette hdcSrc
DeleteDC hdcSrc
ReleaseDC 0&, hdcScreen
Case Else
GoTo PaintTransparentStdPic_InvalidParam
End Select
Exit Sub
PaintTransparentStdPic_InvalidParam:
'Err.Raise giINVALID_PICTURE
Exit Sub
End Sub
Public Sub Subclass(frm As Form, tv As TreeView)
'Subclass the TreeView and store an object
'pointer to the form.
Dim lProc As Long
If GetProp(tv.hWnd, "VBTWndProc") <> 0 Then
Exit Sub
End If
lProc = GetWindowLong(tv.hWnd, GWL_WNDPROC)
SetProp tv.hWnd, "VBTWndProc", lProc
SetProp tv.hWnd, "VBTWndPtr", ObjPtr(frm)
SetWindowLong tv.hWnd, GWL_WNDPROC, _
AddressOf WndProcTV
End Sub
Public Sub UnSubclass(tv As TreeView)
Dim lProc As Long
lProc = GetProp(tv.hWnd, "VBTWndProc")
If lProc = 0 Then
Exit Sub
End If
SetWindowLong tv.hWnd, GWL_WNDPROC, lProc
RemoveProp tv.hWnd, "VBTWndProc"
RemoveProp tv.hWnd, "VBTWndPtr"
End Sub
Public Function WndProcTV(ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
On Error Resume Next
Dim lProc As Long
Dim lPtr As Long
Dim tmpForm As Form
Dim bUseRetVal As Boolean
Dim lRetVal As Long
bUseRetVal = False
lProc = GetProp(hWnd, "VBTWndProc")
lPtr = GetProp(hWnd, "VBTWndPtr")
'Copy the form's object pointer into an
'object variable and call the message handler.
CopyMemory tmpForm, lPtr, 4
tmpForm.TreeViewMessage hWnd, wMsg, wParam, lParam, _
lRetVal, bUseRetVal
CopyMemory tmpForm, 0&, 4
If bUseRetVal = True Then
'Use the return value from the form's
'handler
WndProcTV = lRetVal
Else
'Pass on to original wndproc
WndProcTV = CallWindowProc(lProc, hWnd, wMsg, _
wParam, lParam)
End If
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -