Introduction :
Voici un exemple d'énumération de ressources.
Dans cette exemple nous allons afficher dans une ListView toutes les icônes
de la dll "moricons.dll".
C'est la fonction EnumResourceNames qui lance l'énumération.
Les ressources peuvent être récupérer dans une fonction CALLBACK,
nommée ResNameProc dans l'exemple.
Code complet :
winmain.c :
#include <windows.h>
#include <commctrl.h>
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK ResNameProc(HANDLE, LPCTSTR, LPTSTR, LONG);
HINSTANCE hinst;
HWND hListView;
HIMAGELIST himgList;
LV_ITEM lvi;
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wc;
hinst = hinstance;
wc.style = 0 ;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "MaWinClass";
if(!RegisterClass(&wc)) return FALSE;
hwnd = CreateWindow("MaWinClass", "List View", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, NULL, hinstance, NULL);
if (!hwnd) return FALSE;
ShowWindow(hwnd, nCmdShow);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/******************************************************************************/
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
HINSTANCE dllhinst;
HICON hIcon;
InitCommonControls();
himgList = ImageList_Create(GetSystemMetrics(SM_CXICON),
GetSystemMetrics(SM_CYICON), ILC_COLOR32 , 3, 3);
ImageList_SetBkColor(himgList, GetSysColor(COLOR_WINDOW));
ZeroMemory(&lvi, sizeof(LV_ITEM));
lvi.mask = LVIF_TEXT | LVIF_IMAGE ;
hListView =CreateWindowEx(WS_EX_CLIENTEDGE , WC_LISTVIEW, "",
WS_CHILD | WS_VISIBLE , 0, 0, 0, 0, hwnd, NULL, hinst, NULL);
ListView_SetImageList(hListView, himgList, LVSIL_NORMAL);
dllhinst = LoadLibrary("moricons.dll");
EnumResourceNames(dllhinst, RT_GROUP_ICON,
(ENUMRESNAMEPROC)ResNameProc,NULL);
FreeLibrary(dllhinst);
return 0;
}
case WM_SIZE:
MoveWindow(hListView, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
ListView_Arrange(hListView,LVA_DEFAULT);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
BOOL CALLBACK ResNameProc(HANDLE hModule, LPCTSTR lpszType,LPTSTR lpszName,
LONG lParam)
{
if (IS_INTRESOURCE(lpszName))
{
HICON hIcon;
char st[256];
hIcon = LoadIcon(hModule, MAKEINTRESOURCE((USHORT)lpszName));
ImageList_AddIcon(himgList, hIcon);
wsprintf(st,"Icône %d",(USHORT)lpszName);
lvi.pszText = st;
lvi.iImage = (USHORT)lpszName;
lvi.iItem = (USHORT)lpszName;
ListView_InsertItem(hListView, &lvi);
}
else
{
/* icones nommées */
}
return TRUE;
}
J'ai testé les compilations avec C++ Builder et DevC++.
A vos PC.
CGi
|