我想让Panel可改变大小,该怎么做?先谢了!:)

解决方案 »

  1.   

    是不是在程序中改变
    可以,但是有点烦的地方是 
    你需要自己计算panel的大小 你可以在程序中改变 panel 的大小 width 、height
      

  2.   

    procedure DragControl(WinControl: TWincontrol);
    const SC_DRAGMOVE = $F008;
    begin
      ReleaseCapture;
      WinControl.Perform(WM_SYSCOMMAND, SC_DRAGMOVE, 0);
    end;
      

  3.   

    以下是我做的一个可运行时修改大小位置的EDIT(有一部份是别人的代码),可以设置是否可移动你照着改就是
    unit Eliedit;interfaceuses
      SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
      Forms, Dialogs, StdCtrls, Buttons, Menus;type
    TValueType = (vtInteger,vtDate,vtTime,{vtDateTime,}vtString,vtCurrency,vtDouble);
      
      TElipsisEdit = class(TCustomMemo)  private
        { Private declarations }
         FMaskString   : String;
        FCaption:string;
        Fcolor:tcolor;
        FMtext:string;
        FSQL:TSTRINGS;
        Fhotkey:word;
        FInputMask    : String;
        FIsDown:           Boolean;
        FMouseDown:        Boolean;
        FState:            TButtonState;
        FStayDown:         Boolean;
        FMovable:          Boolean; { to make it movable at runtime}
        PosY,PosX:         integer; {mousepos on start drag}
        FAutoAligning : Boolean;
        FValueType    : TValueType;
        FOldValue     : String;
        Frequired:integer;
        FPrint:integer;
        FPrintLeft:integer;
        FPrintWidth:integer;
        FPrintTop:integer;
        FPrintHeight:integer;
        FTablename:string;
        FFieldName:string;
        FConsultTableName:string;
        FConsultFieldName:string;
        FDataBind:integer;
        FConsultAble:integer;
      protected
        { Protected declarations }
        FElipsis: TSpeedButton;
        FLabel:tlabel;
        function InsideBtn(X,Y: Integer): boolean;
        function  GetOnClick: TNotifyEvent;
        procedure SetOnClick(Value: TNotifyEvent);
        procedure UpdateFormatRect;
        procedure WMSize(var Msg: TWMSize); message WM_SIZE;
        procedure WMSetCursor(var Msg: TWMSetCursor); message WM_SETCURSOR;
        procedure CMEnabledChanged(var Msg: TWMNoParams);
          message CM_ENABLEDCHANGED;
        procedure CreateHandle; override;
        FUNction GetCaption:string;
        procedure setcaption(acaption:string);
        FUNction GetFcolor:tcolor;
        procedure setFcolor(afcolor:tcolor);
        FUNction getMtext:string;
        procedure setMtext(aMtext:string);
        FUNction getSQL:TstringS;
        procedure setSQL(const Value: TStrings);
        procedure wndproc(var message:Tmessage);override;
    procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
    procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
     procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
      procedure KeyPress(var Key: Char);override;
       procedure KeyDown(var Key: Word; Shift: TShiftState);override;
       function GetMasked: Boolean;
    function GetMaskString(Val: String): String;
    procedure SetInputMask(Val: String);
     function CharKeys(var CharCode: Char): Boolean;
     function DeleteSelection(PStr: String; SelStart,SelStop: Integer): String;
      procedure ArrowKeys(CharCode: Word; Shift: TShiftState);
      procedure GetSel(var SelStart: Integer; var SelStop: Integer);
     procedure CursorDec(CursorPos: Integer);
     procedure CursorInc(CursorPos: Integer);
      function GetNextEditChar(Offset: Integer): Integer;
      function IsMaskChars(Const NewChar: Char; Offset: Integer; var uPos: Integer): Boolean;
      procedure SetAutoAligning(Val: Boolean);
      procedure ValidateEdit(aPos: Integer);
       procedure ValidIt;
       function GetPriorEditChar(Offset: Integer): Integer;
        function AllTrim(Str: String): String;
        function SlashStr(Str,Str1: String; Long: Integer; FStyle: Boolean): String;
      function IsLiteralChar(const EditMask,Text: string; Offset: Integer): Boolean;
       procedure SetSel(SelStart: Integer; SelStop: Integer);
      function InputChar(var NewChar: Char; Offset: Integer): Boolean;
      procedure Reset;
      function DateToStrProc(FDate: TDateTime): String;
      function TimeToStrProc(FTime: TDateTime): String;
      procedure DeleteKeys(CharCode: Word);
      procedure CreateParams(var Params: TCreateParams); override;
      procedure SetValueType(Val: TValueType);
      function GetValue:Variant;
      procedure SetValue(aValue: Variant);
       function StrToDateProc(Str: String): TDateTime;
      public
        { Public declarations }
        constructor Create(AOwner: TComponent); override;
         procedure settwotext;
      published
        { Published declarations }
            property OnEnter;
            property IsMasked: Boolean read GetMasked;
        property OnExit;
        property Align;
        property Alignment;
        property AutoSelect;
        property AutoSize;
        property BorderStyle;
        property CharCase;
        property Color;
        property Ctl3D;
        property DragCursor;
        property DragMode;
        property Enabled;
        property Font;
        property HideSelection;
        property MaxLength;
        property OEMConvert;
        property ParentColor;
        property ParentCtl3D;
        property ParentFont;
        property ParentShowHint;
        property PasswordChar;
        property PopupMenu;
        property ReadOnly;
        property ShowHint;
        property TabOrder;
        property TabStop;
        property Text;
        property OnEndDrag;
        property Visible;
        property OnChange;
        property OnClick;
        property OnDblClick;
        property OnDragDrop;
        property OnDragOver;
        property OnKeyDown;
        property OnKeyPress;
        property OnKeyUp;
        property OnMouseDown;
        property OnMouseMove;
        property OnMouseUp;
        property MCaption:string read GetCaption write SetCaption;
        property OnElipsisClick: TNotifyEvent read GetOnClick write SetOnClick;
        property Mcolor:Tcolor read getFcolor write setFcolor default clBlue;
        property Mtext:string read getMtext write setMtext;
        PROPERTY MSQL:TstringS READ getSQL WRITE setSQL;
        property hotkey:word read fhotkey write fhotkey default VK_F2;
        property Movable: Boolean read FMovable write FMovable default False;
        property InputMask: String read FInputMask write SetInputMask;
         property Value: Variant read GetValue write SetValue;
        property AutoAligning: Boolean read FAutoAligning write SetAutoAligning default True;
      

  4.   

    太长了,可能就这一部份有用吧
    function  TElipsisEdit.GetOnClick: TNotifyEvent;
    begin
      Result := FElipsis.OnClick;
    end;  { TElipsisEdit.GetOnClick }
    procedure TElipsisEdit.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    var tmp:TPoint;Msg:TMsg;
    begin
      if not Enabled then Exit;
      inherited;
      if FIsDown then Exit
      else FIsDown := not FIsDown;  inherited MouseDown(Button,Shift,X,Y);   If FMovable then begin
         posX:= X;
         posY:= Y;
       end;   if Assigned(OnMouseDown) then OnMouseDown(Self,Button,Shift,X,Y);   if InsideBtn(X,Y) then
       begin
         FMouseDown := True;
         FState := bsDown;
         Repaint; 
       end;
    end;
    procedure TElipsisEdit.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin  if not Enabled then Exit;
      if not FIsDown then Exit
      else FIsDown := not FIsDown;
      if FStayDown then Exit; 
      inherited MouseUp(Button,Shift,X,Y);
      FMouseDown := False;
      FState := bsUp;
      Repaint;
      if Assigned(OnMouseUp) then OnMouseUp(Self,Button,Shift,X,Y);
    end;procedure TElipsisEdit.MouseMove(Shift: TShiftState; X, Y: Integer);
    begin
       inherited MouseMove(Shift,X,Y);
      if Assigned(OnMouseMove) then OnMouseMove(Self,Shift,X,Y);
       if FMouseDown then //检查鼠标是否按下
       begin
         if Movable then  //检查是否充许移动 
           begin 
            Top:=Top+Y-PosY; 
            Left:=Left+X-PosX;
            text:=name+': TOP:'+inttostr(TOP)+'  Left:'+inttostr(left);
            end; 
         if not InsideBtn(X,Y) then
         begin
           if FState = bsDown then
           begin
             FState := bsUp;
             Repaint;
           end;
         end
         else
         begin
           if FState = bsUp then
           begin
             FState := bsDown;
             Repaint; 
           end;
         end;
       end; 
    end;
      

  5.   

    procedure DragControl(WinControl:TWinControl);
    const
      SM=$F012;
    begin
      ReleaseCapture;
      WinControl.Perform(WM_SYSCOMMAND,SM,0);
    end;
    在你的OnMouseMove中调用此函数
      

  6.   

    不好意思,可能我没讲清楚,我的意思是:在运行期,当鼠标移动到PANEL边界上时,显示为可调节大小的状态,然后可拖动并改变PANEL的大小。TO:  xybh97102(冰点),ollea(一定要mf到胡萝卜) 
      可以改变控件的位置,即可拖动,但不能改变大小。是不是?
    TO: lifencheng(李奉城)
      兄台费心了,呵呵,我这就好好看一下:)
      

  7.   

    SC_DRAGMOVE = $F008改变大小
     SC_DRAGMOVE = $F012自由移动
      

  8.   

    TO:  ollea(一定要mf到胡萝卜) 
      不好意思,具体代码怎么写呢?ps: mf怎么解?呵呵(孤陋寡闻,见笑:)
      

  9.   

    TO:  ollea(一定要mf到胡萝卜)   我是说,我要在鼠标按下且鼠标再边界上时,才触发。应该怎么写?其实我不知道怎么判断鼠标是否按下。
      

  10.   

    用splitter很好控制啊,在窗体上放两个panel,一个align设置alright,放一个splitter,将align设置成alrigth,将另一个panel的align属性设置成alclient就可以用了.运行后拖splitter就可以改变它的大小。
      

  11.   

    to: hhzxf(因为有你)    这样局限性太大,控件的位置肯定不能altop,alright,alleft等。
      

  12.   

    我恰好做了一个这样的PanelPro(继承自TPanel)控件,需要的话我可以给你,以下是一些关键代码:
    1、首先重载MouseMove、WndProc过程:
      protected
        { Protected declarations }
        procedure WndProc(var Message: TMessage); override;
        procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
    2、
    procedure TPanelPro.WndProc(var Message: TMessage);
    begin
      case Message.Msg of
        WM_SIZE: begin inherited; InvalidateRect(Handle, nil, True); end;
        WM_SETFOCUS, WM_KILLFOCUS:
          Repaint;
        WM_ENTERSIZEMOVE:
          begin
            Cursor := crSizeAll;
            BringToFront;
          end;
        WM_EXITSIZEMOVE:
          begin
            Cursor := Parent.Cursor;
            if Assigned(FPosChanged) then FPosChanged(Self);
          end;
        WM_WINDOWPOSCHANGING:
          begin
            if FClipToParent then with PWINDOWPOS(Message.lParam)^ do
            begin
              if x < 0 then x := 0;
              if y < 0 then y := 0;
              if x + cx > Parent.ClientWidth then
                x := Parent.ClientWidth - cx;
              if y + cy > Parent.ClientHeight then
                y := Parent.ClientHeight - cy;
            end;
            if Assigned(FPosChanging) then FPosChanging(Self);
          end;
        WM_WINDOWPOSCHANGED:
          if Assigned(FPosChanged) then FPosChanged(Self);
      end;
      inherited WndProc(Message);
    end;
    procedure TPanelPro.MouseMove(Shift: TShiftState; X, Y: Integer);
    var ARect: TRect;
        nThumb, nBorder: Integer;
    begin
      inherited MouseMove(Shift, X, Y);
      if not (FCanMove or FCanSize) then Exit;
      CopyRect(ARect, ClientRect);
      nBorder := GetSystemMetrics(SM_CYFIXEDFRAME);
      nThumb := GetSystemMetrics(SM_CXHTHUMB);
      InflateRect(ARect, -nBorder, -nBorder);
      if FCanMove and PtInRect(ARect, Point(X, Y)) and (Align = alNone) then
      begin
        if Shift = [ssLeft] then
        begin
          ReleaseCapture;
          Perform(WM_SYSCOMMAND, SC_MOVEWINDOW, 0);
        end;
      end;
      if FCanSize then
      begin
        //左边
        if PtInRect(Rect(0, nThumb, nBorder, Height - nThumb), Point(X, Y)) then
          SizeWindow(Shift, SC_SIZELEFT, crSizeWE)
        else //上边
        if PtInRect(Rect(nThumb, 0, Width - nThumb, nBorder), Point(X, Y)) then
          SizeWindow(Shift, SC_SIZETOP, crSizeNS)
        else//下边
        if PtInRect(Rect(nThumb, Height - nBorder, Width - nThumb, Height), Point(X, Y)) then
          SizeWindow(Shift, SC_SIZEBOTTOM, crSizeNS)
        else //右边
        if PtInRect(Rect(Width - nBorder, nThumb, Width, Height - nThumb), Point(X, Y)) then
          SizeWindow(Shift, SC_SIZERIGHT, crSizeWE)
        else //左下
        if PtInRect(Rect(0, Height - nThumb, nBorder, Height), Point(X, Y)) or
           PtInRect(Rect(0, Height - nBorder, nThumb, Height), Point(X, Y)) then
          SizeWindow(Shift, SC_SIZELEFTBOTTOM, crSizeNESW)
        else //右下
        if PtInRect(Rect(Width - nThumb, Height - nBorder, Width, Height), Point(X, Y)) or
           PtInRect(Rect(Width - nBorder, Height - nThumb, Width, Height), Point(X, Y)) then
          SizeWindow(Shift, SC_SIZERIGHTBOTTOM, crSizeNWSE)
        else //左上
        if PtInRect(Rect(0, 0, nBorder, nThumb), Point(X, Y)) or
           PtInRect(Rect(0, 0, nThumb, nBorder), Point(X, Y)) then
          SizeWindow(Shift, SC_SIZELEFTTOP, crSizeNWSE)
        else //右上
        if PtInRect(Rect(Width - nThumb, 0, Width, nBorder), Point(X, Y)) or
           PtInRect(Rect(Width - nBorder, 0, Width, nThumb), Point(X, Y)) then
          SizeWindow(Shift, SC_SIZERIGHTTOP, crSizeNESW)
        else Cursor := Parent.Cursor;
      end;
    end;procedure TPanelPro.SizeWindow(Shift: TShiftState; nSize: Integer; ACursor: TCursor);
    begin
      Cursor := ACursor;
      if Shift = [ssLeft] then
      begin
        ReleaseCapture;
        Perform(WM_SYSCOMMAND, nSize, 0);
      end;
    end;
      

  13.   

    TO: : baguli(巴古里)   方便的话,把源吗发给我学习学习,谢谢!
       另开贴给分。
       [email protected]
      

  14.   

    楼主是butone的。偶的导师就在butone。呵呵,江开耀不知道你认识不。代码就是我给你回复里写的呀。mf的意思是magic find。呵呵.你不玩暗黑和warcraftIII?
      

  15.   

    procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      if button = mbLeft then ...
    end;
    这个就是判断鼠标左键是否按下。then后面加处理代码就可以了。
      

  16.   

    to: ollea(一定要mf到胡萝卜) 
      在MouseDown里可以判断鼠标是否按下,但在
      procedure DragControl(WinControl: TWincontrol);
      const SC_DRAGMOVE = $F008;
      begin
        ReleaseCapture;
        WinControl.Perform(WM_SYSCOMMAND, SC_DRAGMOVE, 0);
      end;
      执行之前,不应该判断鼠标是否按下吗?可是这个过程是在MouseMove里写的呀。
      

  17.   

    to: ollea(一定要mf到胡萝卜)    你有短消息!
      

  18.   

    多谢大家,我已经搞定了。你们的热诚让我感动!
     
    特别感谢: ollea(一定要mf到胡萝卜) ;baguli(巴古里) ; lifencheng(李奉城);  
              xybh97102  (冰点) 。 ps: lifencheng(李奉城); xybh97102  (冰点) 你们若给我发了源码,再开贴给分。
      

  19.   

    我毕业设计做的dbgrid自由移动缩放,你改成panel就可以了shift按下后移动,alt按下后改变大小,从你的问题看,最好在用户做完界面调整后将他的布局习惯存在ini中下回程序执行时候按用户的习惯布局。
    procedure TAnalyform.DBGrid3MouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
      begin
        if ssleft in Shift
          then
            begin
           ReleaseCapture;
           (Sender as TDBGrid).Perform(WM_SYSCOMMAND,$F012,0);
            end
       else if ssalt in shift
            then
            begin
            ReleaseCapture;
            (Sender as TDbGrid).Perform(WM_SYSCOMMAND,$F008,0);
            end;
      end;
      

  20.   

    下面的函数估计应该能满足你的要求,不知道你还需不需要,依然在OnMouseMove中调用
    procedure DragControl(Control: TControl; Shift: TShiftState; X, Y, Precision: integer);//Precision为鼠标在控件上的精度,试试就知道了
    var
      SC_MANIPULATE: Word;
    begin
      if (X<=Precision) and (Y>Precision) and (Y<Control.Height-Precision) then
      begin
        SC_MANIPULATE := $F001;
        Control.Cursor := crSizeWE;
      end
      else if (X>=Control.Width-Precision) and (Y>Precision) and (Y<Control.Height-Precision) then
      begin
        SC_MANIPULATE := $F002;
        Control.Cursor := crSizeWE;
      end
      else if (X>Precision) and (X<Control.Width-Precision) and (Y<=Precision) then
      begin
        SC_MANIPULATE := $F003;
        Control.Cursor := crSizeNS;
      end
      else if (X<=Precision) and (Y<=Precision) then
      begin
        SC_MANIPULATE := $F004;
        Control.Cursor := crSizeNWSE;
      end
      else if (X>=Control.Width-Precision) and (Y<=Precision) then
      begin
        SC_MANIPULATE := $F005;
        Control.Cursor := crSizeNESW ;
      end
      else if (X>Precision) and (X<Control.Width-Precision) and (Y>=Control.Height-Precision) then
      begin
        SC_MANIPULATE := $F006;
        Control.Cursor := crSizeNS;
      end
      else if (X<=Precision) and (Y>=Control.Height-Precision) then
      begin
        SC_MANIPULATE := $F007;
        Control.Cursor := crSizeNESW;
      end
      else if (X>=Control.Width-Precision) and (Y>=Control.Height-Precision) then
      begin
        SC_MANIPULATE := $F008;
        Control.Cursor := crSizeNWSE;
      end
      else if (X>5) and (Y>5) and (X<Control.Width-5) and (Y<Control.Height-5) then
      begin
        SC_MANIPULATE := $F009;
        Control.Cursor := crSizeAll;
      end else
      begin
        SC_MANIPULATE := $F000;
        Control.Cursor := crDefault;
      end;
      if Shift=[ssLeft] then
      begin
        ReleaseCapture;
        Control.Perform(WM_SYSCOMMAND, SC_MANIPULATE, 0);
      end;
    end;