Page 1 of 1

New Password Joker

Posted: Thu Oct 21, 2010 2:34 pm
by flaith
Hi all :)

because i want to have a another joker ('*') when i enter a password, i made this little code snippet
I'm using 3 different fonts, each need a specific char, just have a try
And thanks to srod and his tutorial about subclassing :wink:

Code: Select all

;Change your password '*'
;From Subclassing tutorial by Stephen Rodriguez.
;flaith 10/21/2010

Global.i oldStringGadgetProc
Global.s password = ""

Enumeration 
  #Window_FormPassword
  #Gadget_FormPassword_PASSWORD
EndEnumeration

;A subclassing procedure for the string gadget.
Procedure.i stringGadgetProc(hWnd, uMsg, wParam, lParam)
  Protected result
  Select uMsg
    Case #WM_CHAR
      ; if char if BACK
      If wParam = #VK_BACK
          password = Left(password,Len(password)-1)
      Else
        ; add the char
        password + Chr(wparam)
        
        ; display the 'hidden' char selected by the correct font
        ;wparam = 183     ;without loading font
        ;wparam = '¨'    ;Symbol font
        ;wparam = '='    ;Webdings font
        wparam = '£'    ;Wingdings font
      EndIf
      result = CallWindowProc_(oldStringGadgetProc, hWnd, uMsg, wParam, lParam)

    ;The following message allows us to prevent the context menu from displaying.
    ;This is achieved by simply not calling CallWindowProc_() etc.
    Case #WM_CONTEXTMENU
      result = 0

;     Case #WM_DESTROY
;       SetGadgetText(GetDlgCtrlID_(hwnd),pass)
;       result = CallWindowProc_(oldStringGadgetProc, hWnd, uMsg, wParam, lParam)

    Default
      result = CallWindowProc_(oldStringGadgetProc, hWnd, uMsg, wParam, lParam)
  EndSelect
  
  ProcedureReturn result
EndProcedure

