随笔-341  评论-2670  文章-0  trackbacks-0

    今天封装好了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) 阅读(2189) 评论(3)  编辑 收藏 引用 所属分类: C++

评论:
# re: 使用ComboBox在输入路径的时候自动显示文件名 2008-08-04 15:08 | foxtail
挺人性化的,类似google或者是firefox3里的功能  回复  更多评论
  
# re: 使用ComboBox在输入路径的时候自动显示文件名 2008-08-04 17:48 | 908971
确是8错  回复  更多评论
  
# re: 使用ComboBox在输入路径的时候自动显示文件名 2008-08-04 20:28 | sinodragon21
one day one work, NBA  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理