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

📄 jvgrids.pas

📁 East make Tray Icon in delphi
💻 PAS
📖 第 1 页 / 共 3 页
字号:
  if FTracking then
  begin
    TrackButton(-1, -1);
    FTracking := False;
    MouseCapture := False;
  end;
end;

procedure TJvDrawGrid.TrackButton(X, Y: Integer);
var
  Cell: TGridCoord;
  NewPressed: Boolean;
begin
  Cell := MouseCoord(X, Y);
  NewPressed := PtInRect(Rect(0, 0, ClientWidth, ClientHeight), Point(X, Y)) and
    (FPressedCell.X = Cell.X) and (FPressedCell.Y = Cell.Y);
  if FPressed <> NewPressed then
  begin
    FPressed := NewPressed;
    InvalidateCell(Cell.X, Cell.Y);
    InvalidateCell(FPressedCell.X, FPressedCell.Y);
  end;
end;

{$IFDEF VCL}
function TJvDrawGrid.IsActiveControl: Boolean;
var
  Handle: THandle;
  ParentForm: TCustomForm;
begin
  ParentForm := GetParentForm(Self);
  if Assigned(ParentForm) then
    Result := ParentForm.ActiveControl = Self
  else
  begin
    Handle := GetFocus;
    Result := False;
    while not Result and IsWindow(Handle) do
    begin
      if Handle = WindowHandle then
        Result := True
      else
        Handle := GetParent(Handle);
    end;
  end;
end;
{$ENDIF VCL}

procedure TJvDrawGrid.MouseDown(Button: TMouseButton; Shift: TShiftState;
  X, Y: Integer);
var
  Cell: TGridCoord;
  EnableClick, Fixed: Boolean;
begin
  if DrawButtons then
  begin
    if (Button = mbLeft) and ((Shift - [ssLeft]) = []) then
    begin
      MouseToCell(X, Y, Cell.X, Cell.Y);
      if (Cell.X >= FixedCols) and (Cell.Y >= FixedRows) then
      begin
        FCellDown := Cell;
        InvalidateCell(Cell.X, Cell.Y);
      end;
    end;
    inherited MouseDown(Button, Shift, X, Y);
    Exit;
  end;

  HideEditor;
  if not (csDesigning in ComponentState) and
    (CanFocus or (GetParentForm(Self) = nil)) then
  begin
    SetFocus;
    if not IsActiveControl then
    begin
      MouseCapture := False;
      Exit;
    end;
  end;
  if (Button = mbLeft) and (ssDouble in Shift) then
  begin
    if FFixedCellsButtons then
    begin
      Cell := MouseCoord(X, Y);
      if not ((Cell.X >= 0) and (Cell.X < FixedCols)) and
        not ((Cell.Y >= 0) and (Cell.Y < FixedRows)) then
      begin
        DblClick;
        Exit;
      end;
    end
    else
    begin
      DblClick;
      Exit;
    end;
  end;
  if Sizing(X, Y) then
    inherited MouseDown(Button, Shift, X, Y)
  else
  begin
    Cell := MouseCoord(X, Y);
    Fixed := ((Cell.X >= 0) and (Cell.X < FixedCols)) or
      ((Cell.Y >= 0) and (Cell.Y < FixedRows));
    if FFixedCellsButtons and Fixed and not (csDesigning in ComponentState) then
    begin
      if ([goRowMoving, goColMoving] * Options <> []) and
        (Button = mbRight) then
      begin
        Button := mbLeft;
        FSwapButtons := True;
        MouseCapture := True;
      end
      else
      if Button = mbLeft then
      begin
        EnableClick := True;
        CheckFixedCellButton(Cell.X, Cell.Y, EnableClick);
        if EnableClick then
        begin
          MouseCapture := True;
          FTracking := True;
          FPressedCell := Cell;
          TrackButton(X, Y);
        end
        else
        if BeepOnError then
          Beep;
        Exit;
      end;
    end;
    inherited MouseDown(Button, Shift, X, Y);
  end;
end;

