cxchecklistbox.pas

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

PAS
2,093
字号
          Result := ecsPressed
        else
          Result := ecsNormal;
  end;

  procedure DrawCheckRegion(const ACheckRegion: TRect; AIsItemEnabled: Boolean);

    function GetGlyphRect: TRect;
    var
      ACheckBorderOffset, AGlyphOffset: Integer;
      ACheckSize: TSize;
    begin
      AGlyphOffset := FInnerCheckListBox.Metrics.ContentOffset +
        FInnerCheckListBox.Metrics.ImageFrameWidth;
      if ShowChecks and (ImageLayout = ilAfterChecks) then
      begin
        FInnerCheckListBox.GetCheckMetrics(ACheckSize, ACheckBorderOffset);
        ACheckSize.cx := ACheckSize.cx - ACheckBorderOffset * 2;
        Inc(AGlyphOffset, ACheckSize.cx + FInnerCheckListBox.Metrics.CheckFrameWidth * 2);
      end;

      with ACheckRegion do
      begin
        Result.Top := Top + (Bottom - Top - Images.Height) div 2;
        Result.Bottom := Result.Top + Images.Height;
        if UseRightToLeftAlignment then
        begin
          Result.Right := Right - AGlyphOffset;
          Result.Left := Result.Right - Images.Width;
        end
        else
        begin
          Result.Left := Left + AGlyphOffset;
          Result.Right := Result.Left + Images.Width;
        end;
      end;
    end;

  var
    AImageIndex: Integer;
  begin
    if ShowChecks then
      FInnerCheckListBox.DrawCheck(ACheckRegion, Items[Index].State,
        GetCheckState(AIsItemEnabled));
    AImageIndex := Items[Index].ImageIndex;
    if VerifyImages(Images) and (AImageIndex <> -1) and (AImageIndex < Images.Count) then
      DrawGlyph(FInnerCheckListBox.Canvas, Images, Items[Index].ImageIndex,
      GetGlyphRect, Color, AIsItemEnabled);
  end;

const
  ADrawTextAlignmentFlags: array[Boolean] of LongWord = (DT_LEFT, DT_RIGHT);
var
  ABaseTestFlag: LongWord;
  ACheckRegion, ATextRect, ADrawEventRect: TRect;
  ADrawTextParams: DrawTextParams;
  AEnabled: Boolean;
  FText: string;
begin
  if Index < Items.Count then
  begin
    FInnerCheckListBox.Canvas.Font.Assign(ActiveStyle.GetVisibleFont);
    ACheckRegion := Rect;
    ATextRect := Rect;
    AEnabled := Enabled and Items[Index].Enabled;
    if not UseRightToLeftAlignment then
    begin
      Dec(ACheckRegion.Left, FInnerCheckListBox.GetCheckRegionWidth);
      Inc(ATextRect.Left, FInnerCheckListBox.Metrics.TextOffset);
    end
    else
    begin
      Inc(ACheckRegion.Right, FInnerCheckListBox.GetCheckRegionWidth);
      Dec(ATextRect.Right, FInnerCheckListBox.Metrics.TextOffset);
    end;
    DrawCheckRegion(ACheckRegion, AEnabled);
    PrepareColors(AEnabled);
    FInnerCheckListBox.Canvas.FillRect(Rect);
    FText := Items[Index].Text;
    SetBkMode(FInnerCheckListBox.Canvas.Handle, TRANSPARENT);
    PrepareColors(AEnabled);
    ABaseTestFlag := DT_NOPREFIX or DT_VCENTER or DT_SINGLELINE or
      ADrawTextAlignmentFlags[UseRightToLeftAlignment];
    if FInnerCheckListBox.TabWidth > 0 then
      ABaseTestFlag := ABaseTestFlag or DT_EXPANDTABS or DT_TABSTOP;
    with ADrawTextParams do
    begin
      cbSize := SizeOf(ADrawTextParams);
      iTabLength := FInnerCheckListBox.TabWidth;
      iLeftMargin := 0;
      iRightMargin := 0;
    end;
    Windows.DrawTextEx(FInnerCheckListBox.Canvas.Handle,
      PChar(FText), Length(FText), ATextRect,
      ABaseTestFlag, @ADrawTextParams);
    if odFocused in State then
      FInnerCheckListBox.Canvas.DrawFocusRect(Rect);
  end;
  ADrawEventRect := Rect;
  if Assigned(FOnDrawItem) then
    FOnDrawItem(Self, Index, ADrawEventRect, State);
end;

procedure TcxCustomCheckListBox.GetOptimalHeight(var ANewHeight: Integer);

  function GetItemHeight(AIndex: Integer): Integer;
  begin
    Result := FInnerCheckListBox.GetStandardItemHeight;
  end;

var
  I: Integer;
  ABorderExtent: TRect;
  AItemHeight: Integer;
  AListClientSize, AListSize, AScrollBarSize: TSize;
  AScrollWidth: Integer;
  AVScrollBar: Boolean;
