cxcolorcombobox.pas

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

PAS
1,962
字号
    property OnAddedMRUColor;
    property OnChange;
    property OnCloseUp;
    property OnDeletedMRUColor;
    property OnEditValueChanged;
    property OnGetDefaultColor;
    property OnInitPopup;
    property OnNamingConvention;
    property OnNewLookupDisplayText;
    property OnPopup;
    property OnSelectCustomColor;
  end;

  { TcxCustomColorComboBoxInnerEdit }

  TcxCustomColorComboBoxInnerEdit = class(TcxCustomComboBoxInnerEdit);

  { TcxColorComboBoxPopupWindow }

  TcxColorComboBoxPopupWindow = class(TcxComboBoxPopupWindow)
  public
    property ViewInfo;
    property SysPanelStyle;
  end;

  { TcxCustomColorComboBox }

  TcxCustomColorComboBox = class(TcxCustomComboBox)
  private
    FColorDialog: TColorDialog;
    FDontCheckModifiedWhenUpdatingMRUList: Boolean;
    FIsDialogShowed: Boolean;
    FNeedsUpdateMRUList: Boolean;
    FPropertiesUpdate: Boolean;
    function GetColorDialog: TColorDialog;
    function GetColorValue: TColor;
    function IsColorValueStored: Boolean;
    procedure SetColorValue(Value: TColor);
    function GetLookupData: TcxColorComboBoxLookupData;
    function GetProperties: TcxCustomColorComboBoxProperties;
    function GetActiveProperties: TcxCustomColorComboBoxProperties;
    procedure SetProperties(Value: TcxCustomColorComboBoxProperties);
    procedure PropertiesLoadColorListHandler(Sender: TObject);
    procedure UpdateMRUList;
    procedure FlushEditValue;
  protected
    procedure AfterPosting; override;
    procedure ContainerStyleChanged(Sender: TObject); override;
    procedure PropertiesChanged(Sender: TObject); override;
    procedure DblClick; override;
    procedure DoButtonClick(AButtonVisibleIndex: Integer); override;
    function GetDisplayValue: string; override;
    function GetEditingValue: TcxEditValue; override;
    function GetInnerEditClass: TControlClass; override;
    procedure Initialize; override;
    procedure InitializePopupWindow; override;
    function IsValidChar(AChar: Char): Boolean; override;
    procedure DoEditKeyDown(var Key: Word; Shift: TShiftState); override;
    function LookupKeyToEditValue(const AKey: TcxEditValue): TcxEditValue; override;
    procedure InternalSetEditValue(const Value: TcxEditValue;
      AValidateEditValue: Boolean); override;
    procedure SynchronizeDisplayValue; override;
    procedure CloseUp(AReason: TcxEditCloseUpReason); override;
    procedure DoSelectCustomColor(Sender: TObject); virtual;
    procedure FixMRUPosition(AColor: TColor); virtual;
    procedure ClearEditValue; virtual;
    procedure DoOnSelectCustomColor(var AColor: TColor;
      var AColorDescription: string; var AddToList: Boolean);
    property ColorValue: TColor read GetColorValue write SetColorValue
      stored IsColorValueStored;
    property ColorDialog: TColorDialog read GetColorDialog;
    property LookupData: TcxColorComboBoxLookupData read GetLookupData;
  public
    destructor Destroy; override;

    function Deactivate: Boolean; override;
    function Focused: Boolean; override;
    class function GetPropertiesClass: TcxCustomEditPropertiesClass; override;
    function IsChildWindow(AWnd: THandle): Boolean; override;
    procedure PrepareEditValue(const ADisplayValue: TcxEditValue;
      out EditValue: TcxEditValue; AEditFocused: Boolean); override;

    function AddMRUColor(const AColor: TColor): TcxMRUColorAction;
    function DelMRUColor(const AColor: TColor): TcxMRUColorAction;

    procedure PrepareColorList(APrepareList: TcxColorPrepareList; ASaveCustom, ASaveMRU: Boolean);    
    procedure PrepareDelphiColorList(const ASaveCustom, ASaveMRU: Boolean);
    procedure PrepareHTML4ColorList(const ASaveCustom, ASaveMRU: Boolean);
    procedure PrepareX11ColorList(const ASaveCustom, ASaveMRU: Boolean);
    procedure PrepareX11OrderedColorList(const ASaveCustom, ASaveMRU: Boolean);

    property ActiveProperties: TcxCustomColorComboBoxProperties
      read GetActiveProperties;
    property Properties: TcxCustomColorComboBoxProperties read GetProperties
      write SetProperties;
  end;

  { TcxColorComboBox }

  TcxColorComboBox = class(TcxCustomColorComboBox)
  private
    function GetActiveProperties: TcxColorComboBoxProperties;
    function GetProperties: TcxColorComboBoxProperties;
    procedure SetProperties(Value: TcxColorComboBoxProperties);
  public
    class function GetPropertiesClass: TcxCustomEditPropertiesClass; override;
    property ActiveProperties: TcxColorComboBoxProperties
      read GetActiveProperties;
  published
    property Anchors;
    property AutoSize;
    property BeepOnEnter;
    property ColorValue;
    property Constraints;
    property DragCursor;
    property DragKind;
    property DragMode;
    property Enabled;
    property ImeMode;
    property ImeName;
    property ParentColor;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property Properties: TcxColorComboBoxProperties read GetProperties
      write SetProperties;
    property ShowHint;
    property Style;
    property StyleDisabled;
    property StyleFocused;
    property StyleHot;
    property TabOrder;
    property TabStop;
    property Visible;
    property OnClick;
    property OnContextPopup;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnEditing;
    property OnEndDock;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseEnter;
    property OnMouseLeave;
    property OnMouseMove;
    property OnMouseUp;
    property OnStartDock;
    property OnStartDrag;
  end;

  { TcxFilterColorComboBoxHelper }

  TcxFilterColorComboBoxHelper = class(TcxFilterComboBoxHelper)
  public
    class function GetFilterEditClass: TcxCustomEditClass; override;
    class function GetSupportedFilterOperators(
      AProperties: TcxCustomEditProperties; AValueTypeClass: TcxValueTypeClass;
      AExtendedSet: Boolean = False): TcxFilterControlOperators; override;
    class procedure InitializeProperties(AProperties,
      AEditProperties: TcxCustomEditProperties; AHasButtons: Boolean); override;
  end;