procedure TJvDrawGrid.MouseMove(Shift: TShiftState; X, Y: Integer);
var
  Cell: TGridCoord;
begin
  if DrawButtons then
  begin
    if Shift = [ssLeft] then
    begin
      MouseToCell(X, Y, Cell.X, Cell.Y);
      if not CompareMem(@Cell, @FCellDown, SizeOf(Cell)) then
      begin
        if (FCellDown.X >= 0) and (FCellDown.Y >= 0) then
          InvalidateCell(FCellDown.X, FCellDown.Y);
        FCellDown := Cell;
        InvalidateCell(Cell.X, Cell.Y);
      end;
    end;
    inherited MouseMove(Shift, X, Y);
    Exit;
  end;

  if FTracking then
    TrackButton(X, Y);
  inherited MouseMove(Shift, X, Y);
end;

procedure TJvDrawGrid.MouseUp(Button: TMouseButton; Shift: TShiftState;
  X, Y: Integer);
var
  Cell: TGridCoord;
  ACol, ARow: Longint;
  DoClick: Boolean;
begin
  if DrawButtons then
  begin
    if (Button = mbLeft) and (Shift = []) then
    begin
      InvalidateCell(FCellDown.X, FCellDown.Y);
      FCellDown.X := -1;
      FCellDown.Y := -1;
    end;
    inherited MouseUp(Button, Shift, X, Y);
    Exit;
  end;

  if FTracking and (FPressedCell.Y >= 0) and (FPressedCell.X >= 0) then
  begin
    Cell := MouseCoord(X, Y);
    DoClick := PtInRect(Rect(0, 0, ClientWidth, ClientHeight), Point(X, Y)) and
      (Cell.Y = FPressedCell.Y) and (Cell.X = FPressedCell.X);
    StopTracking;
    if DoClick then
    begin
      ACol := Cell.X;
      ARow := Cell.Y;
      if (ARow < RowCount) and (ACol < ColCount) then
        DoFixedCellClick(ACol, ARow);
    end;
  end
  else
  if FSwapButtons then
  begin
    FSwapButtons := False;
    MouseCapture := False;
    if Button = mbRight then
      Button := mbLeft;
  end;
  inherited MouseUp(Button, Shift, X, Y);
end;

procedure TJvDrawGrid.Paint;
var
  R: TRect;
begin
  FDefaultDrawing := inherited DefaultDrawing;
  inherited DefaultDrawing := False;
  try
    inherited Paint;
  finally
    inherited DefaultDrawing := FDefaultDrawing;
  end;
  if not (csDesigning in ComponentState) and DefaultDrawing and Focused and
    ([goRowSelect, goRangeSelect] * Options = [goRowSelect]) then
  begin
    Canvas.Font.Color := Font.Color;
    Canvas.Brush.Color := Color;
    if Row >= FixedRows then
    begin
      R := BoxRect(FixedCols, Row, ColCount - 1, Row);
      if not (goHorzLine in Options) then
        Inc(R.Bottom, GridLineWidth);
      DrawFocusRect(Canvas.Handle, R);
    end;
  end;
end;

procedure TJvDrawGrid.CallDrawCellEvent(ACol, ARow: Longint; ARect: TRect;
  AState: TGridDrawState);
begin
  inherited DrawCell(ACol, ARow, ARect, AState);
end;

procedure TJvDrawGrid.DoDrawCell(ACol, ARow: Longint; ARect: TRect;
  AState: TGridDrawState);
begin
  CallDrawCellEvent(ACol, ARow, ARect, AState);
end;

procedure TJvDrawGrid.DrawCell(ACol, ARow: Longint; ARect: TRect;
  AState: TGridDrawState);
var
  Down: Boolean;
  TempRect: TRect;
  FrameFlags1, FrameFlags2: DWORD;
  Style: DWORD;
const
  EdgeFlag: array [Boolean] of UINT = (BDR_RAISEDINNER, BDR_SUNKENINNER);
begin
  if DrawButtons then
  begin
    TempRect := ARect;
    if not (gdFixed in AState) then
    begin
      Canvas.Brush.Color := clBtnFace;
      Canvas.Font.Color := clBtnText;
      Style := DFCS_BUTTONPUSH or DFCS_ADJUSTRECT;
      if (FCellDown.X = ACol) and (FCellDown.Y = ARow) then
        Style := Style or DFCS_PUSHED;
      {$IFDEF VisualCLX}
      RequiredState(Canvas, [csHandleValid, csPenValid, csBrushValid]);
      {$ENDIF VisualCLX}
      DrawFrameControl(Canvas.Handle, TempRect, DFC_BUTTON, Style);
    end;
    inherited DrawCell(ACol,ARow,ARect,AState);
    Exit;
  end;

  if FDefaultDrawing or (csDesigning in ComponentState) then
    with Canvas do
    begin
      Font := Self.Font;
      if ([goRowSelect, goVertLine] * Options = [goRowSelect]) and
        not (gdFixed in AState) then
        Inc(ARect.Right, GridLineWidth);
      if ([goRowSelect, goHorzLine] * Options = [goRowSelect]) and
        not (gdFixed in AState) then
        Inc(ARect.Bottom, GridLineWidth);
      if (gdSelected in AState) and (not (gdFocused in AState) or
        ([goDrawFocusSelected, goRowSelect] * Options <> [])) then
      begin
        Brush.Color := clHighlight;
        Font.Color := clHighlightText;
      end
      else
      begin
        if gdFixed in AState then
          Brush.Color := FixedColor
        else
          Brush.Color := Color;
      end;
      FillRect(ARect);
    end;
  Down := FFixedCellsButtons and (gdFixed in AState) and
    {$IFDEF VCL}
    Ctl3D and
    {$ENDIF VCL}
    not (csLoading in ComponentState) and FPressed and FDefaultDrawing and
    (FPressedCell.X = ACol) and (FPressedCell.Y = ARow);
  inherited DefaultDrawing := FDefaultDrawing;
  if Down then
  begin
    Inc(ARect.Left, GridLineWidth);
    Inc(ARect.Top, GridLineWidth);
  end;
  try
    DoDrawCell(ACol, ARow, ARect, AState);
  finally
    inherited DefaultDrawing := False;
    if Down then
    begin
      Dec(ARect.Left, GridLineWidth);
      Dec(ARect.Top, GridLineWidth);
    end;
  end;
  if FDefaultDrawing and
     {$IFDEF VCL}
     Ctl3D and
     {$ENDIF VCL}
     (gdFixed in AState) then
  begin
    FrameFlags1 := 0;
    FrameFlags2 := 0;
    if goFixedVertLine in Options then
    begin
      FrameFlags1 := BF_RIGHT;
      FrameFlags2 := BF_LEFT;
    end;
    if goFixedHorzLine in Options then
    begin
      FrameFlags1 := FrameFlags1 or BF_BOTTOM;
      FrameFlags2 := FrameFlags2 or BF_TOP;
    end;
    if (FrameFlags1 or FrameFlags2) <> 0 then
    begin
      TempRect := ARect;
      if ((FrameFlags1 and BF_RIGHT) = 0) and
        (goFixedVertLine in Options) then
        Inc(TempRect.Right, GridLineWidth)
      else
      if ((FrameFlags1 and BF_BOTTOM) = 0) and
        (goFixedVertLine in Options) then
        Inc(TempRect.Bottom, GridLineWidth);
      {$IFDEF VisualCLX}
      RequiredState(Canvas, [csHandleValid, csPenValid, csBrushValid]);
      {$ENDIF VisualCLX}
      DrawEdge(Canvas.Handle, TempRect, EdgeFlag[Down], FrameFlags1);
      DrawEdge(Canvas.Handle, TempRect, EdgeFlag[Down], FrameFlags2);
    end;
  end;
  if FDefaultDrawing and not (csDesigning in ComponentState) and
    (gdFocused in AState) and
    ([goEditing, goAlwaysShowEditor] * Options <> [goEditing, goAlwaysShowEditor]) and
    not (goRowSelect in Options) then
    DrawFocusRect(Canvas.Handle, ARect);
