cxstylesheeteditor.pas

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

PAS
534
字号

{********************************************************************}
{                                                                    }
{           Developer Express Visual Component Library               }
{           Express Cross Platform Library classes                   }
{                                                                    }
{           Copyright (c) 2000-2008 Developer Express Inc.           }
{           ALL RIGHTS RESERVED                                      }
{                                                                    }
{   The entire contents of this file is protected by U.S. and        }
{   International Copyright Laws. Unauthorized reproduction,         }
{   reverse-engineering, and distribution of all or any portion of   }
{   the code contained in this file is strictly prohibited and may   }
{   result in severe civil and criminal penalties and will be        }
{   prosecuted to the maximum extent possible under the law.         }
{                                                                    }
{   RESTRICTIONS                                                     }
{                                                                    }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES            }
{   (DCU, OBJ, DLL, ETC.) ARE CONFIDENTIAL AND PROPRIETARY TRADE     }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS    }
{   LICENSED TO DISTRIBUTE THE EXPRESSCROSSPLATFORMLIBRARY AND ALL   }
{   ACCOMPANYING VCL CONTROLS AS PART OF AN EXECUTABLE PROGRAM       }
{   ONLY.                                                            }
{                                                                    }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED       }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE         }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE        }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT   }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                       }
{                                                                    }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON        }
{   ADDITIONAL RESTRICTIONS.                                         }
{                                                                    }
{********************************************************************}

unit cxStyleSheetEditor;

{$I cxVer.inc}

interface

uses
{$IFDEF DELPHI6}
  Variants,
{$ENDIF}
  Windows, Messages, ExtDlgs,
  SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls,
  cxClasses, cxGraphics, cxStyles;

type
  TcxStyleSheetEditorPreview = class
  public
    constructor Create(AOwner: TComponent); virtual;
    function Control: TWinControl; virtual; abstract;
    function GetSize: TPoint; virtual;
    class function GetStyleSheetClass: TcxCustomStyleSheetClass; virtual;
    procedure SetStyleSheet(AStyleSheet: TcxCustomStyleSheet); virtual; abstract;
  end;

  TcxStyleSheetEditorPreviewClass = class of TcxStyleSheetEditorPreview;

  TfrmcxStyleSheetEditor = class(TForm)
    pnlBottom: TPanel;
    pnlButtons: TPanel;
    btnOK: TButton;
    bntCancel: TButton;
    FontDialog: TFontDialog;
    ColorDialog: TColorDialog;
    pnlClient: TPanel;
    Bevel: TBevel;
    pnlStyles: TPanel;
    pnlStylesCaption: TPanel;
    pnlStylesButtons: TPanel;
    pnlStylesClient: TPanel;
    lbStyles: TListBox;
    pnlPreview: TPanel;
    pnlPreviewCaption: TPanel;
    pnlPreviewClient: TPanel;
    cbColor: TCheckBox;
    btnBitmap: TButton;
    btnColor1: TButton;
    btnFont1: TButton;
    cbFont: TCheckBox;
    cbBitmap: TCheckBox;
    procedure lbStylesClick(Sender: TObject);
    procedure lbStylesMeasureItem(Control: TWinControl; Index: Integer;
      var Height: Integer);
    procedure lbStylesDrawItem(Control: TWinControl; Index: Integer; Rect: TRect;
      State: TOwnerDrawState);
    procedure cbClick(Sender: TObject);
    procedure btnColor1Click(Sender: TObject);
    procedure btnFont1Click(Sender: TObject);
    procedure btnBitmapClick(Sender: TObject);
  private
    FCanvas: TcxCanvas;
    FPreview: TcxStyleSheetEditorPreview;
    FStateUpdating: Boolean;
    FStyleList: TList;
    FStyleSheet: TcxCustomStyleSheet;
    function GetCheckBoxStyleValue(ACheckBox: TCheckBox): TcxStyleValue;
    function GetFirstSelectedStyle: TcxStyle;
    procedure RecreateListBox(AListBox: TListBox);
    procedure SetSelectedStylesAssignValue(ACheckBox: TCheckBox);
    procedure SetStyles(AStyleSheet: TcxCustomStyleSheet);
    procedure UpdateStyles(AStyleSheet: TcxCustomStyleSheet; AGetStyleName: TcxStyleGetName);
    procedure UpdateState;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

function ShowcxStyleSheetEditor(AStyleSheet: TcxCustomStyleSheet; AGetStyleName: TcxStyleGetName): Boolean;

procedure RegisterStyleSheetEditorPreview(APreviewClass: TcxStyleSheetEditorPreviewClass);
procedure UnregisterStyleSheetEditorPreview(APreviewClass: TcxStyleSheetEditorPreviewClass);
function GetPreviewByStyleSheetClass(AStyleSheetClass: TcxCustomStyleSheetClass): TcxStyleSheetEditorPreviewClass;

implementation

{$R *.dfm}

uses
  TypInfo, cxControls;

function VerifyBitmap(ABitmap: TBitmap): Boolean;
begin
  Result := (ABitmap.Width > 0) and (ABitmap.Height > 0);
end;

{ TcxStyleSheetEditorPreview }

constructor TcxStyleSheetEditorPreview.Create(AOwner: TComponent);
begin
  inherited Create;
end;

function TcxStyleSheetEditorPreview.GetSize: TPoint;
begin
  Result.X := 350;
  Result.Y := 250;
end;

class function TcxStyleSheetEditorPreview.GetStyleSheetClass: TcxCustomStyleSheetClass;
begin
  Result := nil;
end;

function ShowcxStyleSheetEditor(AStyleSheet: TcxCustomStyleSheet;
        AGetStyleName: TcxStyleGetName): Boolean;
var
  AForm: TfrmcxStyleSheetEditor;
begin
  AForm := TfrmcxStyleSheetEditor.Create(nil);
  try
    AForm.SetStyles(AStyleSheet);
    AForm.UpdateState;
    AForm.ShowModal;
    Result := AForm.ModalResult = mrOK;
    if Result then
      AForm.UpdateStyles(AStyleSheet, AGetStyleName);
  finally
    AForm.Free;
  end;
end;

{ TfrmcxStyleSheetEditor }

constructor TfrmcxStyleSheetEditor.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FStyleList := TList.Create;
  FCanvas := TcxCanvas.Create(nil);
end;

destructor TfrmcxStyleSheetEditor.Destroy;
begin
  FreeAndNil(FCanvas);
  FStyleList.Free;
  FStyleSheet.Free;
  FPreview.Free;
  inherited Destroy;
end;

function TfrmcxStyleSheetEditor.GetCheckBoxStyleValue(ACheckBox: TCheckBox): TcxStyleValue;
begin
  if ACheckBox = cbBitmap then
    Result := svBitmap
  else
    if ACheckBox = cbColor then
      Result := svColor
    else
      Result := svFont;
end;

function TfrmcxStyleSheetEditor.GetFirstSelectedStyle: TcxStyle;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to lbStyles.Items.Count - 1 do
    if lbStyles.Selected[I] then
    begin
      Result := TcxStyle(FStyleList[I]);
      Break;
    end;
end;

procedure TfrmcxStyleSheetEditor.RecreateListBox(AListBox: TListBox);
var
  I: Integer;
  ASelected: TList;
begin
  ASelected := TList.Create;
  try
    for I := 0 to AListBox.Items.Count - 1 do
      if AListBox.Selected[I] then
        ASelected.Add(Pointer(I));
    RecreateControlWnd(AListBox);
    for I := 0 to ASelected.Count - 1 do
      AListBox.Selected[Integer((ASelected[I]))] := True;
  finally
    ASelected.Free;
  end;
end;

procedure TfrmcxStyleSheetEditor.SetSelectedStylesAssignValue(ACheckBox: TCheckBox);

  function cxStyleValueTocxStyleValues(AStyleValue: TcxStyleValue): TcxStyleValues;
  begin
    case AStyleValue of
      svBitmap:
        Result := [svBitmap];
      svColor:
        Result := [svColor];
      svFont:
        Result := [svFont, svTextColor];
      else
        Result := [];
    end;
  end;

var
  AStyleValues: TcxStyleValues;
  I: Integer;
begin
  AStyleValues := cxStyleValueTocxStyleValues(GetCheckBoxStyleValue(ACheckBox));
  for I := 0 to lbStyles.Items.Count - 1 do
    if lbStyles.Selected[I] then
      with TcxStyle(FStyleList[I]) do
        if ACheckBox.Checked then
        begin
          if (ACheckBox = cbBitmap) and not VerifyBitmap(Bitmap) then
            Continue;
          AssignedValues := AssignedValues + AStyleValues
        end
        else
          AssignedValues := AssignedValues - AStyleValues;
end;

procedure TfrmcxStyleSheetEditor.SetStyles(AStyleSheet: TcxCustomStyleSheet);
var
  I, ACount: Integer;
  APropList: TPropList;
  AStyle, ACacheStyle: TcxStyle;
begin
  FStyleSheet := TcxCustomStyleSheetClass(AStyleSheet.ClassType).Create(nil);

⌨️ 快捷键说明

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