自制获取文件图标工具(getIconImage函数)
By
o3o_o3o_o3o
at 2022-05-19 • 1人收藏 • 1481人看过
原理:把文件创建一个快捷方式,保存到另一个文件夹里(保证这个文件的快捷方式是在文件夹里面的第一位),用win.ui.explorer里的loadFile把快捷方式的图标显示出来,截个图,再关闭explorer的窗口(速度要快),再把图片保存一下。
值得一提的是:这个东西也可以获取文件夹的图标。
import win.ui;
import fsys.lnk;
import win.ui.explorer;
import soImage;
lnk=fsys.lnk();
Img = soImage();
getIconImage = function(filepath){
var Form = win.form(text="aardio form";right=200;bottom=70)
Form.add(treeview={cls="treeview";left=-54;top=0;right=50;bottom=70;asel=false;bgcolor=16777215;edge=1;z=1})
string.save("F:\getIconImage\1.lnk","1");
lnk.path = filepath;
lnk.save("F:\getIconImage\1.lnk")
var explorer = win.ui.explorer( Form.treeview );
explorer.setIconSize("extralarge")
explorer.loadFile("F:\getIconImage");
Form.setPos(-300,-300,,,1/*_HWND_BOTTOM*/);
Form.show(true);
Form.setPos(0,0,,,1/*_HWND_BOTTOM*/);
win.delay(150);
Img.capture(Form.treeview.hwnd);
Img.crop(69,2,129,66);
Img.save("\icon.jpg");
Form.close();
}
getIconImage(/*路径*/);可能会因为电脑版本的不同,crop的位置也会不同。这是我使用的电脑:
版本:Windows 10 家庭中文版
版本号:21H
操作系统内部版本:19044.1706
5 个回复 | 最后更新于 2022-10-01
这是来自官方论坛的例子,可以参考下获取图标的做法
import win.ui;
/*DSG{{*/
mainForm = win.form(text="aardio form";right=427;bottom=458)
mainForm.add(
button={cls="button";text="清空";left=124;top=390;right=293;bottom=429;z=2};
listview={cls="listview";left=9;top=10;right=417;bottom=375;acceptfiles=1;edge=1;mode="icon";z=1}
)
/*}}*/
import fsys.info
import win.imageList
import process
import fsys.table
var fsystable = fsys.table("/paths.table")
mainForm.enableDpiScaling();
mainForm.show();
var add = function(filePath,dontsave){
var sfi = fsys.info.get(filePath, 0x100/*_SHGFI_ICON*/|0x4000/*_SHGFI_SYSICONINDEX*/)
var imageList = win.imageList(topointer(sfi.returnValue))
mainForm.listview.setImageList( imageList,0/*_LVSIL_NORMAL*/ )
var spth = io.splitpath(filePath)
var itemtext = spth.file
if(!#itemtext) itemtext = spth.drive
mainForm.listview.addItem(itemtext,,sfi.iIcon)
::DestroyIcon(sfi.hIcon)
imageList.destroy()
if(dontsave) return ;
if(!fsystable) fsystable = fsys.table("/paths.table")
table.push(fsystable,filePath)
fsystable.save()
}
for(k,v in fsystable){
if(k = "_gc") continue ;
if(!io.exist(fsystable[k])){
fsystable[k] = null
fsystable.save()
continue;
}
add(fsystable[k],true)
}
mainForm.listview.wndproc = function(hwnd,message,wParam,lParam){
select(message) {
case 0x233/*_WM_DROPFILES*/{
var tmpfiletab = win.getDropFile(wParam)
for(i=1;#tmpfiletab;1){
add(tmpfiletab[i])
}
}
}
}
mainForm.listview.onnotify = function(id,code,ptr){
select(code) {
case 0xFFFFFFFD/*_NM_DBLCLK*/ {
var nm = mainForm.listview.getNotifyMessage(code,ptr)
if(nm.iItem){
process.execute(fsystable[nm.iItem])
}
}
}
}
mainForm.button.oncommand = function(id,event){
fsystable = null
collectgarbage("collect")
string.save("/paths.table","{}" )
mainForm.listview.clear()
}
return win.loopMessage();登录后方可回帖



另辟蹊径