当前位置: 首页 > news >正文

wxwidgets直接获取系统图标,效果类似QFileIconProvider

目前只做了windows版本,用法类似QFileIconProvider

// 头文件
#ifndef WXFILEICONPROVIDER_H
#define WXFILEICONPROVIDER_H#include <wx/wx.h>
#include <wx/icon.h>
#include <wx/image.h>
#include <wx/bmpcbox.h>  // Include for wxBitmapBundleclass wxFileIconProvider : public wxObject
{
public:wxFileIconProvider();virtual ~wxFileIconProvider();// Get icon for the given pathvirtual wxBitmapBundle GetIcon(const wxString& path);
};#endif
#include "wxFileIconProvider.h"
#include <wx/filename.h>#ifdef __WXMSW__
#include <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#include <wx/msw/private.h>
#include <wx/bitmap.h>
#include <wx/image.h>
#include <wx/bmpcbox.h>wxBitmapBundle wxFileIconProvider::GetIcon(const wxString &path)
{wxBitmapBundle bundle;SHFILEINFO shFileInfo;wxFileName fileName(path);if (!fileName.Exists()){wxLogMessage("Error: File does not exist or is not accessible: %s", path);return bundle;}wxString absolutePath = fileName.GetFullPath();DWORD_PTR result = SHGetFileInfo(absolutePath.wc_str(), 0, &shFileInfo, sizeof(shFileInfo), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_LARGEICON);if (result){if (shFileInfo.hIcon){wxIcon smallIcon;smallIcon.CreateFromHICON(shFileInfo.hIcon);// Extract Large IconSHFILEINFO largeFileInfo = shFileInfo;largeFileInfo.iIcon = 0;SHGetFileInfo(absolutePath.wc_str(), 0, &largeFileInfo, sizeof(largeFileInfo), SHGFI_ICON | SHGFI_LARGEICON);wxIcon largeIcon;largeIcon.CreateFromHICON(largeFileInfo.hIcon);if (smallIcon.IsOk() && largeIcon.IsOk()){wxBitmap smallBitmap = wxBitmap(smallIcon);wxBitmap largeBitmap = wxBitmap(largeIcon);bundle = wxBitmapBundle::FromBitmaps(smallBitmap, largeBitmap);DestroyIcon(shFileInfo.hIcon);DestroyIcon(largeFileInfo.hIcon);}else{DestroyIcon(shFileInfo.hIcon);DestroyIcon(largeFileInfo.hIcon);}}else{DestroyIcon(shFileInfo.hIcon);}}else{DWORD error = GetLastError();wxLogMessage("SHGetFileInfo failed with error code: %lu", error);}return bundle;
}#endifwxFileIconProvider::wxFileIconProvider() {}
wxFileIconProvider::~wxFileIconProvider() {}
// 用法
wxSize iconSize = wxSize(42, 42);
wxFileIconProvider iconProvider;
wxString filePath = "c:/users/xxx/"; // 传入文件夹或文件的路径
wxBitmapBundle bundle = iconProvider.GetIcon(filePath);
wxBitmapButton *btTest = new wxBitmapButton(this, wxID_ANY, bundle.GetBitmap(iconSize),wxDefaultPosition, buttonSize);

传入路径,出现的就是为文件夹图标,传入文件,则是文件类型相对应的图标。

http://www.lryc.cn/news/527115.html

相关文章:

  • Arduino大师练成手册 -- 控制 PN532 NFC 模块
  • 解决日志中 `NOT NULL constraint failed` 异常的完整指南
  • C动态库的生成与在Python和QT中的调用方法
  • UE求职Demo开发日志#7 强化属性完善
  • Day35:字符串的大小写转换
  • 喜报丨迪捷软件入选2025年浙江省“重点省专”
  • 深度剖析 PyTorch框架:从基础概念到高级应用的深度学习之旅!
  • 基于C++的DPU医疗领域编程初探
  • Linux 执行 fdisk -l 出现 GPT PMBR 大小不符 解决方法
  • 图漾相机搭配VisionPro使用简易教程
  • 第一届“启航杯”网络安全挑战赛WP
  • 大模型训练策略与架构优化实践指南
  • 新电脑安装系统找不到硬盘原因和解决方法来了
  • 【Linux】21.基础IO(3)
  • 深度学习算法:从基础到实践
  • 27. 【.NET 8 实战--孢子记账--从单体到微服务】--简易报表--报表服务
  • coffee销售数据集分析:基于时间趋势分析的实操练习
  • 【转帖】eclipse-24-09版本后,怎么还原原来版本的搜索功能
  • Centos 修改历史读录( HISTSIZE)
  • lwIP——4 网络接口
  • pytest自动化测试 - pytest夹具的基本概念
  • FreeRtos的使用教程
  • yolov11 解读简记
  • 实验二 数据库的附加/分离、导入/导出与备份/还原
  • Kafka常见问题之 `javax.management.InstanceAlreadyExistsException`
  • 性能测试丨JVM 性能数据采集
  • 计算机图形学实验练习(实验1.2-4.1AND补充实验12)
  • JWT实现单点登录
  • 云计算的概念与特点:开启数字化时代的新篇章
  • salesforce中如何获取一个profile的18位id