CppControls.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:9k
源码类别:

.net编程

开发平台:

Visual C++

  1. // This is the main project file for VC++ application project 
  2. // generated using an Application Wizard.
  3. #include "stdafx.h"
  4. #using <mscorlib.dll>
  5. #using <System.dll>
  6. #using <System.Drawing.dll>
  7. #using <System.Windows.Forms.dll>
  8. using namespace System;
  9. using namespace System::Drawing;
  10. using namespace System::ComponentModel;
  11. using namespace System::Windows::Forms;
  12. using namespace System::IO;
  13. __gc class CppWindow : public Form
  14. {
  15. public:
  16.     CppWindow()
  17.     {
  18.         Setup_TreeView();
  19.         Setup_ListView();
  20.         Setup_Splitter();
  21.         Setup_ToolBar();
  22.         Setup_StatusBar();
  23.         
  24.         // The order of the following three controls is vital to the proper
  25.         // operation of the splitter bar.
  26.         Controls->Add(listView1);
  27.         Controls->Add(splitter1);
  28.         Controls->Add(treeView1);
  29.                 
  30.         Controls->Add(toolBar1);
  31.         Controls->Add(statusBar1);
  32.     }
  33. private:
  34.     TreeView* treeView1;
  35. private:
  36.     void TreeView_BeforeExpand(Object* pSender, TreeViewCancelEventArgs* pe)
  37.     {
  38.         // First zap the dummy node, assuming there is one
  39.         if (pe->Node->Nodes->Count != 0)
  40.             pe->Node->Nodes->RemoveAt(0);
  41.         
  42.         // Get a list of subdirectories
  43.         String* dirs[];
  44.         try
  45.         { 
  46.             dirs = Directory::GetDirectories(pe->Node->FullPath);
  47.         }
  48.         catch(System::Exception* pe)
  49.         {
  50.             MessageBox::Show(pe->Message, "Error");
  51.             return;
  52.         }
  53.         // Add a child node for each one
  54.         for(int i = 0; i < dirs->Count; i++)
  55.         {
  56.             String* dirName = dynamic_cast<String*>(dirs->get_Item(i));
  57.             TreeNode* tn = new TreeNode(Path::GetFileName(dirName));
  58.             pe->Node->Nodes->Add(tn);
  59.             // Add a dummy child node
  60.             tn->Nodes->Add(new TreeNode("<dummy>"));
  61.         }
  62.         // Fill the ListView with details of the current node
  63.         Fill_ListView(pe->Node->FullPath);
  64.     }
  65. private:
  66.     void Setup_TreeView()
  67.     {
  68.         // Create the control
  69.         treeView1 = new TreeView();
  70.         treeView1->Dock = DockStyle::Left;
  71.         treeView1->Size = System::Drawing::Size(121, 273);
  72.         // Set up the ImageList
  73.         ImageList* il = new ImageList();
  74.         il->Images->Add(new Bitmap("CLSDFOLD.ICO"));
  75.         il->Images->Add(new Bitmap("OPENFOLD.ICO"));
  76.         treeView1->ImageList = il;
  77.         treeView1->ImageIndex = 0;
  78.         treeView1->SelectedImageIndex = 1;
  79.         // Add an event handler for BeforeExpand events
  80.         treeView1->BeforeExpand += new TreeViewCancelEventHandler(
  81.             this, &CppWindow::TreeView_BeforeExpand);
  82.         // Add the same handler for BeforeSelect events
  83.         treeView1->BeforeSelect += new TreeViewCancelEventHandler(
  84.             this, &CppWindow::TreeView_BeforeExpand);
  85.                 
  86.         // Get the logical drives
  87.         String* drives[] = Directory::GetLogicalDrives();
  88.         for(int i = 0; i < drives->Count; i++)
  89.         {
  90.             String* name = dynamic_cast<String*>(drives->get_Item(i));
  91.             TreeNode* tn = new TreeNode(name);
  92.             treeView1->Nodes->Add(tn);
  93.             tn->Nodes->Add(new TreeNode("<dummy>"));
  94.         }
  95.     }
  96. private:
  97.     ListView* listView1;
  98. private:
  99.     void Setup_ListView()
  100.     {
  101.         // Create the ListView and set its properties
  102.         listView1 = new ListView();
  103.         listView1->Dock = DockStyle::Fill;
  104.         listView1->Location = Point(124, 0);
  105.         listView1->Size = System::Drawing::Size(168, 273);
  106.         // Set to use Details view
  107.         listView1->View = View::Details;
  108.         // Set up the columns for use with Details view
  109.         ColumnHeader* col1 = new ColumnHeader();
  110.         col1->Text = S"Type";
  111.         col1->Width = 40;
  112.         ColumnHeader* col2 = new ColumnHeader();
  113.         col2->Text = S"Name";
  114.         col2->Width = 85;
  115.         ColumnHeader* col3 = new ColumnHeader();
  116.         col3->Text = S"Size";
  117.         col3->Width = 65;
  118.         listView1->Columns->Add(col1);
  119.         listView1->Columns->Add(col2);
  120.         listView1->Columns->Add(col3);
  121.     }
  122. private:
  123.     void Fill_ListView(String* path)
  124.     {
  125.         // Put the path in the status bar
  126.         statusBarPanel1->Text = path;
  127.         // Clear everything from the ListView
  128.         listView1->Items->Clear();
  129.         // Start with the directories
  130.         String* dirs[];
  131.         try
  132.         {
  133.             dirs = Directory::GetDirectories(path);
  134.         }
  135.         catch(System::Exception* pe)
  136.         {
  137.             MessageBox::Show(pe->Message, "Error");
  138.             return;
  139.         }
  140.         // Create new ListViewItem objects to represent the directories
  141.         for(int i=0; i<dirs->Count; i++)
  142.         {
  143.             String* pathName = dynamic_cast<String*>(dirs->get_Item(i));
  144.             String* dirName = Path::GetFileName(pathName);
  145.             // Create an array of String* to hold the subitems
  146.             String* subItems[] = new String*[3];
  147.             subItems[0]= S"Dir";
  148.             subItems[1] = dirName;
  149.             subItems[2] = S" ";
  150.             // Create the ListViewItems from the subitems
  151.             ListViewItem* itm = new ListViewItem(subItems);
  152.             // Add the ListViewItem to the ListView
  153.             listView1->Items->Add(itm);
  154.         }
  155.         // Now follow with the files
  156.         String* files[];
  157.         try
  158.         {
  159.             files = Directory::GetFiles(path);
  160.         }
  161.         catch(System::Exception* pe)
  162.         {
  163.             MessageBox::Show(pe->Message, "Error");
  164.             return;
  165.         }
  166.         // Create new ListViewItem objects to represent the files
  167.         for(int i=0; i<files->Count; i++)
  168.         {
  169.             String* pathName = dynamic_cast<String*>(files->get_Item(i));
  170.             String* dirName = Path::GetFileName(pathName);
  171.             // Create an array of String* to hold the subitems
  172.             String* subItems[] = new String*[3];
  173.             subItems[0]= S"File";
  174.             subItems[1] = dirName;
  175.             // Find the file size
  176.             FileInfo* f = new FileInfo(pathName);
  177.             subItems[2] = __box(f->Length)->ToString();
  178.             // Create the ListViewItems from the subitems
  179.             ListViewItem* itm = new ListViewItem(subItems);
  180.             // Add the ListViewItem to the ListView
  181.             listView1->Items->Add(itm);
  182.         }
  183.     }
  184. private:
  185.     Splitter* splitter1;
  186. private:
  187.     void Setup_Splitter()
  188.     {
  189.         splitter1 = new Splitter();
  190.         splitter1->Location = Point(121, 0);
  191.         splitter1->Size = System::Drawing::Size(3, 273);
  192.         
  193.         // Don't include this control in the tab order
  194.         splitter1->TabStop = false;
  195.     }
  196. private:
  197.     ToolBar* toolBar1;
  198.     ToolBarButton* button1;
  199.     ToolBarButton* button2;
  200.     ToolBarButton* button3;
  201. private:
  202.     void Setup_ToolBar()
  203.     {
  204.         toolBar1 = new ToolBar();
  205.         toolBar1->ShowToolTips = true;
  206.         // Set up the imagelist
  207.         ImageList* il = new ImageList();
  208.         il->Images->Add(new Bitmap("PROP.BMP"));
  209.         il->Images->Add(new Bitmap("DELETE.BMP"));
  210.         // Set the toolbar to use the imagelist
  211.         toolBar1->ImageList = il;
  212.         // Create the first button
  213.         button1 = new ToolBarButton();
  214.         button1->Style = ToolBarButtonStyle::DropDownButton;
  215.         button1->ImageIndex = 0;
  216.         button1->ToolTipText = "View";
  217.         System::Windows::Forms::ContextMenu* cm = new System::Windows::Forms::ContextMenu();
  218.         MenuItem* item1 = new MenuItem("Details");
  219.         cm->MenuItems->Add(item1);
  220.         button1->DropDownMenu = cm;
  221.         // Create a separator button
  222.         button2 = new ToolBarButton();
  223.         button2->Style = ToolBarButtonStyle::Separator;
  224.         // Create a regular pushbutton
  225.         button3 = new ToolBarButton();
  226.         // PushButton is the default style, so you don't need to set it
  227.         button3->Style = ToolBarButtonStyle::PushButton;
  228.         button3->ImageIndex = 1;
  229.         button3->ToolTipText = "Exit";
  230.         // Add buttons to the toolbar
  231.         toolBar1->Buttons->Add(button1);
  232.         toolBar1->Buttons->Add(button2);
  233.         toolBar1->Buttons->Add(button3);
  234.         // Set up the handler
  235.         toolBar1->ButtonClick += new ToolBarButtonClickEventHandler(
  236.             this, &CppWindow::ToolBar_Click);
  237.     }
  238. private:
  239.     void ToolBar_Click(Object* pSender, ToolBarButtonClickEventArgs* pe)
  240.     {
  241.         if (pe->Button == button3)
  242.         {
  243.             Application::Exit();
  244.         }
  245.     }
  246. private:
  247.     StatusBar* statusBar1;
  248.     StatusBarPanel* statusBarPanel1;
  249.     StatusBarPanel* statusBarPanel2;
  250. private:
  251.     void Setup_StatusBar()
  252.     {
  253.         statusBar1 = new StatusBar();
  254.         // Set the bar to use panels...
  255.         statusBar1->ShowPanels = true;
  256.         statusBarPanel1 = new StatusBarPanel();
  257.         statusBarPanel1->Width = 200;
  258.         statusBarPanel2 = new StatusBarPanel();
  259.         statusBarPanel2->Width = 100;
  260.         statusBar1->Panels->Add(statusBarPanel1);
  261.         statusBar1->Panels->Add(statusBarPanel2);
  262.     }
  263. };
  264. // This is the entry point for this application
  265. #ifdef _UNICODE
  266. int wmain(void)
  267. #else
  268. int main(void)
  269. #endif
  270. {
  271.     Console::WriteLine(S"Controls Example");
  272.     // Create a form.
  273.     Application::Run(new CppWindow());
  274.     return 0;
  275. }