【Delphi】Utils.StdCtrls

unit Utils.StdCtrls;

interface

uses
    Classes, Controls, StdCtrls, Windows;

type
    TUiUtil = class(TObject)
    public
        { 控件查找 TWinControl }
        class function GetControlByName(ctrl: TWinControl; const name: string): TControl;
        { 控件查找 TComponent }
        class function GetComponentByName(ctrl: TComponent; const name: string): TComponent;
    end;

////////////////////////////////////////////////////////////////////////////////

    TEditUtil = class(TObject)
    public
        { 内容居中 }
        class function SetTextCenter(text: TEdit): TEdit;
        { 内容居右 }
        class function SetTextRight(text: TEdit): TEdit;
        { 数字框 }
        class function SetTypeNumber(text: TEdit): TEdit;
        { 密码框 }
        class function SetTypePassword(text: TEdit; mask: Char = '*'): TEdit;
        { 强制小写 }
        class function SetLowerCase(text: TEdit): TEdit;
        { 强制大写 }
        class function SetUpperCase(text: TEdit): TEdit;
    end;

implementation

{ 控件查找 TWinControl }
class function TUiUtil.GetControlByName(ctrl: TWinControl; const name: string): TControl;
begin
    Result := ctrl.FindChildControl(name);
end;

{ 控件查找 TComponent }
class function TUiUtil.GetComponentByName(ctrl: TComponent; const name: string): TComponent;
begin
    Result := ctrl.FindComponent(name);
end;


////////////////////////////////////////////////////////////////////////////////

{ 内容居中 }
class function TEditUtil.SetTextCenter(text: TEdit): TEdit;
var
    owner: HWND;
begin
    owner := text.Handle;
    SetWindowLong(owner, GWL_STYLE, GetWindowLong(owner, GWL_STYLE) or ES_CENTER);
    Result := text;
end;

{ 内容居右 }
class function TEditUtil.SetTextRight(text: TEdit): TEdit;
var
    owner: HWND;
begin
    owner := text.Handle;
    SetWindowLong(owner, GWL_STYLE, GetWindowLong(owner, GWL_STYLE) or ES_RIGHT);
    Result := text;
end;

{ 数字框 }
class function TEditUtil.SetTypeNumber(text: TEdit): TEdit;
var
    owner: HWND;
begin
    owner := text.Handle;
    SetWindowLong(owner, GWL_STYLE, GetWindowLong(owner, GWL_STYLE) or ES_NUMBER);
    Result := text;
end;

{ 密码框 }
class function TEditUtil.SetTypePassword(text: TEdit; mask: Char = '*'): TEdit;
var
    owner: HWND;
begin
    if text.PasswordChar = #0 then begin
        text.PasswordChar := mask;
    end;
    owner := text.Handle;
    SetWindowLong(owner, GWL_STYLE, GetWindowLong(owner, GWL_STYLE) or ES_PASSWORD);
    Result := text;
end;

{ 强制小写 }
class function TEditUtil.SetLowerCase(text: TEdit): TEdit;
var
    owner: HWND;
begin
    owner := text.Handle;
    SetWindowLong(owner, GWL_STYLE, GetWindowLong(owner, GWL_STYLE) or ES_LOWERCASE);
    Result := text;
end;

{ 强制大写 }
class function TEditUtil.SetUpperCase(text: TEdit): TEdit;
var
    owner: HWND;
begin
    owner := text.Handle;
    SetWindowLong(owner, GWL_STYLE, GetWindowLong(owner, GWL_STYLE) or ES_UPPERCASE);
    Result := text;
end;

end.


原文地址:https://www.cnblogs.com/zhuzhongxing/p/14147082.html