我们都知道,Windows的应用程序中包含有好多图标文件。作为一名程序员,会经常为制作、设计程序图标费尽心思,当我们看到许多应用软件的图标非常漂亮的时候,是多么的羡慕!我们可不可以借鉴一下他们的图标?完全可以!我们利用 ExtractIcon API函数就能够轻松地从ICO文件或可执行文件以及DLL文件中提取图标。读者从这个实例中可以学到如何提取可执行文件内部的所有图标并应用到自己的程序中。读者从这个实例中可以学到如何提取可执行文件内部的所有图标并应用到自己的所编制的LabWindows/CVI的程序中。
//退出按钮
int CVICALLBACK QuitCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
1楼
0
0
回复
代码如下:
//引入windows头文件和shellapi头文件
#include "windows.h"
#include "shellapi.h"
#include "windows.h"
#include "shellapi.h"
#include
#include
#include
#include "提取图标.h"
#include
#include
#include "提取图标.h"
static int panelHandle;
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
if (InitCVIRTE (hInstance, 0, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "提取图标.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
LPSTR lpszCmdLine, int nCmdShow)
{
if (InitCVIRTE (hInstance, 0, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "提取图标.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
//获取按钮
int CVICALLBACK OkCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int handle;
char path[300];
HDC hdc;
int nicons;
HICON Large, Small;
int i;
int kind;
switch (event)
{
case EVENT_COMMIT:
//为大图标和小图标赋初值
Large = 0;
Small = 0;
//打开文件
if (FileSelectPopup ("", "*.exe", "*.exe;*.dll;*.lib", "打开", VAL_OK_BUTTON, 0, 0, 1, 1, path)>0)
{
/* 提取文件中的图标函数
UINT ExtractIconEx(
LPCTSTR lpszFile, // 文件名
int nIconIndex, // 图标索引 若为-1,则表示获得总图标数目
HICON *phiconLarge, // 大图标索引
HICON *phiconSmall, // 小图标索引
UINT nIcons // 图标数目
);
*/
//获得文件图标总数
nicons = ExtractIconEx((LPCTSTR)path, -1, &Large, &Small, 1);
}
//将图标分别提取出来,并在面板中显示
for (i=0; i<(int)nicons; i++)
{
ExtractIconEx((LPCTSTR)path, i, &Large, &Small, 1);
int CVICALLBACK OkCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int handle;
char path[300];
HDC hdc;
int nicons;
HICON Large, Small;
int i;
int kind;
switch (event)
{
case EVENT_COMMIT:
//为大图标和小图标赋初值
Large = 0;
Small = 0;
//打开文件
if (FileSelectPopup ("", "*.exe", "*.exe;*.dll;*.lib", "打开", VAL_OK_BUTTON, 0, 0, 1, 1, path)>0)
{
/* 提取文件中的图标函数
UINT ExtractIconEx(
LPCTSTR lpszFile, // 文件名
int nIconIndex, // 图标索引 若为-1,则表示获得总图标数目
HICON *phiconLarge, // 大图标索引
HICON *phiconSmall, // 小图标索引
UINT nIcons // 图标数目
);
*/
//获得文件图标总数
nicons = ExtractIconEx((LPCTSTR)path, -1, &Large, &Small, 1);
}
//将图标分别提取出来,并在面板中显示
for (i=0; i<(int)nicons; i++)
{
ExtractIconEx((LPCTSTR)path, i, &Large, &Small, 1);
//获得面板句柄
GetPanelAttribute (panelHandle, ATTR_SYSTEM_WINDOW_HANDLE, &handle);
//获取指定窗口的设备场景
hdc = GetDC((HWND)handle);
//要显示的图标类型
GetCtrlVal (panelHandle, PANEL_BINARYSWITCH, &kind);
if (kind == 0)
{
//依次在面板中绘制大图标
DrawIcon(hdc, (i/4)*40+10, (i%4)*40+10, Large);
}
else
{
//依次在面板中绘制小图标
DrawIcon(hdc, (i/4)*40+10, (i%4)*40+10, Small);
}
//释放图标句柄
DestroyIcon(Large);
DestroyIcon(Small);
}
break;
}
return 0;
}
GetPanelAttribute (panelHandle, ATTR_SYSTEM_WINDOW_HANDLE, &handle);
//获取指定窗口的设备场景
hdc = GetDC((HWND)handle);
//要显示的图标类型
GetCtrlVal (panelHandle, PANEL_BINARYSWITCH, &kind);
if (kind == 0)
{
//依次在面板中绘制大图标
DrawIcon(hdc, (i/4)*40+10, (i%4)*40+10, Large);
}
else
{
//依次在面板中绘制小图标
DrawIcon(hdc, (i/4)*40+10, (i%4)*40+10, Small);
}
//释放图标句柄
DestroyIcon(Large);
DestroyIcon(Small);
}
break;
}
return 0;
}
//退出按钮
int CVICALLBACK QuitCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}