dcmain.pas.svn-base

来自「TFormDesigner allows you move and resize」· SVN-BASE 代码 · 共 2,066 行 · 第 1/5 页

SVN-BASE
2,066
字号
procedure TDesignAccessComponent.SetDesigningPublic(const Value: Boolean);
begin
  SetDesigning(Value);
end;

procedure TCustomDesignerComponent.SetActive(const Value: Boolean);
var
  i: Integer;
begin
  if FDesignTime then FActive:=Value
  else
    if Value<>FActive then
    begin
      FActive:=Value;
      if Assigned(ParentForm) then
      begin
        if FActive then
        begin
          FGrabHandles:=TGrabHandles.Create(Self);
          Update;
        end
        else
        begin
          FGrabHandles.Free;
          FGrabHandles:=nil;
        end;
        with ParentForm do
        begin
          if FActive then
          begin
            DisableAutoRange;
            if caEditable in ComponentAttributes(ActiveControl) then ActiveControl:=nil;
            for i:=0 to Pred(ComponentCount) do
              if Components[i] is TWinControl then
                with TWinControl(Components[i]) do
                begin
                  EnableWindow(Handle,True);
                  ShowWindow(Handle,SW_SHOW);
                end;
            CreateContainers;
            if apAutoShow in FAlignmentPalette then ShowAlignmentPalette;
            Designer:=TDesignerInterface.Create(Self);
          end
          else
          begin
            HideAlignmentPalette;
            for i:=0 to Pred(SelectedControlCount) do RemoveMultiSelect(SelectedControls[i]);
            FSelected.Clear;
            {$IFDEF DESIGNERASCLASS}
            Designer.Free;
            {$ENDIF}
            Designer:=nil;
            for i:=0 to Pred(ComponentCount) do
              if Components[i] is TWinControl then
                with TWinControl(Components[i]) do
                begin
                  EnableWindow(Handle,Enabled);
                  if not Visible then ShowWindow(Handle,SW_HIDE);
                end;
            DestroyContainers;
            EnableAutoRange;
            Screen.Cursor:=crDefault;
          end;
          ProcessProtected(ParentForm);
          RedrawWindow(Handle,nil,0,RDW_INVALIDATE or RDW_ERASE or RDW_ALLCHILDREN);
        end;
      end;
    end;
end;

procedure TCustomDesignerComponent.SetShowNonVisual(const Value: Boolean);
begin
  if Value<>FShowNonVisual then
  begin
    FShowNonVisual:=Value;
    if FShowNonVisual then CreateContainers
    else DestroyContainers;
  end;
end;

procedure TCustomDesignerComponent.SetAlignmentPalette(Value: TAlignmentPaletteOptions);
begin
  FAlignmentPalette:=Value;
  if Assigned(FAPForm) then
    with TfrmAlignmentPalette(FAPForm) do
    begin
      if apStayOnTop in FAlignmentPalette then FormStyle:=fsStayOnTop
      else FormStyle:=fsNormal;
      ShowHint:=apShowHints in FAlignmentPalette;
      SetFlatButtons(apFlatButtons in FAlignmentPalette);
    end;
end;

procedure TCustomDesignerComponent.SetDisplayGrid(const Value: Boolean);
begin
  if Value<>FDisplayGrid then
  begin
    FDisplayGrid:=Value;
    if Assigned(ParentForm) then ParentForm.Invalidate;
  end;
end;

procedure TCustomDesignerComponent.SetGridStep(const Value: Integer);
var
  Val: Integer;
begin
  Val:=Value;
  if Val<2 then Val:=2;
  if Val>128 then Val:=128;
  if Val<>FGridStep then
  begin
    FGridStep:=Val;
    if Assigned(ParentForm) and FActive then ParentForm.Invalidate;
  end;
end;

procedure TCustomDesignerComponent.SetGridColor(const Value: TColor);
begin
  if Value<>FGridColor then
  begin
    FGridColor:=Value;
    if FGridColor=clNone then FGridColor:=clWindowText;
    if Assigned(ParentForm) and FActive then ParentForm.Invalidate;
  end;
end;

procedure TCustomDesignerComponent.SetDesignerColor(const Value: TColor);
begin
  if Value<>FDesignerColor then
  begin
    FDesignerColor:=Value;
    if FDesignerColor=clNone then FDesignerColor:=clWindow;
    if Assigned(ParentForm) and FActive then ParentForm.Invalidate;
  end;
end;

procedure TCustomDesignerComponent.SetGrabSize(const Value: Integer);
var
  Val: Integer;
begin
  Val:=Value;
  if Val<3 then Val:=3;
  if Val>32 then Val:=32;
  if Val<>FGrabSize then FGrabSize:=Val;
end;

procedure TCustomDesignerComponent.SetNormalGrabBorder(const Value: TColor);
begin
  if Value<>FNormalGrabBorder then FNormalGrabBorder:=Value;
end;

procedure TCustomDesignerComponent.SetNormalGrabFill(const Value: TColor);
begin
  if Value<>FNormalGrabFill then FNormalGrabFill:=Value;
end;

procedure TCustomDesignerComponent.SetLockedGrabBorder(const Value: TColor);
begin
  if Value<>FLockedGrabBorder then FLockedGrabBorder:=Value;
end;

procedure TCustomDesignerComponent.SetLockedGrabFill(const Value: TColor);
begin
  if Value<>FLockedGrabFill then FLockedGrabFill:=Value;
end;

procedure TCustomDesignerComponent.SetMultiGrabBorder(const Value: TColor);
begin
  if Value<>FMultiGrabBorder then FMultiGrabBorder:=Value;
end;

procedure TCustomDesignerComponent.SetMultiGrabFill(const Value: TColor);
begin
  if Value<>FMultiGrabFill then FMultiGrabFill:=Value;
end;

function TCustomDesignerComponent.FindHandle(AComponent: TComponent): TGrabPosition;
begin
  if Assigned(FGrabHandles) then Result:=FGrabHandles.FindHandle(AComponent)
  else Result:=gpNone;
end;

function TCustomDesignerComponent.IsGrabHandle(AComponent: TComponent): Boolean;
begin
  Result:=FindHandle(AComponent)<>gpNone;
end;

procedure TCustomDesignerComponent.HideGrabs;
begin
  if Assigned(FGrabHandles) then FGrabHandles.Visible:=False;
end;

procedure TCustomDesignerComponent.ShowGrabs;
begin
  if Assigned(FGrabHandles) then FGrabHandles.Visible:=SelectedControlCount=1;
end;

procedure TCustomDesignerComponent.ShowHint(AHint: string; Mode: THintMode);
var
  R: TRect;
  P: TPoint;
  Offset: Integer;
begin
  if FShowMoveSizeHint then
    with FHintWindow do
    begin
      R:=CalcHintRect(255,AHint,nil);
      GetCursorPos(P);
      if Mode=hmMove then Offset:=18
      else Offset:=8;
      OffsetRect(R,P.X,P.Y+Offset);
      ActivateHint(R,AHint);
    end;
end;

procedure TCustomDesignerComponent.HideHint;
begin
  FHintWindow.ReleaseHandle;
end;

procedure TCustomDesignerComponent.DrawDragRects;
var
  i: Integer;
  DR: TRect;
  Offset: TPoint;
  Canvas: TCanvas;
begin
  if Assigned(ParentForm) and Assigned(Control) then
  begin
    Canvas:=TCanvas.Create;
    try
      with Canvas do
      begin
        Handle:=GetDCEx(Control.Parent.Handle,0,DCX_CACHE or DCX_CLIPSIBLINGS or DCX_PARENTCLIP);
        try
          Offset.X:=FDragRect.Left-Control.Left;
          Offset.Y:=FDragRect.Top-Control.Top;
          with Control.Parent do
          begin
            DR:=ClientRect;
            if GetParent(ParentForm.Handle)<>0 then
              MapWindowPoints(0,GetParent(ParentForm.Handle),DR,2);
          end;
          with Pen do
          begin
            Style:=psSolid;
            Mode:=pmXor;
            Width:=2;
            Color:=clGray;
          end;
          Brush.Style:=bsClear;
          for i:=0 to Pred(SelectedControlCount) do
          begin
            if i=0 then DR:=FDragRect
            else
            begin
              DR:=SelectedControls[i].BoundsRect;
              OffsetRect(DR,Offset.X,Offset.Y);
            end;
            with DR do Rectangle(Left+1,Top+1,Right,Bottom);
          end;
        finally
          ReleaseDC(0,Handle);
          Handle:=0;
        end;
      end;
    finally
      Canvas.Free;
    end;
  end;
end;

procedure TCustomDesignerComponent.DrawSelectRect;
var
  SR: TRect;
  Canvas: TCanvas;
begin
  if Assigned(ParentForm) then
  begin
    Canvas:=TCanvas.Create;
    try
      with Canvas do
      begin
        Handle:=GetDCEx(ParentForm.Handle,0,DCX_CACHE or DCX_CLIPSIBLINGS);
        try
          with Pen do
          begin
            Style:=psSolid;
            Mode:=pmXor;
            Width:=1;
            Style:=psDot;
            Color:=clWhite;
          end;
          Brush.Style:=bsClear;
          SR:=FSelectRect;
          MapWindowPoints(0,ParentForm.Handle,SR,2);
          with SR do Rectangle(Left+1,Top+1,Right,Bottom);
        finally
          ReleaseDC(0,Handle);
          Handle:=0;
        end;
      end;
    finally
      Canvas.Free;
    end;
  end;
end;

procedure TCustomDesignerComponent.DrawMultiSelect(AControl: TControl);
var
  R: TRect;
  Canvas: TCanvas;
begin
  Canvas:=TCanvas.Create;
  with Canvas do
  try
    if AControl is TWinControl then Handle:=GetDC(TWinControl(AControl).Handle)
    else Handle:=GetDC(AControl.Parent.Handle);
    R:=AControl.ClientRect;
    if not (AControl is TWinControl) then
      with AControl do OffsetRect(R,Left,Top);
    with R do
    begin
      PaintGrab(Canvas,Rect(Left,Top,Left+FGrabSize,Top+FGrabSize),gtMulti,gpLeftTop);
      PaintGrab(Canvas,Rect(Left,Bottom-FGrabSize,Left+FGrabSize,Bottom),gtMulti,gpLeftBottom);
      PaintGrab(Canvas,Rect(Right-FGrabSize,Top,Right,Top+FGrabSize),gtMulti,gpRightTop);
      PaintGrab(Canvas,Rect(Right-FGrabSize,Bottom-FGrabSize,Right,Bottom),gtMulti,gpRightBottom);
    end;
  finally
    Handle:=0;
    Free;
  end
end;

procedure TCustomDesignerComponent.RemoveMultiSelect(AControl: TControl);
var
  R: TRect;
  Rgn,Rgn2: HRGN;
begin
  if Assigned(AControl) and Assigned(AControl.Parent) then
  begin
    R:=AControl.ClientRect;
    if not (AControl is TWinControl) then
      with AControl do OffsetRect(R,Left,Top);
    with R do
    begin
      Rgn:=CreateRectRgn(Left,Top,Left+FGrabSize,Top+FGrabSize);
      try
        Rgn2:=CreateRectRgn(Left,Top,Left+FGrabSize,Top+FGrabSize);
        try
          CombineRgn(Rgn,Rgn,Rgn2,RGN_OR);
        finally
          DeleteObject(Rgn2);
        end;
        Rgn2:=CreateRectRgn(Left,Bottom-FGrabSize,Left+FGrabSize,Bottom);
        try
          CombineRgn(Rgn,Rgn,Rgn2,RGN_OR);
        finally
          DeleteObject(Rgn2);
        end;
        Rgn2:=CreateRectRgn(Right-FGrabSize,Top,Right,Top+FGrabSize);
        try
          CombineRgn(Rgn,Rgn,Rgn2,RGN_OR);
        finally
          DeleteObject(Rgn2);
        end;
        Rgn2:=CreateRectRgn(Right-FGrabSize,Bottom-FGrabSize,Right,Bottom);
        try
          CombineRgn(Rgn,Rgn,Rgn2,RGN_OR);
        finally
          DeleteObject(Rgn2);
        end;
        if AControl is TWinControl then
          InvalidateRgn(TWinControl(AControl).Handle,Rgn,True)
        else
          InvalidateRgn(AControl.Parent.Handle,Rgn,True);
      finally
        DeleteObject(Rgn);
      end;
    end;
  end;
end;

procedure TCustomDesignerComponent.CheckParent(MainControl: TControl);
var
  i: Integer;
begin
  if Assigned(MainControl) then
  begin
    i:=0;
    while i<SelectedControlCount do
      if SelectedControls[i].Parent<>MainControl.Parent then
      begin
        if not DeselectControl(SelectedControls[i]) then Inc(i);
      end
      else Inc(i);
  end;
end;

procedure TCustomDesignerComponent.ProcessProtected(ARoot: TComponent);
var
  i: Integer;
begin
  if not (ARoot is TCustomDesignerComponent) then
    TDesignAccessComponent(ARoot).SetDesigningPublic(not (caProtected in ComponentAttributes(ARoot)) and FActive);
  if ARoot is TCustomForm then
    with TCustomForm(ARoot) do
      if FActive then DisableAutoRange
      else EnableAutoRange;
  with ARoot do
    for i:=0 to Pred(ComponentCount) do ProcessProtected(Components[i]);
end;

function TCustomDesignerComponent.IsPressed(Key: Word): Boolean;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?