implementation

uses
  cxExtEditUtils, dxThemeConsts, dxThemeManager, dxUxTheme;

type
  TCanvasAccess = class(TCanvas);

function ConvertColorName(AColor: TColor; ADescription: string;
  const ANamingConvention: TcxColorNamingConvention;
  const AColorValueFormat: TcxColorValueFormat): string;

  function ConvertUnknowColor: string;
  var
    RGB: Cardinal;
  begin
    RGB := ColorToRGB(AColor);
    case AColorValueFormat of
      cxcvRGB:
        Result := Format('%d.%d.%d', [GetRValue(RGB), GetGValue(RGB), GetBValue(RGB)]);
      cxcvHexadecimal:
        Result := Format('%s%.2x%.2x%.2x', [HexDisplayPrefix, GetRValue(RGB), GetGValue(RGB), GetBValue(RGB)]);
      else
        Result := IntToStr(AColor);
    end;
  end;

  function GetColorName(AColorValues: array of TColor; AColorNames: array of string): string;
  var
    I: Integer;
  begin
    for I := Low(AColorValues) to High(AColorValues) do
      if AColorValues[I] = AColor then
      begin
        Result := AColorNames[I];
        Exit;
      end;
    Result := ConvertUnknowColor;
  end;

begin
  case ANamingConvention of
    cxncNone: Result := ADescription;
    cxncDelphi: Result := GetColorName(cxDelphiColorValues, cxDelphiColorNames);
    cxncHTML4: Result := GetColorName(cxHTML4ColorValues, cxHTML4ColorNames);
    cxncX11: Result := GetColorName(cxX11ColorValues, cxX11ColorNames);
  end;
end;

procedure DrawColorBox(ACanvas: TcxCanvas;ARect: TRect; const AFrameColor,
  ABoxColor, ABkColor: TColor; const DefaultColorStyle: TcxDefaultColorStyle);
var
  FRectangle: TRect;
