Windows 桌面新文件夹 C++



目的 : Windows 资源管理器(桌面版(中的新文件夹创建功能 (图标坐标设置+标签校正(

我想知道如何创建一个新文件夹,如Windows资源管理器(桌面(。

我想知道与"右键单击桌面→新建→文件夹"相同的功能。

我想要的是达到特定的坐标。

++ 如果您也可以修改标签会更好。

我就是这样做的。

CreateDirectory → FindWindow (Desktop Listview( → SendMessage (LVM_SETITEMPOSITION(

但是当我这样做时CreateDirectory它在桌面上显得太慢了。 (默认 1000 毫秒或更长(

所以我尝试了SHChangeNotify但它看起来很慢。

该源代码的功能是将鼠标移动到桌面上的特定位置,然后按快捷方式创建具有该坐标的文件夹。 但是,我可以看到文件夹看起来很慢且移动(SendMessage (LVM_SETITEMPOSITION)(。

// this is Console Application
HWND hDesk = FindWindow(NULL, L"Program Manager");
HWND hDesk2 = FindWindowEx(hDesk, NULL, L"SHELLDLL_DefView", NULL);
HWND hListView = FindWindowEx(hDesk2, NULL, L"SysListView32", NULL);
int iconCount = (int)SendMessage(hListView, LVM_GETITEMCOUNT, NULL, NULL);
printf("컨트롤(Control) + F2n");
RegisterHotKey(NULL, 1, MOD_CONTROL, VK_F2);
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
PeekMessage(&msg, 0, 0, 0, 0x0001);
switch (msg.message)
{
case WM_HOTKEY:
if (msg.wParam == 1)
{
printf("ㅇㅋ 누름 (Hotkey Event)n");
int a = SendMessage(hListView, LVM_GETITEMCOUNT, 0, 0);
int b;
int ms = GetTickCount();
CreateDirectory(L"C:\Users\root\Desktop\new Folder", NULL);
//SendMessage(hListView, LVM_REDRAWITEMS, 0, a+5);
//SendMessage(hListView, LVM_UPDATE, a, 0);
//SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST| SHCNF_PATHW, L"C:\Users\root\Desktop", NULL);
while (true)
{
b = SendMessage(hListView, LVM_GETITEMCOUNT, 0, 0);
if (a != b) {
ms = GetTickCount() - ms;
break;
}
}
Sleep(1);
POINT mouse;
GetCursorPos(&mouse);
SendMessage(hListView, LVM_SETITEMPOSITION, b - 1, MAKEWPARAM(mouse.x, mouse.y));
SendMessage(hListView, LVM_EDITLABEL, b - 1, 0);
}
}
}

我认为CreateDirectory问题所在,并尝试使用NtCreateFile(用户空间/Ring3(创建一个目录,但它仍然很慢。 该图标在桌面上仍然显示缓慢。

有WinAPI/DesktopAPI/COM/ATL吗?

这是一个模拟什么是新的代码...文件夹菜单从控制台应用程序执行。

它会打开某个文件夹的视图,并在其中创建一个子文件夹,然后进入名称编辑模式。它使用Shell的IFileOperation接口以及ATL的智能指针只是为了简单起见。

int main()
{
CoInitialize(NULL);
{
CComHeapPtr<ITEMIDLIST> pidl;
CComHeapPtr<ITEMIDLIST> newFolderPidl;
CComPtr<IFileOperationProgressSink> sink;
CComPtr<IFileOperation> fo;
CComPtr<IShellItem> folder;
LPCITEMIDLIST pidls = { nullptr };
CFileOperationProgressSink csink;
// get some folder's PIDL
HRESULT hr = SHParseDisplayName(L"c:\temp", NULL, &pidl, 0, NULL);
if (SUCCEEDED(hr))
{
// open it and, for demo purposes here, selects nothing (pass a terminator PIDL)
ITEMIDLIST idl = { 0 };
pidls = { &idl };
hr = SHOpenFolderAndSelectItems(pidl, 1, &pidls, 0);
if (SUCCEEDED(hr))
{
// get a Shell Item from this PIDL
hr = SHCreateItemFromIDList(pidl, IID_PPV_ARGS(&folder));
}
}
if (SUCCEEDED(hr))
{
// we want to operate on files and directory using Shell's API
hr = fo.CoCreateInstance(CLSID_FileOperation);
if (SUCCEEDED(hr))
{
csink.QueryInterface(IID_PPV_ARGS(&sink));
// create the new folder "New Folder", using the sink
// we need a sink to be advised of what really is the new folder
// we can't use the name we've passed
// because it could have been changed like 'New Folder (2)' if there's already a 'New Folder', etc.
hr = fo->NewItem(folder, FILE_ATTRIBUTE_DIRECTORY, L"New Folder", NULL, sink);
if (SUCCEEDED(hr))
{
// commit
hr = fo->PerformOperations();
}
}
}
if (SUCCEEDED(hr))
{
// we want the new child's relative-to-the-parent PIDL
CComPtr<IParentAndItem> pai;
hr = csink.m_newItem->QueryInterface(&pai);
if (SUCCEEDED(hr))
{
hr = pai->GetParentAndItem(NULL, NULL, &newFolderPidl);
}
}
if (SUCCEEDED(hr))
{
// now, select the new child and get into name-edit mode (from the parent again)
pidls = { newFolderPidl };
hr = SHOpenFolderAndSelectItems(pidl, 1, &pidls, OFASI_EDIT);
}
}
CoUninitialize();
return 0;
}
// the sink. this implementation's only interested by PostNewItem
class CFileOperationProgressSink : public IFileOperationProgressSink
{
LONG m_cRef;
public:
CComPtr<IShellItem> m_newItem;
CFileOperationProgressSink() : m_cRef(1)
{
}
STDMETHOD(QueryInterface)(REFIID riid, void** ppv)
{
static const QITAB qit[] =
{
QITABENT(CFileOperationProgressSink, IFileOperationProgressSink),
{ 0 },
};
return QISearch(this, qit, riid, ppv);
}
STDMETHOD_(ULONG, AddRef)() { return InterlockedIncrement(&m_cRef); }
STDMETHOD_(ULONG, Release)()
{
if (InterlockedDecrement(&m_cRef))
return m_cRef;
delete this;
return 0;
}
STDMETHOD(StartOperations)() { return S_OK; };
STDMETHOD(FinishOperations)(HRESULT hrResult) { return S_OK; };
STDMETHOD(PreRenameItem)(DWORD dwFlags, IShellItem* psiItem, LPCWSTR pszNewName) { return S_OK; };
STDMETHOD(PostRenameItem)(DWORD dwFlags, IShellItem* psiItem, LPCWSTR pszNewName, HRESULT hrRename, IShellItem* psiNewlyCreated) { return S_OK; };
STDMETHOD(PreMoveItem)(DWORD dwFlags, IShellItem* psiItem, IShellItem* psiDestinationFolder, LPCWSTR pszNewName) { return S_OK; };
STDMETHOD(PostMoveItem)(DWORD dwFlags, IShellItem* psiItem, IShellItem* psiDestinationFolder, LPCWSTR pszNewName, HRESULT hrMove, IShellItem* psiNewlyCreated) { return S_OK; };
STDMETHOD(PreCopyItem)(DWORD dwFlags, IShellItem* psiItem, IShellItem* psiDestinationFolder, LPCWSTR pszNewName) { return S_OK; };
STDMETHOD(PostCopyItem)(DWORD dwFlags, IShellItem* psiItem, IShellItem* psiDestinationFolder, LPCWSTR pszNewName, HRESULT hrCopy, IShellItem* psiNewlyCreated) { return S_OK; };
STDMETHOD(PreDeleteItem)(DWORD dwFlags, IShellItem* psiItem) { return S_OK; };
STDMETHOD(PostDeleteItem)(DWORD dwFlags, IShellItem* psiItem, HRESULT hrDelete, IShellItem* psiNewlyCreated) { return S_OK; };
STDMETHOD(PreNewItem)(DWORD dwFlags, IShellItem* psiDestinationFolder, LPCWSTR pszNewName) { return S_OK; };
STDMETHOD(PostNewItem)(DWORD dwFlags, IShellItem* psiDestinationFolder, LPCWSTR pszNewName, LPCWSTR pszTemplateName, DWORD dwFileAttributes, HRESULT hrNew, IShellItem* psiNewItem)
{
if (SUCCEEDED(hrNew))
{
psiNewItem->QueryInterface(&m_newItem);
}
return S_OK;
}
STDMETHOD(UpdateProgress)(UINT iWorkTotal, UINT iWorkSoFar) { return S_OK; };
STDMETHOD(ResetTimer)() { return S_OK; };
STDMETHOD(PauseTimer)() { return S_OK; };
STDMETHOD(ResumeTimer)() { return S_OK; };
};

相关内容

  • 没有找到相关文章

最新更新