今天封裝好了ComboBox,于是做了個Demo。這個Demo只有一個窗口,上面放著一個ComboBox。如果往里面打文件名的話,會把當前路徑下的被篩選過的文件名填充到ComboBox,并自動打開下拉列表。打完文件名之后,按回車執行。
開始的時候,輸入一個路徑,一邊輸入會一邊顯示:
經過提示,我們繼續輸入Programming and Language\pr,則下拉列表出現了文件夾中pr開頭的所有文件和文件夾:
最后。我們選中了第一個文件并按回車。這個文件是一個pdf文檔,于是自動調用Adobe Reader并打開:
成功!代碼不長,只有129行。后來經過一些驗證,沒發現代碼有什么問題。
1 #include "..\..\..\..\VL++\Library\Windows\VL_WinMain.h"
2 #include "..\..\..\..\VL++\Library\Windows\Commctrl\VL_WinButton.h"
3 #include "..\..\..\..\VL++\Library\Windows\Commctrl\VL_WinContainers.h"
4 #include "..\..\..\..\VL++\Library\Windows\Commctrl\VL_WinText.h"
5 #include "..\..\..\..\VL++\Library\Data\VL_System.h"
6
7 using namespace vl;
8 using namespace vl::windows;
9 using namespace vl::system;
10
11 class MyForm : public VL_WinForm
12 {
13 protected:
14 VL_WinComboBox* ComboBox;
15 VUnicodeString LastDirectory;
16 VL_List<VUnicodeString , false> LastItems;
17 VL_Array<VUnicodeString , 1> Drivers;
18
19 void InitControls()
20 {
21 ComboBox=new VL_WinComboBox(this,false);
22 ComboBox->Move(10,10,380,25);
23 ComboBox->OnChange.Bind(this,&MyForm::ComboBox_OnChange);
24 ComboBox->OnKeyDown.Bind(this,&MyForm::ComboBox_OnKeyDown);
25 }
26
27 void Initialize()
28 {
29 Drivers=VFSO_GetLogicalDrives();
30 ComboBox->SetSelectedIndex(-1);
31 }
32
33 VBool DirectoryExists(VUnicodeString Directory)
34 {
35 if(Directory.Length()==3)
36 {
37 for(VInt i=0;i<Drivers.GetCount();i++)
38 {
39 if(Directory.LowerCase()==Drivers[i].LowerCase())
40 {
41 return true;
42 }
43 }
44 return false;
45 }
46 else if(Directory.Length()>3)
47 {
48 return VFSO_DirectoryExists(Directory);
49 }
50 else
51 {
52 return false;
53 }
54 }
55
56 void FillDirectory(VUnicodeString Path)
57 {
58 VUnicodeString Directory=VFileName(Path).GetPath().GetStrW();
59 if(Directory!=LastDirectory)
60 {
61 LastDirectory=Directory;
62 LastItems.Clear();
63 if(DirectoryExists(LastDirectory))
64 {
65 VL_List<VUnicodeString , false> Files;
66 VL_List<VUnicodeString , false> Directories;
67 VFSO_EnumerateFiles(LastDirectory,Files,Directories);
68 for(VInt i=0;i<Files.GetCount();i++)
69 {
70 LastItems.Add(Directory+Files[i]);
71 }
72 for(VInt i=0;i<Directories.GetCount();i++)
73 {
74 LastItems.Add(Directory+Directories[i]);
75 }
76 }
77 }
78 ComboBox->Clear();
79 for(VInt i=0;i<LastItems.GetCount();i++)
80 {
81 if(LastItems[i].LowerCase().Pos(Path.LowerCase())==0)
82 {
83 ComboBox->AddString(LastItems[i]);
84 }
85 }
86 }
87
88 void ComboBox_OnChange(VL_Base* Sender)
89 {
90 FillDirectory(ComboBox->GetText());
91 if(ComboBox->GetCount()>0)
92 {
93 ComboBox->OpenList();
94 }
95 else
96 {
97 ComboBox->CloseList();
98 }
99 }
100
101 void ComboBox_OnKeyDown(VL_Base* Sender,VLS_KeyStruct KeyStruct)
102 {
103 if(KeyStruct.KeyCode==L'\r')
104 {
105 ShellExecute(GetHandle(),L"open",ComboBox->GetText().Buffer(),L"",L"",SW_SHOW);
106 }
107 }
108
109 public:
110
111 MyForm():VL_WinForm(true)
112 {
113 SetBorder(vwfbSingle);
114 SetMaximizeBox(false);
115 SetClientWidth(400);
116 SetClientHeight(45);
117 SetText(L"Get your file run!");
118 MoveCenter();
119 InitControls();
120 Initialize();
121 Show();
122 }
123 };
124
125 void main()
126 {
127 new MyForm;
128 GetApplication()->Run();
129 }
posted on 2008-08-04 07:20
陳梓瀚(vczh) 閱讀(2227)
評論(3) 編輯 收藏 引用 所屬分類:
C++