RT

解决方案 »

  1.   

    为什么要这样?放在外面,用Panel装起来不行吗?
      

  2.   

    先放个Panel,相当于容器,然后把Edit和Button放在上面
      

  3.   

    unit EditY;interfaceuses
      Windows, SysUtils, Messages, Classes, Graphics, Controls,
      Forms, Dialogs, StdCtrls,Menus;
    type
      TLinkStyle=(IsNormal,IsButton);
    type
      TEditY = class(TEdit)
      private
        FButtonWidth:Integer;
        FLinkStyle:TLinkStyle;
        FPress:Boolean;
       // FONButtonClick:TNotifyEvent;
        FOnButton :Boolean;
        procedure GetEditRealRect;
        procedure DrawButton(CHand:HDC;ARect:TRect);
        function  CheckPosition(X,Y:integer):Boolean;
        { Private declarations }
      protected
        procedure Loaded;override;
        function  GetLinkStyle:TLinkStyle;
        procedure DoEnter;override;
        procedure DoExit;override;
        procedure SetLinkStyle(Value:TLinkStyle);
        procedure CreateParams(var Params:TCreateParams);override;
        procedure PaintWindow(DC: HDC); override;
        procedure WMPAINT(var message:TWMPaint);message WM_PAINT;
        procedure MouseMove(Shift: TShiftState; X, Y: Integer);override;
        procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
          X, Y: Integer); override;
        procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
          X, Y: Integer); override;
        procedure WMSETCURSOR(var MSg:TWMSETCURSOR);message WM_SETCURSOR;
        procedure WMLButtonDblClk(var Msg:TWMLButtonDblClk);message WM_LButtonDblClk;
        { Protected declarations }
      public
        constructor Create(AOwner:TComponent);override;
        destructor Destroy;override;
        procedure CreateWnd;override;
        procedure DestroyWnd;override;
        property Ctl3D;
        { Public declarations }
      published
        property LinkStyle:TLinkStyle read GetLinkStyle write SetLinkStyle default IsNormal;    { Published declarations }
      end;procedure Register;implementation
      

  4.   

    我写的100个符合你的要求!
    procedure Register;
    begin
      RegisterComponents('Yang11-8', [TEditY]);
    end;{ TEditY }function TEditY.CheckPosition(X, Y:integer): Boolean;
    var
      R:TRect;
    begin
      SetRect(R,ClientWidth-FButtonWidth,1,ClientWidth-1,ClientHeight-1);
      if PtInRect(R,Point(X,Y)) then
          FOnButton:=True
      else
          FOnButton:=False;
      Result:=FOnButton;
    end;constructor TEditY.Create(AOwner: TComponent);
    begin
       inherited Create(AOwner);
       FButtonWidth:=GetSystemMetrics(SM_CXVSCROLL)+4; //得到和系统滚动条的宽度。
       FLinkStyle:=IsNormal;
       FONButton:=False;end;procedure TEditY.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);
      if  PassWordChar=#0 then
       with Params do
        begin
           Style:=Style or ES_MULTILINE;
        end;
    end;procedure TEditY.CreateWnd;
    begin
      inherited;
      Ctl3D:=False;
      Height:=21;
    end;destructor TEditY.Destroy;
    begin inherited Destroy;
    end;
    procedure TEditY.DestroyWnd;
    begin  inherited;end;procedure TEditY.DoEnter;//Respond to receiving input focus.TWinControl的调度函数
    begin
      if FLinkStyle=ISButton then
         GetEditRealRect;
      inherited DoEnter;
      SendMessage(Handle,EM_SETSEL, 0, -1); //If the nStart parameter is 0 and the nEnd parameter is -1, all the text in the edit control is selected.
    end;procedure TEditY.DoExit;
    begin
      if FLinkStyle=IsButton then
         GetEditRealRect;
      inherited DoExit;
    end;procedure TEditY.DrawButton(CHand: HDC; ARect: TRect);
    var
     ACanvas:TConTrolCanvas;
     I:integer;
     R,G,B:integer;
    begin
        ACanvas:=TConTrolCanvas.Create;
        TConTrolCanvas(ACanvas).Control:=Self;
        ACanvas.Handle:=CHand;
        with ACanvas do
          begin
            if (not FPress ) then
            begin
            Brush.Color:=clWhite;
            FillRect(ARect);
            R:=104;
            G:=104;
            B:=180;
            for I:=0 to 15 do
            begin
              Brush.Color:=RGB(R+10*I,G+10*I,B+5*I);
              FillRect(Rect(ARect.Left,ARect.Top+I+1,ARect.Right,ARect.Top+I+2));
            end;
         end
         else
         begin
            Brush.Color:=clwindow;
            FillRect(ARect);
            R:=239;
            for I:=0 to 14 do
             begin
               Brush.Color:=RGB(R-9*I,R-9*I,R-9*I);
               FillRect(Rect(ARect.Left,ARect.Top+I+1,ARect.Right,ARect.Top+I+2));
             end;
          end;
      end;
      if (not FPress )   then
        ACanvas.Pen.Color:=clwindow
      else
        ACanvas.Pen.Color:=clOlive;
      ACanvas.Rectangle(Round (ARect.Left+((ARect.Right-ARect.Left)/4)-1),Round(((ARect.Bottom-ARect.Top)/2)+1),
                      Round (ARect.Left+((ARect.Right-ARect.Left)/4)+1),Round(((ARect.Bottom-ARect.Top)/2)+3));
      ACanvas.Rectangle(Round (ARect.Left+2*((ARect.Right-ARect.Left)/4)-1),Round(((ARect.Bottom-ARect.Top)/2)+1),
                      Round (ARect.Left+2*((ARect.Right-ARect.Left)/4)+1),Round(((ARect.Bottom-ARect.Top)/2)+3));
      ACanvas.Rectangle(Round (ARect.Left+3*((ARect.Right-ARect.Left)/4)-1),Round(((ARect.Bottom-ARect.Top)/2)+1),
                      Round (ARect.Left+3*((ARect.Right-ARect.Left)/4)+1),Round(((ARect.Bottom-ARect.Top)/2)+3));
      ACanvas.Free;
    end;procedure TEditY.GetEditRealRect;
    var
      ARect:TRect;
    begin
      SetRect(ARect,0,0,ClientWidth,ClientHeight);
      if Focused and (FLinkStyle=IsButton) then
        DEC(Arect.Right,FButtonWidth);
       PerForm(EM_SETRECT,0,Longint(@ARect));
       Repaint;
    end;function TEditY.GetLinkStyle: TLinkStyle;
    begin
       Result:=FLinkStyle;
    end;procedure TEditY.loaded;
    begin
      inherited;
      Font.Name:='Times New Roman';
      Font.Size:=9;
      Font.Style:=[];
      Invalidate;
    end;
      

  5.   

    procedure TEditY.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
      Y: Integer);
    var
     R:TRect;
    begin
      SetRect(R,ClientWidth-FButtonWidth-2,1,ClientWidth-1,ClientHeight-1);
      if CheckPosition(X,Y) then
        begin
          FPress:=True;
          MouseCapture:=True;
        end;
       invalidateRect(Handle,@R,False);
       inherited;
    end;procedure TEditY.MouseMove(Shift: TShiftState; X, Y: Integer);
    var
     R:TRect;
    begin
      if not (CheckPosition(X,Y)) and FPress then
        begin
           FPress:=False;
           InvalidateRect(Handle,@R,False);
        end;
      inherited MouseMove(Shift, X, Y);
    end;procedure TEditY.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
      Y: Integer);
    var
      R:TRect;
    begin
      SetRect(R,ClientWidth-FButtonWidth-2,1,ClientWidth-1,ClientHeight-1);
      if CheckPosition(X,Y) then
         begin
           FPress:=False;
         end;
      Repaint;
      inherited;
    end;procedure TEditY.PaintWindow(DC: HDC);
    var
      R: TRect;
      ACanvas:TControlCanvas;
    begin
       ACanvas:=TControlCanvas.Create;
       TControlCanvas(ACanvas).Control:=Self;
       ACanvas.Handle:=DC;
       ACanvas.Brush.Color:=RGB(112,112,184);
       SetRect(R,0,0,Width,Height);
       ACanvas.FrameRect(R);
       if (FLinkStyle=IsButton) and ((csDesigning in ComponentState) or (Focused)) then
        begin
         ACanvas.Pen.Color:=RGB(145,145,200);
         SetRect(R,Width-FButtonWidth,1,Width-1,Height-1);
         ACanvas.Brush.Color:=clWindow;
         ACanvas.RoundRect(R.Left,r.Top,r.Right,r.Bottom,4,4);
         SetRect(R,ClientWidth-FButtonWidth+2,2,ClientWidth-3,ClientHeight-2);
         DrawButton(DC,R);
         ExcludeClipRect(DC,R.Left,R.Top,R.Right,R.Bottom);
       end;
      ACanvas.Free;
      inherited PaintWindow(DC);
    end;procedure TEditY.SetLinkStyle(Value: TLinkStyle);
    begin
      if Value = FLinkStyle then Exit;
      FLinkStyle := Value;
      if not HandleAllocated then Exit;
      if Focused or (csDesigning in ComponentState) then
         GetEditRealRect;
    end;procedure TEditY.WMLButtonDblClk(var Msg: TWMLButtonDblClk);begin
      if PtInRect(Rect(Self.ClientWidth-FButtonWidth,0,Self.ClientWidth,Self.ClientHeight),Point(Msg.XPos,Msg.YPos)) then
        Exit;
      inherited;
    end;procedure TEditY.WMPAINT(var message: TWMPaint);
    begin
      Inherited;
      PaintWindow(message.DC);
    end;procedure TEditY.WMSETCURSOR(var MSg: TWMSETCURSOR);
    var
      P:TPoint;
    begin
      GetCurSorPos(P);
      if (FLinkStyle=IsButton) then
        begin
          if PtInRect(Rect(ClientWidth-FButtonWidth,0,ClientWidth,Self.ClientHeight),ScreenToClient(P)) then
            Cursor:=crArrow
          else
           CurSor:=crDefault;
        end;
      inherited
    end;
    end.
      

  6.   

    自己新写一个类,继承自TEdit,在类里面声明一个按钮类的成员,然后的事情我想你就明白了