PDA

Click to See Complete Forum and Search --> : More about TextBoxes


Yonatan
Nov 5th, 1999, 12:23 AM
Let's say I have access to the hWnd of a TextBox, and to the hWnd of its parent. I have no access to any other properties of them. I am able to subclass both of them, though.

How do I set/get the TextBox's BackColor and ForeColor?

------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)

Serge
Nov 5th, 1999, 01:42 AM
Try this:


Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Declare Function GetBkColor Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function GetTextColor Lib "gdi32" (ByVal hdc As Long) As Long
Private Const CLR_INVALID = -1
Public Function GetTextForeColor(pTextBoxHwnd As Long) As Long
Dim lDC As Long
Dim lRet As Long

lDC = GetWindowDC(Text1.hwnd)
lRet = GetTextColor(lDC)
If lRet = CLR_INVALID Then
GetTextForeColor = 0
Else
GetTextForeColor = lRet
End If
End Function

Public Function GetTextBackColor(pTextBoxHwnd As Long) As Long
Dim lDC As Long
Dim lRet As Long

lDC = GetWindowDC(Text1.hwnd)
lRet = GetBkColor(lDC)
If lRet = CLR_INVALID Then
GetTextColor = 0
Else
GetTextColor = lRet
End If
End Function




Just for a testing purpose....drop a picture box and a textbox on the form.

Picture1.BackColor = GetTextBackColor(Text1.hWnd)


You can create the same functions to use SetBkColor and SetTextColor APIs.


Regards,

------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819


[This message has been edited by Serge (edited 11-05-1999).]

Yonatan
Nov 5th, 1999, 02:09 AM
Just the same problem I had earlier...
GetBkColor and GetTextColor work fine... SetBkColor and SetTextColor don't...

Here's my code:

Option Explicit


Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Declare Function GetBkColor Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function GetTextColor Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function SetBkColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long
Private Declare Function SetTextColor Lib "gdi32" (ByVal hDC As Long, ByVal crColor As Long) As Long
Private Declare Function OleTranslateColor Lib "olepro32" (ByVal oleColor As OLE_COLOR, ByVal hPalette As Long, pColorRef As Long) As Long


Private Const CLR_INVALID = &HFFFFFFFF


Function TranslateColor(ByVal oleColor As OLE_COLOR, Optional ByVal hPalette As Long = 0) As Long
If OleTranslateColor(oleColor, hPalette, TranslateColor) <> 0 Then TranslateColor = CLR_INVALID
End Function


Property Get TextBackColor(ByVal hWndText As Long) As OLE_COLOR
Dim lhDC As Long
lhDC = GetWindowDC(hWndText)
TextBackColor = TranslateColor(GetBkColor(lhDC))
Call ReleaseDC(hWndText, lhDC)
End Property


Property Let TextBackColor(ByVal hWndText As Long, ByVal oNewColor As OLE_COLOR)
Dim lhDC As Long
lhDC = GetWindowDC(hWndText)
Call SetBkColor(lhDC, TranslateColor(oNewColor))
Call ReleaseDC(hWndText, lhDC)
End Property


Property Get TextForeColor(ByVal hWndText As Long) As OLE_COLOR
Dim lhDC As Long
lhDC = GetWindowDC(hWndText)
TextForeColor = TranslateColor(GetTextColor(lhDC))
Call ReleaseDC(hWndText, lhDC)
End Property


Property Let TextForeColor(ByVal hWndText As Long, ByVal oNewColor As OLE_COLOR)
Dim lhDC As Long
lhDC = GetWindowDC(hWndText)
Call SetTextColor(lhDC, TranslateColor(oNewColor))
Call ReleaseDC(hWndText, lhDC)
End Property


Private Sub Text1_Change()
Select Case UCase(Text1.Text)
Case "GET B"
Cls
Print "B = " & Hex(TextBackColor(Text1.hWnd))
Print "Built-in B = " & Hex(TranslateColor(Text1.BackColor)) ' Just for testing purposes
Case "GET F"
Cls
Print "F = " & Hex(TextForeColor(Text1.hWnd))
Print "Built-in F = " & Hex(TranslateColor(Text1.ForeColor)) ' Just for testing purposes
Case "B RED"
TextBackColor(Text1.hWnd) = vbRed
Case "B COOL"
TextBackColor(Text1.hWnd) = &HFF9900
Case "B NORM", "B BLACK"
TextBackColor(Text1.hWnd) = 0
Case "F RED"
TextForeColor(Text1.hWnd) = vbRed
Case "F COOL"
TextForeColor(Text1.hWnd) = &HFF9900
Case "F NORM", "F BLACK"
TextForeColor(Text1.hWnd) = 0
End Select
End Sub


------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)

Juan Carlos Rey
Nov 5th, 1999, 04:20 AM
Congrats, Yonatan, didn't you realize this is post number 10.000?

Wow! :)

Yonatan
Nov 5th, 1999, 04:30 AM
Hey cool! I posted topic number 10000! :)
And 924 posts of my own :)

------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)

Yonatan
Nov 8th, 1999, 12:04 AM
Anyone??? (And please don't notice the previous two replies)

------------------
Yonatan
Teenage Programmer
E-Mail: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)