end;

{$IFDEF VCL}

procedure TJvDrawGrid.WMRButtonUp(var Msg: TWMMouse);
begin
  if not (FGridState in [gsColMoving, gsRowMoving]) then
    inherited
  else
  if not (csNoStdEvents in ControlStyle) then
    with Msg do
      MouseUp(mbRight, KeysToShiftState(Keys), XPos, YPos);
end;

procedure TJvDrawGrid.WMLButtonDblClk(var Msg: TWMLButtonDblClk);
var
  Cell: TGridCoord;
begin
  if FFixedCellsButtons then
  begin
    with Msg do
      Cell := MouseCoord(XPos, YPos);
    if ((Cell.X >= 0) and (Cell.X < FixedCols)) or
      ((Cell.Y >= 0) and (Cell.Y < FixedRows)) then
    begin
      SendCancelMode(Self);
      if csCaptureMouse in ControlStyle then
        MouseCapture := True;
      if not (csNoStdEvents in ControlStyle) then
        with Msg do
          MouseDown(mbLeft, KeysToShiftState(Keys) - [ssDouble], XPos, YPos);
      Exit;
    end;
  end;
  inherited;
end;

{$ENDIF VCL}

procedure TJvDrawGrid.FocusKilled(NextWnd: HWND);
begin
  inherited FocusKilled(NextWnd);
  if Assigned(FOnChangeFocus) then
    FOnChangeFocus(Self);
end;

procedure TJvDrawGrid.FocusSet(PrevWnd: HWND);
begin
  inherited FocusSet(PrevWnd);
  if Assigned(FOnChangeFocus) then
    FOnChangeFocus(Self);
end;

{$IFDEF VCL}
procedure TJvDrawGrid.WMCancelMode(var Msg: TMessage);
begin
  StopTracking;
  inherited;
end;
{$ENDIF VCL}

function TJvDrawGrid.CreateEditor: TInplaceEdit;
begin
  Result := TJvInplaceEdit.Create(Self);
  TEdit(Result).OnChange := EditChanged;
end;

function TJvDrawGrid.GetEditAlignment(ACol, ARow: Longint): TAlignment;
begin
  Result := taLeftJustify;
  if Assigned(FOnGetEditAlign) then
    FOnGetEditAlign(Self, ACol, ARow, Result);
end;

function TJvDrawGrid.GetEditStyle(ACol, ARow: Longint): TInplaceEditStyle;
begin
  Result := ieSimple;
  if Assigned(FOnGetEditStyle) then
    FOnGetEditStyle(Self, ACol, ARow, Result);
end;

procedure TJvDrawGrid.GetPickList(ACol, ARow: Longint; PickList: TStrings);
begin
  if Assigned(FOnGetPicklist) then
    FOnGetPicklist(Self, ACol, ARow, PickList);
end;

procedure TJvDrawGrid.EditButtonClick;
begin
  if Assigned(FOnEditButtonClick) then
    FOnEditButtonClick(Self);
end;

{$IFDEF VCL}
procedure TJvDrawGrid.WMHScroll(var Msg: TWMHScroll);
begin
  inherited;
  if Assigned(FOnHScroll) then
    FOnHScroll(Self);
end;

procedure TJvDrawGrid.WMVScroll(var Msg: TWMVScroll);
begin
  inherited;
  if Assigned(FOnVScroll) then
    FOnVScroll(Self);
end;
{$ENDIF VCL}

procedure TJvDrawGrid.SetDrawButtons(const Value: Boolean);
begin
  if FDrawButtons <> Value then
  begin
    FDrawButtons := Value;
    Invalidate;
  end;
end;

function TJvDrawGrid.SelectCell(ACol, ARow: Integer): Boolean;
begin
  if DrawButtons then
    Result := False
  else
    Result := inherited SelectCell(ACol, ARow);
end;

{$IFDEF UNITVERSIONING}
initialization
  RegisterUnitVersion(HInstance, UnitVersioning);

finalization
  UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}

end.

⌨️ 快捷键说明

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