⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tntjvcolorcombo.pas

📁 TntExUpdate 是 流行的 TntUnicodeControls控件的扩展包.包括很难找到的 TntJVCL 也在里面. TntSysUtils2.pas/TntSysUtilsEx.pa
💻 PAS
📖 第 1 页 / 共 4 页
字号:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.

The Original Code is: JvColorCombo.PAS, released on 2002-05-26.

The Initial Developer of the Original Code is Peter Thrnqvist [peter3 at sourceforge dot net]
Portions created by Peter Thrnqvist are Copyright (C) 2002 Peter Thrnqvist.
All Rights Reserved.

Contributor(s):
Brian Cook (borland.public.vcl.components.writing)

You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net

Description:
  Comboboxes for displaying colors and fonts

Known Issues:
  If you set AutoComplete in TJvColorComboBox to True and use the same text for
  all Custom colors, the inherited Change behaviour from TJvComboBox makes the *first*
  custom color selected, not the last added as it should be thus AutoComplete is
  set to default to False. (p3)
-----------------------------------------------------------------------------}
// $Id: JvColorCombo.pas,v 1.56 2005/11/22 18:20:08 ahuser Exp $

unit TntJvColorCombo;

{$I jvcl.inc}
{$I vclonly.inc}

interface

uses
  {$IFDEF UNITVERSIONING}
  JclUnitVersioning,
  {$ENDIF UNITVERSIONING}
  {$IFDEF CLR}
  System.Runtime.InteropServices, System.Security,
  System.Text, Borland.Vcl.WinUtils, Types, JvJCLUtils,
  {$ENDIF CLR}
  Windows, TntWindows, Messages,
  Classes, TntClasses, Controls, Dialogs, Graphics,
  TntJvComboBox, JvColorCombo;

