怎样在openfiledialog打开时自动选中某个文件?(300分)

  • 主题发起人 主题发起人 cAkk
  • 开始时间 开始时间
我现在用pipi的方法来判断是否是正确文件,然后如果不是就继续finditem,<br>但是为什么每次find结果都是同一个,没有继续查找剩下的item??<br><br>finditem的几个参数怎样使用?
 
第2个参数:<br>iStart 是从下一个开始找,比如 <br>-1 就是 从0开始 , 0就是从1开始,1就是从2开始找<br>因此,你要找到的如果不是,下次 iStart 的值就是这次找到的index,不要加1了
 
procedure TForm1.OpenDialog1FolderChange(Sender: TObject);<br>var hparent,hlist:hwnd;<br>&nbsp; Index: Integer;<br>&nbsp; buf:array[0..256] of char;<br>&nbsp; sFileName:string;<br>&nbsp; item:LV_ITEM;<br>begin<br>&nbsp; hparent:=getparent(OpenDialog1.handle);<br>&nbsp; hlist:=findwindowex(hparent,0,'SHELLDLL_DefView',nil);<br>&nbsp; hlist:=findwindowex(hlist,0,'SysListView32',nil);<br><br>&nbsp; windows.setfocus(hlist); &nbsp;//给他设置焦点<br><br>&nbsp; index := ListView_GetNextItem(hlist,-1,LVNI_ALL);<br>&nbsp; while index &lt;&gt; -1 do<br>&nbsp; begin<br>&nbsp; &nbsp; item.mask:=LVIF_PARAM ;<br>&nbsp; &nbsp; item.iItem:=index; &nbsp; &nbsp;//第0个文件<br>&nbsp; &nbsp; item.iSubItem:=0;<br>&nbsp; &nbsp; ListView_GetItem(hlist,item);<br>&nbsp; &nbsp; SHGetPathFromIDList(PItemIDList(item.lParam),buf);<br><br>&nbsp; &nbsp; sFileName := ExtractFileName(string(buf));<br>&nbsp; &nbsp; if sFileName = 'Project1.exe' then &nbsp;break;<br><br>&nbsp; &nbsp; index := ListView_GetNextItem(hlist,index,LVNI_ALL);<br>&nbsp; end;<br>&nbsp; ListView_EnsureVisible(Hlist, Index div 5,false); &nbsp;//强制visible<br>&nbsp; ListView_SetItemState(Hlist, Index, LVIS_SELECTED+LVIS_FOCUSED, LVIS_SELECTED+LVIS_FOCUSED);<br>end;<br><br>
 
首先说明一下,上面你自己找到的方法中ListView_FindItem的第2个参数你用0<br>是错误的,要-1才是从头开始,0是从index 1开始(就是第二个文件)<br><br>找下一个:<br><br>iStart:=-1<br><br>do<br>&nbsp; iStart := ListView_FindItem(.., iStart ,..)<br>until (iStart&lt;0(表示没了) 或者 (合我要求))<br><br>
 
呵呵,吃饭前不知道SHGetPathFromIDList(PItemIDList(item.lParam),buf);<br>一直用<br>ListView_EnsureVisible<br>ListView_SetItemState<br>加上<br>OpenDialog1.FileName<br>在实验,但总是会相差一位(就是说定位不到project1.exe,到了Project1.res<br>上),很奇怪。用了SHGetPathFromIDList就一切OK了。<br><br>Pipi大侠,能否告知你怎么知道要用这个SHGetPathFromIDList的?<br>(对你的敬仰如滔滔江水,绵绵不绝。)<br><br><br>&nbsp; &nbsp;
 
cakk再不给我分,前卫兄就要抢了我的座次了<br><br>DreamTiger:没有资料的,一靠跟踪,二靠幻想
 
Pipi.:能否说说在这个例子你跟踪了哪些东西?
 
还没完那,现在判断当前系统是否设置为"隐藏后缀名",如果是,再判断该文件的<br>后缀名是否被注册了,如果是,用"xxx"查找,如果不是用"xxx.xxx"查找.<br><br>其实DreamTiger的方法可以解决这个问题,就是我嫌速度慢了一点,因为要<br>遍历所有的item. 我会额外拿出100分奖励DreamTiger的.<br><br>pipi: 这600分肯定是你的,把最后一点疑问也解决掉吧!
 
请大家对最后的疑问踊跃发言,如果pipi解决了, &nbsp;pipi得到600分.<br>如果别人解决的,从pipi的600分里面扣除100分.
 
[HKEY_USERS/S-1-5-21-1091263236-1637486018-14044502-1001/Software/Microsoft/Windows/CurrentVersion/Explorer/HideFileExt<br>是用来判断是否设置为"隐藏后缀名"。
 
DreamTiger: 你又得到50分.<br>&nbsp; &nbsp; &nbsp;<br>现在怎么判断后缀名是"已知文件类型的后缀名" ?
 
用registry 打开HKEY_CLASSES_ROOT/.xxx,如成功,则是"已知文件类型的后缀名" ?
 
前卫: 但是当"隐藏后缀名"时, *.sys,*.vxd仍然显示后缀名, 而且sys,vxd用<br>&nbsp; &nbsp; &nbsp; 你的方法是存在的.
 
你好要点隐藏文件的那部分嘛,系统是分开处理的。
 
先用ListView_FindItem查找,<br>然后再用ListView_GetNextItem查找,<br>这样应该是可以加快速度了吧,呵呵。<br><br>procedure TForm1.OpenDialog1FolderChange(Sender: TObject);<br>var<br>&nbsp; Info: TLVFindInfo;<br>&nbsp; hparent,hlist:hwnd;<br>&nbsp; Index: Integer;<br>&nbsp; buf:array[0..256] of char;<br>&nbsp; sFileName:string;<br>&nbsp; item:LV_ITEM;<br>begin<br>&nbsp; hparent:=getparent(OpenDialog1.handle);<br>&nbsp; hlist:=findwindowex(hparent,0,'SHELLDLL_DefView',nil);<br>&nbsp; hlist:=findwindowex(hlist,0,'SysListView32',nil);<br><br>&nbsp; windows.setfocus(hlist); &nbsp;//给他设置焦点<br><br>&nbsp; with Info do<br>&nbsp; begin<br>&nbsp; &nbsp; flags := LVFI_STRING or 0 or LVFI_WRAP;<br>&nbsp; &nbsp; psz := PChar('Project1');<br>&nbsp; end;<br>&nbsp; Index := ListView_FindItem(hlist, 0, Info);<br><br>&nbsp; while index &lt;&gt; -1 do<br>&nbsp; begin<br>&nbsp; &nbsp; item.mask:=LVIF_PARAM ;<br>&nbsp; &nbsp; item.iItem:=index; &nbsp; &nbsp;//第0个文件<br>&nbsp; &nbsp; item.iSubItem:=0;<br>&nbsp; &nbsp; ListView_GetItem(hlist,item);<br>&nbsp; &nbsp; SHGetPathFromIDList(PItemIDList(item.lParam),buf);<br><br>&nbsp; &nbsp; sFileName := ExtractFileName(string(buf));<br>&nbsp; &nbsp; if sFileName = 'Project1.exe' then &nbsp;break;<br><br>&nbsp; &nbsp; sFileName := ChangeFileExt(sFileName,'');<br>&nbsp; &nbsp; if sFilename &lt;&gt; 'Project1' then break;<br><br>&nbsp; &nbsp; index := ListView_GetNextItem(hlist,index,LVNI_ALL);<br>&nbsp; end;<br>&nbsp; ListView_EnsureVisible(Hlist, Index div 5,false); &nbsp;//强制visible<br>&nbsp; ListView_SetItemState(Hlist, Index, LVIS_SELECTED+LVIS_FOCUSED, LVIS_SELECTED+LVIS_FOCUSED);<br>end;<br><br>
 
错了:<br>Index := ListView_FindItem(hlist, 0, Info);<br>应该是<br>Index := ListView_FindItem(hlist, -1, Info);<br><br>&nbsp;
 
还是没有解决呀? 现在问题是我查找的时候是用"project"还是"project.exe"??<br>我必须先判断当前是否"隐藏后缀名"才能决定呀.
 
还是不对,应该是这样的:<br><br>procedure TForm1.OpenDialog1FolderChange(Sender: TObject);<br>var<br>&nbsp; Info: TLVFindInfo;<br>&nbsp; hparent,hlist:hwnd;<br>&nbsp; Index: Integer;<br>&nbsp; buf:array[0..256] of char;<br>&nbsp; sFileName:string;<br>&nbsp; item:LV_ITEM;<br>begin<br>&nbsp; hparent:=getparent(OpenDialog1.handle);<br>&nbsp; hlist:=findwindowex(hparent,0,'SHELLDLL_DefView',nil);<br>&nbsp; hlist:=findwindowex(hlist,0,'SysListView32',nil);<br><br>&nbsp; windows.setfocus(hlist); &nbsp;//给他设置焦点<br><br>&nbsp; with Info do<br>&nbsp; begin<br>&nbsp; &nbsp; flags := LVFI_STRING or 0 or LVFI_WRAP;<br>&nbsp; &nbsp; psz := PChar('Project1.exe');<br>&nbsp; end;<br>&nbsp; Index := ListView_FindItem(hlist, -1, Info);<br><br>&nbsp; if index &lt;&gt; -1 then<br>&nbsp; begin<br>&nbsp; &nbsp; ListView_EnsureVisible(Hlist, Index div 5,false); &nbsp;//强制visible<br>&nbsp; &nbsp; ListView_SetItemState(Hlist, Index, LVIS_SELECTED+LVIS_FOCUSED, LVIS_SELECTED+LVIS_FOCUSED);<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; with Info do<br>&nbsp; begin<br>&nbsp; &nbsp; flags := LVFI_STRING or 0 or LVFI_WRAP;<br>&nbsp; &nbsp; psz := PChar('Project1');<br>&nbsp; end;<br>&nbsp; Index := ListView_FindItem(hlist, -1, Info);<br><br>&nbsp; while index &lt;&gt; -1 do<br>&nbsp; begin<br>&nbsp; &nbsp; item.mask:=LVIF_PARAM ;<br>&nbsp; &nbsp; item.iItem:=index;<br>&nbsp; &nbsp; item.iSubItem:=0;<br>&nbsp; &nbsp; ListView_GetItem(hlist,item);<br>&nbsp; &nbsp; SHGetPathFromIDList(PItemIDList(item.lParam),buf);<br><br>&nbsp; &nbsp; sFileName := ExtractFileName(string(buf));<br>&nbsp; &nbsp; if sFileName = 'Project1.exe' then &nbsp;break;<br><br>&nbsp; &nbsp; sFileName := ChangeFileExt(sFileName,'');<br>&nbsp; &nbsp; if sFilename &lt;&gt; 'Project1' then break;<br><br>&nbsp; &nbsp; index := ListView_GetNextItem(hlist,index,LVNI_ALL);<br>&nbsp; end;<br>&nbsp; ListView_EnsureVisible(Hlist, Index div 5,false); &nbsp;//强制visible<br>&nbsp; ListView_SetItemState(Hlist, Index, LVIS_SELECTED+LVIS_FOCUSED, LVIS_SELECTED+LVIS_FOCUSED);<br>end;<br>
 
嘿嘿,先按全名找,找得到就绝对正确了。<br>(你不会告诉我同时存在Project1.sys和<br>Project1.sys.txt这种文件名吧。如果有<br>的话,那就对找到的全名也进行index循环。)<br>找不到全名就按后缀隐藏的找。
 
这个方法可行,不知道还有没有更好的办法?<br><br>这道题pipi帮助非常大,就不扣他的分了 :-)<br><br>预定分配分数如下:<br>pipi: 600 &nbsp; &nbsp;:-)<br><br>DreamTiger: 200<br><br>请中奖的各位到外面拿分.<br><br><br>
 
后退
顶部