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

📄 jvcustomitemviewer.pas

📁 East make Tray Icon in delphi
💻 PAS
📖 第 1 页 / 共 4 页
字号:
    FPattern := nil;
  end;
  Result := FPattern;
end;

procedure TJvBrushPattern.SetEvenColor(const Value: TColor);
begin
  if FEvenColor <> Value then
  begin
    if FPattern <> nil then
      ReleasePattern(EvenColor, OddColor);
    FEvenColor := Value;
    FPattern := nil;
  end;
end;

procedure TJvBrushPattern.SetOddColor(const Value: TColor);
begin
  if FOddColor <> Value then
  begin
    if FPattern <> nil then
      ReleasePattern(EvenColor, OddColor);
    FOddColor := Value;
    FPattern := nil;
  end;
end;

//=== { TJvCustomItemViewerOptions } =========================================

constructor TJvCustomItemViewerOptions.Create(AOwner: TJvCustomItemViewer);
begin
  inherited Create;
  FOwner := AOwner;
  FWidth := 120;
  FHeight := 120;
  FVertSpacing := 4;
  FHorzSpacing := 4;
  FScrollBar := tvVertical;
  FSmooth := False;
  FTracking := True;
  FLazyRead := True;
  FShowCaptions := False;
  FAlignment := taCenter;
  FLayout := tlBottom;
  FDragAutoScroll := True;
  FBrushPattern := TJvBrushPattern.Create;
end;

destructor TJvCustomItemViewerOptions.Destroy;
begin
  FBrushPattern.Free;
  inherited Destroy;
end;

procedure TJvCustomItemViewerOptions.Assign(Source: TPersistent);
begin
  if Source is TJvCustomItemViewerOptions then
  begin
    if Source <> Self then
    begin
      FWidth := TJvCustomItemViewerOptions(Source).Width;
      FHeight := TJvCustomItemViewerOptions(Source).Height;
      FVertSpacing := TJvCustomItemViewerOptions(Source).VertSpacing;
      FHorzSpacing := TJvCustomItemViewerOptions(Source).HorzSpacing;
      FScrollBar := TJvCustomItemViewerOptions(Source).ScrollBar;
      FAutoCenter := TJvCustomItemViewerOptions(Source).AutoCenter;
      FSmooth := TJvCustomItemViewerOptions(Source).Smooth;
      FTracking := TJvCustomItemViewerOptions(Source).Tracking;
      FHotTrack := TJvCustomItemViewerOptions(Source).HotTrack;
      FMultiSelect := TJvCustomItemViewerOptions(Source).MultiSelect;
      BrushPattern.FEvenColor := BrushPattern.EvenColor;
      BrushPattern.FOddColor := BrushPattern.OddColor;
      BrushPattern.FActive := BrushPattern.Active;
      Change;
    end;
  end
  else
    inherited Assign(Source);
end;

procedure TJvCustomItemViewerOptions.Change;
begin
  if FOwner <> nil then
    FOwner.OptionsChanged;
end;

procedure TJvCustomItemViewerOptions.SetAlignment(const Value: TAlignment);
begin
  if FAlignment <> Value then
  begin
    FAlignment := Value;
    if ShowCaptions then
      Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetAutoCenter(const Value: Boolean);
begin
  if FAutoCenter <> Value then
  begin
    FAutoCenter := Value;
    Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetBrushPattern(const Value: TJvBrushPattern);
begin
  //  FBrushPattern := Value;
end;

procedure TJvCustomItemViewerOptions.SetHeight(const Value: Integer);
begin
  if FHeight <> Value then
  begin
    FHeight := Value;
    Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetHorzSpacing(const Value: Integer);
begin
  if FHorzSpacing <> Value then
  begin
    FHorzSpacing := Value;
    Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetHotTrack(const Value: Boolean);
begin
  if FHotTrack <> Value then
  begin
    FHotTrack := Value;
    Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetLayout(const Value: TTextLayout);
begin
  if FLayout <> Value then
  begin
    FLayout := Value;
    if ShowCaptions then
      Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetLazyRead(const Value: Boolean);
begin
  if LazyRead <> Value then
  begin
    FLazyRead := Value;
    if not FLazyRead then
      FReduceMemoryUsage := False;
    Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetMultiSelect(const Value: Boolean);
begin
  if FMultiSelect <> Value then
  begin
    FMultiSelect := Value;
    Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetReduceMemoryUsage(const Value: Boolean);