begin
  if (ARect.Left <> ARect.Right) and (ARect.Bottom <> ARect.Top) then
  begin
    ACanvas.Brush.Style := bsSolid;
    ACanvas.Brush.Color := ABkColor;
    ACanvas.FrameRect(ARect);
    InflateRect(ARect, -1, -1);
    ACanvas.Brush.Color := AFrameColor;
    ACanvas.FrameRect(ARect);
    InflateRect(ARect, -1, -1);
    case DefaultColorStyle of
      cxdcClear:
      begin
        ACanvas.Brush.Color := ABkColor;
        ACanvas.FillRect(ARect);
      end;
      cxdcColor, cxdcCustom, cxdcText:
      begin
        ACanvas.Brush.Color := ABoxColor;
        ACanvas.FillRect(ARect);
      end;
      cxdcHatched: begin
        ACanvas.Brush.Color := ABkColor;
        ACanvas.FillRect(ARect);
        ACanvas.Pen.Color := ABkColor;
        ACanvas.Brush.Color := ABoxColor;
        ACanvas.Brush.Style := bsDiagCross;
        FRectangle := ARect;
        InflateRect(FRectangle, 1, 1);
{$IFDEF DELPHI5}
        ACanvas.Canvas.Rectangle(FRectangle);
{$ELSE}
        ACanvas.Canvas.Rectangle(FRectangle.Left, FRectangle.Top, FRectangle.Right, FRectangle.Bottom);
{$ENDIF}
        ACanvas.Pen.Color := AFrameColor;
        ACanvas.Polyline([Point(ARect.Left -1 , ARect.Top - 1), Point(ARect.Right, ARect.Top - 1),
          Point(ARect.Right, ARect.Bottom), Point(ARect.Left - 1, ARect.Bottom),
          Point(ARect.Left - 1, ARect.Top - 1)]);
      end;
    end;
    ACanvas.Brush.Style := bsSolid;
  end;
end;

{ TcxColorComboBoxItem }

constructor TcxColorComboBoxItem.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  FIsCustomColor := True;
  FDescription := '';
end;

procedure TcxColorComboBoxItem.Assign(Source: TPersistent);
begin
  if Source is TcxColorComboBoxItem then
    with TcxColorComboBoxItem(Source) do
    begin
      Self.Color := Color;
      Self.Description := Description;
      Self.FIsCustomColor := IsCustomColor;
      Self.Tag := Tag;
    end
  else
    inherited Assign(Source);
end;

procedure TcxColorComboBoxItem.SetDescription(const Value: TCaption);
begin
  if FDescription <> Value then
  begin
    FDescription := Value;
    Changed(False);
  end;
end;

function TcxColorComboBoxItem.GetDescription: TCaption;
begin
  Result := FDescription;
end;

function TcxColorComboBoxItem.IsTagStored: Boolean;
begin
  Result := FTag <> 0;
end;

procedure TcxColorComboBoxItem.SetColor(const Value: TColor);
begin
  if FColor <> Value then
  begin
    FColor := Value;
    Changed(False);
  end;
end;

{ TcxColorComboBoxItems }

function TcxColorComboBoxItems.GetItems(Index: Integer): TcxColorComboBoxItem;
begin
  Result := TcxColorComboBoxItem(inherited Items[Index]);
end;

procedure TcxColorComboBoxItems.SetItems(Index: Integer;const Value: TcxColorComboBoxItem);
begin
  inherited Items[Index] := Value;
end;

procedure TcxColorComboBoxItems.Update(Item: TCollectionItem);
begin
  if Assigned(OnUpdate) then
    OnUpdate(Item);
  if Owner <> nil then
    Owner.Changed;
end;

function TcxColorComboBoxItems.Owner: TcxCustomColorComboBoxProperties;
begin
  if GetOwner is TcxCustomColorComboBoxProperties then
    Result := TcxCustomColorComboBoxProperties(GetOwner)
  else
    Result := nil;
end;

function TcxColorComboBoxItems.FindColorItem(const AColor: TColor): TcxColorComboBoxItem;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to Count - 1 do
  begin
    if Items[I].Color = AColor then
    begin
      Result := Items[I];
      Break;
    end;
  end;
end;

function TcxColorComboBoxItems.GetIndexByColor(AColor: TColor): Integer;
var
  I : Integer;
begin
  Result := -1;
  for I := 0 to Count - 1 do
  begin
    if Items[I].Color = AColor then
    begin
      Result := I;
      Break;
    end;
  end;
end;

function TcxColorComboBoxItems.GetColorByIndex(AIndex: Integer;
  ADefaultColor: TColor): TColor;
begin
  Result := ADefaultColor;
  if (AIndex >= 0) and (AIndex <= (Count - 1)) then
    Result := Items[AIndex].Color;
end;

function TcxColorComboBoxItems.Add: TcxColorComboBoxItem;
begin
  Result := TcxColorComboBoxItem(inherited Add);
end;

function TcxColorComboBoxItems.Insert(Index: Integer): TcxColorComboBoxItem;
begin
  Result := TcxColorComboBoxItem(inherited Insert(Index));
end;

function TcxColorComboBoxItems.AddColor(const AColor: TColor;

⌨️ 快捷键说明

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