📄 stocksmain.pas
字号:
unit StocksMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, DB, DBTables, Menus, ImgList,
cxControls, cxSSheet, cxSSUtils, cxSSTypes, cxSSHeaders, cxSSStyles,
StdActns, ActnList;
type
TStocksMainForm = class(TForm)
edtCellEdit: TEdit;
imgStandart : TImageList;
pnCellsRect: TPanel;
Panel2 : TPanel;
Query: TQuery;
SaveDialog: TSaveDialog;
ToolBar1 : TToolBar;
ToolBar2 : TToolBar;
alMain: TActionList;
actDeleteCells: TAction;
actSaveSpeadSheet: TAction;
actInsertCells: TAction;
actExit: TAction;
actCut: TAction;
actCopy: TAction;
actPaste: TAction;
actFormatCells: TAction;
actHideCells: TAction;
actShowCells: TAction;
actHideCol: TAction;
actShowCol: TAction;
actHideRow: TAction;
actShowRow: TAction;
pmSheetPopup: TPopupMenu;
pmiCut: TMenuItem;
pmiCopy: TMenuItem;
pmiPaste: TMenuItem;
N6: TMenuItem;
actMerge1: TMenuItem;
actSplitCells1: TMenuItem;
MenuItem1: TMenuItem;
pmiDelete: TMenuItem;
pmiInsert: TMenuItem;
MenuItem2: TMenuItem;
pmiCols: TMenuItem;
pmiColsHide: TMenuItem;
pmiColsShow: TMenuItem;
pmiRows: TMenuItem;
pmiRowsHide: TMenuItem;
pmiRowsShow: TMenuItem;
MenuItem3: TMenuItem;
MenuItem4: TMenuItem;
pmiFormatCells: TMenuItem;
ToolBar3: TToolBar;
ToolButton7: TToolButton;
ToolButton8: TToolButton;
ToolButton9: TToolButton;
ToolButton10: TToolButton;
ToolButton11: TToolButton;
mnuMain: TMainMenu;
mnuFile: TMenuItem;
miSaveSpreadSheet: TMenuItem;
MenuItem6: TMenuItem;
miExit: TMenuItem;
mnuEdit: TMenuItem;
miCut: TMenuItem;
miCopy: TMenuItem;
miPaste: TMenuItem;
N7: TMenuItem;
miCells: TMenuItem;
miFormat: TMenuItem;
miHide: TMenuItem;
miShow: TMenuItem;
miDeletecells: TMenuItem;
Insertcells1: TMenuItem;
miColumns: TMenuItem;
MenuItem7: TMenuItem;
MenuItem8: TMenuItem;
miRows: TMenuItem;
Hide3: TMenuItem;
Show3: TMenuItem;
mnuConfig: TMenuItem;
actApplyFormatting: TAction;
actLoadData: TAction;
ApplyFormatting1: TMenuItem;
LoadData1: TMenuItem;
cxSpreadBook: TcxSpreadSheetBook;
Panel3: TPanel;
Button1: TButton;
Button2: TButton;
Button4: TButton;
Button3: TButton;
actSplitCells: TAction;
actMergeCells: TAction;
procedure FormShow(Sender: TObject);
procedure ToolButton1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure cxSpreadBookSetSelection(Sender: TObject;
ASheet: TcxSSBookSheet);
procedure cxSpreadBookSheetPopupMenu(Sender: TObject; X,
Y: Integer);
procedure edtCellEditKeyPress(Sender: TObject; var Key: Char);
procedure actCellsExecute(Sender: TObject);
procedure actFormatCellsExecute(Sender: TObject);
procedure actHideCellsExecute(Sender: TObject);
procedure actShowCellsExecute(Sender: TObject);
procedure actHideColExecute(Sender: TObject);
procedure actShowColExecute(Sender: TObject);
procedure actHideRowExecute(Sender: TObject);
procedure actShowRowExecute(Sender: TObject);
procedure actCutExecute(Sender: TObject);
procedure actCopyExecute(Sender: TObject);
procedure actPasteExecute(Sender: TObject);
procedure actSaveSpeadSheetExecute(Sender: TObject);
procedure actExitExecute(Sender: TObject);
procedure actApplyFormattingUpdate(Sender: TObject);
procedure actSaveSpeadSheetUpdate(Sender: TObject);
procedure actLoadDataExecute(Sender: TObject);
procedure AlwaysEnabled(Sender: TObject);
procedure edtCellEditChange(Sender: TObject);
procedure edtCellEditExit(Sender: TObject);
procedure actApplyFormattingExecute(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure actMergeCellsExecute(Sender: TObject);
procedure actSplitCellsExecute(Sender: TObject);
private
CurRow : Integer;
FIsUpdate: Boolean;
FIsApplyFormatting: Boolean;
FSaveSpreadSheet: Boolean;
procedure SetCellText(ACol, ARow : Integer; AText : String);
procedure SetCellDate(ACol, ARow : Integer; ADate : TDateTime);
procedure SetCellFont(ALeftCol, ATopRow, ARightCol, ABottomRow : Integer;
AStyle : TFontStyles; ASize : Integer);
procedure SetCellPattern(ALeftCol, ATopRow, ARightCol, ABottomRow,
ABackground, AForeGround : Integer; AFillStyle : TcxSSFillStyle);
procedure SetCellFormat(ALeftCol, ATopRow, ARightCol, ABottomRow,
AFormat : Integer);
procedure SetCellAlignment(ALeftCol, ATopRow, ARightCol, ABottomRow : Integer;
AHorzAlign : TcxHorzTextAlign; AVertAlign : TcxVertTextAlign);
procedure SetCellBorders(ALeftCol, ATopRow, ARightCol, ABottomRow : Integer;
AEdge: integer; AStyle: TcxSSEdgeLineStyle);
procedure SaveSpreadSheet;
function GetCellText(SelectionRect: TRect; R1C1: Boolean): String;
end;
var
StocksMainForm: TStocksMainForm;
implementation
uses StocksModify;
{$R *.dfm}
procedure TStocksMainForm.FormShow(Sender: TObject);
begin
with cxSpreadBook do
begin
BeginUpdate;
Try
AutoRecalc := False;
ActiveSheet.Caption := 'Current'; // set the page caption for the first page
AddSheetPage('HiLo'); // add a second page and set it's caption
ActivePage := 0; // turn our attention to the first page
SetCellText(0,0,'Current Stockholding Valuations'); // set a title in the first column, first row cell
SetCellText(0,2,'Company'); // Col A - Set column titles
SetCellText(1,2,'Holding'); // B
SetCellText(2,2,'Purchased'); // C
SetCellText(3,2,'Price'); // D
SetCellText(4,2,'Cost'); // E = B * D
SetCellText(5,2,'Value'); // F
SetCellText(6,2,'Worth'); // G = B * F
SetCellText(7,2,'Gain'); // H = G - E
SetCellText(8,2,'%age Gain'); // I = H / E
ActivePage := 1; // switch to the second page
SetCellText(0,0,'High, Low and Current Stock Prices');
SetCellText(0,2,'Company'); // Col A
SetCellText(1,2,'Price'); // B
SetCellText(2,2,'High'); // C
SetCellText(3,2,'Low'); // D
SetCellText(4,2,'Average'); // E
AutoRecalc := True;
ActivePage := 0; // back to the first page
cxSpreadBookSetSelection(Nil,ActiveSheet); // call to set the current cell and cell contents display
Finally
EndUpdate;
end;
end;
end;
procedure TStocksMainForm.ToolButton1Click(Sender: TObject);
begin
with SaveDialog do // using the standard save dialog
if Execute then // if executed,
cxSpreadBook.SaveToFile(FileName); // then save the spreadsheet
end;
procedure TStocksMainForm.Button4Click(Sender: TObject);
begin
Close;
end;
procedure TStocksMainForm.cxSpreadBookSetSelection(Sender: TObject;
ASheet: TcxSSBookSheet);
var
CellObject: TcxSSCellObject;
begin
CellObject := cxSpreadBook.ActiveSheet.GetCellObject(ASheet.SelectionRect.Left, ASheet.SelectionRect.Top);
try
FIsUpdate := True;
edtCellEdit.Text := CellObject.Text;
pnCellsRect.Caption := GetCellText(ASheet.SelectionRect, cxSpreadBook.R1C1ReferenceStyle);
finally
CellObject.Free;
FIsUpdate := False;
end;
end;
procedure TStocksMainForm.cxSpreadBookSheetPopupMenu(Sender: TObject; X,
Y: Integer);
begin
pmSheetPopup.Popup(X, Y); // popup our menu where clicked
end;
procedure TStocksMainForm.edtCellEditKeyPress(Sender: TObject; var Key: Char);
begin
if Key = #13 then // pressing RETURN in our edit box
Windows.SetFocus(cxSpreadBook.Handle); // set focus back to the spreadsheet
end;
procedure TStocksMainForm.SetCellText(ACol, ARow : Integer; AText : String);
begin
// get a cell object for the request col and row
with cxSpreadBook.ActiveSheet.GetCellObject(ACol,ARow) do
begin
try
Text := AText;
finally
Free; // and free it (important!)
end;
end;
end;
procedure TStocksMainForm.SetCellDate(ACol, ARow : Integer; ADate : TDateTime);
begin
// get a cell object for the request col and row
with cxSpreadBook.ActiveSheet.GetCellObject(ACol,ARow) do
begin
try
DateTime := ADate;
finally
Free; // free it
end;
end;
end;
procedure TStocksMainForm.SetCellFont(ALeftCol, ATopRow, ARightCol, ABottomRow : Integer; AStyle : TFontStyles; ASize : Integer);
var
I, J : Integer;
begin
with cxSpreadBook.ActiveSheet do // using our active page
for I := ALeftCol to ARightCol do // for each column specified
for J := ATopRow to ABottomRow do // for each row specified
with GetCellObject(I,J) do // get the cell
try
Style.Font.Style := AStyle;
Style.Font.Size := ASize;
finally
Free; // free it
end;
end;
procedure TStocksMainForm.SetCellPattern(ALeftCol, ATopRow, ARightCol, ABottomRow, ABackground, AForeGround : Integer; AFillStyle : TcxSSFillStyle);
var
I, J : Integer;
begin
with cxSpreadBook.ActiveSheet do // using our active page
for I := ALeftCol to ARightCol do // for each column specified
for J := ATopRow to ABottomRow do // for each row specified
with GetCellObject(I,J) do // get the cell
try
Style.Brush.BackgroundColor := ABackground;
Style.Brush.ForegroundColor := AForeground;
Style.Brush.Style := AFillStyle;
finally
Free;
end;
end;
procedure TStocksMainForm.SetCellFormat(ALeftCol, ATopRow, ARightCol, ABottomRow, AFormat : Integer);
var
I, J : Integer;
begin
with cxSpreadBook.ActiveSheet do // using our active page
for I := ALeftCol to ARightCol do // for each column specified
for J := ATopRow to ABottomRow do // for each row specified
with GetCellObject(I,J) do // get the cell
try
Style.Format := AFormat;
finally
Free; // free it
end;
end;
procedure TStocksMainForm.SetCellAlignment(ALeftCol, ATopRow, ARightCol, ABottomRow : Integer;
AHorzAlign : TcxHorzTextAlign; AVertAlign : TcxVertTextAlign);
var
I, J : Integer;
begin
with cxSpreadBook.ActiveSheet do // using our active page
for I := ALeftCol to ARightCol do // for each column specified
for J := ATopRow to ABottomRow do // for each row specified
with GetCellObject(I,J) do // get the cell
try
Style.HorzTextAlign := AHorzAlign;
Style.VertTextAlign := AVertAlign;
finally
Free; // free it
end;
end;
procedure TStocksMainForm.SetCellBorders(ALeftCol, ATopRow, ARightCol, ABottomRow : Integer;
AEdge: Integer; AStyle: TcxSSEdgeLineStyle);
var
I, J : Integer;
begin
with cxSpreadBook.ActiveSheet do // using our active page
for I := ALeftCol to ARightCol do // for each column specified
for J := ATopRow to ABottomRow do // for each row specified
with GetCellObject(I,J) do // get the cell
try
Case AEdge of // depending on which edge has been requested
0: Style.Borders.Left.Style := AStyle; // set to the specified style
1: Style.Borders.Top.Style := AStyle;
2: Style.Borders.Right.Style := AStyle;
3: Style.Borders.Bottom.Style := AStyle;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -