ColorRequester with custom title

Share your advanced PureBasic knowledge/code with the community.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

ColorRequester with custom title

Post 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
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: ColorRequester with custom title

Post 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:
ImageThe happiness is a road...
Not a destination
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: ColorRequester with custom title

Post 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)
User avatar
mk-soft
Always Here
Always Here
Posts: 5393
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ColorRequester with custom title

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: ColorRequester with custom title

Post 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!
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: ColorRequester with custom title

Post 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
Last edited by Kwai chang caine on Tue Jan 30, 2018 2:00 pm, edited 1 time in total.
ImageThe happiness is a road...
Not a destination
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: ColorRequester with custom title

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: ColorRequester with custom title

Post by Dude »

Awesome, nco2k! That's just what I needed. :D PureBasic needs to do this natively.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4661
Joined: Sun Apr 12, 2009 6:27 am

Re: ColorRequester with custom title

Post 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()
Egypt my love
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: ColorRequester with custom title

Post by Kwai chang caine »

NCO2K and RAHSAD code works perfectly here with W10
Thanks at you two for sharing 8)
ImageThe happiness is a road...
Not a destination
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: ColorRequester with custom title

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply