Bay Six Software Forum Index Bay Six Software
Beyond the Basics
 
 FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[BETA] Toggle TextBox Colors

 
Post new topic   Reply to topic    Bay Six Software Forum Index -> Snippet Testing
View previous topic :: View next topic  
Author Message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: May 13th, 2008, 6:44am    Post subject: [BETA] Toggle TextBox Colors Reply with quote

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

_________________
Brent
Back to top
View user's profile Send private message Send e-mail
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: May 13th, 2008, 8:31am    Post subject: Re: [BETA] Toggle TextBox Colors Reply with quote

Thanks Brent for showing the plebs how things are done correctly.
_________________
Stefan

Any code I post can be freely used, just give credit.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Bay Six Software Forum Index -> Snippet Testing All times are GMT
Page 1 of 1
Jump to:  
Quick Reply
Username:
Message:
   Shortcut keys: Alt+Q to activate, Alt+P to preview, Alt+S to submit
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



Lo-Fi Version
Powered by phpBB © 2001, 2005 phpBB Group