成功了!!!(泪) 但不知代码应放在哪里。直接贴不太好吧…… 不过给点 hint : WM_CTLCOLOREDIT 消息
TO James0001(James) <- 为什么不复我信息呀???
即然做得出来,就贴出来在家共享一下吧~~~
对不起,总觉得长长的代码会影响贴子的美观。 不过既然要的话那就贴出来。其实也不是什么技巧,就是一个消息的问题(WM_CTLCOLOREDIT) 绝对没用 Forecolor 三个滚动条分别代表文本框文字颜色的RGB值,W98+VB6+SP5 下通过'-------------------------------------- '模块 Attribute VB_Name = "Module1"'Developped by James(James0001) on CSDN ' '演示怎样用API改变文本框文字的颜色 '当文本框要“画”文字时,它会给它的父窗体发送一条 WM_CTLCOLOREDIT 消息,lParam是文本框的句柄,wParam是准备输出文字的DCOption ExplicitPrivate Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long Private Const WH_CALLWNDPROCRET = 12 Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long Private Type CWPRETSTRUCT lResult As Long lParam As Long wParam As Long message As Long hwnd As Long End Type Private Const WM_CTLCOLOREDIT = &H133 Dim hHook As Long, frm As Form, txt As TextBoxPublic txtColor As Long '***** Text Color *****Public Function Init(xfrm As Form, xtxt As TextBox) As Boolean hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, AddressOf EditHookProc, &H0, App.ThreadID) If hHook = 0 Then Init = False Exit Function End If Set frm = xfrm Set txt = xtxt Init = True End FunctionPublic Sub Terminate() UnhookWindowsHookEx hHook Set frm = Nothing End SubPrivate Function EditHookProc(ByVal nCode As Long, ByVal wParam As Long, lParam As CWPRETSTRUCT) As Long 'If nCode >= 0 Then If (lParam.hwnd = frm.hwnd) And (lParam.message = WM_CTLCOLOREDIT) And (lParam.lParam = txt.hwnd) Then SetTextColor lParam.wParam, txtColor End If EditHookProc = CallNextHookEx(hHook, nCode, wParam, lParam) 'End If End Function'-------------------------------------- '窗体 VERSION 5.00 Begin VB.Form Form1 BorderStyle = 3 'Fixed Dialog Caption = "TextColor" ClientHeight = 2280 ClientLeft = 45 ClientTop = 330 ClientWidth = 3720 LinkTopic = "Form1" MaxButton = 0 'False MinButton = 0 'False ScaleHeight = 2280 ScaleWidth = 3720 StartUpPosition = 3 'Windows Default Begin VB.HScrollBar Clr Height = 210 Index = 2 LargeChange = 5 Left = 120 Max = 255 TabIndex = 4 Top = 1995 Width = 3495 End Begin VB.HScrollBar Clr Height = 210 Index = 1 LargeChange = 5 Left = 120 Max = 255 TabIndex = 3 Top = 1710 Width = 3495 End Begin VB.HScrollBar Clr Height = 210 Index = 0 LargeChange = 5 Left = 120 Max = 255 TabIndex = 2 Top = 1440 Width = 3495 End Begin VB.CommandButton Command1 Caption = "Refresh" Height = 360 Left = 1230 TabIndex = 1 Top = 990 Width = 1290 End Begin VB.TextBox Text1 Height = 795 Left = 120 MultiLine = -1 'True TabIndex = 0 Text = "Form1.frx":0000 Top = 120 Width = 3555 End End Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = FalseOption ExplicitPrivate Sub Clr_Scroll(Index As Integer) txtColor = (CLng(Clr(0).Value)) + (CLng(Clr(1).Value) * &H100) + (CLng(Clr(2).Value) * &H10000) Text1.Refresh End SubPrivate Sub Clr_Change(Index As Integer) Clr_Scroll Index End SubPrivate Sub Command1_Click() Text1.Refresh End SubPrivate Sub Form_Load() Init Me, Text1 End SubPrivate Sub Form_Unload(Cancel As Integer) Terminate End Sub
T_hdc = GetDC(Text1.hwnd) If SetTextColor(T_hdc, RGB(255, 0, 0)) = CLR_INVALID Then MsgBox "NO...:"
' MsgBox GetTextColor(T_hdc)
'如果用这句的话,返回255,表明已经设置,但为什么Text还会是黑色???
Call ReleaseDC(Text1.hwnd, T_hdc)
End Sub
但不知代码应放在哪里。直接贴不太好吧……
不过给点 hint : WM_CTLCOLOREDIT 消息
即然做得出来,就贴出来在家共享一下吧~~~
不过既然要的话那就贴出来。其实也不是什么技巧,就是一个消息的问题(WM_CTLCOLOREDIT)
绝对没用 Forecolor
三个滚动条分别代表文本框文字颜色的RGB值,W98+VB6+SP5 下通过'--------------------------------------
'模块
Attribute VB_Name = "Module1"'Developped by James(James0001) on CSDN
'
'演示怎样用API改变文本框文字的颜色
'当文本框要“画”文字时,它会给它的父窗体发送一条 WM_CTLCOLOREDIT 消息,lParam是文本框的句柄,wParam是准备输出文字的DCOption ExplicitPrivate Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Const WH_CALLWNDPROCRET = 12
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Type CWPRETSTRUCT
lResult As Long
lParam As Long
wParam As Long
message As Long
hwnd As Long
End Type
Private Const WM_CTLCOLOREDIT = &H133
Dim hHook As Long, frm As Form, txt As TextBoxPublic txtColor As Long '***** Text Color *****Public Function Init(xfrm As Form, xtxt As TextBox) As Boolean
hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, AddressOf EditHookProc, &H0, App.ThreadID)
If hHook = 0 Then
Init = False
Exit Function
End If
Set frm = xfrm
Set txt = xtxt
Init = True
End FunctionPublic Sub Terminate()
UnhookWindowsHookEx hHook
Set frm = Nothing
End SubPrivate Function EditHookProc(ByVal nCode As Long, ByVal wParam As Long, lParam As CWPRETSTRUCT) As Long
'If nCode >= 0 Then
If (lParam.hwnd = frm.hwnd) And (lParam.message = WM_CTLCOLOREDIT) And (lParam.lParam = txt.hwnd) Then
SetTextColor lParam.wParam, txtColor
End If
EditHookProc = CallNextHookEx(hHook, nCode, wParam, lParam)
'End If
End Function'--------------------------------------
'窗体
VERSION 5.00
Begin VB.Form Form1
BorderStyle = 3 'Fixed Dialog
Caption = "TextColor"
ClientHeight = 2280
ClientLeft = 45
ClientTop = 330
ClientWidth = 3720
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 2280
ScaleWidth = 3720
StartUpPosition = 3 'Windows Default
Begin VB.HScrollBar Clr
Height = 210
Index = 2
LargeChange = 5
Left = 120
Max = 255
TabIndex = 4
Top = 1995
Width = 3495
End
Begin VB.HScrollBar Clr
Height = 210
Index = 1
LargeChange = 5
Left = 120
Max = 255
TabIndex = 3
Top = 1710
Width = 3495
End
Begin VB.HScrollBar Clr
Height = 210
Index = 0
LargeChange = 5
Left = 120
Max = 255
TabIndex = 2
Top = 1440
Width = 3495
End
Begin VB.CommandButton Command1
Caption = "Refresh"
Height = 360
Left = 1230
TabIndex = 1
Top = 990
Width = 1290
End
Begin VB.TextBox Text1
Height = 795
Left = 120
MultiLine = -1 'True
TabIndex = 0
Text = "Form1.frx":0000
Top = 120
Width = 3555
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = FalseOption ExplicitPrivate Sub Clr_Scroll(Index As Integer)
txtColor = (CLng(Clr(0).Value)) + (CLng(Clr(1).Value) * &H100) + (CLng(Clr(2).Value) * &H10000)
Text1.Refresh
End SubPrivate Sub Clr_Change(Index As Integer)
Clr_Scroll Index
End SubPrivate Sub Command1_Click()
Text1.Refresh
End SubPrivate Sub Form_Load()
Init Me, Text1
End SubPrivate Sub Form_Unload(Cancel As Integer)
Terminate
End Sub