begin
  ABorderExtent := GetBorderExtent;
  AListClientSize.cy := ABorderExtent.Top + ABorderExtent.Bottom;
  AScrollBarSize := GetScrollBarSize;
  AScrollWidth := ScrollWidth;
  if AScrollWidth > 0 then
  Inc(AScrollWidth, 4);
  I := 0;
  repeat
    AItemHeight := GetItemHeight(I);
    AListClientSize.cy := AListClientSize.cy + AItemHeight;
    AListSize.cy := AListClientSize.cy;
    AListClientSize.cx := Width - (ABorderExtent.Left + ABorderExtent.Right);
    AVScrollBar := I + 1 < Count;
    if AVScrollBar then
      AListClientSize.cx := AListClientSize.cx - AScrollBarSize.cx;
    if AListClientSize.cx < AScrollWidth then
      AListSize.cy := AListSize.cy + AScrollBarSize.cy;
    if AListSize.cy = ANewHeight then
      Break;
    if AListSize.cy > ANewHeight then
    begin
      if I > 0 then
      begin
        AListClientSize.cy := AListClientSize.cy - AItemHeight;
        AListSize.cy := AListClientSize.cy;
        AListClientSize.cx := Width - (ABorderExtent.Left + ABorderExtent.Right);
        AVScrollBar := I < Count;
        if AVScrollBar then
          AListClientSize.cx := AListClientSize.cx - AScrollBarSize.cx;
        if AListClientSize.cx < AScrollWidth then
          AListSize.cy := AListSize.cy + AScrollBarSize.cy;
      end;
      Break;
    end;
    Inc(I);
  until False;
  ANewHeight := AListSize.cy;
end;

function TcxCustomCheckListBox.IsInternalControl(AControl: TControl): Boolean;
begin
  Result := (AControl = FInnerCheckListBox.HScrollBar) or (AControl = FInnerCheckListBox.VScrollBar) or
    inherited IsInternalControl(AControl);
end;

class function TcxCustomCheckListBox.GetDataBindingClass: TcxCustomDataBindingClass;
begin
  Result := TcxCustomDataBinding;
end;

procedure TcxCustomCheckListBox.DataChange;
begin
  if ShowChecks then
    EditValue := DataBinding.GetStoredValue(evsValue, Focused)
  else
    if DataBinding.IsDataSourceLive then
      ItemIndex := Items.IndexOf(VarToStr(DataBinding.GetStoredValue(evsText, Focused)))
    else
      ItemIndex := -1;
end;

procedure TcxCustomCheckListBox.UpdateData;
begin
  if ShowChecks then
    DataBinding.SetStoredValue(evsValue, EditValue)
  else
    if ItemIndex >= 0 then
      DataBinding.SetStoredValue(evsText, Items[ItemIndex].Text)
    else
      DataBinding.SetStoredValue(evsText, '');
end;

procedure TcxCustomCheckListBox.SetSize;
var
  ANewHeight: Integer;
begin
  if IsLoading then
    Exit;
// TODO
//  try
    if not IntegralHeight or (Align in [alLeft, alRight, alClient]) then
    begin
      inherited SetSize;
      Exit;
    end;
    ANewHeight := Height;
    GetOptimalHeight(ANewHeight);
    Height := ANewHeight;
    inherited SetSize;
//  finally
//    if FInnerCheckListBox.HandleAllocated then
//      KillMouseMoveMessages;
//  end;
end;

procedure TcxCustomCheckListBox.WndProc(var Message: TMessage);
begin
  if FInnerCheckListBox <> nil then
    case Message.Msg of
      LB_ADDSTRING .. LB_MSGMAX:
        begin
          with Message do
            Result := SendMessage(FInnerCheckListBox.Handle, Msg, WParam, LParam);
          Exit;
        end;
    end;
  inherited WndProc(Message);
  if (FInnerCheckListBox <> nil) and (Message.Msg = WM_COMMAND) and (Message.WParamHi = LBN_SELCHANGE) then
    FInnerCheckListBox.SetExternalScrollBarsParameters;
end;

function TcxCustomCheckListBox.GetOnClickCheck : TcxClickCheckEvent;
begin
  Result := FInnerCheckListBox.FOnClickCheck;
end;

function TcxCustomCheckListBox.GetAllowGrayed : Boolean;
begin
  Result := FInnerCheckListBox.FAllowGrayed;
end;

function TcxCustomCheckListBox.GetAllowDblClickToggle: Boolean;
begin
  Result := FInnerCheckListBox.AllowDblClickToggle;
end;

function TcxCustomCheckListBox.GetAutoComplete: Boolean;
begin
  Result := FInnerCheckListBox.AutoComplete;
end;

function TcxCustomCheckListBox.GetAutoCompleteDelay: Cardinal;
begin
  Result := FInnerCheckListBox.AutoCompleteDelay;
end;

