zoukankan      html  css  js  c++  java
  • delphi 获取文件图标

    {根据文件的名字得到此文件在系统中对应大小的图标large=true(64*64) false(32*32)}
    procedure GetFileIcon(TypeName: Widestring; Icon: TIcon;Large:Boolean=False);
    var
    strTmp: Widestring;
    IndexS, IndexL: HIcon;
    ShFileInfo: TSHFileInfo ;
    imgList: TImageList;
    begin
    strTmp := TypeName;
    TypeName := Tnt_WideLowerCase(wideExtractFileExt(strTmp));
    if pos('.', TypeName) = 0 then
    TypeName := '.' + TypeName;
    if Large then
    begin
    //如果是EXE、Ico文件,直接取文件的图标
    if (TypeName = '.exe') or (TypeName = '.ico') then
    begin
    ExtractIconExw(pwidechar(strTmp), 0, IndexL, IndexS, 1);
    if IndexS <> 0 then
    begin
    Icon.Handle := IndexL;
    exit;
    end;
    end;
    //在临时目录下建立一个空类型文件,便于取图标
    TypeName := GetWindowsTempPath + TypeName;
    if not wideFileExists(TypeName) then
    with TUniFileStream.Create(TypeName, fmCreate) do
    Free;
    imgList := TImageList.CreateSize(64, 64);
    try
    {将系统图象列表连接到TListView控件上。注意我们设置动态建立的图象列表
    的ShareImages属性为真,这可以确保我们不试图释放Windows系统拥有的图象}
    imgList.ShareImages := True;
    imgList.Handle := ShGetFileInfo ('', 0, SHFileInfo, SizeOf(SHFileInfo),
    SHGFI_SYSICONINDEX or
    SHGFI_LARGEICON);
    ShGetFileInfo (pchar( string( TypeName)), 0, SHFileInfo, SizeOf(SHFileInfo),
    SHGFI_SYSICONINDEX or
    SHGFI_LARGEICON);
    imgList.GetIcon(SHFileInfo.iIcon, Icon);
    finally
    imgList.Free;
    end;
    end else
    begin
    //如果是EXE、Ico文件,直接取文件的图标
    if (TypeName = '.exe') or (TypeName = '.ico') then
    begin
    ExtractIconExw(pwidechar(strTmp), 0, IndexL, IndexS, 1);
    if IndexS <> 0 then
    begin
    Icon.Handle := IndexS;
    exit;
    end;
    end;
    //在临时目录下建立一个空类型文件,便于取图标
    TypeName := GetWindowsTempPath + TypeName;
    if not wideFileExists(TypeName) then
    with TUniFileStream.Create(TypeName, fmCreate) do
    Free;
    imgList := TImageList.CreateSize(32, 32);
    try
    imgList.ShareImages := True;
    imgList.Handle := ShGetFileInfo ('', 0, SHFileInfo, SizeOf(SHFileInfo),
    SHGFI_SYSICONINDEX or
    SHGFI_SMALLICON);
    ShGetFileInfo (pchar( string( TypeName)), 0, SHFileInfo, SizeOf(SHFileInfo),
    SHGFI_SYSICONINDEX or
    SHGFI_SMALLICON);
    imgList.GetIcon(SHFileInfo.iIcon, Icon);
    finally
    imgList.Free;
    end;
    end;
    end;

  • 相关阅读:
    HashMap遍历的两种方式
    抽象类和接口的区别是什么
    “用户、组或角色'XXX'在当前数据库中已存在”问题
    FCKEditor在IE10下的不兼容问题解决方法
    ADODB.Connection 错误 '800a0e7a' 未找到提供程序。该程序可能未正确安装。解决方法!
    ASP.NET中Url重写后,打不开真正的Html页面
    运用正则表达式在Asp中过滤Html标签代码的四种不同方法
    静态页分页功能js代码
    .NET生成静态页面的方案总结
    禁止ViewState的3种解决方法
  • 原文地址:https://www.cnblogs.com/blogpro/p/11446769.html
Copyright © 2011-2022 走看看