use IContextMenu::InvokeCommand. For a sample, see http://blogs.msdn.com/oldnewthing/archive/2004/09/20/231739.aspx For better manner, see http://community.csdn.net/HomePage/SmartQuestion.aspx
function WjcMoveFile(FromPath, topath: string): Boolean; var lpFileOp: TShFileOpStruct; begin with lpFileOp do begin Wnd := Application.handle; wFunc := FO_COPY; pFrom := Pchar(FromPath + #0#0); pTo := Pchar(topath + #0#0); ; fFlags := FOF_ALLOWUNDO or FOF_RENAMEONCOLLISION or fof_noconfirmmkdir; hNameMappings := nil; lpszProgressTitle := nil; fAnyOperationsAborted := True; end; if SHFileOperation(lpFileOp) <> 0 then Result := False else Result := True; end;procedure TForm1.Button1Click(Sender: TObject); var aa: string; begin aa := extractfilepath(Application.ExeName)+'新建文件夹'; WjcMoveFile(aa,aa) end; 有点问题,自己改吧
For a sample, see
http://blogs.msdn.com/oldnewthing/archive/2004/09/20/231739.aspx
For better manner, see http://community.csdn.net/HomePage/SmartQuestion.aspx
新建文件夹这个菜单项是在单击ListView的空白处才出来的, 自己能力有限无法实现,哪位大佬指点迷津,
都快疯了
自增
try到可以创建为止。。
http://community.csdn.net/Expert/topic/4782/4782203.xml?temp=.3747675
var
lpFileOp: TShFileOpStruct;
begin
with lpFileOp do
begin
Wnd := Application.handle;
wFunc := FO_COPY;
pFrom := Pchar(FromPath + #0#0);
pTo := Pchar(topath + #0#0); ;
fFlags := FOF_ALLOWUNDO or FOF_RENAMEONCOLLISION or fof_noconfirmmkdir;
hNameMappings := nil;
lpszProgressTitle := nil;
fAnyOperationsAborted := True;
end;
if SHFileOperation(lpFileOp) <> 0 then
Result := False
else
Result := True;
end;procedure TForm1.Button1Click(Sender: TObject);
var
aa: string;
begin
aa := extractfilepath(Application.ExeName)+'新建文件夹';
WjcMoveFile(aa,aa)
end;
有点问题,自己改吧
我试了一下,楼主的问题不难.简单这样几步:
1,取得文件夹的IShellFolder接口.
2,从IShellFolder接口CreateViewObject从而得到IShellView
3,从IShellView.GetItemObject(第一个参数用SVGIO_BACKGROUND)得到IContextMenu
4,IContextMenu.InvokeCommand,其中Verb命令用'NewFolder'
以上几步容易误入歧途的是取IContextMenu的方法..
如果按这样取的上下文菜单,是没有NewFolder命令可以执行的.
ShellFolder.ParentShellFolder.GetUIObjectOf(0, 1, PIDL, IID_IContextMenu, nil, contextmenu))
var
P: PWideChar;
folder:IShellFolder;
NewPIDL: PItemIDList;
NumChars,
Flags,
HR: LongWord;begin
P:=StringToOleStr(path);
NumChars:=Length(path);
OleCheck(owner.ParseDisplayName(0, nil, P, NumChars, NewPIDL, Flags));
OleCheck(owner.BindToObject(NewPIDL,nil,IShellFolder,folder)); Result:=folder;
end;procedure ExecuteContextMenuCommand(cm:IContextMenu;cmd:string);
var
menu:HMENU;
ci: TCMInvokeCommandInfo;
// rt:TRect;
begin
menu:=CreatePopupMenu;
OleCheck(cm.QueryContextMenu(menu,0,0,99999,CMF_NORMAL));
// TrackPopupMenu (menu,TPM_RETURNCMD or TPM_LEFTALIGN,200,200, 0,Handle,@rt);
DestroyMenu(menu); FillChar(ci,sizeof(ci),#0);
ci.cbSize:=sizeof(ci);
ci.lpVerb:=pchar(cmd);
OleCheck(cm.InvokeCommand(ci));end;procedure TForm1.Button1Click(Sender: TObject);
var
folder:IShellFolder;
view:IShellView;
cm:IContextMenu;
desk:IShellFolder;begin
//
OleCheck(SHGetDesktopFolder(desk));
folder:=GetShellFolderFromPath(desk,'D:\tmp2');
OleCheck(folder.CreateViewObject(Handle,IShellView,view));
OleCheck(view.GetItemObject(SVGIO_BACKGROUND,IContextMenu,pointer(cm)));
ExecuteContextMenuCommand(cm,'NewFolder');end;
全程丝丝录像!
算你狠
看你c是不是多了11目录
#define NO_WIN32_LEAN_AND_MEAN
#include <shlobj.h>
#include <vcl.h>
#pragma hdrstop#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
#include <Comobj.hpp>
//---------------------------------------------------------------------------
IShellFolder *CrnGetShellFolderFromPath(IShellFolder *sfOwner, LPSTR lpPath)
{
IShellFolder *pShellFolder; wchar_t *wszFolder = StringToOleStr(lpPath);
DWORD dwEaten = strlen(lpPath);
LPITEMIDLIST lpNewPIDL;
DWORD dwFlags; OleCheck(sfOwner->ParseDisplayName(0, NULL, wszFolder, &dwEaten, &lpNewPIDL, &dwFlags));
OleCheck(sfOwner->BindToObject(lpNewPIDL, NULL, IID_IShellFolder, (void **)&pShellFolder));
return pShellFolder;
}
//---------------------------------------------------------------------------
void CrnExecContextMenuCmd(IContextMenu *cm, LPSTR lpCmd)
{
CMINVOKECOMMANDINFO cmici; HMENU hMenu = CreatePopupMenu();
OleCheck(cm->QueryContextMenu(hMenu, 0, 0, 99999, CMF_NORMAL));
DestroyMenu(hMenu); ZeroMemory(&cmici, sizeof(cmici));
cmici.cbSize = sizeof(cmici);
cmici.lpVerb = lpCmd;
OleCheck(cm->InvokeCommand(&cmici));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
IShellFolder *lpFolder;
IShellView *lpShellView;
IContextMenu *lpConMenu;
IShellFolder *lpDeskFolder; OleCheck(SHGetDesktopFolder(&lpDeskFolder));
lpFolder = CrnGetShellFolderFromPath(lpDeskFolder, "C:\\ccrun");
OleCheck(lpFolder->CreateViewObject(Handle, IID_IShellView, (void **)&lpShellView));
OleCheck(lpShellView->GetItemObject(SVGIO_BACKGROUND, IID_IContextMenu, (void **)&lpConMenu));
CrnExecContextMenuCmd(lpConMenu, "NewFolder");
}
//---------------------------------------------------------------------------