Tip: InputBox

Share your advanced PureBasic knowledge/code with the community.
User avatar
Jacobus
Enthusiast
Enthusiast
Posts: 140
Joined: Wed Nov 16, 2005 7:51 pm
Location: France
Contact:

Re: Tip: InputBox

Post by Jacobus »

A small modification to akj's code above.
I found this code simple and quick to use for a password query. Simple Add hide/show for a password requester and add an icon to the window.

Code: Select all

Procedure.s InputBox(txtw, txth, title$, prompt$, def$="", flags=0)
  ; A version of InputRequester() by BackupUser
  ; Modified by AKJ 18-Oct-06
  
  ; Edited by Jacobus on August 31, 2025 
  ; => Added password display or hiding and added an icon extracted from shell32.dll 
  
  ; Downloaded: www.purebasic.fr/english/viewtopic.php?t=2412
  ; textw and txth are the outer dimensions of the prompt text gadget
  
  ; Available flags are:
  ;   #PB_String_{BorderLess  LowerCase  Numeric  Password  ReadOnly  Uppercase}
  
  Protected winBox, ImgInput, txtPrompt, strInput, str2Input, butEye, butOK, butCancel ; Gadgets
  
  Protected box, ev, id, ret, esc, a, thread1, thread2, text$=""
  
  If txtw < 128 
    txtw = 128 
  EndIf ; To show buttons correctly
  
  If txth < 20 
    txth = 20 
  EndIf ; Height of a normal text line
  
  winBox = OpenWindow(#PB_Any, 0, 0, txtw + 13, txth + 20 + 23 + 8*4, title$, #PB_Window_ScreenCentered)
  box = WindowID(winBox)
  
  If winBox
    
    ImgInput = ImageGadget(#PB_Any, 5, 8, 32, 32, ExtractIcon_(GetModuleHandle_(#Null), "Shell32.dll", 44)) ; To add an image/icon to the window (windows 11 key icon)
    
    txtPrompt = TextGadget(#PB_Any, 50, 15, txtw-50, txth, prompt$)
    
    If flags & #PB_String_Password = 0 
      flags|#ES_MULTILINE 
    EndIf
       
    strInput   = StringGadget(#PB_Any, 6, txth + 8*2, txtw, 20, def$, flags|#ES_AUTOVSCROLL)
    
    str2Input  = StringGadget(#PB_Any, 6, txth + 8*2, txtw, 20, "", #ES_AUTOVSCROLL) : HideGadget(str2Input, #True)
    
    butEye     = ButtonGadget(#PB_Any, 6, txth + 20 + 8*3, 60, 23, "Show", #PB_Button_Toggle)
    
    butOK      = ButtonGadget(#PB_Any, txtw - 122, txth + 20 + 8*3, 60, 23, "OK")
    
    butCancel  = ButtonGadget(#PB_Any, txtw - 54, txth + 20 + 8*3, 60, 23, "Cancel")
    
    SendMessage_(GadgetID(strInput), #EM_SETSEL, 0, Len(def$)) : SendMessage_(GadgetID(str2Input), #EM_SETSEL, 0, Len(def$))
    
    GetAsyncKeyState_(#VK_RETURN) : GetAsyncKeyState_(#VK_ESCAPE)
    
    StickyWindow(winBox, #True) : SetForegroundWindow_(box) : SetActiveGadget(strInput) : MessageBeep_(#MB_ICONQUESTION)
    
    Repeat
      ev  = WaitWindowEvent(1) 
      id  = EventGadget() 
      ret = GetAsyncKeyState_(#VK_RETURN) 
      esc = GetAsyncKeyState_(#VK_ESCAPE)
      a   = GetForegroundWindow_()            
         
      If a <> box
        thread1 = GetWindowThreadProcessId_(@a, 0) : thread2 = GetWindowThreadProcessId_(@box, 0)
        
        If thread1 <> thread2 
          AttachThreadInput_(thread1, thread2, #True) 
        EndIf
        
        SetForegroundWindow_(box) : Sleep_(1)
        
        If thread1 <> thread2 
          AttachThreadInput_(thread1, thread2, #False) 
        EndIf
        
        SetActiveGadget(strInput) ;: MessageBeep_(#MB_ICONQUESTION)
               
      EndIf
      
      If id = butEye 
      BtnState = GetGadgetState(butEye)
      If BtnState = 1  ; enfoncé
        HideGadget(strInput, #True) : HideGadget(str2Input, #False) : SetGadgetText(str2Input, GetGadgetText(strInput))       
        SetGadgetText(butEye, "Hide")
      ElseIf BtnState = 0 
        HideGadget(strInput, #False) : HideGadget(str2Input, #True) : SetGadgetText(strInput, GetGadgetText(str2Input))   
        SetGadgetText(butEye, "Show")
      EndIf 
    EndIf
      
    Until (ev = #PB_Event_Gadget And (id = butOK Or id = butCancel)) Or ret <> 0 Or esc <> 0 Or ev = #PB_Event_CloseWindow
    
    If id = butOK Or ret <> 0 
      text$ = GetGadgetText(strInput) 
    Else 
      text$ = def$ 
    EndIf
    
    CloseWindow(winBox)
  EndIf
  ProcedureReturn text$
EndProcedure


; Example 
Debug InputBox(300, 50, "Input box", "Enter password:", "Gloubi!BoulgA$", #PB_String_Password)
PureBasicien tu es, PureBasicien tu resteras.
Post Reply