一个提取图标的问题(100分)

  • 主题发起人 主题发起人 e意孤行
  • 开始时间 开始时间
E

e意孤行

Unregistered / Unconfirmed
GUEST, unregistred user!
先声明,源代码是某位网友的,我只是把他没完成的保存图标部分加上.但遇到了问题.保存的<br>图标文件丢失了部分颜色信息.请各位帮忙看看.先谢了.下面是源码:<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, ExtCtrls, TFlatPanelUnit, TFlatSpeedButtonUnit, ComCtrls,<br>&nbsp; ImgList, Menus, StdCtrls,shellapi;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; FlatPanel1: TFlatPanel;<br>&nbsp; &nbsp; FlatSpeedButton1: TFlatSpeedButton;<br>&nbsp; &nbsp; FlatSpeedButton2: TFlatSpeedButton;<br>&nbsp; &nbsp; FlatSpeedButton3: TFlatSpeedButton;<br>&nbsp; &nbsp; ListView1: TListView;<br>&nbsp; &nbsp; ImageList1: TImageList;<br>&nbsp; &nbsp; OpenDialog1: TOpenDialog;<br>&nbsp; &nbsp; SaveDialog1: TSaveDialog;<br>&nbsp; &nbsp; Image1: TImage;<br>&nbsp; &nbsp; StatusBar1: TStatusBar;<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; N1: TMenuItem;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; procedure FlatSpeedButton3Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FlatSpeedButton1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FlatSpeedButton2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure ListView1SelectItem(Sender: TObject; Item: TListItem;<br>&nbsp; &nbsp; &nbsp; Selected: Boolean);<br>&nbsp; &nbsp; procedure N1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Label1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>Function Enumresname(hmodule:Hmodule;lptype,lpname:Pchar;lparam:longint):Bool;stdcall;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; hexe:Hinst;<br>&nbsp; Filename:string;<br>&nbsp; Ficon:Ticon;<br><br>implementation<br><br>{$R *.dfm}<br><br>Function Enumresname(hmodule:Hmodule;lptype,lpname:Pchar;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lparam:longint):Bool;stdcall;<br>var<br>&nbsp; hicon1:hicon;<br>&nbsp; hresource:hrsrc;<br>&nbsp; hmem:hrsrc;<br>&nbsp; lpresource:pbyte;<br>&nbsp; listitem:Tlistitem;<br>begin<br>&nbsp;hresource:=findresource(hexe,lpname,RT_ICON);<br>&nbsp;hmem:=loadresource(hexe,hresource);<br>&nbsp;lpresource:=lockresource(hmem);<br>&nbsp;hicon1:=Createiconfromresourceex(lpresource,sizeofresource(hexe,hresource),<br>&nbsp;True,$0030000,32,32,LR_DEFAULTCOLOR);<br>&nbsp;With Form1 do<br>&nbsp;begin<br>&nbsp;Ficon.handle:=hicon1;<br>&nbsp;imagelist1.addicon(ficon);<br>&nbsp;listitem:=listview1.items.add;<br>&nbsp;ListItem.ImageIndex := imagelist1.count - 1;<br>&nbsp;end;<br>&nbsp;result:=True;<br>end;<br><br><br><br>procedure TForm1.FlatSpeedButton3Click(Sender: TObject);<br>begin<br>&nbsp; close;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>&nbsp; Ficon:=Ticon.Create;<br>end;<br><br>procedure TForm1.FlatSpeedButton1Click(Sender: TObject);<br>var flag:integer;<br>begin<br>&nbsp; if OpenDialog1.Execute then<br>&nbsp;begin<br>&nbsp; Filename:=OpenDialog1.FileName ;<br>&nbsp; StatusBar1.simpletext:=filename;<br>&nbsp; OpenDialog1.HistoryList.Add(filename);<br>&nbsp; hexe:=loadlibrary(pchar(filename));<br>&nbsp; if (hExe =0 ) then Showmessage('Could not load exe.');<br>&nbsp; flag:=1;<br>&nbsp; imagelist1.Clear ;<br>&nbsp; listview1.Items.Clear ;<br>&nbsp; Enumresourcenames(hexe,RT_ICON,@enumresname,flag);<br>&nbsp;end;<br>end;<br><br>procedure TForm1.FlatSpeedButton2Click(Sender: TObject);<br>begin<br>&nbsp; if savedialog1.Execute then<br>&nbsp; &nbsp; image1.Picture.Icon.SaveToFile(savedialog1.filename);<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; ficon.free;<br>end;<br><br>procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;<br>&nbsp; Selected: Boolean);<br>var<br>&nbsp; myicon:Ticon;<br>begin<br>&nbsp; if selected then begin<br>&nbsp; &nbsp; flatspeedbutton2.Enabled:=true;<br>&nbsp; &nbsp; myicon:=Ticon.Create;<br>&nbsp; &nbsp; imagelist1.GetIcon(item.ImageIndex,myicon);<br>&nbsp; &nbsp; image1.Picture.Icon.Handle:=myicon.handle;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.N1Click(Sender: TObject);<br>begin<br>&nbsp; listview1.arrange(arDefault)<br>end;<br><br>procedure TForm1.Label1Click(Sender: TObject);<br>begin<br>&nbsp; shellexecute(handle,nil,'mailto:cjpqj@263.net',nil,nil,SW_SHOWNORMAL);<br>end;<br><br>end.
 
多少色的ICO
 
把Createiconfromresourceex(lpresource,sizeofresource(hexe,hresource),<br>&nbsp;True,$0030000,32,32,LR_DEFAULTCOLOR);<br>换成CreateIconFromResource(lpresource, sizeofresource(hexe,hresource),<br>&nbsp;Trkue,$00030000);来创建图标就可以了。<br>因为你用第一个函数来创建图标指定了它的大小和颜色,但是你提取出来的图标资源里包含<br>的图标可能不是这种格式的,用第二个函数可以让系统来决定图标的大小和颜色就能避免颜<br>色丢失了!
 
试过了.不行.
 
你是用TIcon的SaveToFile方法来保存图标的,问题就出在这里,Delphi提供的这个方法有<br>缺陷,你应该用写文件的方式来保存图标。
 
不用这个方法该怎么做呢?我是个新手不是很清楚.能不能说详细一点.在这里先谢过.
 
后退
顶部