Edge in a TEdit

0

I need to put a border on the TEdit of a register, when this field is required and is blank when saving the registration.

I've been able to put the border, but I'm not able to disable it when the field is filled in or the registration canceled.

Below is the code I use to create this border.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DBCtrls, Mask;

type

  TEdit = class(StdCtrls.TEdit)
    procedure MessagePaint(var Msg: TWMPaint); message WM_PAINT;
    procedure SetBorder(AColor: TColor);
    procedure PaintEdit(DC: HDC; ARect: TRect; EColor, BColor: TColor);
    procedure ChecarValorInvalido(VRequerido: Boolean);
    procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
    procedure WMKEYUP(var Message: TWMPaint); message WM_KEYUP;
  private
    FPaintedRed: Boolean;
    FRequired: Boolean;
    FRequeridoOld: Boolean;
    FColorBorda : TColor;
  public
      property  Requerido: Boolean read FRequired write FRequired;
  end;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// TEdit's metodes
procedure TEdit.SetBorder(AColor: TColor);
var
  Canvas: TCanvas;

begin

  Canvas := TCanvas.Create;
  try
    Canvas.Handle := GetWindowDC(Handle);
    Canvas.Pen.Style := psSolid;
    Canvas.Pen.Color := AColor;
    Canvas.Brush.Style := bsClear;
    Canvas.Rectangle(0, 0, Width, Height);
  finally
    ReleaseDC(Handle, Canvas.Handle);
    Canvas.Free;
  end;

end;


procedure TEdit.MessagePaint(var Msg: TWMPaint);
var
  DC: HDC;
  Rect: TRect;

begin

  inherited;
  if (Requerido) and (Length(Trim(Text)) = 0) then begin
    FPaintedRed := true;
    DC := GetWindowDC(Handle);
    try
      Windows.GetClientRect(Handle, Rect);
      PaintEdit(DC, Rect, clWindow, clRed);
    finally
      ReleaseDC(Handle, DC);
    end;
  end
  else
    FPaintedRed := false;

end;

procedure TEdit.PaintEdit(DC: HDC; ARect: TRect; EColor, BColor: TColor);
var
  WindowColor: TColor;
  BorderColor: TColor;

begin

  WindowColor := EColor; // Color of TEdit
  BorderColor := BColor; // Border Color of TEdit

  if not Enabled then begin
    WindowColor := clBtnFace;
    BorderColor := clBtnShadow;
  end;

  InflateRect(ARect, 4, 4);
  Brush.Color := WindowColor;
  Windows.FillRect(DC, ARect, Brush.Handle);
  SetBorder(BorderColor);

end;

procedure TEdit.ChecarValorInvalido(VRequerido: Boolean);
begin

  Requerido := VRequerido;
  FRequeridoOld := VRequerido;

  if Requerido and (Length(Trim(Text)) = 0) then begin
    if not FPaintedRed then
      Invalidate;
  end
  else if FPaintedRed then
    Invalidate;

end;

procedure TEdit.CMTextChanged(var Message: TMessage);
begin
  inherited;
  ChecarValorInvalido(FRequeridoOld);
end;

procedure TEdit.WMKEYUP(var Message: TWMPaint);
begin
  inherited;
  ChecarValorInvalido(FRequeridoOld);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.ChecarValorInvalido(True);
end;

end.

Previously in the function that draws the rectangle I put Canvas.Rectangle(ClientRect) , working correctly, but the rectangle was only inside Edit.

Does anyone have a solution for this situation?

    
asked by anonymous 09.08.2018 / 19:02

2 answers

0

The component itself has a border, you can control the style of the border from bsSingle to bsNone. But for this you should put all the TEdit with style bsNone , if this is not the case you can also use a TShape.

Add a TShape on top of TEdit so it stays 1 pixel larger in all directions. Brush property, set Style to bsClear , and the Pen property modify the color you want to display.

Then control with ShapeX.Visible := True/False as needed.

    
31.08.2018 / 16:30
0

You can check if when your TEdit.Text is other than empty you deactivate the border and also click the cancel button.

if TEdit.Text <> '' then
  TEdit.BorderStyle := bsNone;
    
07.09.2018 / 05:14