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

📄 stocksmain.cpp

📁 DevExpress公司出品的Borland Delphi和C++ Builder的控件(包含完整源代码)。 ExpressSpreadSheet:交叉数据表格控件。 一款Delphi
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdlib.h>
#pragma hdrstop

#include "StocksMain.h"
#include "StocksModify.h"
#include "string.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxControls"
#pragma link "cxSSheet"
#pragma resource "*.dfm"
TStocksMainForm *StocksMainForm;
//---------------------------------------------------------------------------
__fastcall TStocksMainForm::TStocksMainForm(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TStocksMainForm::SetCellText(int ACol, int ARow, String AText)
{
  TcxSSCellObject *CellObject;
  // get a cell object for the request col and row
  CellObject = cxSpreadBook->ActiveSheet->GetCellObject(ACol, ARow);
  try {
//    edtCellEdit->Text = CellObject->Text;
    CellObject->SetCellText(AText, true);
  }
  __finally {
    delete CellObject;                                      // and free it
  }
}

void __fastcall TStocksMainForm::SetCellDate(int ACol, int ARow, TDateTime ADate)
{
  TcxSSCellObject *ObjectCell;
  // get a cell object for the request col and row
  ObjectCell = cxSpreadBook->ActiveSheet->GetCellObject(ACol, ARow);
    try {
      ObjectCell->DateTime = ADate;
    }
    __finally {
      delete ObjectCell;                                    // free it
    }
}

void __fastcall TStocksMainForm::SetCellFont(int ALeftCol, int ATopRow, int ARightCol, int ABottomRow,
                        TFontStyles AStyle, int ASize)
{
  TcxSSCellObject *ObjectCell;
  for(int i = ALeftCol; i <= ARightCol; i++)                // for each column specified
    for(int j = ATopRow; j <= ABottomRow; j++) {            // form each row specified
      ObjectCell = cxSpreadBook->ActiveSheet->GetCellObject(i, j); // get the cell
      try {
        ObjectCell->Style->Font->Style = AStyle;            // set the font style
        ObjectCell->Style->Font->Size = ASize;              // and size
      }
      __finally {
        delete ObjectCell;                                  // free it
      }
    }
}

void __fastcall TStocksMainForm::SetCellPattern(int ALeftCol, int ATopRow, int ARightCol, int ABottomRow,
                           Word ABackground, Word AForeground, TcxSSFillStyle AFillStyle)
{
  TcxSSCellObject *ObjectCell;
  for(int i = ALeftCol; i <= ARightCol; i++)                // for each column specified
    for(int j = ATopRow; j <= ABottomRow; j++) {            // form each row specified
      ObjectCell = cxSpreadBook->ActiveSheet->GetCellObject(i, j);  // get the cell
      try {
        ObjectCell->Style->Brush->BackgroundColor = ABackground;    // set the specified background
        ObjectCell->Style->Brush->ForegroundColor = AForeground;    //               and foreground colour
        ObjectCell->Style->Brush->Style = AFillStyle;               //               and fill style
      }
      __finally {
        delete ObjectCell;
      }
   }
}

void __fastcall TStocksMainForm::SetCellFormat(int ALeftCol, int ATopRow, int ARightCol, int ABottomRow, Word
                          AFormat)
{
  TcxSSCellObject *ObjectCell;
  for(int i = ALeftCol; i <= ARightCol; i++)                // for each column specified
    for(int j = ATopRow; j <= ABottomRow; j++) {            // form each row specified
      ObjectCell = cxSpreadBook->ActiveSheet->GetCellObject(i, j); // get the cell
      try {
        ObjectCell->Style->Format = AFormat;                // set the specified cell format
      }
      __finally {
        delete ObjectCell;
      }
   }
}

void __fastcall TStocksMainForm::SetCellAlignment(int ALeftCol, int ATopRow, int ARightCol, int ABottomRow,
                             TcxHorzTextAlign AHorzAlign, TcxVertTextAlign AVertAlign)
{
  TcxSSCellObject *ObjectCell;
  for(int i = ALeftCol; i <= ARightCol; i++)                // for each column specified
    for(int j = ATopRow; j <= ABottomRow; j++) {            // form each row specified
      ObjectCell = cxSpreadBook->ActiveSheet->GetCellObject(i, j); // get the cell
      try {
        ObjectCell->Style->HorzTextAlign = AHorzAlign;      // set the specified horizontal text alignment
        ObjectCell->Style->VertTextAlign = AVertAlign;      // and vertical alignment
      }
      __finally {
        delete ObjectCell;
      }
   }
}

void __fastcall TStocksMainForm::SetCellBorders(int ALeftCol, int ATopRow, int ARightCol, int ABottomRow,
                           int AEdge, TcxSSEdgeLineStyle AStyle)
{
  TcxSSCellObject *ObjectCell;
  for(int i = ALeftCol; i <= ARightCol; i++)                // for each column specified
    for(int j = ATopRow; j <= ABottomRow; j++) {            // form each row specified
      ObjectCell = cxSpreadBook->ActiveSheet->GetCellObject(i, j); // get the cell
      try {
        switch (AEdge) {
          case 0: ObjectCell->Style->Borders->Left->Style = AStyle;
          case 1: ObjectCell->Style->Borders->Top->Style = AStyle;
          case 2: ObjectCell->Style->Borders->Right->Style = AStyle;
          case 3: ObjectCell->Style->Borders->Bottom->Style = AStyle;
        }
      }
      __finally {
        delete ObjectCell;
      }
   }
}


void __fastcall TStocksMainForm::AlwaysEnabled(TObject *Sender)
{
  ((TCustomAction*)Sender)->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TStocksMainForm::actLoadDataExecute(TObject *Sender)
{
  TCursor CurCursor;
  CurCursor = Screen->Cursor;                               // preserve the current one
  Screen->Cursor = crHourGlass;                             // wait cursor
  try {
    cxSpreadBook->BeginUpdate();                            // turn off updates
    cxSpreadBook->ActivePage = 0;                           // ensure we are looking at the right page
    FCurRow = 3;                                            // set the starting current row
    Query->Open();                                          // open the data
    while (!Query->Eof) {
        SetCellText(0, FCurRow,                             // set the company name
                         Query->FieldByName("CO_NAME")->AsString);
        SetCellText(1,FCurRow,                              // the number of shares
                          Query->FieldByName("SHARES")->AsString);
        SetCellDate(2,FCurRow,                              // the purchase date
                      Query->FieldByName("PUR_DATE")->AsDateTime);
        SetCellText(3,FCurRow,                              // the purchase price
                       Query->FieldByName("PUR_PRICE")->AsString);
        SetCellText(4,FCurRow,                              // calculate the purchase cost = number of shares * purchase price
              Format("=B%d*D%d/100",
              ARRAYOFCONST((FCurRow + 1, FCurRow + 1))));   // [FCurRow+1,FCurRow+1]));
        SetCellText(5,FCurRow,                              // the current price
                       Query->FieldByName("CUR_PRICE")->AsString);
        SetCellText(6,FCurRow,                              // calculate the current valuation = number of shares * current price
              Format("=B%d*F%d/100", ARRAYOFCONST((FCurRow + 1, FCurRow + 1))));
        SetCellText(7,FCurRow,                              // calculate the gain/loss = current valuation - purchase cost
                  Format("=G%d-E%d",ARRAYOFCONST((FCurRow + 1, FCurRow + 1))));
        SetCellText(8,FCurRow,                              // calculate the %age gain/loss
                  Format("=H%d/E%d",ARRAYOFCONST((FCurRow+1,FCurRow+1))));
        Query->Next();                                      // and the next record
        FCurRow++;                                          // and bump the row number
      }
      FCurRow++;
      SetCellText(4, FCurRow,                               // set the formula for the total purchase cost
                       Format("=SUM(E3:E%d)",ARRAYOFCONST((FCurRow - 1))));
      SetCellText(6, FCurRow,                               // total current valuation
                       Format("=SUM(G3:G%d)", ARRAYOFCONST((FCurRow - 1))));
      SetCellText(7, FCurRow,                               // total gain/loss
                       Format("=SUM(H3:H%d)",ARRAYOFCONST((FCurRow - 1))));
      SetCellText(8, FCurRow,                               // and overall percentage
                  Format("=H%d/E%d", ARRAYOFCONST((FCurRow + 1, FCurRow + 1))));
      Query->First();                                       // back to the first record
      cxSpreadBook->ActivePage = 1;                         // and move to the second page
      // and repeat the process to setup the data
      FCurRow = 3;
      while(!Query->Eof){
        SetCellText(0,FCurRow,                              // company name
                         Query->FieldByName("CO_NAME")->AsString);
        SetCellText(1,FCurRow,                              // current price
                       Query->FieldByName("CUR_PRICE")->AsString);
        SetCellText(2,FCurRow,                              // year high
                        Query->FieldByName("YRL_HIGH")->AsString);
        SetCellText(3,FCurRow,                              // year low
                         Query->FieldByName("YRL_LOW")->AsString);
        SetCellText(4,FCurRow,                              // average of Hi/Lo prices
              Format("=(C%d+D%d)/2", ARRAYOFCONST((FCurRow + 1, FCurRow + 1))));
        Query->Next();
        FCurRow++;
      }
      FCurRow++;
      Query->Close();
      FIsApplyFormatting = true;                            // enable the formatting button
    }
  __finally {
    cxSpreadBook->EndUpdate();                              // turn update back on
    cxSpreadBook->Recalc();                                 // auto recalc on
    cxSpreadBook->ActivePage = 0;                           // back to first page
    Screen->Cursor = CurCursor;                             // and back to default cursor
  }
}
//---------------------------------------------------------------------------
void __fastcall TStocksMainForm::FormShow(TObject *Sender)
{
    cxSpreadBook->BeginUpdate();                            // turn off updates
    try {
      cxSpreadBook->AutoRecalc = false;                     // turn off automatic recalculation
      cxSpreadBook->ActiveSheet->Caption = "Current";       // set the page caption for the first page
      cxSpreadBook->AddSheetPage("HiLo");                   // and a second page and set it's caption
      cxSpreadBook->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
      cxSpreadBook->ActivePage          = 1;                            // over now to the second page
      SetCellText(0, 0, "High, Low and Current Stock Prices");  // and repeat the process
      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
      cxSpreadBook->AutoRecalc = true;                      // autorecalc back on
      cxSpreadBook->ActivePage = 0;                         // and back to the first page
      cxSpreadBookSetSelection(NULL, cxSpreadBook->ActiveSheet);     // call to set the current cell and cell contents display
    }
    __finally {
      cxSpreadBook->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TStocksMainForm::cxSpreadBookSetSelection(TObject *Sender,
      TcxSSBookSheet *ASheet)
{
  TcxSSCellObject *CellObject;
  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 {
    delete CellObject;
    FIsUpdate = false;
  }
}
//---------------------------------------------------------------------------
String __fastcall TStocksMainForm::GetCellText(TRect SelectionRect, bool R1C1)
{
  return cxSpreadBook->CellsNameByRef(cxSpreadBook->ActivePage, SelectionRect, R1C1);
}

void __fastcall TStocksMainForm::actApplyFormattingExecute(TObject *Sender)
{
  TcxSSHeader *CHeader, *RHeader;
  TCursor CurCursor = Screen->Cursor;                       // preserve the current cursor
  Screen->Cursor = crHourGlass;                             // wait cursor
  FIsSaveSpreadSheet = false;
  cxSpreadBook->BeginUpdate();
  try {

⌨️ 快捷键说明

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