If OpenWindow(#Window_FormPassword, 100, 200, 300, 100, "Enter your password", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(#Gadget_FormPassword_PASSWORD, 60, 40, 180,20,"")

  ;Choose one font
  ;SetGadgetFont(#Gadget_FormPassword_PASSWORD,LoadFont(1,"Symbol",8))
  ;SetGadgetFont(#Gadget_FormPassword_PASSWORD,LoadFont(1,"Webdings",8))
  SetGadgetFont(#Gadget_FormPassword_PASSWORD,LoadFont(1,"Wingdings",8))
  
  password = ""

  ;Subclass the string gadget by replacing it's window procedure by one of our own.
  ;Sets a new address for the window procedure
  oldStringGadgetProc = SetWindowLong_(GadgetID(#Gadget_FormPassword_PASSWORD), #GWL_WNDPROC, @stringGadgetProc())

  quitFormPassword = 0
  SetActiveGadget(#Gadget_FormPassword_PASSWORD)

  Repeat
    EventID     = WaitWindowEvent()
    EventGadget = EventGadget()

    Select EventID
      Case #PB_Event_CloseWindow
        If WindowID=#Window_FormPassword
          quitFormPassword=1
        EndIf

      Case #WM_KEYDOWN
        Select EventwParam()
          Case #VK_RETURN                                              ;Appui sur la touche "ENTREE"
            Select GetActiveGadget()                                   ;Suivant le gadget sélectionné
              Case #Gadget_FormPassword_PASSWORD
                SetGadgetText(#Gadget_FormPassword_PASSWORD, password)
                quitFormPassword=1
            EndSelect
          Case #VK_ESCAPE                                              ;Appui sur la touche "Escape"
            SetGadgetText(#Gadget_FormPassword_PASSWORD, "")
            quitFormPassword=1
        EndSelect
    EndSelect

  Until quitFormPassword = 1
  
  ; Cleanup
  ;SetWindowLong_(GadgetID(#Gadget_FormPassword_PASSWORD), #GWL_WNDPROC, oldStringGadgetProc)
EndIf

Debug GetGadgetText(#Gadget_FormPassword_PASSWORD)

Re: New Password Joker

Posted: Thu Oct 21, 2010 5:16 pm
by IdeasVacuum
Works extremely well, I'm impressed flaith. Seems like a hell of a lot of code to get there though.

Re: New Password Joker

Posted: Thu Oct 21, 2010 5:27 pm
by flaith
Thanks IdeasVacuum,

my first try was to change the font, but it only change the '*' so i remembered the tutorial from srod (this helped me a lot :wink:)
and here we are, with this little code :mrgreen:

Re: New Password Joker

Posted: Thu Oct 21, 2010 5:54 pm
by Arctic Fox
How about this? Did I miss something?

Code: Select all

OpenWindow(0, 100, 100, 300, 100, "")
StringGadget(0, 5, 5, 290, 20, "", #ES_PASSWORD)

SendMessage_(GadgetID(0), #EM_SETPASSWORDCHAR, '#', 0)

Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

End

Re: New Password Joker

Posted: Thu Oct 21, 2010 6:27 pm
by flaith
:shock: :) :D :lol:
Arctic Fox wrote:How about this? Did I miss something?
yep, the rope to hang me in the basement Image

Re: New Password Joker

Posted: Fri Oct 22, 2010 2:26 pm
by doctorized
The point is to write a code that is not using windows' API. Both of you, flaith and Arctic Fox, you use API. I know it is the only way but we must find alternative ways. Personally, I hate using windows' API but in many cases I am forced to do it.

Re: New Password Joker

Posted: Fri Oct 22, 2010 2:41 pm
by Alireza
which both methods is good in my option, thanks

Re: New Password Joker

Posted: Fri Oct 22, 2010 5:09 pm
by RASHAD
Changing the Password Joker Cross-Platform :lol:
It is not perfect it needs more work to do

Code: Select all

Global Password$

If OpenWindow(0, 0, 0, 300, 100, "Enter Password", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    StringGadget(0, 10,  10, 280, 23, "",#PB_String_Password)
    SetGadgetColor(0, #PB_Gadget_FrontColor, $D8FFFF)
    SetGadgetColor(0, #PB_Gadget_BackColor, $D8FFFF)
    TextGadget(1, 12,14, 0, 18, "")
    SetGadgetColor(1, #PB_Gadget_FrontColor, $0101FE)
    SetGadgetColor(1, #PB_Gadget_BackColor, $D8FFFF)   

Repeat
  SetGadgetText(1,LSet("",Len(Password$),Chr(174)))
  Select WaitWindowEvent()

      Case #PB_Event_CloseWindow
        Q = 1
     
      Case #PB_Event_Gadget
          Select EventGadget()
            Case 0
              Password$ = GetGadgetText(0)
              If Len(Password$)*7 < 275
                ResizeGadget(1,#PB_Ignore,#PB_Ignore,Len(Password$)*7,#PB_Ignore)
              EndIf
              SetGadgetText(1,LSet("",Len(Password$),Chr(174)))
            Debug Password$
          EndSelect          
            
  EndSelect 
Until Q = 1
EndIf

Re: New Password Joker

Posted: Fri Oct 22, 2010 5:14 pm
by PureLust
@flaith,

just as a hint: The way you've realized it does not work, if you do a repositioning of the Cursor (e.g. by setting a new cursor position with the mouse).
You always add new Chars at the end (or delete always from the end), and not at the position the cursor is.

Whereas the solution from Arctic Fox will have the "normal" behavior.

@doctorized:

as long as the functionality of Flaiths version is good enough for you, a working Version should be realizable with native PB under Windows as well.
So, if you're interested in, I (or maybe someone else) could give it a try.
BUT - it could be, that it will work under Windows, but not under Linux or OSX, because there is a huge difference in the amount of the passed through events between the three OSs. So it could be, that one do not get the needed Events under Linux and OSX, but which you get under Windows.
(I'd run into this problem before, when I tried to adapt a native-PB Custom-Gadget from Windows to Linux and OSX.)

[Edit]
PureLust wrote:So, if you're interested in, I (or maybe someone else) could give it a try.
As you can see ... already done by RASHAD. Different to my approach, but working. :mrgreen:

Re: New Password Joker

Posted: Fri Oct 22, 2010 10:28 pm
by flaith
Alireza wrote:which both methods is good in my option, thanks
thanks, for me too
RASHAD wrote:Changing the Password Joker Cross-Platform :lol:
It is not perfect it needs more work to do
nice one RASHAD :)
PureLust wrote:just as a hint: The way you've realized it does not work, if you do a repositioning of the Cursor (e.g. by setting a new cursor position with the mouse).
You always add new Chars at the end (or delete always from the end), and not at the position the cursor is.
Whereas the solution from Arctic Fox will have the "normal" behavior.
completely agree with you :wink: