cxcheckbox.pas

来自「胜天进销存源码,国产优秀的进销存」· PAS 代码 · 共 1,873 行 · 第 1/5 页

PAS
1,873
字号
      (cxbsNormal, cxbsHot, cxbsPressed, cxbsDisabled);
  var
    ACheckColor: TColor;
  begin
    cxEditFillRect(ACanvas.Handle, AContentRect,
      GetSolidBrush(GetCheckBoxContentColor));
    if (AState = cbsUnchecked) or
      (AState = cbsGrayed) and (AGrayedShowingStyle <> nssGrayedChecked) then
        Exit;
    if (ACheckState = ecsDisabled) or (AState = cbsGrayed) then
      ACheckColor := clBtnShadow
    else
      ACheckColor := clBtnText;
    DrawCheck(AContentRect, ACheckColor);
  end;

  procedure DrawWindowsCheckBoxContent(AContentRect: TRect);
  const
    ABorder3DStyleMap: array [Boolean] of Integer = (DFCS_FLAT, 0);
    AGrayedShowingStyleMap: array [TcxCheckBoxNullValueShowingStyle] of Integer =
      (0, DFCS_INACTIVE, DFCS_CHECKED);
  var
    AClipRgnExists: Boolean;
    AFlags: Integer;
    APrevClipRgn: HRGN;
  begin
    if ACheckState = ecsDisabled then
    begin
      AFlags := DFCS_BUTTON3STATE or DFCS_PUSHED;
      if (AState = cbsUnchecked) or ((AState = cbsGrayed) and (AGrayedShowingStyle <> nssGrayedChecked)) then
        AFlags := AFlags or DFCS_INACTIVE
      else
        AFlags := AFlags or DFCS_CHECKED;
    end
    else
    begin
      AFlags := 0;
      case AState of
        cbsGrayed:
          AFlags := DFCS_BUTTON3STATE or
            AGrayedShowingStyleMap[AGrayedShowingStyle];
        cbsChecked:
          AFlags := DFCS_CHECKED;
      end;
      if ACheckState = ecsPressed then
        AFlags := AFlags or DFCS_PUSHED;
    end;

    APrevClipRgn := CreateRectRgn(0, 0, 0, 0);
    AClipRgnExists := GetClipRgn(ACanvas.Handle, APrevClipRgn) = 1;
    with AContentRect do
      IntersectClipRect(ACanvas.Handle, Left, Top, Right, Bottom);
    InflateRect(AContentRect, cxEditMaxCheckBoxBorderWidth, cxEditMaxCheckBoxBorderWidth);

    DrawFrameControl(ACanvas.Handle, AContentRect, DFC_BUTTON, DFCS_BUTTONCHECK or AFlags or ABorder3DStyleMap[ABorderStyle = ebs3D]);

    if AClipRgnExists then
      SelectClipRgn(ACanvas.Handle, APrevClipRgn)
    else
      SelectClipRgn(ACanvas.Handle, 0);
    DeleteObject(APrevClipRgn);
  end;

  procedure DrawCheckBoxContent;
  var
    ACheckBoxBorderWidth: Integer;
    R: TRect;
  begin
    if ANativeStyle then
      Exit;

    ACheckBoxBorderWidth := cxEditMaxCheckBoxBorderWidth;
    R := ACheckRect;
    InflateRect(R, -ACheckBoxBorderWidth, -ACheckBoxBorderWidth);

    if ABorderStyle in [ebsUltraFlat, ebsOffice11] then
      InternalDrawCheckBoxContent(R)
    else
      DrawWindowsCheckBoxContent(R);
  end;

begin
  if not ADrawBackground then
    ABackgroundColor := clNone;
  if (AState = cbsGrayed) and (AGrayedShowingStyle = nssUnchecked) then
    AState := cbsUnchecked;

  if IsGlyphValid(AGlyph, AGlyphCount) then
    DrawCheckBoxGlyph
  else
  begin
    if APainter <> nil then
    begin
      if ADrawBackground then
        ACanvas.FillRect(ACheckRect, ABackgroundColor);
      APainter.DrawCheckButton(ACanvas, ACheckRect,
        CheckState2ButtonState[ACheckState], AState);
    end
    else
    begin
      DrawCheckBoxBorder;
      DrawCheckBoxContent;
    end;  
  end;
end;

function GetEditCheckBorderOffset(ACheckBorderStyle: TcxContainerBorderStyle;
  ANativeStyle, AHasGlyph: Boolean; APainter: TcxCustomLookAndFeelPainterClass): Integer;
begin
  if ANativeStyle or AHasGlyph or (APainter <> nil) then
    Result := 0
  else
    Result := cxContainerMaxBorderWidth - GetContainerBorderWidth(ACheckBorderStyle);
end;

function GetEditCheckBorderOffset(ACheckBorderStyle: TcxEditBorderStyle;
  ANativeStyle, AHasGlyph: Boolean; APainter: TcxCustomLookAndFeelPainterClass): Integer; overload;
begin
  Result := GetEditCheckBorderOffset(
    TcxContainerBorderStyle(ACheckBorderStyle), ANativeStyle, AHasGlyph, APainter);
end;

function GetEditCheckBorderOffset(ALookAndFeelKind: TcxLookAndFeelKind;
  ANativeStyle, AHasGlyph: Boolean; APainter: TcxCustomLookAndFeelPainterClass): Integer;
begin
  if ANativeStyle or AHasGlyph or (APainter <> nil) then
    Result := 0
  else
    Result := cxContainerMaxBorderWidth - GetContainerBorderWidth(ALookAndFeelKind);
end;

function GetEditCheckGlyphIndex(AState: TcxCheckBoxState;
  ACheckState: TcxEditCheckState; ASupportGrayed: Boolean;
  AGlyphCount: Integer): Integer;
var
  AStateCount: Integer;
begin
  AStateCount := Integer(High(TcxCheckBoxState)) -
    Integer(Low(TcxCheckBoxState)) + 1;
  if not ASupportGrayed and (AGlyphCount mod 3 <> 0) and (AGlyphCount mod 2 = 0) then
    Dec(AStateCount);
  case AState of
    cbsUnchecked:
      Result := 0;
    cbsChecked:
      Result := 1;
    else
      Result := 2;
  end;
  if ACheckState = ecsPressed then
    Inc(Result, AStateCount);
  if (Result >= AGlyphCount) and (Result > AStateCount - 1) then
    Result := Result mod AStateCount;
end;

function GetEditCheckSize(ACanvas: TcxCanvas; ANativeStyle: Boolean;
  AGlyph: TBitmap; AGlyphCount: Integer; APainter: TcxCustomLookAndFeelPainterClass): TSize;
var
  AHasGlyph: Boolean;
begin
    AHasGlyph := IsGlyphValid(AGlyph, AGlyphCount);
    if AHasGlyph then
    begin
      Result.cx := AGlyph.Width div AGlyphCount;
      Result.cy := AGlyph.Height;
    end
    else
      if APainter <> nil then
         Result := APainter.CheckButtonSize
      else
        if AreVisualStylesMustBeUsed(ANativeStyle, totButton) then
          with TcxWinXPLookAndFeelPainter do
            Result := CheckButtonSize
        else
          with TcxCustomLookAndFeelPainter do
            Result := CheckButtonSize;
end;

{ TcxCustomCheckBoxViewInfo }

procedure TcxCustomCheckBoxViewInfo.Assign(Source: TObject);
begin
  if Source is TcxCustomCheckBoxViewInfo then
    with Source as TcxCustomCheckBoxViewInfo do
    begin
      Self.CheckBoxState := CheckBoxState;
      Self.State := State;
    end;
  inherited Assign(Source);
end;

procedure TcxCustomCheckBoxViewInfo.DrawText(ACanvas: TcxCanvas);
begin
  DrawCheckBoxText(ACanvas, Text, Font, TextColor, TextRect, DrawTextFlags, IsTextEnabled);
end;

function TcxCustomCheckBoxViewInfo.GetUpdateRegion(AViewInfo: TcxContainerViewInfo): TcxRegion;
var
  AEquals: Boolean;
  ATempRgn: TcxRegion;
begin
  Result := inherited GetUpdateRegion(AViewInfo);
  if not(AViewInfo is TcxCustomCheckBoxViewInfo) then
    Exit;
  with TcxCustomCheckBoxViewInfo(AViewInfo) do
    AEquals := (Self.CheckBoxState = CheckBoxState) and (Self.State = State);
  if not AEquals then
  begin
    ATempRgn := TcxRegion.Create(CheckBoxRect);
    UniteRegions(Result, ATempRgn);
    ATempRgn.Free;
  end;
end;

function TcxCustomCheckBoxViewInfo.IsHotTrack: Boolean;
begin
  Result := True;
end;

function TcxCustomCheckBoxViewInfo.IsHotTrack(P: TPoint): Boolean;
begin
  Result := IsHotTrack;
end;

function TcxCustomCheckBoxViewInfo.NeedShowHint(ACanvas: TcxCanvas;
  const P: TPoint; out AText: TCaption; out AIsMultiLine: Boolean;
  out ATextRect: TRect): Boolean;
begin
  Result := False;
end;

procedure TcxCustomCheckBoxViewInfo.Offset(DX, DY: Integer);
begin
  inherited Offset(DX, DY);
  OffsetRect(CheckBoxRect, DX, DY);
  OffsetRect(FocusRect, DX, DY);
end;

function TcxCustomCheckBoxViewInfo.Repaint(AControl: TWinControl;
  const AInnerEditRect: TRect; AViewInfo: TcxContainerViewInfo = nil): Boolean;
var
  R: TRect;
begin
  Result := AControl.HandleAllocated;
  if not Result then
    Exit;

  Result := inherited Repaint(AControl, AInnerEditRect, AViewInfo);
  with TcxCustomCheckBoxViewInfo(AViewInfo) do
  begin
    Result := Result or (AViewInfo <> nil) and
      ((Self.CheckBoxState <> CheckBoxState) or (Self.State <> State));
    if (AViewInfo = nil) or (Self.CheckBoxState <> CheckBoxState) or (Self.State <> State) then
    begin
      R := Self.CheckBoxRect;
      OffsetRect(R, Self.Left, Self.Top);
      InternalInvalidate(AControl.Handle, R, cxEmptyRect, False);
    end;
  end;
end;

function TcxCustomCheckBoxViewInfo.IsTextEnabled: Boolean;
begin
  Result := IsContainerInnerControl or Enabled or IsTextColorAssigned or NativeStyle
end;

procedure TcxCustomCheckBoxViewInfo.InternalPaint(ACanvas: TcxCanvas);
begin
  DrawCustomCheckBox(ACanvas, Self);
end;

function TcxCustomCheckBoxViewInfo.GetEdit: TcxCustomCheckBox;
begin
  Result := TcxCustomCheckBox(FEdit);
end;

{ TcxCustomCheckBoxViewData }

procedure TcxCustomCheckBoxViewData.Calculate(ACanvas: TcxCanvas; const ABounds: TRect;
  const P: TPoint; Button: TcxMouseButton; Shift: TShiftState; AViewInfo: TcxCustomEditViewInfo;
  AIsMouseEvent: Boolean);

  procedure CalculateCheckRects(const ACheckSize: TSize);
  var
    ACaptionRect, ACheckBoxRect: TRect;
    AAlignment: TAlignment;
  begin
    AAlignment := TcxCustomCheckBoxViewInfo(AViewInfo).Alignment;
    ACheckBoxRect := AViewInfo.ClientRect;
    ACaptionRect := ACheckBoxRect;

    if IsInplace then
    begin
      if AAlignment = taLeftJustify then
      begin
        Dec(ACaptionRect.Right, 2);
        if not Properties.IsEmbeddedEdit then
          Inc(ACheckBoxRect.Left, 2);
      end
      else
        if AAlignment = taRightJustify then
        begin
          Inc(ACaptionRect.Left, 2);
          if not Properties.IsEmbeddedEdit then
            Dec(ACheckBoxRect.Right, 2);
        end;
      Inc(ACaptionRect.Top, EditContentParams.Offsets.Top);
      Dec(ACaptionRect.Bottom, EditContentParams.Offsets.Bottom);
    end
    else
    begin
      InflateRect(ACaptionRect, -2, -2);
      if not Properties.IsEmbeddedEdit and (not EmulateStandardControlDrawing or
        not AreVisualStylesMustBeUsed(AViewInfo.NativeStyle, totButton) and (Style.LookAndFeel.SkinPainter = nil)) then
          Dec(ACaptionRect.Top);
    end;
    if not (IsInplace or AreVisualStylesMustBeUsed(AViewInfo.NativeStyle, totButton) and Properties.IsEmbeddedEdit) then
      InflateRect(ACheckBoxRect, -2, -2);
      
    if ACheckBoxRect.Bottom - ACheckBoxRect.Top > ACheckSize.cy then
    begin
      Inc(ACheckBoxRect.Top, (ACheckBoxRect.Bottom - ACheckBoxRect.Top - ACheckSize.cy) div 2);
      ACheckBoxRect.Bottom := ACheckBoxRect.Top + ACheckSize.cy;
    end;
    if ACheckBoxRect.Right - ACheckBoxRect.Left > ACheckSize.cx then
      if AAlignment = taCenter then
      begin
        Inc(ACheckBoxRect.Left, (ACheckBoxRect.Right - ACheckBoxRect.Left - ACheckSize.cx) div 2);
        ACheckBoxRect.Right := ACheckBoxRect.Left + ACheckSize.cx;
      end
      else
        if AAlignment = taLeftJustify then
        begin
          ACheckBoxRect.Right := ACheckBoxRect.Left + ACheckSize.cx;
          ACaptionRect.Left := ACheckBoxRect.Right + 3;
        end
        else
        begin
          ACheckBoxRect.Left := ACheckBoxRect.Right - ACheckSize.cx;
          ACaptionRect.Right := ACheckBoxRect.Left - 3 + Integer(IsInplace and Properties.IsEmbeddedEdit);
        end
    else
      ACaptionRect.Right := ACaptionRect.Left;
    if IsRectEmpty(ACheckBoxRect) then
      ACheckBoxRect := cxEmptyRect;

    if not Enabled and not IsNativeStyle(Style.LookAndFeel) and
      (Style.LookAndFeel.SkinPainter = nil)
    then
    begin
      Inc(ACaptionRect.Right);
      Inc(ACaptionRect.Bottom);
    end;

    TcxCustomCheckBoxViewInfo(AViewInfo).TextRect := ACaptionRect;
    TcxCustomCheckBoxViewInfo(AViewInfo).CheckBoxRect := ACheckBoxRect;
  end;

  function GetCheckBoxBorderStyle(AEditHotState: TcxContainerHotState): TcxEditBorderStyle;
  const
    ABorderStyles: array[TcxContainerHotState, TcxEditBorderStyle] of TcxEditBorderStyle = (
      (ebsNone, ebsSingle, ebsThick, ebsFlat, ebs3D, ebsUltraFlat, ebsOffice11),
      (ebsNone, ebsSingle, ebsThick, ebsFlat, ebs3D, ebsUltraFlat, ebsOffice11),
      (ebsFlat, ebsThick, ebsThick, ebs3D, ebs3D, ebsUltraFlat, ebsOffice11)
    );
  begin
    Result := ABorderStyles[AEditHotState, Style.BorderStyle];
  end;

var
  ACheckBoxViewInfo: TcxCustomCheckBoxViewInfo;  
begin
  inherited Calculate(ACanvas, ABounds, P, Button, Shift, AViewInfo, AIsMouseEvent);

  ACheckBoxViewInfo := TcxCustomCheckBoxViewInfo(AViewInfo);

⌨️ 快捷键说明

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