Page 1 of 1

ColorRequester with custom title

Posted: Sun Jan 28, 2018 4:59 am
by Dude
I needed a ColorRequester() with a custom title, so here's my hack solution. :?

Seems to be working correctly and as expected for all language versions of Windows.

Code: Select all

Procedure ColorRequesterWait(title)
  title$=PeekS(title)
  prev=GetForegroundWindow_()
  Repeat
    Sleep_(1)
    hWnd=GetForegroundWindow_()
    If hWnd<>0 And hWnd<>prev
      c$=Space(999)
      GetClassName_(hWnd,c$,990)
      If c$="#32770" ; Dialog class.
        SetWindowText_(hWnd,title$)
        found=1
      EndIf
    EndIf
  Until found
EndProcedure

Procedure ColorRequesterWithTitle(title$,defaultcolor=-1)
  CreateThread(@ColorRequesterWait(),@title$)
  ProcedureReturn ColorRequester(defaultcolor)
EndProcedure

c=ColorRequesterWithTitle("Pick a color!",#Red)
Debug "Color picked: "+c

Re: ColorRequester with custom title

Posted: Tue Jan 30, 2018 11:18 am
by Kwai chang caine
Thanks for sharing 8)

A little problem, the title change ONLY if the color change :|
Select one color, the title change, cool 8)
Run another time with the same color, and the title is not "Pick a color" :wink:

Re: ColorRequester with custom title

Posted: Tue Jan 30, 2018 12:00 pm
by Dude
I don't know what you mean? Works here with repeated requesters:

Code: Select all

r1=ColorRequesterWithTitle("This title says Red",#Red)
r2=ColorRequesterWithTitle("This title also says Red",#Red)

Re: ColorRequester with custom title

Posted: Tue Jan 30, 2018 12:24 pm
by mk-soft
Very nice :D

a little bit smaller

Code: Select all

Procedure ColorRequesterWait(title)
  Protected cnt
  Repeat
    Delay(10)
    hWnd = FindWindow_(@"#32770", 0)
    If hWnd<>0
      SetWindowText_(hWnd,title)
      Break
    EndIf
    cnt + 1
  Until cnt > 5
EndProcedure

Procedure ColorRequesterWithTitle(title$,defaultcolor=-1)
  CreateThread(@ColorRequesterWait(),@title$)
  ProcedureReturn ColorRequester(defaultcolor)
EndProcedure

c=ColorRequesterWithTitle("Pick a old color!",#Red)
Debug "Color picked: "+c

c=ColorRequesterWithTitle("Pick a new color!",#Red)
Debug "Color picked: "+c

Re: ColorRequester with custom title

Posted: Tue Jan 30, 2018 12:32 pm
by Dude
I didn't realize you could do "SetWindowText_(hWnd,title)" with the address of "title". :shock: I thought you had to use a string after creating it with PeekS(). Thanks for the lesson!

Re: ColorRequester with custom title

Posted: Tue Jan 30, 2018 1:03 pm
by Kwai chang caine
Dude wrote:I don't know what you mean? Works here with repeated requesters:

Code: Select all

r1=ColorRequesterWithTitle("This title says Red",#Red)
r2=ColorRequesterWithTitle("This title also says Red",#Red)
Hello DUDE
In fact it's not all the time, i have tested less 30 times in a row and sometime, not always i have the real title of the ColorRequester, not "Pick a color"
Perhaps a problem of delay
PS: i have actived the thread option

Re: ColorRequester with custom title

Posted: Tue Jan 30, 2018 1:26 pm
by nco2k

Code: Select all

Procedure CCHookProc(hdlg, uiMsg, wParam, lParam)
  Protected *choosecolor.CHOOSECOLOR
  If uiMsg = #WM_INITDIALOG
    *choosecolor = lParam
    SetWindowText_(hdlg, *choosecolor\lCustData)
  EndIf
EndProcedure

Procedure MyColorRequester(WindowID, Title$, DefaultColor=0, Flag=#False)
  Protected Result=-1, Dim ColorArray.l(15), choosecolor.CHOOSECOLOR\lStructSize=SizeOf(CHOOSECOLOR)
  ColorArray(0) = DefaultColor
  choosecolor\hwndOwner = WindowID
  choosecolor\rgbResult = ColorArray(0)
  choosecolor\lpCustColors = @ColorArray()
  choosecolor\Flags = #CC_ENABLEHOOK | #CC_ANYCOLOR | #CC_RGBINIT
  If Flag
    choosecolor\Flags | #CC_FULLOPEN
  EndIf
  choosecolor\lCustData = @Title$
  choosecolor\lpfnHook = @CCHookProc()
  If ChooseColor_(@choosecolor)
    Result = choosecolor\rgbResult
  EndIf
  ProcedureReturn Result
EndProcedure

Debug MyColorRequester(0, "Hi", RGB(255, 0, 0), #True)
c ya,
nco2k

Re: ColorRequester with custom title

Posted: Tue Jan 30, 2018 10:17 pm
by Dude
Awesome, nco2k! That's just what I needed. :D PureBasic needs to do this natively.

Re: ColorRequester with custom title

Posted: Tue Jan 30, 2018 10:32 pm
by RASHAD
Sorry for hijacking this thread
- Simple and very few lines of code
- Custom any text
- Control the position
Do what you want to do

Code: Select all

Global Hook

Procedure HookCB ( uMsg , wParam , lParam)   
   Select uMsg
     Case #HCBT_ACTIVATE
        SetDlgItemText_(wParam, 65535, "Dude Special colors :")
        SetDlgItemText_(wParam, 1, "Option #1")
        SetDlgItemText_(wParam, 2, "Option #2")
        SetDlgItemText_(wParam, 712, "Option #3")
        SetDlgItemText_(wParam, 719, "Option #4")
        SetWindowText_(wParam,@"Pick color :")
        MoveWindow_(wParam,GetSystemMetrics_(#SM_CXSCREEN)/2-112, GetSystemMetrics_(#SM_CYSCREEN)/2-165,225,330,1)
        SetDlgItemText_(wParam, 65535, "Dude Special colors :")
        UnhookWindowsHookEx_ (Hook) 
   EndSelect   
   ProcedureReturn 0
EndProcedure

Hook = SetWindowsHookEx_ ( #WH_CBT, @ HookCB () , 0, GetCurrentThreadId_ ())

Debug ColorRequester()

Re: ColorRequester with custom title

Posted: Wed Jan 31, 2018 8:47 pm
by Kwai chang caine
NCO2K and RAHSAD code works perfectly here with W10
Thanks at you two for sharing 8)

Re: ColorRequester with custom title

Posted: Mon Apr 26, 2021 1:45 am
by IdeasVacuum
Just tried Rashad's code because it can also position the Requester.

The code works perfectly stand-alone, but if the callback procedure is used in a program, that program's main window (the first window displayed) gets hooked instead of the Color Requester window.......

Edit: Ah, I have discovered that applying the hook at the right time in the right place makes all the difference :mrgreen:
I have placed it in the Button event that calls the Requester.