Modifier icone repertoire [c][win32]

Description

Bonjour,

Ce code liste toutes les icones des fichiers *.exe et *.dll présents dans un répertoire, les affiche a l'aide d'une ListView et applique l'icone désirée au répertoire parent.

Source / Exemple :


#include <windows.h>
#include <shlobj.h>
#include <commctrl.h>
#include "resource.h"

#pragma comment(lib, "comctl32.lib")

#define WM_ENDTHR WM_USER + 1
#define SZCLASSDESKTOP "[.ShellClassInfo]\r\nIconFile="
#define SZICONINDEX "\r\nIconIndex="

HWND hDlg, hlist, hpb;
HANDLE hthread;
DWORD dwthread;
char szpath[260];
char szwrite[512];
char *szptr;

//Fonction BruNews
__declspec (naked) char* __stdcall bnultoa(unsigned int dwnum, char* szdst)
{
  __asm {
    mov      eax, [esp+4]
    mov      ecx, [esp+8]
    or       eax, eax
    jnz      short L1
    lea      eax, [ecx+1]
    mov      byte ptr[ecx], 48
    mov      byte ptr[eax], 0
    ret      8
 L1:
    mov      [esp-4], edi
    mov      [esp-8], ecx
    mov      edi, ecx
    mov      ecx, eax
 L2:
    mov      eax, -858993459
    mul      ecx
    mov      eax, edx
    shr      eax, 3
    mov      edx, ecx
    lea      ecx, [eax+eax*8]
    add      ecx, eax
    sub      edx, ecx
    add      dl, 48
    mov      [edi], dl
    mov      ecx, eax
    inc      edi
    test     eax, eax
    jnz      short L2
    mov      [esp-12], edi
    mov      eax, [esp-8]
    mov      byte ptr[edi], 0
 L3:
    dec      edi
    mov      dl, [eax]
    mov      cl, [edi]
    mov      [edi], dl
    mov      [eax], cl
    inc      eax
    cmp      eax, edi
    jb       short L3
    mov      edi, [esp-4]
    mov      eax, [esp-12]
    ret      8
  }
}

__inline char* bnstrcpy(char *dst, char *src) // return ptr sur NULL final
{
  __asm {
    mov   eax, dst
    mov   ecx, src
    dec   eax
LcpyLoop:
    mov   dl, [ecx]
    inc   eax
    inc   ecx
    mov   [eax], dl
    or    dl, dl
    jz    short LcpyOut
    mov   dl, [ecx]
    inc   eax
    inc   ecx
    mov   [eax], dl
    or    dl, dl
    jz    short LcpyOut
    mov   dl, [ecx]
    inc   eax
    inc   ecx
    mov   [eax], dl
    or    dl, dl
    jnz   short LcpyLoop
LcpyOut:
  }
}

DWORD __stdcall ThreadProc(LPVOID lp)
{
	WIN32_FIND_DATA wfd;
	LV_ITEM lvi;
	HIMAGELIST himglist;
	HICON *hbig;
	HANDLE hrep, hheap = GetProcessHeap();
	DWORD dwnumbericon = 0, i, dwdeb, dwtot = 0;
	char *c, *s, buf[128];
	
	strcpy(szptr, "\\*.*"); s = ++szptr; //on sauve s pour la suite du prog.
	hrep = FindFirstFile(szpath, &wfd);
	if (INVALID_HANDLE_VALUE == hrep) return 0;
	dwdeb = GetTickCount();
	SendMessage(hlist, LVM_DELETEALLITEMS, 0, 0);
	lvi.mask = LVIF_TEXT | LVIF_IMAGE;
	lvi.iSubItem = 0;
	himglist = ImageList_Create(GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), ILC_COLORDDB | ILC_MASK, dwnumbericon, 1);
	SendMessage(hlist, LVM_SETIMAGELIST, LVSIL_NORMAL, (LPARAM)himglist);
	while (FindNextFile(hrep, &wfd)) {
		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue;	//si c'est un repertoire, on ignore
		SetDlgItemText(hDlg, IDST_FILE, wfd.cFileName);
		c = wfd.cFileName; while (*c && *c++ != '.');
		if (strcmp(c, "exe") && strcmp(c, "dll")) continue;		//si c'est pas un exe ou dll, on ignore
		strcpy(s, wfd.cFileName);
		dwnumbericon = ExtractIconEx(szpath, -1, 0, 0, 0);
		if (!dwnumbericon) continue;		//si pas d'icones, on ignore
		SendMessage(hpb, PBM_SETPOS, 0, 0);
		SendMessage(hpb, PBM_SETRANGE, 0, MAKELPARAM(0, dwnumbericon));
		hbig = (HICON *) HeapAlloc(hheap, 0, dwnumbericon * 4);
		ExtractIconEx(szpath, 0, hbig, 0, dwnumbericon);
		c += 3;	*c++ = ' ';					
		lvi.pszText = wfd.cFileName;
		for (i = 0; i < dwnumbericon; ++i) {
            lvi.iImage = ImageList_AddIcon(himglist, hbig[i]);
			lvi.iItem = i;
			strcpy(c, itoa(i, buf, 10));		//ajoute l'index de l'icone a la suite du texte
			SendMessage(hlist, LVM_INSERTITEM, 0, (LPARAM)&lvi);
			SendMessage(hpb, PBM_STEPIT, 0, 0);
		}
		dwtot += i;
        HeapFree(hheap, 0, hbig);
	}
	
	dwdeb = (GetTickCount() - dwdeb);	//avoir le temps en secondes

  • ((DWORD *)buf) = 0x706D6F43; // "Comp"
  • ((DWORD *)(buf + 4)) = 0x2074656C; //"let "