type
  TTntJvNewColorEvent = procedure(Sender: TObject; Color: TColor; var DisplayName: WideString;
    var AllowAdd: Boolean) of object;
  TTntJvGetColorNameEvent = procedure(Sender: TObject; Index: Integer; Color: TColor;
    var DisplayName: WideString) of object;
  {
  TJvColorComboOption = (coText, coHex, coRGB, coSysColors, coCustomColors);
  TJvColorComboOptions = set of TJvColorComboOption;
  }

  TTntJvColorComboBox = class(TTntJvCustomComboBox)
  private
    FColorValue: TColor;
    FCustomColorCount: Integer;
    FHiliteColor: TColor;
    FHiliteText: TColor;
    FOptions: TJvColorComboOptions;
    FNewColorText: WideString;
    FColorDialogText: WideString;
    FColorWidth, FUpdateCount: Integer;
    FExecutingDialog: Boolean;
    FNewColor: TTntJvNewColorEvent;
    FOnGetDisplayName: TTntJvGetColorNameEvent;
    FColorNameMap: TTntStringList;
    FOnInsertColor: TTntJvNewColorEvent;
    FOnBeforeCustom: TNotifyEvent;
    FCustomColors: TStrings;
    procedure SetOptions(Value: TJvColorComboOptions);
    procedure SetColorDialogText(Value: WideString);
    procedure SetColorWidth(Value: Integer);
    procedure SetColorValue(Value: TColor);
    procedure ResetItemHeight;
    procedure CNDrawItem(var Msg: TWMDrawItem); message CN_DRAWITEM;
    function GetColorNameMap: TTntStrings;
    procedure SetColorNameMap(const Value: TTntStrings);
    procedure InitColorNames;
    function GetDropDownWidth: Integer;
    procedure SetDropDownWidth(const Value: Integer);
    function GetColor(Index: Integer): TColor;
  protected
    procedure FontChanged; override;
    procedure DrawItem(Index: Integer; R: TRect; State: TOwnerDrawState); override;
    procedure Click; override;

    function GetColorName(AColor: TColor; const Default: WideString): WideString;
    function DoNewColor(Color: TColor; var DisplayName: WideString): Boolean; virtual;
    procedure DoGetDisplayName(Index: Integer; AColor: TColor; var DisplayName: WideString); virtual;
    function DoInsertColor(AIndex: Integer; AColor: TColor; var DisplayName: WideString): Boolean; virtual;
    procedure DoBeforeCustom;
    procedure InternalInsertColor(AIndex: Integer; AColor: TColor; const DisplayName: WideString); virtual;
    procedure DoNameMapChange(Sender: TObject);
    procedure SetParent(AParent: TWinControl); override;
    procedure Loaded; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function BeginUpdate: Integer;
    function EndUpdate: Integer;
    procedure GetColors; virtual;
    function GetCustomColorsStrings: TStrings;
    procedure SetCustomColorsStrings(const Value: TStrings);
    procedure GetCustomColors(AList: TList);
    procedure SetCustomColors(AList: TList);
    // Returns the current name for AColor. Note that this implicitly might call the
    // OnGetDisplayName event if the protected GetColorName returns an empty string
    function ColorName(AColor: TColor): WideString;
    // returns the index of a specific color or -1 if not found
    function FindColor(AColor: TColor): Integer;

    procedure AddColor(AColor: TColor; const DisplayName: WideString);
    procedure ChangeColor(AIndex: Integer; AColor: TColor; const DisplayName: WideString);
    procedure InsertColor(AIndex: Integer; AColor: TColor; const DisplayName: WideString);
    property Text;
    property CustomColorCount: Integer read FCustomColorCount;
    property CustomColors: TStrings read GetCustomColorsStrings write SetCustomColorsStrings;

    property Colors[Index: Integer]: TColor read GetColor;
  published
    property Anchors;
    property AutoComplete default False;
    {$IFDEF COMPILER6_UP}
    property AutoDropDown;
    {$ENDIF COMPILER6_UP}
    property BevelEdges;
    property BevelInner;
    property BevelKind;
    property BevelOuter;
    property BiDiMode;
    property Constraints;
    // color name map is a TStrings property that can contain name/value mappings on the form
    // ColorName=DisplayName
    // If the component finds a matching mapping, it will substitute the default value
    // with the value in the list, otherwise the default value wil be used
    // Example:
    // clBlack=Black
    property ColorNameMap: TTntStrings read GetColorNameMap write SetColorNameMap;
    property ColorValue: TColor read FColorValue write SetColorValue default clBlack;
    property ColorDialogText: WideString read FColorDialogText write SetColorDialogText;
    property ColorWidth: Integer read FColorWidth write SetColorWidth default 21;
    property DroppedDownWidth: Integer read GetDropDownWidth write SetDropDownWidth;
    property HiliteColor: TColor read FHiliteColor write FHiliteColor default clHighlight;
    property HiliteText: TColor read FHiliteText write FHiliteText default clHighlightText;
    property NewColorText: WideString read FNewColorText write FNewColorText;
    property Options: TJvColorComboOptions read FOptions write SetOptions default [coText];
    // called before a new color is inserted as a result of displaying the Custom Colors dialog
    property OnNewColor: TTntJvNewColorEvent read FNewColor write FNewColor;
    // called before any color is inserted
    property OnInsertColor: TTntJvNewColorEvent read FOnInsertColor write FOnInsertColor;
    // called whenever the displayname of an item is needed
    property OnGetDisplayName: TTntJvGetColorNameEvent read FOnGetDisplayName write FOnGetDisplayName;
    // called just before the '(Other)' item is added at the bottom of the list
    property OnBeforeCustom: TNotifyEvent read FOnBeforeCustom write FOnBeforeCustom;

    property Color;
    property DragMode;
    property DragCursor;
    property Enabled;
    property Font;
    property ImeMode;
    property ImeName;
    property ParentColor;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property Sorted;
    property TabOrder;
    property TabStop;
    property Visible;
    property OnChange;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnDropDown;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnStartDrag;
  end;

  (**
  //  TFontDialogDevice = (fdScreen, fdPrinter, fdBoth); { already in Dialogs }
  TJvFontComboOption = (foAnsiOnly, foTrueTypeOnly, foFixedPitchOnly,
    foNoOEMFonts, foOEMFontsOnly, foScalableOnly, foWysiWyg, foDisableVerify,
    foPreviewFont, foMRU);
  // foDisableVerify: if True, allows you to insert a font name that doesn't exist (by assigning to FontName)
  TJvFontComboOptions = set of TJvFontComboOption;
  **)
  TJvDrawPreviewEvent = procedure(Sender: TObject; const AFontName: WideString;
    var APreviewText: WideString; ATextWidth: Integer; var DrawPreview: Boolean) of object;

  TTntJvFontComboBox = class(TTntJvCustomComboBox)
  private
    FTrueTypeBmp: TBitmap;
    FFixBmp: TBitmap;
    FDeviceBmp: TBitmap;
    FDevice: TFontDialogDevice;
    FHiliteColor: TColor;
    FHiliteText: TColor;
    FUseImages: Boolean;
    FOptions: TJvFontComboOptions;
    FMRUCount: Integer;
    FWasMouse: Boolean;
    FShowMRU: Boolean;
    FMaxMRUCount, FUpdateCount: Integer;
    FOnDrawPreviewEvent: TJvDrawPreviewEvent;
    FFontSizes: TStrings;
    FEnumeratorDC: HDC;
    procedure SetUseImages(Value: Boolean);
    procedure SetDevice(Value: TFontDialogDevice);
    procedure SetOptions(Value: TJvFontComboOptions);
    procedure ResetItemHeight;
    procedure Reset;
    // (ahuser) why both WM_FONTCHANGE and CM_FONTCHANGED ?
  //procedure WMFontChange(var Msg: TMessage); message WM_FONTCHANGE;
    function GetFontName: WideString;
    procedure SetFontName(const Value: WideString);
    function GetSorted: Boolean;
    procedure SetSorted(const Value: Boolean);
    function GetDropDownWidth: Integer;
    procedure SetDropDownWidth(const Value: Integer);
    procedure SetShowMRU(const Value: Boolean);
    procedure SetMaxMRUCount(const Value: Integer);
    function GetFontSizes: TStrings;
  protected
    procedure FontChanged; override;
    procedure Loaded; override;
    procedure GetFonts; virtual;
    procedure CNDrawItem(var Msg: TWMDrawItem); message CN_DRAWITEM;
    procedure DrawItem(Index: Integer; R: TRect; State: TOwnerDrawState); override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
      X: Integer; Y: Integer); override;
    procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X: Integer;
      Y: Integer); override;
    procedure CloseUp; override;
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure SetParent(AParent: TWinControl); override;
    function DoDrawPreview(const AFontName: WideString; var APreviewText: WideString;
      ATextWidth: Integer): Boolean; virtual;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function AddToMRU: Integer;
    procedure ClearMRU;
    procedure Click; override;
    function BeginUpdate: Integer;
    function EndUpdate: Integer;
    function FontSubstitute(const AFontName: WideString): WideString;
    procedure FontSizeList(SizeList: TList);
    function IsTrueType: Boolean;
    property Text;
    property MRUCount: Integer read FMRUCount;
    // returns the supported font sizes or a set of default sizes for TrueType fonts
    property FontSizes: TStrings read GetFontSizes;
  published
    property Anchors;
    property AutoComplete default False;
    {$IFDEF COMPILER6_UP}
    property AutoDropDown;
    {$ENDIF COMPILER6_UP}
    property BevelEdges;
    property BevelInner;
    property BevelKind;
    property BevelOuter;
    property BiDiMode;
    property Constraints;
    property Color;
    property DroppedDownWidth: Integer read GetDropDownWidth write SetDropDownWidth;
    property MaxMRUCount: Integer read FMaxMRUCount write SetMaxMRUCount;
    property FontName: WideString read GetFontName write SetFontName;
    property Device: TFontDialogDevice read FDevice write SetDevice default fdScreen;
    property DragMode;
    property DragCursor;
    property Enabled;
    property Font;
    property ItemIndex;
    property HiliteColor: TColor read FHiliteColor write FHiliteColor default clHighlight;
    property HiliteText: TColor read FHiliteText write FHiliteText default clHighlightText;
    property Options: TJvFontComboOptions read FOptions write SetOptions default [];
    property UseImages: Boolean read FUseImages write SetUseImages default True;
    property ImeMode;
    property ImeName;
    property ParentColor;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property Sorted: Boolean read GetSorted write SetSorted;
    property TabOrder;
    property TabStop;
    property Visible;
    property OnChange;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;
    property OnDragOver;
    property OnDropDown;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnStartDrag;
    property OnDrawPreviewEvent: TJvDrawPreviewEvent read FOnDrawPreviewEvent write FOnDrawPreviewEvent;
  end;

  {$IFDEF UNITVERSIONING}
