Accueil > > > MODIFIER ICONE REPERTOIRE [C][WIN32]
MODIFIER ICONE REPERTOIRE [C][WIN32]
Information sur la source
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
- #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;
- }
#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é.
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
RE : WIN APIRE : WIN API par racpp
Cliquez pour lire la suite par racpp WIN APIWIN API par omarino_007
Cliquez pour lire la suite par omarino_007
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|