'子类化窗口函数。
'Module.pas File (在VB的全局模块文件定义如下代码,注:一定要全局模块里定义如下代码)
Declare Function CreatePatternBrush Lib "gdi32" (ByVal hBitmap As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpProc As Long, ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Declare Function SetBkMode Lib "gdi32" (ByVal hdc As Long, ByVal nBkMode As Long) As Long
Public Const GWL_WNDPROC = -4
Public Const WM_CTLCOLOREDIT = &H133
Public Const TRANSPARENT = 1
Public lpOldProc As Long
Public hBlackBrush As Long
Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WM_CTLCOLOREDIT Then
SetBkMode wParam, TRANSPARENT
WndProc = hBlackBrush '替换Text的背景刷子换成我们定义的图像背景刷子。
Exit Function
End If
WndProc = CallWindowProc(lpOldProc, hwnd, uMsg, wParam, lParam)
End Function
'Code File (在程序的代码文件写入如下过程)
Private Sub Form_Load()
Image1.Visible = False '这里我使用了Image1控件加载一张图片并隐藏Image控件
hBlackBrush = CreatePatternBrush(Image1.Picture.Handle) '用Image控件加载的图片创建一个Text使用的刷子
lpOldProc = GetWindowLong(hwnd, GWL_WNDPROC)
SetWindowLong hwnd, GWL_WNDPROC, AddressOf WndProc
End Sub
Private Sub Form_Unload(Cancel As Integer)
If hBlackBrush <> 0 Then
DeleteObject hBlakBrush
End If
End Sub
在模块文件里添加
Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal ncrColor As Long) As Long
Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WM_CTLCOLOREDIT Then
SetBkMode wParam, TRANSPARENT
SetTextColor wParam, &HFF
WndProc = hBlackBrush '替换Text的背景刷子换成我们定义的图像背景刷子。
Exit Function
End If
WndProc = CallWindowProc(lpOldProc, hwnd, uMsg, wParam, lParam)
End Function