c = bnultoa(dwdeb, (buf + 8));
  • ((DWORD *)c) = 0x2D20736D; //"ms -"
c = bnultoa(dwtot, (c + 4));
  • ((DWORD *)c) = 0x6F636920; //" ico"
  • ((DWORD *)(c + 4)) = 0x0073656E; //"nes "
SetDlgItemText(hDlg, IDST_FILE, buf); DeleteObject(himglist); FindClose(hrep); PostMessage(hDlg, WM_ENDTHR, 0, 0); return 1; } void ApplicIcone(int iItem) { LVITEM lvi; LRESULT lresult; HANDLE hfile; DWORD dwWritten = 0; char buf[300], *b = buf, *c; lvi.cchTextMax = 300; lvi.pszText = buf; lvi.iSubItem = 0; lresult = SendMessage(hlist, LVM_GETITEMTEXT, iItem, (LPARAM)&lvi); if (!lresult) return; SetFileAttributes(szpath, FILE_ATTRIBUTE_SYSTEM); strcpy(szptr, "desktop.ini"); hfile = CreateFile(szpath, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN, 0); if (INVALID_HANDLE_VALUE == hfile) return; while (*b) b++; while (*b != ' ') b--;
  • b = 0; b++;
c = bnstrcpy(szwrite, SZCLASSDESKTOP); c = bnstrcpy(c, buf); c = bnstrcpy(c, SZICONINDEX); bnstrcpy(c, b); WriteFile(hfile, szwrite, strlen(szwrite), &dwWritten, 0); MessageBox(0, "Fichier créé, icone appliquée.", "OK", 0); CloseHandle(hfile); } int ObtDirectory() { BROWSEINFO bi; ITEMIDLIST *il; char buff[300]; bi.hwndOwner = hDlg; bi.pidlRoot = NULL; bi.pszDisplayName = buff; bi.lpszTitle = "Choisissez un répertoire :"; bi.ulFlags = 0; bi.lpfn = NULL; if (NULL == (il = SHBrowseForFolder(&bi))) return 0; return SHGetPathFromIDList(il, szpath); } BOOL CALLBACK DlgProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { static int iItem = -1; switch (message) { case WM_INITDIALOG: SetClassLong(hwnd, GCL_HICON, (long)LoadIcon(0, IDI_APPLICATION)); hDlg = hwnd; hlist = GetDlgItem(hwnd, IDLV_ICON); hpb = GetDlgItem(hwnd, IDPB_ICON); SendMessage(hpb, PBM_SETSTEP, 1, 0); return 1; case WM_ENDTHR: CloseHandle(hthread); dwthread = 0; return 0; case WM_COMMAND: switch (wparam) { case IDOK: if (!szpath[0]) { MessageBox(hwnd, "Choisissez un répertoire.", 0, 0x40); return 0; } hthread = CreateThread(0, 0, ThreadProc, 0, 0, &dwthread); return 0; case IDCANCEL: if (dwthread) { TerminateThread(hthread, 0); CloseHandle(hthread); } EndDialog(hwnd, 0); return 0; case IDBT_STOP: if (dwthread) { SuspendThread(hthread); if (IDNO == MessageBox(hwnd, "Etes-vous sur de vouloir stopper la recherche ?", "Fin prématurée", MB_YESNO)) { ResumeThread(hthread); return 0; } TerminateThread(hthread, 0); } return 0; case IDBT_APPLIC: if (iItem == -1) { MessageBox(hwnd, "Sélectionnez un item", 0, 0x40); return 0; } ApplicIcone(iItem); return 0; case IDBT_FIND: SetDlgItemText(hwnd, IDST_FILE, 0); SendMessage(hpb, PBM_SETPOS, 0, 0); if (!ObtDirectory()) { iItem = -1; *szpath = 0; } else { szptr = szpath; while (*szptr) szptr++; } SendMessage(hlist, LVM_DELETEALLITEMS, 0, 0); SetDlgItemText(hwnd, IDEC_PATH, szpath); return 0; } case WM_NOTIFY: if (wparam == IDLV_ICON) if (((LPNMITEMACTIVATE)lparam)->hdr.code == NM_CLICK) iItem = ((LPNMITEMACTIVATE)lparam)->iItem; default: return 0; } } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE p, LPSTR q, int r) { InitCommonControls(); DialogBoxParam(hInstance, (LPCTSTR)IDD_ICON, 0, DlgProc, 0); return 0; }

Conclusion :


Voila, je remercie Arnotic pour sa relecture du code et des changements qu'il y a apporté.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.