delphifocusdelphi-xe8editcontrol

Delphi XE8: TEdit TextHint Disappears When Receiving Focus


Basically, I want the TextHint of my TEdits to disappear when the first character is entered and not when they receive focus, like the Edits on this Microsoft page: Sign in to your Microsoft account. Can someone please walk me through on how to achieve this?

Thank you in advance.


Solution

  • Based on Uwe Raabe's answer, here is a procedure (for Delphi 2007, should work for newer versions of Delphi as well):

    type
      TCueBannerHideEnum = (cbhHideOnFocus, cbhHideOnText);
    
    procedure TEdit_SetCueBanner(_ed: TEdit; const _s: WideString; _WhenToHide: TCueBannerHideEnum = cbhHideOnFocus);
    const
      EM_SETCUEBANNER = $1501;
    var
      wParam: Integer;
    begin
      case _WhenToHide of
        cbhHideOnText: wParam := 1;
      else //    cbhHideOnFocus: ;
        wParam := 0;
      end;
      SendMessage(_ed.Handle, EM_SETCUEBANNER, wParam, Integer(PWideChar(_s)));
    end;
    

    You call it like this:

    constructor TForm1.Create(_Owner: TComponent);
    begin
      inherited;
      TEdit_SetCueBanner(ed_HideOnFocus, 'hide on focus', cbhHideOnFocus);
      TEdit_SetCueBanner(ed_HideOnText, 'hide on text', cbhHideOnText);
    end;
    

    It doesn't check for the Windows version though, you might want to add the if statement Uwe provided:

    if CheckWin32Version(5, 1) and StyleServices.Enabled and _ed.HandleAllocated then
    

    I just tested it with a project where I disabled runtime theming: It didn't work.