cxcomponentcollectioneditor.pas

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

PAS
824
字号
  Item: TcxComponentCollectionItem;
  List: {$IFDEF DELPHI6}IDesignerSelections{$ELSE}TDesignerSelectionList{$ENDIF};
begin
  BeginUpdate;
  try
    ListView1.Selected := nil;
  finally
    CancelUpdate;
  end;
{$IFDEF DELPHI6}
  List := CreateSelectionList;
{$ELSE}
  List := TDesignerSelectionList.Create;
  try
{$ENDIF}
    Designer.GetSelections(List);
    if (List.Count = 0) or (List.Count > Collection.Count) then Exit;
    if not ((List[0] = Component) or (List[0] = Collection)
      or (TcxComponentCollectionItem(List[0]).Collection = Collection)) then Exit;
    if List.Count > ListView1.Items.Count then
      UpdateListbox;
    BeginUpdate;
    try
    {$IFDEF DELPHI6}
      ListView1.ClearSelection;
    {$ELSE}
      for I := 0 to ListView1.Items.Count - 1 do
        ListView1.Items[I].Selected := False;
    {$ENDIF}
      for I := 0 to List.Count - 1 do
      begin
        if List[I] is TcxComponentCollectionItem then
        begin
          Item := TcxComponentCollectionItem(List[I]);
          ListView1.Items[Item.Index].Selected := True
        end;
      end;
    finally
      CancelUpdate;
    end;
{$IFNDEF DELPHI6}
  finally
    List.Free;
  end;
{$ENDIF}
end;

procedure TfrmComponentCollectionEditor.SelectionsChanged(const ASelection: TDesignerSelectionList);
begin
  UpdateContent;
end;

procedure TfrmComponentCollectionEditor.SetSelection;
var
  I: Integer;
  List: {$IFDEF DELPHI6}IDesignerSelections{$ELSE}TDesignerSelectionList{$ENDIF};
begin
  if csAncestor in Component.ComponentState then
    UpdateListBox;
  if FSelectionError then Exit;
  try
    if ListView1.SelCount > 0 then
    begin
    {$IFDEF DELPHI6}
      List := CreateSelectionList;
    {$ELSE}
      List := TDesignerSelectionList.Create;
      try
    {$ENDIF}
        for I := 0 to ListView1.Items.Count - 1 do
          if ListView1.Items[I].Selected then
            List.Add(Collection.Items[I]);
        Designer.SetSelections(List);
    {$IFNDEF DELPHI6}
      finally
        List.Free;
      end;
    {$ENDIF}
    end
    else
      Designer.SelectComponent(Collection);
  except
    FSelectionError := True;
    Application.HandleException(ExceptObject);
    Close;
  end;
end;

procedure TfrmComponentCollectionEditor.UpdateListbox;

  procedure FetchItems(List: TStrings);
  var
    I: Integer;
  begin
    if Collection <> nil then
      for I := 0 to Collection.Count - 1 do
        if CanAdd(I) then
          List.AddObject(GetItemName(I), nil);
  end;

  function ItemsEqual(ListItems: TListItems; Items: TStrings): Boolean;
  var
    I: Integer;
  begin
    Result := False;
    if ListItems.Count <> Items.Count then Exit;
    for I := 0 to ListItems.Count - 1 do
    begin
      if ListItems[I].Caption <> Items[I] then
        Exit;
    end;
    Result := True;
  end;

var
  I: Integer;
  TmpItems: TStringList;
begin
  if Collection = nil then Exit;
  BeginUpdate;
  try
    TmpItems := TStringList.Create;
    FetchItems(TmpItems);
    try
      if (TmpItems.Count = 0) or not ItemsEqual(ListView1.Items, TmpItems) then
      begin
        ListView1.Items.BeginUpdate;
        try
          ListView1.Items.Clear;
          for I := 0 to TmpItems.Count - 1 do
            with ListView1.Items.Add do
              Caption := TmpItems[I];
        finally
          ListView1.Items.EndUpdate;
        end;
      end;
    finally
      TmpItems.Free;
    end;
  finally
    CancelUpdate;
  end;
