"Group by" windows 资源管理器中的某个文件夹

"Group by" a certain folder in windows explorer

我想做的是为我的应用程序创建一个文件夹,并确保每次用户进入该文件夹时,它都会被分组,如下所示:

除了磁盘将被一些 folders/files 取代。

所以基本上我正在尝试实现 "Group by" 函数的作用:

而且我必须在我的应用程序中使用 c/c++ 代码或蝙蝠来执行此操作。我猜这需要在注册表中完成,但我找不到哪里。有什么想法吗?

谢谢。

您必须明白,使用注册表更改资源管理器视图模式是肮脏的黑客行为。所以 使用风险自负。仅在 WINDOWS 7 上测试。

procedure SetFolderGroupBy(AParentWnd: HWND; const AFolder: UnicodeString; const AColumn: TPropertyKey; AAscending: Boolean);
var
  Desktop: IShellFolder;
  Attr: DWORD;
  Eaten: DWORD;
  IDList: PItemIDList;
  Bag: IPropertyBag;
  Direction: DWORD;
begin
  OleCheck(SHGetDesktopFolder(Desktop));
  try
    Attr := 0;
    OleCheck(Desktop.ParseDisplayName(AParentWnd, nil, PWideChar(AFolder), Eaten, IDList, Attr));
    try
      OleCheck(SHGetViewStatePropertyBag(IDList, 'Shell', SHGVSPB_FOLDERNODEFAULTS, IPropertyBag, Bag));
      try
        OleCheck(Bag.Write('SniffedFolderType', 'Generic'));
      finally
        Bag := nil;
      end;
      OleCheck(SHGetViewStatePropertyBag_(IDList, 'Shell\{5C4F28B5-F869-4E84-8E60-F11DB97C5CC7}', SHGVSPB_FOLDERNODEFAULTS, IPropertyBag, Bag));
      try
        if AAscending then Direction := SORT_ASCENDING
                      else Direction := DWORD(SORT_DESCENDING);
        OleCheck(Bag.Write('GroupByDirection', Direction));
        OleCheck(Bag.Write('GroupByKey:FMTID', GUIDToString(AColumn.fmtid)));
        OleCheck(Bag.Write('GroupByKey:PID', AColumn.pid));
        OleCheck(Bag.Write('GroupView', DWORD(-1)));
      finally
        Bag := nil;
      end;
    finally
      CoTaskMemFree(IDList);
    end;
  finally
    Desktop := nil;
  end;
end;