WinAPI资源管理器外壳文档“详细信息”

4
这个问题类似,我想要一种实用的方法来读取在资源管理器中选择“属性”时显示的“详细信息”窗格中的信息。
例如,在下面的屏幕截图中,随机选定了一些详细信息。
我不是想以其他方式确定圈出的特定项目(例如,请不要建议如何查找图像的像素宽度),这不是我想要的。我需要一种解析所有可用信息的方法,以便在我的程序中显示,而无需事先了解文件的内容。这只是为了创建一个特定的用户界面,而不必打开显示的对话框。
值得一提的是,当今流行的语言是Delphi,但我完全能够翻译C++或任何其他WinAPI代码,但如果您恰好有Delphi代码,那对我个人来说会更好。
编辑:我想能够获取文档特定的详情,例如PowerPoint文档中的幻灯片数量,这不符合大多数文档访问属性所需的标准化常量。
例如,我可以使用此代码从PowerPoint文档中获取一些基本信息(但无法获取幻灯片计数)。
uses shellapi,ComObj;
{$R *.dfm}

const
  FmtID_SummaryInformation: TGUID =
    '{F29F85E0-4FF9-1068-AB91-08002B27B3D9}';

function FileTimeToDateTimeStr(F: TFileTime): string;
var
  LocalFileTime: TFileTime;
  SystemTime: TSystemTime;
  DateTime: TDateTime;
begin
  if Comp(F) = 0 then Result := '-'
  else
  begin
    FileTimeToLocalFileTime(F, LocalFileTime);
    FileTimeToSystemTime(LocalFileTime, SystemTime);
    with SystemTime do
      DateTime := EncodeDate(wYear, wMonth, wDay) +
        EncodeTime(wHour, wMinute, wSecond, wMilliseconds);
    Result := DateTimeToStr(DateTime);
  end;
end;

function GetDocInfo(const FileName: WideString): string;
var
  I: Integer;
  PropSetStg: IPropertySetStorage;
  PropSpec: array[2..19] of TPropSpec;
  PropStg: IPropertyStorage;
  PropVariant: array[2..19] of TPropVariant;
  Rslt: HResult;
  S: string;
  Stg: IStorage;
begin
  Result := '';
  try
    OleCheck(StgOpenStorage(PWideChar(FileName), nil, STGM_READ or
      STGM_SHARE_DENY_WRITE,
      nil, 0, Stg));
    PropSetStg := Stg as IPropertySetStorage;
    OleCheck(PropSetStg.Open(FmtID_SummaryInformation,
      STGM_READ or STGM_SHARE_EXCLUSIVE, PropStg));
    for I := 2 to 19 do
    begin
      PropSpec[I].ulKind := PRSPEC_PROPID;
      PropSpec[I].PropID := I;
    end;
    Rslt := PropStg.ReadMultiple(18, @PropSpec, @PropVariant);
    OleCheck(Rslt);
    if Rslt <> S_FALSE then for I := 2 to 19 do
      begin
        S := '';
        if PropVariant[I].vt = VT_LPSTR then
          if Assigned(PropVariant[I].pszVal) then
            S := PropVariant[I].pszVal;
            case I of
              2:  S  := Format('Title: %s', [S]);
              3:  S  := Format('Subject: %s', [S]);
              4:  S  := Format('Author: %s', [S]);
              5:  S  := Format('Keywords: %s', [S]);
              6:  S  := Format('Comments: %s', [S]);
              7:  S  := Format('Template: %s', [S]);
              8:  S  := Format('Last saved by: %s', [S]);
              9:  S  := Format('Revision number: %s', [S]);
              10: S := Format('Total editing time: %g sec',
                  [Comp(PropVariant[I].filetime) / 1.0E9]);
              11: S := Format('Last printed: %s',
                  [FileTimeToDateTimeStr(PropVariant[I].filetime)]);
              12: S := Format('Create time/date: %s',
                  [FileTimeToDateTimeStr(PropVariant[I].filetime)]);
              13: S := Format('Last saved time/date: %s',
                  [FileTimeToDateTimeStr(PropVariant[I].filetime)]);
              14: S := Format('Number of pages: %d', [PropVariant[I].lVal]);
              15: S := Format('Number of words: %d', [PropVariant[I].lVal]);
              16: S := Format('Number of characters: %d',
                  [PropVariant[I].lVal]);
              17:; // thumbnail
              18: S := Format('Name of creating application: %s', [S]);
              19: S := Format('Security: %.8x', [PropVariant[I].lVal]);
            else
               S := Format('unknown property#%d: %s', [i,S]);

        end;
        if S <> '' then Result := Result + S + #13#10;
      end;
  finally
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  memo1.text :=GetDocInfo('C:\mypowerpoint.ppt');
end;

5
请从这里开始:http://msdn.microsoft.com/zh-cn/library/windows/desktop/ff728871.aspx - David Heffernan
1
@DavidHeffernan 我之前看过那个,但是发现很难弄清楚如何获取更多的属性。我的截图没有显示出来的是,通常有更多针对文档类型的特定部分。例如,在PowerPoint中有幻灯片数量属性。我可以获取所有基本的文档属性,如标题、字数、作者等,但任何更具体的内容都不可用。 - unsynchronized
1
我同意这不是最容易使用的API,但所有信息都可以通过属性存储获得。 - David Heffernan
@DavidHeffernan 感谢您的建议。我已经成功地实现了一些功能,暂时可以满足需求,但是它确实有些依赖于预定义常量。如果有人有一个简单的解决方案来获取属性列表,包括它们的可读描述,我将保留这个问题的开放状态。 - unsynchronized
1个回答

2

在Windows 7 SDK中,有关于属性枚举的C++示例代码(位于Samples\winui\shell\appplatform\PropertyEdit目录下),以及一个更长的演示项目可以在CodePlex上找到。

由于属性系统是可扩展的,所以并没有“正式”的属性列表;不过,Microsoft的属性列表可以在SDK的propkey.h文件中找到。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接