unit Unit_CaptionBar;interfaceuses
  SysUtils,Windows,Forms,Classes,Controls,ComCtrls,StdCtrls,ExtCtrls,Messages;type
  TCaptionBar=class(TCustomPanel)
  private
    FOnMouseDown: TMouseEvent;
    procedure DoMouseDown(var Message: TWMMouse; Button: TMouseButton;
    Shift: TShiftState);
    procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
  protected
    procedure Notification(AComponent:TComponent;Operation:TOperation); override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
    X, Y: Integer); override;
  public
    constructor Create(AOwner:TComponent);override;
    property OnMouseDown: TMouseEvent read FOnMouseDown write FOnMouseDown;
  end;
{ TCaptionBar }procedure Register;implementationprocedure Register;
begin
  RegisterComponents('CaptionBar',[TCaptionBar]);
end;constructor TCaptionBar.Create(AOwner: TComponent);
begin
  inherited;
  Top:=0;
  Align:=alTop;
  Height:=19;
end;procedure TCaptionBar.DoMouseDown(var Message: TWMMouse;
  Button: TMouseButton; Shift: TShiftState);
begin
  if not (csNoStdEvents in ControlStyle) then
    with Message do
      if (Width > 32768) or (Height > 32768) then
        with CalcCursorPos do
          MouseDown(Button, KeysToShiftState(Keys) + Shift, X, Y)
      else
        MouseDown(Button, KeysToShiftState(Keys) + Shift, Message.XPos, Message.YPos);
end;procedure TCaptionBar.MouseDown(Button: TMouseButton; Shift: TShiftState;
  X, Y: Integer);
begin
  inherited;
  if (Self.Owner is TForm) then
  begin
    ReleaseCapture;
    (Self.Owner as TForm).Perform(WM_SYSCOMMAND,$F012,0);
  end;
end;procedure TCaptionBar.Notification(AComponent: TComponent;
  Operation: TOperation);
var
  AForm:TForm;
begin
  inherited;
  if not (csDesigning in ComponentState) then
  begin
    //这段代码有问题,大家帮帮忙啊,关闭窗体时报错
    if (Self.Owner is TForm) then
    begin
      AForm:=(Self.Owner as TForm);
      SetWindowLong(AForm.Handle,GWL_STYLE,GetWindowLong(AForm.Handle, GWL_STYLE) and (not WS_CAPTION));
      AForm.Width:=AForm.ClientWidth;
      AForm.Height:=AForm.ClientHeight;
    end;
    //先谢谢了
  end;
end;procedure TCaptionBar.WMLButtonDown(var Message: TWMLButtonDown);
begin
  SendCancelMode(Self);
  inherited;
  if csCaptureMouse in ControlStyle then MouseCapture := True;
  DoMouseDown(Message, mbLeft, []);
end;
end.

解决方案 »

  1.   

    楼主其实应该可以注意到,Operation属性标志着不同时机的调用。最简单的就是在Operation为opRemove的时候,跳过就可以了。if not (csDesigning in ComponentState) and (Operation <> opRemove) then {xiammy: ...}
    begin
      // ...
    end;
      

  2.   

    试试:if (Self.Owner is TForm) and (Operation = opInsert) then
    begin
    AForm:=(Self.Owner as TForm);
    SetWindowLong(AForm.Handle,GWL_STYLE,GetWindowLong(AForm.Handle, GWL_STYLE) and (not WS_CAPTION));
    AForm.Width:=AForm.ClientWidth;
    AForm.Height:=AForm.ClientHeight;
    end;