TreeDemo.cpp
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:6k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "TreeDemo.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "fcDemoRichEdit"
  8. #pragma link "fcStatusBar"
  9. #pragma link "fcTreeView"
  10. #pragma resource "*.dfm"
  11. TTreeViewDemoForm *TreeViewDemoForm;
  12. //---------------------------------------------------------------------------
  13. __fastcall TTreeViewDemoForm::TTreeViewDemoForm(TComponent* Owner)
  14.         : TForm(Owner)
  15. {
  16. }
  17. //---------------------------------------------------------------------------
  18. void __fastcall TTreeViewDemoForm::FormShow(TObject *Sender)
  19. {
  20.   fcTreeView1->FullExpand();
  21.   fcTreeView2->FullExpand();
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TTreeViewDemoForm::fcTreeView1CalcNodeAttributes(
  25.       TfcCustomTreeView *TreeView, TfcTreeNode *Node, TfcItemStates State)
  26. {
  27.   if (State.Contains(fcisHot))
  28.     TreeView->Canvas->Font->Style = TreeView->Canvas->Font->Style << fsUnderline;
  29. }
  30. //---------------------------------------------------------------------------
  31. void __fastcall TTreeViewDemoForm::CheckBox1Click(TObject *Sender)
  32. {
  33.   if (fcTreeView1->Options.Contains(tvoExpandButtons3D))
  34.     fcTreeView1->Options = fcTreeView1->Options >> tvoExpandButtons3D;
  35.   else fcTreeView1->Options = fcTreeView1->Options << tvoExpandButtons3D;
  36. }
  37. //---------------------------------------------------------------------------
  38. void __fastcall TTreeViewDemoForm::CheckBox2Click(TObject *Sender)
  39. {
  40.   if (((TCheckBox *)Sender)->Checked)
  41.     fcTreeView1->Options = fcTreeView1->Options << tvoRowSelect;
  42.   else fcTreeView1->Options = fcTreeView1->Options >> tvoRowSelect;
  43. }
  44. //---------------------------------------------------------------------------
  45. void __fastcall TTreeViewDemoForm::fcTreeView2MouseMove(TfcCustomTreeView *TreeView,
  46.       TfcTreeNode *Node, TShiftState Shift, int X, int Y)
  47. {
  48.   if (Node != NULL && Node->StringData != "" &&
  49.       TreeView->GetHitTestInfoAt(X, Y).Contains(fchtOnItem))
  50.     fcStatusBar1->Panels->Items[0]->Text = Node->StringData;
  51.   else fcStatusBar1->Panels->Items[0]->Text = "";
  52. }
  53. //---------------------------------------------------------------------------
  54. void __fastcall TTreeViewDemoForm::Button1Click(TObject *Sender)
  55. {
  56.   // Reapply filter by refreshing query locally
  57.   Query1->UpdateCursorPos();
  58.   Query1->Resync(TResyncMode() << rmCenter);
  59. }
  60. //---------------------------------------------------------------------------
  61. void __fastcall TTreeViewDemoForm::fcTreeView3ToggleCheckbox(
  62.       TfcCustomTreeView *TreeView, TfcTreeNode *Node)
  63. {
  64.   if (Node->Level == 1 && Node->Checked)
  65.     Node->Parent->Checked = true;
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TTreeViewDemoForm::Query1FilterRecord(TDataSet *DataSet, bool &Accept)
  69. {
  70.   // TreeView handle not available i.e. destroying
  71.   if (!fcTreeView3->HandleAllocated()) return;
  72.   Accept = false;
  73.   bool CountryMatch = true;
  74.   bool AmountOrderedMatch = true;
  75.   bool AmountOwedMatch = true;
  76.   TfcTreeNode* curNode = fcTreeView3->Items->GetFirstNode();
  77.   TfcTreeNode* ChildNode = NULL;
  78.   int AmountOwed = 0, AmountOrdered = 0;
  79.   do {
  80.     if (curNode->StringData == "Country" && curNode->Checked) {
  81.       CountryMatch = false;
  82.       // Search checkboxes for country to see if any countries match current record
  83.       ChildNode = curNode->GetFirstChild();
  84.       do {
  85.         if (ChildNode->Checked) {
  86.           if (DataSet->FieldByName("Country")->AsString == ChildNode->Text) {
  87.             CountryMatch = true;
  88.             break;
  89.           }
  90.         }
  91.         ChildNode = ChildNode->GetNextSibling();
  92.       } while (ChildNode != NULL);
  93.     } else if (curNode->StringData == "Amount Ordered" && curNode->Checked) {
  94.       // Check AmountOrdered to amount matches radio group selected in treeview
  95.       AmountOrderedMatch = false;
  96.       ChildNode = curNode->GetFirstChild();
  97.       do {
  98.         if (ChildNode->Checked) {
  99.           AmountOrdered = DataSet->FieldByName("AmountOrdered")->AsFloat;
  100.           TField* MyField = DataSet->FieldByName("AmountOrdered");
  101.           if ((ChildNode->StringData == "" || AmountOrdered >= StrToInt(ChildNode->StringData)) &&
  102.               (ChildNode->StringData2 == "" || AmountOrdered < StrToInt(ChildNode->StringData2)))
  103.             AmountOrderedMatch = true;
  104.           break;
  105.         }
  106.         ChildNode = ChildNode->GetNextSibling();
  107.       } while (ChildNode != NULL);
  108.     } else if (curNode->StringData == "Amount Owed" && curNode->Checked) {
  109.       // Check AmountOrdered to amount matches radio group selected in treeview
  110.       AmountOwedMatch = false;
  111.       ChildNode = curNode->GetFirstChild();
  112.       do {
  113.         if (ChildNode->Checked) {
  114.           AmountOwed = DataSet->FieldByName("AmountOrdered")->AsFloat -
  115.             DataSet->FieldByName("AmountPaid")->AsFloat;
  116.           if ((ChildNode->StringData == "" || AmountOwed > StrToInt(ChildNode->StringData)) &&
  117.               (ChildNode->StringData2 == "" || AmountOwed <= StrToInt(ChildNode->StringData2)))
  118.             AmountOwedMatch = true;
  119.           break;
  120.         }
  121.         ChildNode = ChildNode->GetNextSibling();
  122.       } while (ChildNode != NULL);
  123.     }
  124.     curNode = curNode->GetNextSibling();
  125.   } while (curNode != NULL);
  126.   if (CountryMatch && AmountOrderedMatch && AmountOwedMatch) Accept = true;
  127. }
  128. //---------------------------------------------------------------------------