如何讀取exe的圖標到speedbutton上(99分)(99分)

  • 主题发起人 主题发起人 luyear
  • 开始时间 开始时间
L

luyear

Unregistered / Unconfirmed
GUEST, unregistred user!
我用ExtractIcon函數讀的,可是不能畫到speedbutton上<br>var i: integer;<br>&nbsp;h:hicon;<br>&nbsp;iconindex:word;<br>begin<br>&nbsp;iconindex:=0;<br>&nbsp;h:=ExtractIcon(hinstance,'c:/windows/notepad.exe',iconindex);<br>&nbsp;//Drawicon(speedbutton1.glyph.canvas.handle,0,0,h);//這樣不可以<br>&nbsp;//Drawicon(speedbutton2.glyph.handle,0,0,h);//這樣不可以<br>&nbsp;form1.Icon.Handle:=h;//這樣可以<br>&nbsp;Drawicon(form1.canvas.handle,0,0,h);//這樣可以<br>end;
 
Icon := TIcon.Create;<br>Icon.Handle := ExtractIcon(hInstance, '*.Exe', 0);<br><br>with Glyph do<br>begin<br>&nbsp; Canvas.FillRect(Canvas.ClipRect);<br>&nbsp; Canvas.Draw(0,0,Icon);<br>end;
 
我要读小图标!!!!不是要读32x32的大图标
 
忍了吧,ExtractIcon只能弄出来32X32的,你要小的,只有用SHGetFileInfo(也不知<br>写对没有:P),HubDog的宝典1.8似乎有详尽论述,你找找看...
 
HubDog的宝典1.8在哪里??????
 
再加100分!!!!!!!!!!!!
 
http://www.51delphi.com/book.asp
 
没有阿!!!!
 
这就是HubDog的《Delphi之未经证实的葵花宝典version 1.8》 中的有关提取图标的内容,<br>请自己试一试吧<br><br><br>&nbsp; &nbsp;WINSHELLAPI DWORD WINAPI SHGetFileInfo(<br>&nbsp; &nbsp; LPCSTR &nbsp;pszPath,<br>&nbsp; &nbsp; DWORD &nbsp;dwFileAttributes,<br>&nbsp; &nbsp; SHFILEINFO FAR &nbsp;*psfi,<br>&nbsp; &nbsp; UINT &nbsp;cbFileInfo,<br>&nbsp; &nbsp; UINT &nbsp;uFlags<br>&nbsp; &nbsp;);<br>它的作用是:取回文件系统中的一个对象的信息,对象可以是文件、文件夹、<br>目录或驱动器的根目录。经过三个多小时的调试,我终于完全弄明白怎样取<br>得并显示一个文件的图标了:包括大图标、小图标,象资源管理器上的那样。<br>操作过程大体如下:<br>var ShFileInfo: TSHFILEINFO;<br>&nbsp; &nbsp; FileList:TListView;<br>begin<br>&nbsp; ...<br>&nbsp; Result := FileList.Items.Add;<br>&nbsp; with Result do<br>&nbsp; begin<br>&nbsp; &nbsp; Caption:=filename;<br>&nbsp; &nbsp; ShGetFileInfo(pchar(vartostr(filename)), 0, SHFileInfo, SizeOf(SHFileInfo),<br>&nbsp; &nbsp; &nbsp; &nbsp; SHGFI_SMALLICON or SHGFI_SYSICONINDEX or SHGFI_TYPENAME)=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; showmessage('error in shgetfileinfo');<br>&nbsp; &nbsp; ImageIndex := SHFileInfo.iIcon;<br>&nbsp; end;<br>&nbsp; ...<br>end;<br>这是最关键的几个地方,中间省略了许多细节。<br>另:我在6月17日的笔记里提到ExtractAssociatedIcon()和ExtractIcon()函数,<br>也可以提取文件的图标,但是速度比这个方法要慢上许多,而且我不会用它们提<br>取小图标。<br>//////////////////////////////////////////////////////////////<br>uses ShellApi;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; Icon : hIcon;<br>&nbsp; IconIndex : word;<br><br>begin<br>&nbsp;Icon := ExtractAssociatedIcon(HInstance,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'C:/SomePath/SomeFile.ext',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IconIndex);<br>&nbsp;DrawIcon(Form1.Canvas.Handle, 10, 10, Icon);<br>end;<br><br>
 
Procedure DrawExeIcon(Const ExeName: STring; // Exe文件名<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IcoIndex: Integer; &nbsp; &nbsp; // Exe文件中图表的索引<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Btn: TSpeedButton);<br>Begin<br>&nbsp; Try<br>&nbsp; &nbsp; With Btn.Glyph Do<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; Icon := TIcon.Create;<br>&nbsp; &nbsp; &nbsp; Icon.Handle := ExtractIcon(hInstance, PChar(ExeName), IcoIndex);<br>&nbsp; &nbsp; End;<br>&nbsp; Finally<br>&nbsp; End;<br>End;
 
to omvm &nbsp; &nbsp;: &nbsp;那本书我也看过! 方法试过,不行<br>to 星际浪人: &nbsp;你的方法是读大图标,我要16x16的小图标
 
能给我一份吗?谢谢!<br>rikduck@8848.net
 
能给我一份看看吗?谢谢!<br>lzw77@21cn.com
 
接受答案了.
 
对这个问题我有了一些心得,先说如何将图标画到SpeedButton上吧。下面有一例程。<br>首先,放一个ImageList控件,导入一个图标文件。<br>其次,读取图标你可以试一试这个函数Createiconfromresourceex。<br>
<br>[red][[/red] [blue]2001.9.23:omvm修改:这个函数不太好用,实际的程序中还是用SHGetFileInfo函数,<br>[^]更正一下以免误导他人。[/blue][red]][/red]<br>
<br>我已有了一个大概的程序,如需要,我可以整理出来发给你。<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls, Buttons, ExtCtrls, &nbsp;ImgList;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; SpeedButton1: TSpeedButton;<br>&nbsp; &nbsp; ImageList1: TImageList;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br><br>&nbsp; with SpeedButton1.Glyph do<br>&nbsp; begin<br>&nbsp; &nbsp; Width := ImageList1.Width;<br>&nbsp; &nbsp; Height := ImageList1.Height;<br>&nbsp; &nbsp; SpeedButton1.Glyph.Canvas.Brush.Color := clFuchsia;//! for lack of a better color<br>&nbsp; &nbsp; SpeedButton1.Glyph.Canvas.FillRect(Rect(0,0, Width, Height));<br>&nbsp; &nbsp; ImageList1.Draw(Canvas, 0, 0, 0);<br>&nbsp; end;<br><br>end;<br><br>end.<br>
 
后退
顶部