Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make my Delphi 5 app display password "blobs"?

Pretty simple one, but I can't find the answer.

I'm building an app in Delphi 5 Enterprise, and want my app to use the new bold black dot in a password field instead of an asterisk.

How can I do this?

like image 477
Drarok Avatar asked Nov 28 '22 04:11

Drarok


2 Answers

See PasswordBox: A Better Way to Enter Passwords:

Getting the black dots to show up based on the visual style was insanely simple!

    private const int ES_PASSWORD = 0x0020;
    ...

    protected override CreateParams CreateParams
    {
        CreateParams cp = base.CreateParams;
        ...

        cp.Style |= ES_PASSWORD;
        ...

        return cp;
    }
like image 86
Sinan Ünür Avatar answered Dec 11 '22 01:12

Sinan Ünür


Thanks to all the above attempts, and all that contributed, but I had to join all the relevant parts together to get to a whole solution.

Thanks to Sinan Ünür for pointing out the ES_PASSWORD flag, which is used by default in Delphi, but only if PasswordChar is <> #0 (NUL).

This means that when you set PasswordChar to something, it sets the ES_PASSWORD flag, and then calls SendMessage(Handle, EM_SETPASSWORDCHAR, Ord(FPasswordChar), 0); (thanks to Stijn Sanders for pointing me towards the StdCtrls source).

If I create a subclass and bypass the line sending the EM_SETPASSWORDCHAR field, I still get only stars.

What I was forgetting to do was enable themes (which in my ancient version of Delphi requires a resource file compiling in). Hey presto, it works; Blobs abound!

So, in summary:

  1. Define the ES_PASSWORD constant if you don't already have it.

    const
      ES_PASSWORD = 32;
    
  2. Create a TEdit subclass and override CreateParams to include ES_PASSWORD in the window style.

    procedure TPasswordEdit.CreateParams(var Params: TCreateParams);
    begin
      inherited;
      Params.Style := Params.Style or ES_PASSWORD;
    end;
    
  3. Enable themes for your program.

And do not set the PasswordChar property. Done!

like image 37
Drarok Avatar answered Dec 11 '22 00:12

Drarok