http://community.csdn.net/Expert/topic/3082/3082833.xml?temp=.2934076请高手帮忙解决!

解决方案 »

  1.   

    Edit&Memo控件的文本得对齐方式    
        
    如何使TEDIT控件中的TEXT显示在控件的中间或下面(一般在上面),像TLABEL中的LAYOUT至为CENTER一样varRct: TRect;beginRct := edit1.ClientRect;Inc(Rct.Top, 3);sendmessage(edit1.handle, EM_SETRECT, 0, Integer(@Rct));end;EM_SETRECT 只对于 Multiline Edit 有效? :-)An application sends an EM_SETRECT message to set the formatting rectangle of amultiline edit control.换成 Memo 就可以了。procedure TForm1.Button2Click(Sender: TObject);varRct: TRect;beginSendMessage(Memo1.Handle, EM_GETRECT, 0, Integer(@Rct));Inc(Rct.Top, 5);SendMessage(Memo1.Handle, EM_SETRECT, 0, Integer(@Rct));end;那也简单, 那些语句前加一句SetWindowLong(edit.Handle,GWL_STYLE, GetWindowLong(edit.Handle, GWL_STYLE) or ES_MULTILINE);不过需要多写个OnKeyPress事件过滤掉输入的回车符.最好自己继承TEdit做个有这功能的控件. 事实上很简单的.unit JackEdit;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;typeTKeySet = (ksReturn, ksEscape);TKeySets = set of TKeySet;TJackEdit = class(TEdit)private{ Private declarations }FAlignment: TAlignment;FKeys: TKeySets;procedure SetAlignment(value: TAlignment);protected{ Protected declarations }procedure CreateParams(var Params: TCreateParams); override;procedure KeyPress(var Key: Char); override;public{ Public declarations }constructor Create(AOwner: TComponent); override;destructor Destroy; override;published{ Published declarations }property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;property Keys: TKeySets read FKeys write FKeys;end; procedure Register;implementationprocedure TJackEdit.SetAlignment(value: TAlignment);beginif value <> FAlignment thenbeginFAlignment := value;RecreateWnd;end;end;procedure TJackEdit.KeyPress(var Key: Char);beginif ksReturn in Keys thenbeginif Key = Chr(Vk_Return) thenbeginKey := Chr(0);(Owner as TControl).Perform(wm_NextDlgCtl,0,0);end;end;if ksEscape in Keys thenbeginif Key = Chr(Vk_Escape) thenbeginKey := Chr(0);(Owner as TControl).Perform(wm_NextDlgCtl,1,0);end;end;inherited KeyPress(Key);end;procedure TJackEdit.CreateParams(var Params: TCreateParams);begininherited CreateParams(Params);case Alignment oftaLeftJustify : Params.Style := Params.Style or (ES_LEFT or Es_MULTILINE);taRightJustify : Params.Style := Params.Style or (ES_RIGHT or ES_MULTILINE);taCenter : Params.Style := Params.Style or (ES_CENTER or Es_MULTILINE);end;end;constructor TJackEdit.Create(AOwner: TComponent);begininherited Create(AOwner);FAlignment := taLeftJustify;end;destructor TJackEdit.Destroy;begininherited Destroy;end;procedure Register;beginRegisterComponents('Jack', [TJackEdit]);end;end.
     
       
      

  2.   

    to lwk_hlj(阿凯(为双星努力)) :
       你好!谢谢你粘贴!但是粘贴之前最好自己先浏览一遍!
      

  3.   

    以下是我自已在透明的TMemo中的TextLayou属性,可以实现靠上靠下居中功能,但是就是在设置TextLayout属性时候Memo却无法透明了,这里人多,哪位高手帮我看一下啊?
    unit UsrMemo;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ComCtrls;type
      TCtrl = class(TWinControl);  TTextLayout = (tlTop, tlCenter, tlBottom);  TUsrMemo = class(TMemo)
      private
        FTransparent: Boolean; //透明
        FPainting: Boolean;
        FModiFlag:Boolean;   
        FCanvas:TCanvas;
        FTextLayout:TTextLayout; //靠上靠下居中
        procedure SetTransparent(Value: Boolean);
        procedure SetModiFlag(Value:Boolean);
        procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
        procedure WMNCPaint(var Message: TMessage); message WM_NCPAINT;
        procedure WMEraseBkGnd(var Message: TWMEraseBkGnd); message WM_ERASEBKGND;
        procedure CNCtlColorEdit(var Message: TWMCtlColorEdit); message CN_CTLCOLOREDIT;
        procedure CNCtlColorStatic(var Message: TWMCtlColorStatic); message CN_CTLCOLORSTATIC;
        procedure CMParentColorChanged(var Message: TMessage); message CM_PARENTCOLORCHANGED;
        procedure WMSize(var Message: TWMSize); message WM_SIZE;
        procedure WMMove(var Message: TWMMove); message WM_MOVE;
        function  GetTextLayout: TTextLayout;
        procedure SetTextLayout(Value: TTextLayout);
        procedure DrawText;
      protected
        procedure RepaintWindow;
        procedure Change; override;
        procedure SetParent(AParent: TWinControl); override;
        function GetMemoText: string; virtual;  //得到内容
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
        property Canvas: TCanvas read FCanvas;
      published
        property Transparent: Boolean read FTransparent write SetTransparent default false;
        property ModiFlag:Boolean read FModiFlag write SetModiFlag default False;
        property TextLayout: TTextLayout read GetTextLayout write SetTextLayout default tlTop;
      end;procedure Register;
    implementationconst
      BorderRec: array[TBorderStyle] of Integer = (1, -1);procedure Register;
    begin
      RegisterComponents('EU', [TUsrMemo]);
    end;function GetScreenClient(Control: TControl): TPoint;
    var
      p: TPoint;
    begin
      p := Control.ClientOrigin;
      ScreenToClient(Control.Parent.Handle, p);
      Result := p;
    end;constructor TUsrMemo.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      FTransparent := False;
      FPainting := false;
      FCanvas := TControlCanvas.Create;
      TControlCanvas(FCanvas).Control := Self;
    end;destructor TUsrMemo.Destroy;
    begin
      inherited Destroy;
    end;procedure TUsrMemo.SetTransparent(Value: Boolean);
    begin
      if FTransparent <> Value then
      begin
        FTransparent := Value;
        Invalidate;
      end;
    end;procedure TUsrMemo.SetModiFlag(Value:Boolean);
    begin
      if FModiFlag <> Value then
      begin
        FModiFlag :=Value;
        Invalidate;
      end;
    end;procedure TUsrMemo.WMEraseBkGnd(var Message: TWMEraseBkGnd);
    var
      DC: hDC;
      i: integer;
      p: TPoint;
    begin
      if FTransparent then
      begin
        if Assigned(Parent) then
        begin
          DC := Message.DC;
          i := SaveDC(DC);  
          p := GetScreenClient(self);
          p.x := -p.x;
          p.y := -p.y;
          MoveWindowOrg(DC, p.x, p.y);
          SendMessage(Parent.Handle, $0014, DC, 0);
          TCtrl(Parent).PaintControls(DC, nil);
          RestoreDC(DC, i);
        end;
      end
      else
        inherited;
    end;procedure TUsrMemo.WMPaint(var Message: TWMPaint);
    begin
      inherited;
      if FTransparent then
        if not FPainting then
          RepaintWindow;
    end;procedure TUsrMemo.WMNCPaint(var Message: TMessage);
    begin
      inherited;
    end;procedure TUsrMemo.CNCtlColorEdit(var Message: TWMCtlColorEdit);
    begin
      inherited;
      if FTransparent then
        SetBkMode(Message.ChildDC, 1);
    end;procedure TUsrMemo.CNCtlColorStatic(var Message: TWMCtlColorStatic);
    begin
      inherited;
      if FTransparent then
        SetBkMode(Message.ChildDC, 1);
    end;
    procedure TUsrMemo.CMParentColorChanged(var Message: TMessage);
    begin
      inherited;
      if FTransparent then
        Invalidate;
    end;procedure TUsrMemo.WMSize(var Message: TWMSize);
    begin
      inherited;
      Invalidate;
    end;procedure TUsrMemo.WMMove(var Message: TWMMove);
    begin
      inherited;
      Invalidate;
    end;procedure TUsrMemo.RepaintWindow;
    var
      DC: hDC;
      TmpBitmap, Bitmap: hBitmap;
    begin
      if FTransparent then
      begin
        FPainting := true;
        HideCaret(Handle);
        DC := CreateCompatibleDC(GetDC(Handle));
        TmpBitmap := CreateCompatibleBitmap(GetDC(Handle), Succ(ClientWidth), Succ(ClientHeight));
        Bitmap := SelectObject(DC, TmpBitmap);
        PaintTo(DC, 0, 0);
        BitBlt(GetDC(Handle), BorderRec[BorderStyle], BorderRec[BorderStyle], ClientWidth, ClientHeight, DC, 1, 1,
          SRCCOPY);
        SelectObject(DC, Bitmap);
        DeleteDC(DC);
        ReleaseDC(Handle, GetDC(Handle));
        DeleteObject(TmpBitmap);
        ShowCaret(Handle);
        FPainting := false;
      end;
    end;procedure TUsrMemo.Change;
    begin
      RepaintWindow;
      inherited Change;
    end;procedure TUsrMemo.SetParent(AParent: TWinControl);
    begin
      inherited SetParent(AParent);
    end;function TUsrMemo.GetTextLayout: TTextLayout;
    begin
      Result := FTextLayout;
    end;procedure TUsrMemo.SetTextLayout(Value:TTextLayout); //Layout属性
    begin
      FTextLayout:=Value;
      DrawText;
    end;function TUsrMemo.GetMemoText: string;
    begin
      Result :=Trim(Text);
    end;procedure TUsrMemo.DrawText;     //垂直对齐
    var
      Rct: TRect;
      YOffset:integer;
      Text:string;
    begin
      Text :=GetMemoText;
      case FTextLayout of
        tlTop: YOffset := 0;
        tlCenter: YOffset := (ClientHeight - Canvas.TextHeight(Text)) div 2 + 1;
        tlBottom: YOffset := ClientHeight - Canvas.TextHeight(Text);
      else
        YOffset := 0;
      end;
      Rct:=Rect(0,0,ClientWidth,ClientHeight);
      Canvas.TextRect(Rct,0,YOffset,Text);       
    end;end.