2011/05/26

[vc6]Load Icon File

參考網址:MSDN:About Icons
Icon有分四種尺寸

  • System small
  • System large
  • Shell small
  • Shell large
System small的意思:
顯示在視窗標題列上的Icon尺寸就是System small。


System large的意思:
(圖我cut不出來,請前輩賜教!)
當你按鍵盤alt+tab組合鍵時,會跳出一個切換應用程式的視窗,視窗上面有每個應用程式自己的Icon,這個Icon的尺寸就是System large。


Shell small的意思:
在檔案總管理面看到的notepad icon尺寸Shell small。
(關於這點其實我不是很確定,因為在檢視裡面可以選擇"縮圖"、"並排"、"圖示"、"清單"、"詳細資料"五種不同的檢視方式,而這五種都會使icon尺寸產生變化,知道的前輩再請告知,謝謝^^)


Shell large的意思:
桌面上的圖示尺寸即是Shell large。


----------------------------------------我是分隔線----------------------------------------
在來說一下怎麼把icon抓出來吧~
方法一:
<code>

{//Load Icon
HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
SHFILEINFO sfi;
ZeroMemory(&sfi,sizeof(SHFILEINFO));
SHGetFileInfo("C:\\windows\\system32\\notepad.exe",
 0,
 &sfi,
 sizeof(SHFILEINFO),
 SHGFI_ICON | SHGFI_SMALLICON);
HICON g_icon = sfi.hIcon;


m_btnP1.SetWindowPos(NULL,0,0,32,32,SWP_SHOWWINDOW);
m_btnP1.SetIcon(g_icon);
}

</code>
SHGetFileInfo的第五個參數:看你要哪一種icon,範例是用system small,也有SHGFI_LARGEICON。
如果SHFILEINFO::hIcon return為NULL,可能是沒有這個icon,或是第五個參數下錯。


方法二:
<code>

{
HICON phIcon;
phIcon = ::ExtractIcon((HINSTANCE)GetCurrentProcess(), "C:\\windows\\system32\\notepad.exe",(UINT)0);
if(!phIcon)return;
m_btnP1.SetIcon(phIcon);
m_btnP1.SetWindowPos(this,0,0,36,36,SWP_NOZORDER);
}

</code>
ExtractIcon的最後一個參數:可以決定要抓這個檔案的第幾個icon,0 抓第一張,以此類推。


方法三:
<code>

{
HICON phIconLarge,phIconSmall;
int total = ::ExtractIconEx("C:\\windows\\system32\\notepad.exe",0,&phIconLarge,&phIconSmall,1);


m_btnP1.SetIcon(phIconLarge);
m_btnP1.SetWindowPos(this,0,0,36,36,SWP_NOZORDER);
m_btnP2.SetIcon(phIconSmall);
m_btnP2.SetWindowPos(this,40,0,20,20,SWP_NOZORDER);
}

</code>
ExtractIconEx第二個參數帶-1,回傳值表示這個檔案有幾張icon,帶0,回傳的數字表示是第幾張icon,但我不確定是large的還是small的index。


reference:
MSDN:SHGetFileInfo
MSDN:ExtractIcon
MSDN:ExtractIconEx
以上~

沒有留言:

張貼留言