Page 1 of 1

Change Caret and caret Colour for specific gadget

Posted: Wed Jan 22, 2014 1:39 pm
by IdeasVacuum
I have a StringGadget() whose background colour is a murky mustard. The default caret is almost impossible to see, so I would like to force the caret to be jet black.

My experiments didn't achieve this because Windows decides the caret colour contrast, even if a bitmap is used for the caret. So, the compromise would be to make the caret a 'fat block' instead of a slender line. Not too bad, a fat block is easier to see - but two problems:

1) As soon as the StringGadget() is clicked, the default Windows caret (slender line) is restored. If the String Gadget already contains text, we are back to square one - difficult to see the caret.

2) Caret position via the API is controlled with pixel input rather than character count, so placing the caret at the end of the text is tricky.

At the moment, the solution is to change the background colour of the StringGadget(), but..........

Code: Select all

Enumeration
#Win
#Str
#Car
EndEnumeration

;LoadImage(#Car, "C:\Caret.bmp")

iFlags.i = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
pt.POINT

If OpenWindow(#Win, 0, 0, 300, 100, "Change Caret and Caret Colour?", iFlags)

               StringGadget(#Str, 10, 50, 280, 20, "")
            ;SetGadgetColor(#Str, #PB_Gadget_BackColor, RGB(136,136,064))
              DestroyCaret_()
              ;CreateCaret_(GadgetID(#Str), ImageID(#Car), 8, 16)
               CreateCaret_(GadgetID(#Str), #Null, 10, 16)
               GetCaretPos_(@pt)
               ;pt\x = pt\x + 25
              ;SetCaretPos_(pt\x, pt\y)
         SetCaretBlinkTime_(600)
                 ShowCaret_(GadgetID(#Str))
EndIf

Repeat
Until WaitWindowEvent(1) = #PB_Event_CloseWindow

DestroyCaret_()

End

Re: Change Caret and caret Colour for specific gadget

Posted: Wed Jan 22, 2014 2:53 pm
by RASHAD
Hi

Code: Select all

CreateImage(0, 16, 20)
StartDrawing(ImageOutput(0))
    Box(0,0,12,20,(#Yellow ! RGB(136,136,064))) ;The req color xor The back color
StopDrawing()

Font1=LoadFont(#PB_Any,"Courier",14)

    OpenWindow(0,0,0,300,190,"",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)

    StringGadget(0,20,20,260,24,"")
    StringGadget(1,20,60,260,25,"")
    SetGadgetFont(1,FontID(Font1))
   
    StringGadget(2,20,100,260,25,"")
    SetGadgetFont(2,FontID(Font1))
   
    StringGadget(3,20,140,260,25,"")
    SetGadgetColor(3,#PB_Gadget_BackColor,RGB(136,136,064))
    SetGadgetColor(3,#PB_Gadget_FrontColor,#Yellow)
    SetGadgetFont(3,FontID(Font1))
       
    Repeat
        Event=WaitWindowEvent()
        If Event=#PB_Event_Gadget
            If EventGadget()=0
                DestroyCaret_()
                CreateCaret_(GadgetID(0),0,1,14)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
            If EventGadget()=1
                DestroyCaret_()
                CreateCaret_(GadgetID(1),1,10,20)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
            If EventGadget()=2
                DestroyCaret_()
                CreateCaret_(GadgetID(2),0,10,20)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
            If EventGadget()=3
                DestroyCaret_()
                CreateCaret_(GadgetID(3),ImageID(0),0,0)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
        EndIf
    Until Event=#PB_Event_CloseWindow
Edit : Code updated
Edit 2 :Updated again

Re: Change Caret and caret Colour for specific gadget

Posted: Wed Jan 22, 2014 3:19 pm
by Kwai chang caine
Nice RASHAD...you have not loose the hand :wink:
Thanks for sharing 8)

Re: Change Caret and caret Colour for specific gadget

Posted: Wed Jan 22, 2014 4:01 pm
by IdeasVacuum
That's brilliant Rashad 8)
Just goes to show what can be done, when you know what you are doing!

Re: Change Caret and caret Colour for specific gadget

Posted: Wed Jan 22, 2014 4:20 pm
by ts-soft
good one, but the destroycaret is missing:

Code: Select all

            If EventGadget()=1
              Select EventType()
                Case  #PB_EventType_Focus
                  CreateCaret_(GadgetID(1),1,10,20)
                  SetCaretBlinkTime_(500) ; immobile = -1, normal blinking  = 500
                  ShowCaret_(GadgetID(1))
                Case #PB_EventType_LostFocus
                  DestroyCaret_()
              EndSelect
            EndIf
greetings Thomas

Re: Change Caret and caret Colour for specific gadget

Posted: Wed Jan 22, 2014 4:26 pm
by Kwai chang caine
Thanks TSSOFT i have add your modification 8)

Re: Change Caret and caret Colour for specific gadget

Posted: Wed Jan 22, 2014 11:07 pm
by IdeasVacuum
....ah, but it doesn't solve the contrast problem. Try the StringGadgets with a back colour of RGB(136,136,064) as per my original (crap) code. The caret is displayed in a pale colour when black would be much better.....

Re: Change Caret and caret Colour for specific gadget

Posted: Thu Jan 23, 2014 12:55 am
by RASHAD
Previous post updated

Re: Change Caret and caret Colour for specific gadget

Posted: Thu Jan 23, 2014 3:53 am
by IdeasVacuum
Box(0,0,12,20,(#Yellow ! RGB(136,136,064))) ;The req color xor The back color
aha ~ thanks Rashad, works perfectly. Not even described on MSDN!

Re: Change Caret and caret Colour for specific gadget

Posted: Fri Jan 24, 2014 12:23 am
by RASHAD
Hi
Solved an ugly effect for the Caret at the end of the StringGadget()

Code: Select all

CreateImage(0, 12, 19)
StartDrawing(ImageOutput(0))
    Box(0,0,12,19,(#Yellow ! RGB(136,136,064))) ;The req color xor The back color
StopDrawing()

Font1=LoadFont(#PB_Any,"Courier",14)

    OpenWindow(0,0,0,300,190,"",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)

    StringGadget(0,20,20,260,24,"")
    StringGadget(1,20,60,260,25,"")
    SendMessage_(GadgetID(1), #EM_SETMARGINS, #EC_RIGHTMARGIN, 0|9 << 16)
    SetGadgetFont(1,FontID(Font1))
   
    StringGadget(2,20,100,260,25,"")
    SendMessage_(GadgetID(2), #EM_SETMARGINS, #EC_RIGHTMARGIN, 0|9 << 16)
    SetGadgetFont(2,FontID(Font1))
   
    StringGadget(3,20,140,260,25,"")
    SendMessage_(GadgetID(3), #EM_SETMARGINS, #EC_RIGHTMARGIN, 0|11 << 16)
    SetGadgetColor(3,#PB_Gadget_BackColor,RGB(136,136,064))
    SetGadgetColor(3,#PB_Gadget_FrontColor,#Yellow)
    SetGadgetFont(3,FontID(Font1))
   
    Repeat
        Event=WaitWindowEvent()
        If Event=#PB_Event_Gadget
            If EventGadget()=0
                DestroyCaret_()
                CreateCaret_(GadgetID(0),0,1,14)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
            If EventGadget()=1
                DestroyCaret_()
                CreateCaret_(GadgetID(1),1,10,20)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
            If EventGadget()=2
                DestroyCaret_()
                CreateCaret_(GadgetID(2),0,10,20)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
            If EventGadget()=3
                DestroyCaret_()
                CreateCaret_(GadgetID(3),ImageID(0),0,0)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
        EndIf
    Until Event=#PB_Event_CloseWindow