Subject: [BETA] Toggle TextBox Colors
This demo requires Windows 2000 or later. It allows you to toggle the color the colors of a text box between the defaults and white-on-red. With a few modifications, you could give each text box different colors at different times.

Code:
    ' Toggle TextBox Color Demo
    ' By Brent D. Thorn, 05/2008

    ' REQUIRES WINDOWS 2000 OR LATER.

    Global g.LastControl : g.LastControl = 3
    Dim g.hwndAlerts(g.LastControl)

    Open "WMLiberty" For DLL As #wmlib

    WindowWidth = 256
    WindowHeight = 130
    StyleBits #demo, _DS_CENTER, 0, 0, 0

    TextBox     #demo.txt.1, 10, 10, 200, 20
    Checkbox    #demo.chk.1, "", demo.chk.Set, demo.chk.Reset, 220, 10, 20, 20
    TextBox     #demo.txt.2, 10, 40, 200, 20
    Checkbox    #demo.chk.2, "", demo.chk.Set, demo.chk.Reset, 220, 40, 20, 20
    TextBox     #demo.txt.3, 10, 70, 200, 20
    Checkbox    #demo.chk.3, "", demo.chk.Set, demo.chk.Reset, 220, 70, 20, 20

    Open "Toggle Color Demo" For Dialog As #demo

    #demo "TrapClose demo.Close"
    #demo "Font Microsoft_Sans_Serif 8"
    #demo.txt.1 "!SetFocus"

    Callback lpfnCB, OnCtlColorEdit( ULong, ULong, ULong, ULong ), Long
    ret = SetWMHandler(HWnd(#demo), _WM_CTLCOLOREDIT, lpfnCB, GetStockObject(18))

    Call DoEvents

Sub demo.Close wnd$
    Close #wmlib
    Close #wnd$
    End
End Sub

Sub DoEvents
[DoEvents]
    Scan
    CallDLL #kernel32, "Sleep", _
        50 As ULong, ret As Void
    GoTo [DoEvents]
End Sub

Sub demo.chk.Set chk$
    n = g.hwndAlerts(0) + 1
    g.hwndAlerts(0) = n
    txt$ = Word$(chk$, 1, ".")+".txt."+Word$(chk$, 3, ".")
    hwnd = HWnd(#txt$)
    g.hwndAlerts(n) = hwnd
    Call EraseWindow hwnd
End Sub

Sub demo.chk.Reset chk$
    txt$ = Word$(chk$, 1, ".")+".txt."+Word$(chk$, 3, ".")
    hwnd = HWnd(#txt$)
    n = g.hwndAlerts(0)
    For i = 1 To n
        If hwnd = g.hwndAlerts(i) Then
            g.hwndAlerts(i) = 0
            Sort g.hwndAlerts(), n, 1
            Exit For
        End If
    Next
    g.hwndAlerts(0) = n - 1
    Call EraseWindow hwnd
End Sub

Function OnCtlColorEdit( hWnd, uMsg, hdcEdit, hwndEdit )
    crFore = RGB(255, 255, 255)
    crBack = RGB(192, 0, 0)

    For i = 1 To g.hwndAlerts(0)
        If hwndEdit = g.hwndAlerts(i) Then found = 1 : Exit For
    Next

    If Not( found ) Then Exit Function

    CallDLL #gdi32, "SetTextColor", _
        hdcEdit As ULong, _
        crFore As ULong, _
        ret As ULong

    CallDLL #gdi32, "SetBkColor", _
        hdcEdit As ULong, _
        crBack As ULong, _
        ret As ULong

    hbrDCBrush = GetStockObject(18) 'DC_BRUSH

    CallDLL #gdi32, "SelectObject", _
        hdcEdit As ULong, _
        hbrDCBrush As ULong, _
        ret As ULong

    CallDLL #gdi32, "SetDCBrushColor", _
        hdcEdit As ULong, _
        crBack As ULong, _
        ret As ULong

    OnCtlColorEdit = hbrDCBrush
End Function

Function SetWMHandler( hWnd, uMsg, lpfnCB, lSuccess )
    CallDLL #wmlib, "SetWMHandler", _
        hWnd As ULong, _
        uMsg As ULong, _
        lpfnCB As ULong, _
        lSuccess As Long, _
        SetWMHandler As Long
End Function

Function GetStockObject( nObject )
    calldll #gdi32, "GetStockObject", _
      nObject  As Long, _
      GetStockObject As ULong
End Function

function RGB( R, G, B )
    RGB = R + 256 * G + 65536 * B
end function

Sub EraseWindow hWnd
    flags = _RDW_ERASE Or _RDW_INVALIDATE
    CallDLL #user32, "RedrawWindow", _
        hWnd  as ulong, _
        _NULL  as ulong, _
        _NULL as ulong, _
        flags as ulong, _
        ret   as long
End Sub

Subject: Re: [BETA] Toggle TextBox Colors
Thanks Brent for showing the plebs how things are done correctly.


Page 1 of 1


Jump to:  
You cannot post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum

Full Version
Powered by phpBB © phpBB Group
Design by phpBBXS.Com | Lo-Fi Mod.