function TcxCustomCheckListBox.GetGlyph: TBitmap;
begin
  Result := FInnerCheckListBox.Glyph;
end;

function TcxCustomCheckListBox.GetGlyphCount: Integer;
begin
  Result := FInnerCheckListBox.GlyphCount;
end;

function TcxCustomCheckListBox.GetItemHeight: Integer;
begin
  Result := FInnerCheckListBox.ItemHeight;
end;

function TcxCustomCheckListBox.GetItems: TcxCheckListBoxItems;
begin
  Result := FInnerCheckListBox.CheckItems;
end;

function TcxCustomCheckListBox.GetColumns: Integer;
begin
  Result := FInnerCheckListBox.Columns;
end;

function TcxCustomCheckListBox.GetCount: Integer;
begin
  Result := FInnerCheckListBox.Items.Count;
end;

function TcxCustomCheckListBox.GetItemIndex: Integer;
begin
  Result := FInnerCheckListBox.ItemIndex;
end;

function TcxCustomCheckListBox.GetScrollWidth: Integer;
begin
  Result := FInnerCheckListBox.ScrollWidth;
end;

function TcxCustomCheckListBox.GetSelected(Index: Integer): Boolean;
begin
  Result := FInnerCheckListBox.Selected[Index];
end;

function TcxCustomCheckListBox.GetTabWidth: Integer;
begin
  Result := FInnerCheckListBox.TabWidth;
end;

function TcxCustomCheckListBox.GetTopIndex: Integer;
begin
  Result := FInnerCheckListBox.TopIndex;
end;

procedure TcxCustomCheckListBox.ImagesChanged(Sender: TObject);
begin
  if FInnerCheckListBox <> nil then
    FInnerCheckListBox.AdjustItemHeight;
end;

function TcxCustomCheckListBox.IsItemHeightStored: Boolean;
begin
  Result := FListStyle <> lbStandard;
end;

procedure TcxCustomCheckListBox.SetOnClickCheck(Value: TcxClickCheckEvent);
begin
  FInnerCheckListBox.FOnClickCheck := Value;
end;

procedure TcxCustomCheckListBox.SetAllowGrayed(Value: Boolean);
begin
  FInnerCheckListBox.FAllowGrayed := Value;
end;

procedure TcxCustomCheckListBox.SetAllowDblClickToggle(Value: Boolean);
begin
  FInnerCheckListBox.AllowDblClickToggle := Value;
end;

procedure TcxCustomCheckListBox.SetAutoComplete(Value: Boolean);
begin
  FInnerCheckListBox.AutoComplete := Value;
end;

procedure TcxCustomCheckListBox.SetAutoCompleteDelay(Value: Cardinal);
begin
  FInnerCheckListBox.AutoCompleteDelay := Value;
end;

procedure TcxCustomCheckListBox.SetDataBinding(Value: TcxCustomDataBinding);
begin
  FDataBinding.Assign(Value);
end;

procedure TcxCustomCheckListBox.SetEditValueFormat(Value: TcxCheckStatesValueFormat);

  procedure ResetGrayedStates;
  var
    I: Integer;
  begin
    Items.LockChanged(True);
    try
      for I := 0 to Items.Count - 1 do
        if Items[I].State = cbsGrayed then
          Items[I].State := cbsUnchecked;
    finally
      Items.LockChanged(False, False);
    end;
  end;

begin
  if Value <> FEditValueFormat then
  begin
    FEditValueFormat := Value;
    if IsModified then
    begin
      if Value = cvfInteger then
        ResetGrayedStates;
      InnerCheckListBox.UpdateEditValue;
    end
    else
      InnerCheckListBox.UpdateCheckStates;
  end;
end;

procedure TcxCustomCheckListBox.SetGlyph(Value: TBitmap);
begin
  FInnerCheckListBox.SetGlyph(Value);
end;

procedure TcxCustomCheckListBox.SetGlyphCount(Value: Integer);
begin
  FInnerCheckListBox.SetGlyphCount(Value);
end;

procedure TcxCustomCheckListBox.SetItemHeight(Value: Integer);
begin
  if FListStyle <> lbStandard then
    FInnerCheckListBox.ItemHeight := Value;
end;

procedure TcxCustomCheckListBox.SetItems(Value: TcxCheckListBoxItems);
begin
  FInnerCheckListBox.CheckItems.Assign(Value);
  DataChange;
end;

procedure TcxCustomCheckListBox.SetColumns(Value: Integer);
begin
  FInnerCheckListBox.Columns := Value;
{$IFDEF DELPHI5}
  //FInnerCheckListBox.SetExternalScrollBarsParameters; {<- Release 4.2.1}
{$ENDIF}
end;

procedure TcxCustomCheckListBox.SetImageLayout(Value: TcxCheckListBoxImageLayout);
begin
  if Value <> FImageLayout then
  begin
    FImageLayout := Value;
    FInnerCheckListBox

⌨️ 快捷键说明

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