请问一下如何修改DELPHI中的控件的属性?比如我们给Edit添加一个边框颜色的属性,请问一下该怎么写,请各位高手写出代码好吗,小弟是菜鸟一个,谢谢!!

解决方案 »

  1.   

    自己改写控件啊,,继承TEdit 这个类吗重写一个就行了
      

  2.   

    先继承TEdit,
    然后增加所需
      

  3.   

    unit EditEx;interfaceuses
      Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics, Forms;type
      TEditEx = class(TEdit)
      private
        { Private declarations }
        FBorderColor: TColor;
        procedure SetColors(Value: TColor);
        procedure RedrawBorder (const Clip: HRGN);
        procedure WMNCPaint (var Message: TMessage); message WM_NCPAINT;
      protected
        { Protected declarations }
      public
        { Public declarations }
        constructor Create (AOwner: TComponent); override;
      published
        { Published declarations }
        property ColorBorder: TColor read FBorderColor write SetColors default $008396A0;
      end;procedure Register;implementationconstructor TEditEx.Create(AOwner: TComponent);
    begin
      inherited;
      FBorderColor := $008396A0;
    end;procedure TEditEx.WMNCPaint (var Message: TMessage);
    begin
      inherited;
      RedrawBorder(HRGN(Message.WParam));
    end;procedure TEditEx.SetColors(Value: TColor);
    begin
      if FBorderColor <> Value then
        FBorderColor := Value;
      RedrawBorder(0);
    end;procedure TEditEx.RedrawBorder (const Clip: HRGN);
    var
      DC: HDC;
      R: TRect;
      BtnFaceBrush: HBRUSH;
    begin
      DC := GetWindowDC(Handle);
      try
        GetWindowRect(Handle, R);
        OffsetRect(R, -R.Left, -R.Top);
        BtnFaceBrush := CreateSolidBrush(ColorToRGB(FBorderColor));
        if (not(csDesigning in ComponentState) and
          (Focused or (not(Screen.ActiveControl is TEdit)))) then
        begin
          { Focus }
          FrameRect(DC, R, BtnFaceBrush);
          InflateRect(R, -1, -1); 
        end
        else
        begin
          { non Focus }
          FrameRect(DC, R, BtnFaceBrush);
          InflateRect(R, -1, -1);
        end;
      finally
        ReleaseDC(Handle, DC);
      end;
      DeleteObject(BtnFaceBrush);
    end;procedure Register;
    begin
      RegisterComponents('Test', [TEditEx]);
    end;end.
    以上可以实现,不过代码我没有时间进行优化。你自己研究一下。
      

  4.   

    不过如果是做控件的话最好是从TCustom开头的类继承好些。
      

  5.   

    高手!!Very Thank you!!