CppControls.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:9k
- // This is the main project file for VC++ application project
- // generated using an Application Wizard.
- #include "stdafx.h"
- #using <mscorlib.dll>
- #using <System.dll>
- #using <System.Drawing.dll>
- #using <System.Windows.Forms.dll>
- using namespace System;
- using namespace System::Drawing;
- using namespace System::ComponentModel;
- using namespace System::Windows::Forms;
- using namespace System::IO;
- __gc class CppWindow : public Form
- {
- public:
- CppWindow()
- {
- Setup_TreeView();
- Setup_ListView();
- Setup_Splitter();
- Setup_ToolBar();
- Setup_StatusBar();
-
- // The order of the following three controls is vital to the proper
- // operation of the splitter bar.
- Controls->Add(listView1);
- Controls->Add(splitter1);
- Controls->Add(treeView1);
-
- Controls->Add(toolBar1);
- Controls->Add(statusBar1);
- }
- private:
- TreeView* treeView1;
- private:
- void TreeView_BeforeExpand(Object* pSender, TreeViewCancelEventArgs* pe)
- {
- // First zap the dummy node, assuming there is one
- if (pe->Node->Nodes->Count != 0)
- pe->Node->Nodes->RemoveAt(0);
-
- // Get a list of subdirectories
- String* dirs[];
- try
- {
- dirs = Directory::GetDirectories(pe->Node->FullPath);
- }
- catch(System::Exception* pe)
- {
- MessageBox::Show(pe->Message, "Error");
- return;
- }
- // Add a child node for each one
- for(int i = 0; i < dirs->Count; i++)
- {
- String* dirName = dynamic_cast<String*>(dirs->get_Item(i));
- TreeNode* tn = new TreeNode(Path::GetFileName(dirName));
- pe->Node->Nodes->Add(tn);
- // Add a dummy child node
- tn->Nodes->Add(new TreeNode("<dummy>"));
- }
- // Fill the ListView with details of the current node
- Fill_ListView(pe->Node->FullPath);
- }
- private:
- void Setup_TreeView()
- {
- // Create the control
- treeView1 = new TreeView();
- treeView1->Dock = DockStyle::Left;
- treeView1->Size = System::Drawing::Size(121, 273);
- // Set up the ImageList
- ImageList* il = new ImageList();
- il->Images->Add(new Bitmap("CLSDFOLD.ICO"));
- il->Images->Add(new Bitmap("OPENFOLD.ICO"));
- treeView1->ImageList = il;
- treeView1->ImageIndex = 0;
- treeView1->SelectedImageIndex = 1;
- // Add an event handler for BeforeExpand events
- treeView1->BeforeExpand += new TreeViewCancelEventHandler(
- this, &CppWindow::TreeView_BeforeExpand);
- // Add the same handler for BeforeSelect events
- treeView1->BeforeSelect += new TreeViewCancelEventHandler(
- this, &CppWindow::TreeView_BeforeExpand);
-
- // Get the logical drives
- String* drives[] = Directory::GetLogicalDrives();
- for(int i = 0; i < drives->Count; i++)
- {
- String* name = dynamic_cast<String*>(drives->get_Item(i));
- TreeNode* tn = new TreeNode(name);
- treeView1->Nodes->Add(tn);
- tn->Nodes->Add(new TreeNode("<dummy>"));
- }
- }
- private:
- ListView* listView1;
- private:
- void Setup_ListView()
- {
- // Create the ListView and set its properties
- listView1 = new ListView();
- listView1->Dock = DockStyle::Fill;
- listView1->Location = Point(124, 0);
- listView1->Size = System::Drawing::Size(168, 273);
- // Set to use Details view
- listView1->View = View::Details;
- // Set up the columns for use with Details view
- ColumnHeader* col1 = new ColumnHeader();
- col1->Text = S"Type";
- col1->Width = 40;
- ColumnHeader* col2 = new ColumnHeader();
- col2->Text = S"Name";
- col2->Width = 85;
- ColumnHeader* col3 = new ColumnHeader();
- col3->Text = S"Size";
- col3->Width = 65;
- listView1->Columns->Add(col1);
- listView1->Columns->Add(col2);
- listView1->Columns->Add(col3);
- }
- private:
- void Fill_ListView(String* path)
- {
- // Put the path in the status bar
- statusBarPanel1->Text = path;
- // Clear everything from the ListView
- listView1->Items->Clear();
- // Start with the directories
- String* dirs[];
- try
- {
- dirs = Directory::GetDirectories(path);
- }
- catch(System::Exception* pe)
- {
- MessageBox::Show(pe->Message, "Error");
- return;
- }
- // Create new ListViewItem objects to represent the directories
- for(int i=0; i<dirs->Count; i++)
- {
- String* pathName = dynamic_cast<String*>(dirs->get_Item(i));
- String* dirName = Path::GetFileName(pathName);
- // Create an array of String* to hold the subitems
- String* subItems[] = new String*[3];
- subItems[0]= S"Dir";
- subItems[1] = dirName;
- subItems[2] = S" ";
- // Create the ListViewItems from the subitems
- ListViewItem* itm = new ListViewItem(subItems);
- // Add the ListViewItem to the ListView
- listView1->Items->Add(itm);
- }
- // Now follow with the files
- String* files[];
- try
- {
- files = Directory::GetFiles(path);
- }
- catch(System::Exception* pe)
- {
- MessageBox::Show(pe->Message, "Error");
- return;
- }
- // Create new ListViewItem objects to represent the files
- for(int i=0; i<files->Count; i++)
- {
- String* pathName = dynamic_cast<String*>(files->get_Item(i));
- String* dirName = Path::GetFileName(pathName);
- // Create an array of String* to hold the subitems
- String* subItems[] = new String*[3];
- subItems[0]= S"File";
- subItems[1] = dirName;
- // Find the file size
- FileInfo* f = new FileInfo(pathName);
- subItems[2] = __box(f->Length)->ToString();
- // Create the ListViewItems from the subitems
- ListViewItem* itm = new ListViewItem(subItems);
- // Add the ListViewItem to the ListView
- listView1->Items->Add(itm);
- }
- }
- private:
- Splitter* splitter1;
- private:
- void Setup_Splitter()
- {
- splitter1 = new Splitter();
- splitter1->Location = Point(121, 0);
- splitter1->Size = System::Drawing::Size(3, 273);
-
- // Don't include this control in the tab order
- splitter1->TabStop = false;
- }
- private:
- ToolBar* toolBar1;
- ToolBarButton* button1;
- ToolBarButton* button2;
- ToolBarButton* button3;
- private:
- void Setup_ToolBar()
- {
- toolBar1 = new ToolBar();
- toolBar1->ShowToolTips = true;
- // Set up the imagelist
- ImageList* il = new ImageList();
- il->Images->Add(new Bitmap("PROP.BMP"));
- il->Images->Add(new Bitmap("DELETE.BMP"));
- // Set the toolbar to use the imagelist
- toolBar1->ImageList = il;
- // Create the first button
- button1 = new ToolBarButton();
- button1->Style = ToolBarButtonStyle::DropDownButton;
- button1->ImageIndex = 0;
- button1->ToolTipText = "View";
- System::Windows::Forms::ContextMenu* cm = new System::Windows::Forms::ContextMenu();
- MenuItem* item1 = new MenuItem("Details");
- cm->MenuItems->Add(item1);
- button1->DropDownMenu = cm;
- // Create a separator button
- button2 = new ToolBarButton();
- button2->Style = ToolBarButtonStyle::Separator;
- // Create a regular pushbutton
- button3 = new ToolBarButton();
- // PushButton is the default style, so you don't need to set it
- button3->Style = ToolBarButtonStyle::PushButton;
- button3->ImageIndex = 1;
- button3->ToolTipText = "Exit";
- // Add buttons to the toolbar
- toolBar1->Buttons->Add(button1);
- toolBar1->Buttons->Add(button2);
- toolBar1->Buttons->Add(button3);
- // Set up the handler
- toolBar1->ButtonClick += new ToolBarButtonClickEventHandler(
- this, &CppWindow::ToolBar_Click);
- }
- private:
- void ToolBar_Click(Object* pSender, ToolBarButtonClickEventArgs* pe)
- {
- if (pe->Button == button3)
- {
- Application::Exit();
- }
- }
- private:
- StatusBar* statusBar1;
- StatusBarPanel* statusBarPanel1;
- StatusBarPanel* statusBarPanel2;
- private:
- void Setup_StatusBar()
- {
- statusBar1 = new StatusBar();
- // Set the bar to use panels...
- statusBar1->ShowPanels = true;
- statusBarPanel1 = new StatusBarPanel();
- statusBarPanel1->Width = 200;
- statusBarPanel2 = new StatusBarPanel();
- statusBarPanel2->Width = 100;
- statusBar1->Panels->Add(statusBarPanel1);
- statusBar1->Panels->Add(statusBarPanel2);
- }
- };
- // This is the entry point for this application
- #ifdef _UNICODE
- int wmain(void)
- #else
- int main(void)
- #endif
- {
- Console::WriteLine(S"Controls Example");
- // Create a form.
- Application::Run(new CppWindow());
- return 0;
- }