13,825
社区成员
发帖
与我相关
我的任务
分享
获取的exe图标生成TIcon对象,加入ImageList作为ListView的LargeImages属性,ListView的ViewStyle设置为vsIcon,为什么显示出来的图标有黑边(不透明)?用Image单独显示这个TIcon对象是正常的。
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
//窗体一个ListView,一个Image,一个ImageList,一个OpenDialog,一个Button
this->ImageList1->Width=32;
this->ImageList1->Height=32;
this->ListView1->ViewStyle=vsIcon;
this->ListView1->LargeImages=this->ImageList1;
this->OpenDialog1->Filter=L"*.exe|*.exe";
this->Button1->Caption=L"添加";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (this->OpenDialog1->Execute())
{
HICON hIcon1;
hIcon1 = ExtractIcon(HInstance, this->OpenDialog1->FileName.c_str(), 0);
TIcon *Icon1 = new TIcon;
Icon1->Handle = hIcon1;
this->Image1->Picture->Icon = Icon1;
this->Caption = IntToStr(Icon1->Width) + L" , " +
IntToStr(Icon1->Height);
this->ImageList1->AddIcon(Icon1);
delete Icon1;
TListItem *li=this->ListView1->Items->Add();
li->ImageIndex=this->ImageList1->Count-1;
}
}
//---------------------------------------------------------------------------
你左边的程序是用CB6写的吧,默认没有启用反锯齿,对比窗体上的文字最明显
group icon是一个图标,里面有多个不同分辨率的图,用ExtractIcon、ExtractIconEx、ExtractAssociatedIcon、ExtractAssociatedIconEx都只能取到第一个图标。Delphi、CB高本版编译出的exe里默认带的图标就是group icon(一套5个图标),你用工具生成的图标应该也是这种。
// ---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
// ---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
this->Button2->Enabled = false;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (this->OpenDialog1->Execute())
{
fileName = this->OpenDialog1->FileName;
total = (int)ExtractIcon(HInstance, fileName.c_str(), -1);
this->Caption = L"共有" + IntToStr(total) + L"个图标";
i = 0;
this->Image1->Picture = NULL;
this->Button2->Enabled = true;
}
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if (i < total)
{
HICON hIcon1;
hIcon1 = ExtractIcon(HInstance, fileName.c_str(), i);
TIcon *Icon1 = new TIcon;
Icon1->Handle = hIcon1;
this->Image1->Picture->Icon = Icon1;
this->Caption = IntToStr(Icon1->Width) + L" , " +
IntToStr(Icon1->Height);
delete Icon1;
++i;
if (i >= total)
this->Button2->Enabled = false;
}
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (this->OpenDialog1->Execute())
{
HICON hIcon1;
hIcon1 = ExtractIcon(HInstance, this->OpenDialog1->FileName.c_str(), 0);
TIcon *Icon1 = new TIcon;
Icon1->Handle = hIcon1;
this->Image1->Picture->Icon = Icon1;
this->Caption = IntToStr(Icon1->Width) + L" , " +
IntToStr(Icon1->Height);
delete Icon1;
}
}
// ---------------------------------------------------------------------------
icon格式是支持透明的,有一个xor mask和一个and mask,看你的图,左边listview里显示的很可能是原图,右边的可能缩放显示时做了平滑处理(反锯齿)
也许绘制时做了平滑处理?改一下背景色试试
这是网上的获取exe图标的代码:
HICON hIcon;
// Total =(int) ExtractIcon( Form1->Handle, FileName.c_str(), -1);
TIcon *Icon = new TIcon();
hIcon = ExtractIcon( Form1->Handle, FileName.c_str(), 0);
Icon->Handle=hIcon;