发信人: leezy (【HIT】穆子), 信区: BorlandDev
标 题: 使用Windows中的"浏览计算机"对话框摘 要:使用Sele
发信站: 哈工大紫丁香 (2002年01月19日15:53:02 星期六), 站内信件
类 别:网络
CoDelphi.com版权所有,未经允许,不得进行任何形式转载
使用该方法,在CBuilder或Delphi中可以简单的调用windows自带的
浏览计算机对话框,使用户方便地选择某台计算机。
原理就是象操作文件夹一样操作网上邻居对话框。
修改自Delphi中FileCtrl.pas中的SelectDirectory函数。
CBuilder代码(在CBuilder4中调试通过);
#include <shlobj.hpp>
char* __fastcall SelectComputer()
{
char* ComputerName="";
void* WindowList;
TBrowseInfo BrowseInfo;
char* Buffer;
PItemIDList RootItemIDList=NULL, ItemIDList=NULL;
IMalloc* ShellMalloc=NULL;
bool Result;
Result = false;
ComputerName = "";
ZeroMemory(&BrowseInfo,sizeof(BrowseInfo));
if (SUCCEEDED(SHGetMalloc(&ShellMalloc)) && ShellMalloc != NULL)
{
Buffer =(char*) ShellMalloc->Alloc(MAX_PATH);
try
{
RootItemIDList = NULL;
BrowseInfo.hwndOwner = Application->Handle;
BrowseInfo.pidlRoot = RootItemIDList;
BrowseInfo.pszDisplayName = Buffer;
BrowseInfo.lpszTitle ="选择服务器";
BrowseInfo.ulFlags = BIF_BROWSEFORCOMPUTER;
WindowList = DisableTaskWindows(0);
try
{
SHGetSpecialFolderLocation(BrowseInfo.hwndOwner,
CSIDL_NETWORK,
&(ITEMIDLIST*)BrowseInfo.pidlRoot);
ItemIDList =SHBrowseForFolder(&BrowseInfo);
if(ItemIDList!=NULL)
{
CopyMemory(ComputerName,BrowseInfo.pszDisplayName,strlen(BrowseIn
fo.pszDisplayName)+1);
}
else strcpy(ComputerName,"");
}
__finally
{
EnableTaskWindows(WindowList);
}
Result = (ItemIDList != NULL);
if (Result)
{
ShellMalloc->Free(ItemIDList);
}
}
__finally
{
ShellMalloc->Free(Buffer);
};
};
return ComputerName;
};
Delphi代码(在Delphi6中调试通过)
uses ShlObj,ActiveX;
function SelectComputer:string;
var
ComputerName:string;
BrowseInfo:TBrowseInfo;
Buffer:PChar;
RootItemIDList,ItemIDList:PItemIDList;
ShellMalloc:IMalloc;
WindowList:Pointer;
begin
ZeroMemory(@BrowseInfo,sizeof(TBrowseInfo));
if SUCCEEDED(SHGetMalloc(ShellMalloc)) and (ShellMalloc <> nil) then
begin
Buffer:=PChar(ShellMalloc.Alloc(MAX_PATH));
RootItemIDList := nil;
BrowseInfo.hwndOwner := Application.Handle;
BrowseInfo.pidlRoot := RootItemIDList;
BrowseInfo.pszDisplayName := PChar(Buffer);
BrowseInfo.lpszTitle :='选择服务器';
BrowseInfo.ulFlags := BIF_BROWSEFORCOMPUTER;
WindowList := DisableTaskWindows(0);
SHGetSpecialFolderLocation(BrowseInfo.hwndOwner,
CSIDL_NETWORK,
BrowseInfo.pidlRoot);
ItemIDList :=SHBrowseForFolder(BrowseInfo);
if ItemIDList <> nil then
begin
ComputerName:= BrowseInfo.pszDisplayName ;
end
else ComputerName:=';
EnableTaskWindows(WindowList);
if ItemIDList<>nil then
ShellMalloc.Free(ItemIDList);
Result := ComputerName;
end;
end;
--
°★.☆° .★·°∴°★.°·∴°☆ ·°∴° ☆..·°∴°.☆°★°∴°
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.230.122]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.714毫秒