const
  UnitVersioning: TUnitVersionInfo = (
    RCSfile: '$RCSfile: JvColorCombo.pas,v $';
    Revision: '$Revision: 1.56 $';
    Date: '$Date: 2005/11/22 18:20:08 $';
    LogPath: 'JVCL'run'
    );
  {$ENDIF UNITVERSIONING}

implementation

uses
  SysUtils, Math, StdCtrls, Printers,
  JvConsts, JvResources, JvTypes, TntJvTypes;

{$R *.res}

function LoadInternalBitmap(ResName: string): TBitmap;
begin
  Result := TBitmap.Create;
  {$IFDEF CLR}
  Result.Handle := LoadBitmap(HInstance, ResName);
  {$ELSE}
  Result.Handle := LoadBitmap(HInstance, PChar(ResName));
  {$ENDIF CLR}
end;

function GetItemHeight(Font: TFont): Integer;
var
  DC: HDC;
  AFont: HFONT;
  TM: TTextMetric;
begin
  DC := GetDC(HWND_DESKTOP);
  try
    AFont := SelectObject(DC, Font.Handle);
    GetTextMetrics(DC, TM);
    SelectObject(DC, AFont);
  finally
    ReleaseDC(HWND_DESKTOP, DC);
  end;
  Result := TM.tmHeight + 1;
