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

📄 jvshapetypecombobox.pas

📁 A diagram edit component for delphi/c++ builder with full source included
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit JvShapeTypeComboBox;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,extctrls;

type
  TJvShapeTypeComboBox = class(TCustomComboBox)
  private
    FHiliteColor:TColor;
    FHiliteText:TColor;

    function GetShapeType:TShapeType;
    procedure SetShapeType(value:TShapeType);

    procedure GetShapeTypes;
    function GetEnumName(AShape:TShapeType):string;
    function GetEnumValue(AName:String):TShapeType;
  protected
    procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
    procedure DrawItem(Index: Integer; R: TRect; State: TOwnerDrawState); override;
  public
    constructor Create(AOwner:TComponent);override;
  published
    property ShapeType:TShapeType read GetShapeType write SetShapeType;
    property OnChange;
  end;

  TJvBrushStyleComboBox = class(TCustomComboBox)
  private
    FHiliteColor:TColor;
    FHiliteText:TColor;

    function GetBrushStyle:TBrushStyle;
    procedure SetBrushStyle(value:TBrushStyle);

    procedure GetBrushStyles;
    function GetEnumName(AStyle:TBrushStyle):string;
    function GetEnumValue(AName:String):TBrushStyle;
  protected
    procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
    procedure DrawItem(Index: Integer; R: TRect; State: TOwnerDrawState); override;
  public
    constructor Create(AOwner:TComponent);override;
  published
    property BrushStyle:TBrushStyle read GetBrushStyle write SetBrushStyle;
    property OnChange;
  end;

  TJvPenStyleComboBox = class(TCustomComboBox)
  private
    FHiliteColor:TColor;
    FHiliteText:TColor;

    function GetPenStyle:TPenStyle;
    procedure SetPenStyle(value:TPenStyle);

    procedure GetPenStyles;
    function GetEnumName(AStyle:TPenStyle):string;
    function GetEnumValue(AName:String):TPenStyle;
  protected
    procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
    procedure DrawItem(Index: Integer; R: TRect; State: TOwnerDrawState); override;
  public
    constructor Create(AOwner:TComponent);override;
  published
    property PenStyle:TPenStyle read GetPenStyle write SetPenStyle;
    property OnChange;
  end;

  TJvPenWidthComboBox = class(TCustomComboBox)
  private
    FHiliteColor:TColor;
    FHiliteText:TColor;

    function GetPenWidth:integer;
    procedure SetPenWidth(value:integer);

    procedure GetPenWidths;
    function GetEnumName(AStyle:integer):string;
    function GetEnumValue(AName:String):integer;
  protected
    procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
    procedure DrawItem(Index: Integer; R: TRect; State: TOwnerDrawState); override;
  public
    constructor Create(AOwner:TComponent);override;
  published
    property PenWidth:Integer read GetPenWidth write SetPenWidth;
    property OnChange;
  end;

procedure Register;

implementation

const
  ShapeNames:array [0..5] of string=('stRectangle','stRoundRect','stSquare','stRoundSquare','stCircle','stEllipse');
  ShapeValues:array [0..5] of TShapeType=(stRectangle,stRoundRect,stSquare,stRoundSquare,stCircle,stEllipse);

  BrushNames:array [0..7] of string=('bsSolid', 'bsClear', 'bsHorizontal', 'bsVertical', 'bsFDiagonal', 'bsBDiagonal', 'bsCross', 'bsDiagCross');
  BrushValues:array [0..7] of TBrushStyle=(bsSolid, bsClear, bsHorizontal, bsVertical, bsFDiagonal, bsBDiagonal, bsCross, bsDiagCross);

  PenNames:array [0..6] of string=('psSolid', 'psDash', 'psDot', 'psDashDot', 'psDashDotDot', 'psClear', 'psInsideFrame');
  PenValues:array [0..6] of TPenStyle=(psSolid, psDash, psDot, psDashDot, psDashDotDot, psClear, psInsideFrame);

procedure Register;
begin
  RegisterComponents('Samples', [TJvShapeTypeComboBox,TJvBrushStyleComboBox,TJvPenStyleComboBox,TJvPenWidthComboBox]);
end;

constructor TJvShapeTypeComboBox.Create(AOwner:TComponent);
begin
  inherited create(AOwner);

  FHiliteColor := clHighLight;
  FHiLiteText := clHighLightText;
  Style := csOwnerDrawFixed;

  if not (csDesigning in ComponentState) then
    GetShapeTypes
  else
    Text:='stRectangle';
end;

procedure TJvShapeTypeComboBox.GetShapeTypes;
var
  i:integer;
begin
  Clear;
  with Items do begin
    for i:=low(ShapeNames) to High(ShapeNames) do begin
      Add(ShapeNames[i]);
    end;
  end;
  ItemIndex:=0;
end;

function TJvShapeTypeComboBox.GetEnumName(AShape:TShapeType):string;
begin
  result:=ShapeNames[Integer(AShape)];
end;

function TJvShapeTypeComboBox.GetEnumValue(AName:String):TShapeType;
var
  i:integer;
begin
  for i:=low(ShapeNames) to High(ShapeNames) do begin
    if ShapeNames[i]=AName then break;
  end;

  if i<=High(ShapeNames) then
    result:=ShapeValues[i]
  else
    result:=stRectangle;
end;

procedure TJvShapeTypeComboBox.CNDrawItem(var Message: TWMDrawItem);
var
  State: TOwnerDrawState;
begin
  with Message.DrawItemStruct^ do
  begin
    State := [];
    if bool(itemState and ODS_CHECKED) then
      Include(State, odChecked);
    if bool(itemState and ODS_COMBOBOXEDIT) then
      Include(State, odComboBoxEdit);
    if bool(itemState and ODS_DEFAULT) then
      Include(State, odDefault);
    if bool(itemState and ODS_DISABLED) then
      Include(State, odDisabled);
    if bool(itemState and ODS_FOCUS) then
      Include(State, odFocused);
    if bool(itemState and ODS_GRAYED) then
      Include(State, odGrayed);
    if bool(itemState and ODS_SELECTED) then
      Include(State, odSelected);
    Canvas.Handle := hDC;
    Canvas.Font := Font;
    Canvas.Brush := Brush;

    if (Integer(itemID) >= 0) and (odSelected in State) then
    begin
      Canvas.Brush.Color := FHiliteColor;
      Canvas.Font.Color := FHiliteText;
    end;
    if Integer(itemID) >= 0 then
      DrawItem(itemID, rcItem, State)
    else
      Canvas.FillRect(rcItem);
    Canvas.Handle := 0;
  end;
end;

procedure TJvShapeTypeComboBox.DrawItem(Index: Integer; R: TRect;  State: TOwnerDrawState);
  procedure DrawShape(r:TRect);
  var
    x,y,w,h,s:integer;
    aShape:TShapeType;
  begin
    with Canvas do begin
      X := R.Left+2;
      Y := R.top+2;
      W := 28 - Pen.Width + 1;
      H := R.Bottom-R.Top-4 - Pen.Width + 1;

      if Pen.Width = 0 then begin
        Dec(W);
        Dec(H);
      end;

      if W < H then
      S := W
      else
        S := H;

      AShape:=GetEnumValue(Items[Index]);
      if aShape in [stSquare, stRoundSquare, stCircle] then begin
        Inc(X, (W - S) div 2);
        Inc(Y, (H - S) div 2);
        W := S;
        H := S;
      end;

      //Brush.Color:=clYellow;
      case aShape of
        stRectangle, stSquare:
          Rectangle(X, Y, X + W, Y + H);
        stRoundRect, stRoundSquare:
          RoundRect(X, Y, X + W, Y + H, S div 2, S div 2);
        stCircle, stEllipse:
          Ellipse(X, Y, X + W, Y + H);
      end;
    end;
  end;
var
  aColor: TColor;
begin
  with Canvas do  begin
    aColor := Brush.Color;
    Brush.Color := Color;
    FillRect(R);

    Brush.Color:=clGray;
    OffsetRect(R,2,2);
    DrawShape(R);
    OffsetRect(R,-2,-2);
    Brush.Color:=$00f0caa6;
    DrawShape(R);

    R.Left := R.Left + 28 + 6;
    R.Right := R.Left + TextWidth(Items[Index]) + 6;
    Brush.Color := aColor;

    FillRect(R);
    OffsetRect(R, 2, 0);
    DrawText(Canvas.Handle, PChar(Items[Index]), -1, R, DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX);
    OffsetRect(R, -2, 0);

    if odSelected in State then
      DrawFocusRect(R);
  end;
end;

function TJvShapeTypeComboBox.GetShapeType:TShapeType;
begin
  result:=GetEnumValue(text);
end;

procedure TJvShapeTypeComboBox.SetShapeType(value:TShapeType);
begin
  ItemIndex:=Items.IndexOf(GetEnumName(Value));
end;

{TBrushStyleComboBox}
constructor TJvBrushStyleComboBox.Create(AOwner:TComponent);
begin
  inherited create(AOwner);

  FHiliteColor := clHighLight;
  FHiLiteText := clHighLightText;
  Style := csOwnerDrawFixed;

  if not (csDesigning in ComponentState) then
    GetBrushStyles
  else
    Text:='bsSolid';
end;

procedure TJvBrushStyleComboBox.GetBrushStyles;
var
  i:integer;
begin
  Clear;
  with Items do begin
    for i:=low(BrushNames) to High(BrushNames) do begin
      Add(BrushNames[i]);
    end;
  end;
  ItemIndex:=0;
end;

function TJvBrushStyleComboBox.GetEnumName(AStyle:TBrushStyle):string;
begin
  result:=BrushNames[Integer(AStyle)];
end;

function TJvBrushStyleComboBox.GetEnumValue(AName:String):TBrushStyle;
var
  i:integer;
begin
  for i:=low(BrushNames) to High(BrushNames) do begin
    if BrushNames[i]=AName then break;
  end;

  if i<=High(BrushNames) then
    result:=BrushValues[i]
  else
    result:=bsSolid;
end;

procedure TJvBrushStyleComboBox.CNDrawItem(var Message: TWMDrawItem);
var
  State: TOwnerDrawState;
begin
  with Message.DrawItemStruct^ do
  begin
    State := [];
    if bool(itemState and ODS_CHECKED) then
      Include(State, odChecked);
    if bool(itemState and ODS_COMBOBOXEDIT) then
      Include(State, odComboBoxEdit);
    if bool(itemState and ODS_DEFAULT) then
      Include(State, odDefault);
    if bool(itemState and ODS_DISABLED) then
      Include(State, odDisabled);
    if bool(itemState and ODS_FOCUS) then
      Include(State, odFocused);
    if bool(itemState and ODS_GRAYED) then
      Include(State, odGrayed);
    if bool(itemState and ODS_SELECTED) then
      Include(State, odSelected);
    Canvas.Handle := hDC;
    Canvas.Font := Font;
    Canvas.Brush := Brush;

    if (Integer(itemID) >= 0) and (odSelected in State) then
    begin
      Canvas.Brush.Color := FHiliteColor;
      Canvas.Font.Color := FHiliteText;
    end;
    if Integer(itemID) >= 0 then
      DrawItem(itemID, rcItem, State)
    else
      Canvas.FillRect(rcItem);
    Canvas.Handle := 0;
  end;
end;

procedure TJvBrushStyleComboBox.DrawItem(Index: Integer; R: TRect;  State: TOwnerDrawState);
  procedure DrawShape(r:TRect);
  var
    x,y,w,h,s:integer;
  begin
    with Canvas do begin
      X := R.Left+2;
      Y := R.top+2;
      W := 28 - Pen.Width + 1;
      H := R.Bottom-R.Top-4 - Pen.Width + 1;

      if Pen.Width = 0 then begin
        Dec(W);
        Dec(H);
      end;

⌨️ 快捷键说明

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