begin
  if FReduceMemoryUsage <> Value then
  begin
    FReduceMemoryUsage := Value;
    if FReduceMemoryUsage then
    begin
      FLazyRead := True;
      FOwner.DoReduceMemory;
    end;
  end;
end;

procedure TJvCustomItemViewerOptions.SetRightClickSelect(const Value: Boolean);
begin
  FRightClickSelect := Value;
  // no need to tell owner
end;

procedure TJvCustomItemViewerOptions.SetScrollBar(const Value: TJvItemViewerScrollBar);
begin
  if FScrollBar <> Value then
  begin
    FScrollBar := Value;
    Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetShowCaptions(const Value: Boolean);
begin
  if FShowCaptions <> Value then
  begin
    FShowCaptions := Value;
    Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetSmooth(const Value: Boolean);
begin
  if FSmooth <> Value then
  begin
    FSmooth := Value;
    Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetTracking(const Value: Boolean);
begin
  if FTracking <> Value then
  begin
    FTracking := Value;
    Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetVertSpacing(const Value: Integer);
begin
  if FVertSpacing <> Value then
  begin
    FVertSpacing := Value;
    Change;
  end;
end;

procedure TJvCustomItemViewerOptions.SetWidth(const Value: Integer);
begin
  if FWidth <> Value then
  begin
    FWidth := Value;
    Change;
  end;
end;

//=== { TJvViewerItem } ======================================================

constructor TJvViewerItem.Create(AOwner: TJvCustomItemViewer);
begin
  inherited Create;
  FOwner := AOwner;
end;

procedure TJvViewerItem.Changed;
begin
  if FOwner <> nil then
    FOwner.ItemChanged(Self);
end;

function TJvViewerItem.Changing: Boolean;
begin
  Result := True;
  if FOwner <> nil then
    FOwner.ItemChanging(Self, Result);
end;

procedure TJvViewerItem.Delete;
begin
  if FOwner <> nil then
  begin
    FDeleting := True;
    PostMessage(FOwner.Handle, CM_DELETEITEM, Integer(Self), 0);
  end;
end;

procedure TJvViewerItem.ReduceMemoryUsage;
begin
  // override to perform whatever you can to reduce the memory usage
end;

procedure TJvViewerItem.SetData(const Value: Pointer);
begin
  if (FData <> Value) and Changing then
  begin
    FData := Value;
    Changed;
  end;
end;

procedure TJvViewerItem.SetState(const Value: TCustomDrawState);
begin
  if (FState <> Value) and Changing then
  begin
    FState := Value;
    Changed;
  end;
end;

//=== { TJvCustomItemViewer } ================================================

constructor TJvCustomItemViewer.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ParentColor := False;
  ControlStyle := [csCaptureMouse, csDisplayDragImage, csClickEvents, csOpaque, csDoubleClicks];
  FItems := TList.Create;
  FOptions := GetOptionsClass.Create(Self);
  FCanvas := TControlCanvas.Create;
  TControlCanvas(FCanvas).Control := Self;
  FSelectedIndex := -1;
  FLastHotTrack := -1;
  AutoScroll := False;
  HorzScrollBar.Smooth := Options.Smooth;
  HorzScrollBar.Tracking := Options.Tracking;
  VertScrollBar.Smooth := Options.Smooth;
  VertScrollBar.Tracking := Options.Tracking;
  DoubleBuffered := True;
  FBorderStyle := bsSingle;
  Width := 185;
  Height := 150;
  TabStop := True;
end;

destructor TJvCustomItemViewer.Destroy;
begin
  StopScrollTimer;
  Clear;
  FItems.Free;
  FOptions.Free;
  inherited Destroy;
  // (rom) destroy Canvas always after inherited
  FreeAndNil(FCanvas);
end;

function TJvCustomItemViewer.Add(AItem: TJvViewerItem): Integer;
begin
  Insert(FItems.Count + 1, AItem);
  Result := FItems.Count - 1;
end;

procedure TJvCustomItemViewer.BeginUpdate;
begin
  Inc(FUpdateCount);
end;

procedure TJvCustomItemViewer.CalcIndices;
begin
  FTopLeftIndex := ItemAtPos(0, 0, True);
  FBottomRightIndex := ItemAtPos(ClientWidth, ClientHeight, True);
  if FBottomRightIndex < 0 then
    FBottomRightIndex := ItemAtPos(ClientWidth, ClientHeight, False) - 1;
  if FTopLeftIndex < 0 then
    FTopLeftIndex := 0;
  if FTopLeftIndex >= Count then
    FTopLeftIndex := Count - 1;
  if FBottomRightIndex < 0 then
    FBottomRightIndex := 0;
  if FBottomRightIndex >= Count then
    FBottomRightIndex := Count - 1;
  DoReduceMemory;