end;

procedure TfrmComponentCollectionEditor.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  if Component <> nil then
    Designer.SelectComponent(Component);
  with TRegIniFile.Create(GetRegKey) do
  try
    EraseSection(FCollectionClassName);
    WriteInteger(FCollectionClassName, 'Left', Left);
    WriteInteger(FCollectionClassName, 'Top', Top);
    WriteInteger(FCollectionClassName, 'Width', Width);
    WriteInteger(FCollectionClassName, 'Height', Height);
    WriteBool(FCollectionClassName, 'TextLabels', acTextLabels.Checked);
    WriteBool(FCollectionClassName, 'Toolbar', ToolBar1.Visible);
  finally
    Free;
  end;
  BeginUpdate;
  inherited;
end;

procedure TfrmComponentCollectionEditor.FormDestroy(Sender: TObject);
begin
  if FAddedItemIDList <> nil then
    FreeAndNil(FAddedItemIDList);
  inherited;
end;

procedure TfrmComponentCollectionEditor.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_PROCESSKEY then
  {$IFDEF DELPHI6}
    ComponentDesigner.Environment.ModalEdit(#0,Self);
  {$ELSE}
    DelphiIDE.ModalEdit(#0, Self);
  {$ENDIF}
end;

procedure TfrmComponentCollectionEditor.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  {$IFDEF DELPHI6}
    ComponentDesigner.Environment.ModalEdit(#0,Self);
  {$ELSE}
    DelphiIDE.ModalEdit(#0, Self);
  {$ENDIF}
end;

procedure TfrmComponentCollectionEditor.FormShow(Sender: TObject);

  procedure UpdateSizes;
  begin
    with TRegIniFile.Create(GetRegKey) do
    try
      Top := ReadInteger(FCollectionClassName, 'Top', 100);
      Left := ReadInteger(FCollectionClassName, 'Left', 100);
      Width := ReadInteger(FCollectionClassName, 'Width', Width);
      Height := ReadInteger(FCollectionClassName, 'Height', Height);
      ToolBar1.Visible := ReadBool(FCollectionClassName, 'Toolbar', True);
      acTextLabels.Checked := ReadBool(FCollectionClassName, 'TextLabels', False);
    finally
      Free;
    end;
  end;

{$IFNDEF DELPHI6}
var
  ALeft: Integer;
  ATop: Integer;
{$ENDIF}
begin
  UpdateSizes;
{$IFNDEF DELPHI6}
  ALeft := Left;
  ATop := Top;
  if Left + Width > Monitor.Left + Monitor.Width then
    ALeft := Monitor.Left + Monitor.Width - Width;
  if Left < Monitor.Left then
    ALeft := Monitor.Left;
  if Top + Height > Monitor.Top + Monitor.Height then
    ATop := Monitor.Top + Monitor.Height - Height;
  if Top < Monitor.Top then
    ATop := Monitor.Top;
  SetBounds(ALeft, ATop, Width, Height);
{$ELSE}
  MakeFullyVisible;
{$ENDIF}
end;

procedure TfrmComponentCollectionEditor.ListView1Change(Sender: TObject;
  Item: TListItem; Change: TItemChange);
var
  Msg: TMsg;
begin
  if (Change = ctState) and (LockCount = 0) then
    if not PeekMessage(Msg, Handle, AM_DeferUpdate, AM_DeferUpdate, PM_NOREMOVE) then
      PostMessage(Handle, AM_DeferUpdate, 0, 0);
end;

procedure TfrmComponentCollectionEditor.ListView1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  Item: TListItem;
  I, J, InsPos: Integer;
  L: TList;
begin
  Item := ListView1.GetItemAt(X, Y);
  if Item <> nil then
    InsPos := Item.Index
  else Exit;
  L := TList.Create;
  try
    for I := 0 to ListView1.Items.Count - 1 do
      if ListView1.Items[I].Selected then
        L.Add(Collection.Items[I]);

    Collection.BeginUpdate;
    try
      for I := 0 to L.Count - 1 do
      with TcxComponentCollectionItem(L[I]) do
      begin
        J := Index;
        Index := InsPos;
        if (J > InsPos) and (InsPos < Collection.Count) then
          Inc(InsPos);
      end;
    finally
      Collection.EndUpdate;
    end;
  finally
    L.Free;
  end;
  GetSelection;
end;

procedure TfrmComponentCollectionEditor.ListView1DragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
var
  Item: TListItem;
begin
  Item := ListView1.GetItemAt(X, Y);
  Accept := (Item <> nil) and (Source = ListView1) and
    (not Item.Selected);
end;

procedure TfrmComponentCollectionEditor.ListView1KeyDown(Sender: TObject; var Key: word;
  Shift: TShiftState);
begin
  if Key = VK_PROCESSKEY then
  {$IFDEF DELPHI6}
    ComponentDesigner.Environment.ModalEdit(#0,Self);
  {$ELSE}
    DelphiIDE.ModalEdit(#0, Self);
  {$ENDIF}
end;

procedure TfrmComponentCollectionEditor.ListView1KeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key in ['!'..'~'] then
  begin
  {$IFDEF DELPHI6}
    ComponentDesigner.Environment.ModalEdit(Key,Self);
  {$ELSE}
    DelphiIDE.ModalEdit(Key, Self);
  {$ENDIF}
    Key := #0;
  end;
end;

procedure TfrmComponentCollectionEditor.SelectAllCommandUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := (ListView1.Items.Count > 0) and
    (ListView1.SelCount <> ListView1.Items.Count);
end;

procedure TfrmComponentCollectionEditor.AMDeferUpdate(var Msg);
begin
  if LockCount = 0 then
  begin
    if TMessage(Msg).WParam = 0 then
      SetSelection
    else
      ItemsModified(nil);
  end
  else
    PostMessage(Handle, AM_DeferUpdate, TMessage(Msg).WParam, TMessage(Msg).LParam);
end;

procedure TfrmComponentCollectionEditor.SelectionUpdate(Sender: TObject);
var
  Enabled: Boolean;
begin
  Enabled := ListView1.Selected <> nil;
  if Enabled then
    if Sender = acDelete then
      Enabled := coDelete in Options
    else
      if (Sender = acMoveUp) or (Sender = acMoveDown) then
        Enabled := coMove in Options;
  (Sender as TAction).Enabled := Enabled;
end;

procedure TfrmComponentCollectionEditor.SelectAll(DoUpdate: Boolean);
var
  I: Integer;
begin
  BeginUpdate;
  ListView1.Items.BeginUpdate;
  try
    for I := 0 to Listview1.Items.Count-1 do
      Listview1.Items[I].Selected := True;
  finally
    ListView1.Items.EndUpdate;
    CancelUpdate;
    if DoUpdate then SetSelection;
  end;
end;

procedure TfrmComponentCollectionEditor.SelectNone(DoUpdate: Boolean);
var
  I: Integer;
begin
  BeginUpdate;
  ListView1.Items.BeginUpdate;
  try
    for I := 0 to Listview1.Items.Count-1 do
      Listview1.Items[I].Selected := False;
  finally
    ListView1.Items.EndUpdate;
    CancelUpdate;
    if DoUpdate then SetSelection;
  end;
end;

procedure TfrmComponentCollectionEditor.UpdateCaption;
begin
  Caption := Format('Editing %s%s%s', [Component.Name, DotSep, ComponentPropertyName]);
end;

procedure TfrmComponentCollectionEditor.UpdateContent;
begin
  UpdateListbox;
  GetSelection;
end;

procedure TfrmComponentCollectionEditor.Activated;
begin
  inherited;
  SetSelection;
end;

function TfrmComponentCollectionEditor.CanAdd(Index: Integer): Boolean;
begin
  Result := True;
end;

procedure TfrmComponentCollectionEditor.InitFormEditor;
begin
  FCollectionClassName := Collection.ClassName;
  UpdateListbox;
  inherited InitFormEditor;
  Options := FColOptions; //refresh
end;

end.

⌨️ 快捷键说明

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