Results 1 to 6 of 6

Thread: More about TextBoxes

  1. #1

    Thread Starter
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Post

    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



  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Try this:

    Code:
    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).]

  3. #3

    Thread Starter
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Post

    Just the same problem I had earlier...
    GetBkColor and GetTextColor work fine... SetBkColor and SetTextColor don't...

    Here's my code:
    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



  4. #4
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301

    Post

    Congrats, Yonatan, didn't you realize this is post number 10.000?

    Wow!

  5. #5

    Thread Starter
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Post

    Hey cool! I posted topic number 10000!
    And 924 posts of my own

    ------------------
    Yonatan
    Teenage Programmer
    E-Mail: RZvika@netvision.net.il
    ICQ: 19552879



  6. #6

    Thread Starter
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Post

    Anyone??? (And please don't notice the previous two replies)

    ------------------
    Yonatan
    Teenage Programmer
    E-Mail: RZvika@netvision.net.il
    ICQ: 19552879



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width