end;

procedure TJvCustomItemViewer.OptionsChanged;
begin
  Changed;
  if Assigned(FOnOptionsChanged) then
    FOnOptionsChanged(Self);
end;

procedure TJvCustomItemViewer.CheckHotTrack;
var
  P: TPoint;
  I: Integer;
begin
  if Options.HotTrack and GetCursorPos(P) then
  begin
    P := ScreenToClient(P);
    if not PtInRect(ClientRect, P) then
      I := -1
    else
      I := ItemAtPos(P.X, P.Y, True);
    // remove hot track state from previous item
    if (FLastHotTrack >= 0) and (FLastHotTrack < Count) and (I <> FLastHotTrack) then
      Items[FLastHotTrack].State := Items[FLastHotTrack].State - [cdsHot];
    if (I >= 0) and (I < Count) then
    begin
      Items[I].State := Items[I].State + [cdsHot];
      FLastHotTrack := I;
    end
    else
      FLastHotTrack := -1;
  end;
end;

procedure TJvCustomItemViewer.Clear;
var
  I: Integer;
begin
  BeginUpdate;
  try
    for I := 0 to FItems.Count - 1 do
      TObject(FItems[I]).Free;
    FItems.Count := 0;
  finally
    EndUpdate;
  end;
end;

procedure TJvCustomItemViewer.CMCtl3DChanged(var Msg: TMessage);
begin
  if NewStyleControls and (FBorderStyle = bsSingle) then
    RecreateWnd;
  inherited;
end;

procedure TJvCustomItemViewer.CMDeleteItem(var Msg: TMessage);
var
  I: Integer;
begin
  I := FItems.IndexOf(TObject(Msg.WParam));
  if (I >= 0) and (I < Count) then
  begin
    Delete(I);
    InvalidateClipRect(ClientRect);
  end;
end;

procedure TJvCustomItemViewer.MouseLeave(Control: TControl);
begin
  if csDesigning in ComponentState then
    Exit;
  inherited MouseLeave(Control);
  CheckHotTrack;
end;

procedure TJvCustomItemViewer.CMUnselectItem(var Msg: TMessage);
var
  I: Integer;
begin
  if Msg.WParam = Integer(Self) then
  begin
    BeginUpdate;
    try
      for I := 0 to Count - 1 do
        if (Integer(Items[I]) <> Msg.LParam) and
          (cdsSelected in Items[I].State) then
          Items[I].State := Items[I].State - [cdsSelected];
    finally
      EndUpdate;
    end;
  end;
end;

function TJvCustomItemViewer.ColRowToIndex(ACol, ARow: Integer): Integer;
begin
  Result := ACol + ARow * FCols
end;

procedure TJvCustomItemViewer.CreateParams(var Params: TCreateParams);
const
  BorderStyles: array [TBorderStyle] of DWORD = (0, WS_BORDER);
begin
  inherited CreateParams(Params);
  with Params do
  begin
    Style := Style or BorderStyles[BorderStyle];
    if NewStyleControls and Ctl3D and (BorderStyle = bsSingle) then
    begin
      Style := Style and not WS_BORDER;
      ExStyle := ExStyle or WS_EX_CLIENTEDGE;
    end;
  end;
  with Params.WindowClass do
    Style := Style or (CS_HREDRAW or CS_VREDRAW); { or CS_SAVEBITS}
end;

procedure TJvCustomItemViewer.Delete(Index: Integer);
begin
  Deleted(Items[Index]);
  TObject(FItems[Index]).Free;
  FItems.Delete(Index);
  if SelectedIndex >= Count then
    SelectedIndex := Count - 1;
end;

function TJvCustomItemViewer.DoMouseWheel(Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint): Boolean;
var
  WD: Integer;
begin
  if not inherited DoMouseWheel(Shift, WheelDelta, MousePos) then
  begin
    if Shift * KeyboardShiftStates = [ssShift] then
      WD := WheelDelta * 3
    else
      WD := WheelDelta;
    if Options.ScrollBar = tvHorizontal then
      HorzScrollBar.Position := HorzScrollBar.Position - WD
    else
      VertScrollBar.Position := VertScrollBar.Position - WD;
    UpdateOffset;
    Invalidate;
  end;
  Result := True;
end;

⌨️ 快捷键说明

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