如何动态更改应用程序的图标?

sdf123321 2006-08-22 10:00:22
我有个VC程序,想要根据读取配置文件的图标文件名称,找到图标文件,根据图标文件来决定应用程序文件的图片,请问如何实现?
...全文
500 点赞 收藏 13
写回复
13 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
hhyytt 2006-08-22
SetIcon(LoadIcon(0,id), TRUE);
回复
tanyaliji 2006-08-22
LPARAM)LoadIcon(NULL,IDI_QUESTION
回复
折腾_苏州 2006-08-22
有个UpdateResource API可以更换资源,可以从别的exe中挖资源过来
http://kb.csdn.net/.net/Articles/200509/62bb93ea-9db7-4693-9034-3f5051ff13c0.html
回复
sdf123321 2006-08-22
这个我知道,我是想改变应用程序保存在硬盘上的文件的图标
回复
jerry 2006-08-22
在主框架窗口的 OnCreate函数内

m_hIcon = AfxGetApp()->LoadIcon(....);
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);

m_hIcon 即是你从文件读取的图标。
回复
Seu_why 2006-08-22
::SendMessage(AfxGetMainWnd->m_hWnd,WM_SETICON,ICON_SMALL,(LPARAM)hIcon);
回复
折腾_苏州 2006-08-22
HANDLE h = LoadImage(AfxGetResourceHandle(),"C:\\Burn.ico",IMAGE_ICON,0,0,LR_LOADFROMFILE);
::SendMessage(this->m_hWnd,WM_SETICON,ICON_SMALL,(LPARAM)h);
回复
handsomerun 2006-08-22
给窗口发送

WM_SETICON
This message is sent by an application to associate a new big or small icon with a window.

WM_SETICON wParam = (WPARAM)(BOOL) fType;
lParam = (LPARAM)(HICON) hicon;
Parameters
fType
Specifies the type of icon being set. It is one of the following values:
Value Description
ICON_BIG Sets the large icon for the window.
ICON_SMALL Sets the small icon for the window.




hicon
Handle to the new large or small icon. If this parameter is NULL, the icon indicated by fType is removed.


发送这个消息就可以了
回复
折腾_苏州 2006-08-22
::SendMessage(this->m_hWnd,WM_SETICON,ICON_SMALL,(LPARAM)LoadIcon(NULL,IDI_QUESTION));
回复
Bible_Chou 2006-08-22
上面其实就是在注册窗口类的时候直接载入自己想要的图标。
wndclass.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;
回复
Bible_Chou 2006-08-22
// Win32的

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

PSTR szCmdLine, int iCmdShow)

{

TCHAR szAppName[] = TEXT ("IconDemo") ;

HWND hwnd ;

MSG msg ;

WNDCLASS wndclass ;


wndclass.style = CS_HREDRAW | CS_VREDRAW ;

wndclass.lpfnWndProc = WndProc ;

wndclass.cbClsExtra = 0 ;

wndclass.cbWndExtra = 0 ;

wndclass.hInstance = hInstance ;

wndclass.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;

wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;

wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;

wndclass.lpszMenuName = NULL ;

wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))

{

MessageBox ( NULL, TEXT ("This program requires Windows NT!"),

szAppName, MB_ICONERROR) ;

return 0 ;

}



hwnd = CreateWindow (szAppName, TEXT ("Icon Demo"),

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, CW_USEDEFAULT,

CW_USEDEFAULT, CW_USEDEFAULT,

NULL, NULL, hInstance, NULL) ;



ShowWindow (hwnd, iCmdShow) ;

UpdateWindow (hwnd) ;



while (GetMessage (&msg, NULL, 0, 0))

{

TranslateMessage (&msg) ;

DispatchMessage (&msg) ;

}

return msg.wParam ;

}
回复
jerry 2006-08-22
MSDN 的例子,创建快捷方式并改变图标

/*PARAMETERS
fname_to_create_link = (e.g.) "c:\\mytextfile.txt"
lnk_fname = (e.g.) "yourname.lnk"
*/

void CreateLinkThenChangeIcon(LPTSTR fname_to_create_link,
LPTSTR lnk_fname)
{
HRESULT hres;
IShellLink *psl = NULL;
IPersistFile *pPf = NULL;
WORD wsz[256];
TCHAR buf[256];
int id;
LPITEMIDLIST pidl;

hres = CoCreateInstance( CLSID_ShellLink,

NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(LPVOID*)&psl);
if(FAILED(hres))
goto cleanup;
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&pPf);
if(FAILED(hres))
goto cleanup;
hres = psl->SetPath(fname_to_create_link);
if(FAILED(hres))

goto cleanup;
//place the shortcut on the desktop
SHGetSpecialFolderLocation(hwnd, CSIDL_DESKTOP, >pidl);

SHGetPathFromIDList(pidl, buf);

lstrcat(buf,"\\");
lstrcat(buf,lnk_fname);

MultiByteToWideChar(CP_ACP, 0, buf, -1, wsz, MAX_PATH);

hres = pPf->Save(wsz, TRUE);

if(FAILED(hres))

goto cleanup;

GetSystemDirectory(buf, 256);

lstrcat(buf,"\\shell32.dll");

hres = psl->SetIconLocation(buf, 1);

if(FAILED(hres))

goto cleanup;

hres = psl->GetIconLocation(buf, 256, &id);

if(FAILED(hres))

goto cleanup;

pPf-&Save(wsz, TRUE);

cleanup:

if(pPf)

pPf->Release();

if(psl)

psl->Release();

}

回复
jerry 2006-08-22
改变EXE文件的图标? 那就有点难了.不过一个办法是给EXE创建一个连接. 可以在创建的时候改变连接的图标
回复
相关推荐
发帖
界面
创建于2007-09-28

1.5w+

社区成员

VC/MFC 界面
申请成为版主
帖子事件
创建了帖子
2006-08-22 10:00
社区公告
暂无公告