end;

function IncludeFontW(Options: TJvFontComboOptions; LogFont: TLogFontW; FontType: Integer): Boolean;
begin
  Result := True;
  if foAnsiOnly in Options then
    Result := Result and (LogFont.lfCharSet = ANSI_CHARSET);
  if foTrueTypeOnly in Options then
    Result := Result and (FontType and TRUETYPE_FONTTYPE > 0);
  if foFixedPitchOnly in Options then
    Result := Result and (LogFont.lfPitchAndFamily and FIXED_PITCH > 0);
  if foOEMFontsOnly in Options then
    Result := Result and (LogFont.lfCharSet = OEM_CHARSET);
  if foNoOEMFonts in Options then
    Result := Result and (LogFont.lfCharSet <> OEM_CHARSET);
  if foScalableOnly in Options then
    Result := Result and (FontType and RASTER_FONTTYPE = 0);
end;

{$IFDEF CLR}
function EnumFontsProc(var LogFont: TLogFontW; var TextMetric: TTextMetric;
  FontType: DWORD; Param: TObject): Integer;
var
  FontCombo: TJvFontComboBox;
begin
  FontCombo := TJvFontComboBox(Param);
{$ELSE}
function EnumFontsProc(var LogFont: TLogFontW; var TextMetric: TTextMetric;
  FontType: Integer; FontCombo: TTntJvFontComboBox): Integer; stdcall;
begin
{$ENDIF CLR}
  Result := 0;
  if FontCombo = nil then
    Exit;
  if IncludeFontW(FontCombo.Options, LogFont, FontType) then
  begin
    if FontCombo.Items.IndexOf(WideString(LogFont.lfFaceName)) = -1 then
      FontCombo.Items.AddObject(WideString(LogFont.lfFaceName), TObject(FontType));
  end;
  Result := 1;
end;

function ItemStateToOwnerDrawState(State: Integer): TOwnerDrawState;
begin
  Result := [];
  if (State and ODS_CHECKED) <> 0 then
    Include(Result, odChecked);
  if (State and ODS_COMBOBOXEDIT) <> 0 then
    Include(Result, odComboBoxEdit);
  if (State and ODS_DEFAULT) <> 0 then
    Include(Result, odDefault);
  if (State and ODS_DISABLED) <> 0 then
    Include(Result, odDisabled);
  if (State and ODS_FOCUS) <> 0 then
    Include(Result, odFocused);
  if (State and ODS_GRAYED) <> 0 then
    Include(Result, odGrayed);
  if (State and ODS_SELECTED) <> 0 then
    Include(Result, odSelected);
end;

//=== { TTntJvColorComboBox } ===================================================

constructor TTntJvColorComboBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  LoadDescrColorValues;

⌨️ 快捷键说明

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