Le programme d'exemple décrites ci-dessous montre comment extraire une icône d'un programme Windows, si il exécute actuellement ou non. Il existe deux techniques différentes selon que le programme est exécuté dans Windows version 3.0 ou 3.1. La fonction API ExtractIcon, introduite dans Windows version 3.1, simplifie le processus d'extraction de l'icône. Dans Windows version 3.0, une approche différente est nécessaire. Les deux méthodes sont illustrées ci-dessous.
Le programme exemple ci-dessous affiche l'icône d'une application dans une zone d'image. L'exemple illustre la gestion de la propriété hDC du contrôle zone image, en particulier la relation entre la méthode Refresh, la propriété image et la propriété AutoRedraw. Le code de l'événement Command3_Click montre comment transférer l'image d'icône capturées à la propriété Image d'une zone d'image (Picture2).
Démarrez Visual Basic ou dans le menu Fichier, choisissez Nouveau projet (ALT, F, N) si Visual Basic est déjà en cours d'exécution. Form1 est créé par défaut.
Créer les contrôles suivants avec la valeur par défaut de paramètres de propriété :
Picture1
Picture2
Command1
Command2
Command3
Placez le code ci-dessous dans la comptabilité section déclarations de Form1 prise en charge de saisir chaque instruction Declare sur, une seule ligne :
' API declarations used in Windows version 3.0 method.
Declare Function GetActiveWindow Lib "User" () As Integer
Declare Function PostMessage Lib "User" (ByVal hWnd As Integer,
ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Any)
As Integer
Declare Function FindWindow Lib "User" (ByVal lpClassName As Any,
ByVal lpWindowName As Any) As Integer
Declare Function LoadLibrary Lib "Kernel" (ByVal lpLibFileName
As String) As Integer
Declare Function GetWindowWord Lib "User" (ByVal hWnd As Integer,
ByVal nIndex As Integer) As Integer
Declare Function LoadIcon Lib "User" (ByVal hInstance As Integer,
ByVal lpIconName As Any) As Integer
' API declarations used in Windows version 3.1 method.
Declare Function GetModuleHandle Lib "Kernel" (ByVal lpModuleName
As String) As Integer
Declare Function GetClassWord Lib "User" (ByVal hWnd As Integer,
ByVal nIndex As Integer) As Integer
Declare Function ExtractIcon Lib "SHELL" (ByVal hInst As Integer,
ByVal lpszexename As String, ByVal hIcon As Integer) As Integer
' API declaration used by both Windows version 3.0 and 3.1 methods.
Declare Function DrawIcon Lib "User" (ByVal hDC As Integer, ByVal x
As Integer, ByVal Y As Integer, ByVal hIcon As Integer) As Integer
' Window field offsets for GetClassWord() and GetWindowWord().
Const GWW_HINSTANCE = (-6)
Const GCW_HMODULE = (-16)
' Constants for SendMessage and PostMessage.
Const WM_CLOSE = &H10
' If using Visual Basic version 1.0, remove the single quotation mark
' from the following line of code:
' Const NULL = 0&
Placez le code suivant dans l'événement Form_Load de Form1 :
Sub Form_Load ()
Command1.Caption = " 3.0 method "
Command2.Caption = " 3.1 method "
Command3.Caption = " Transfer "
Form1.Caption = " Example of Extracting an Icon"
Form1.Width = Screen.Width * 2 / 3
Form1.Height = Screen.Height / 2
' Center the form on the screen.
' Enter the following two lines as one, single line:
Form1.Move (Screen.Width - Form1.Width) / 2,
(Screen.Height - Form1.Height) / 2
' Size and position the controls dynamically at run time.
' Enter the following two lines as one, single line:
Picture1.Move 0, 0, Form1.Width / 2,
Form1.Height - Command1.Height * 4
' Enter the following two lines as one, single line:
Picture2.Move Form1.Width / 2, 0, Form1.Width,
Form1.Height - Command2.Height * 4
' Enter the following two lines as one, single line:
Command1.Move (Form1.Width / 2 - Command1.Width) / 2,
Form1.Height - Command1.Height * 4
' Enter the following two lines as one, single line:
Command2.Move (Form1.Width / 2 - Command1.Width) / 2,
Form1.Height - Command1.Height * 3
' Enter the following two lines as one, single line:
Command3.Move (Form1.Width * 3 / 2 - Command2.Width) / 2,
Form1.Height - Command2.Height * 4
End Sub
Placez le code suivant dans l'événement Command1_Click. Configurer le code correspond à votre situation en supprimant l'apostrophe commentaire d'une des trois méthodes et Ajout d'apostrophes commentaire pour les deux autres--efficacement activer une des méthodes et désactiver l'autre deux.
Sub Command1_Click ()
Dim hInstance As Integer, handle As Integer, hIcon As Integer
Picture1.Picture = LoadPicture("") ' clear any previous image
' Three alternative ways to obtain the handle of the top-level window
' of the program whose icon you want to extract:
' Method 1: If the program is currently running and you don't know
' the class name.
' AppActivate ("Program Manager") ' Set focus to application.
' handle = GetActiveWindow() ' Get handle to window.
' Command1.SetFocus ' Return focus to button.
' Method 2: If program is running and you know the class name.
' Handle = FindWindow("Progman", "Program Manager")
' Method 3: If program is not running, use path and filename.
' Not_Running_Way "sysedit.exe" ' Call sub at general level.
' Exit Sub ' Bypass remaining code in this Sub.
' Now you have the handle -- use it to obtain the instance handle.
hInstance = GetWindowWord(handle, GWW_HINSTANCE)
Picture2.Print "3.O method "
Picture2.Print "handle="; Hex$(handle)
Picture2.Print "hInstance= "; Hex$(hInstance) ' Sanity check.
' Iterate through icon resource identifier values
' until you obtain a valid handle to an icon.
Do
hIcon = LoadIcon(hInstance, n&)
n& = n& + 1
Loop Until hIcon <> 0
Picture2.Print "hIcon= "; Hex$(hIcon)
Picture1.AutoRedraw = -1 ' Make hDC point to persistent bitmap.
r = DrawIcon(Picture1.hDC, 19, 19, hIcon) 'Draw the icon.
Picture1.Refresh ' Refresh from persistent bitmap to Picture.
End Sub
Placez le code suivant dans l'événement Command2_Click. Notez que les deux premières méthodes commentées hors sont proposées pour informations et le contraste à la méthode préférée, méthode 3.
Sub Command2_Click ()
Dim myhInst As Integer, hIcon As Integer
Picture1.Picture = LoadPicture("") ' Clear the previous image.
' Listed below are three alternative methods that can be used to
' obtain the hInst of your program's module handle.
' Method 1: Use only with .EXE version of your program.
' myhInst = GetModuleHandle("Project1.exe")
' Method 2: Use only with your program running in the environment.
' myhInst = GetModuleHandle("VB.EXE")
' Method 3: The slick way that works in either case.
myhInst = GetClassWord(hWnd, GCW_HMODULE)
' The path and filename of program to extract icon from.
lpzxExeName$ = "moricons.dll" ' Can also use an .EXE file here.
' Get handle to icon.
hIcon = ExtractIcon(myhInst, lpzxExeName$, 0)
Picture2.Print "3.1 method "
Picture2.Print "myhInst= "; Hex$(myhInst) ' Sanity check.
Picture2.Print "hIcon= "; Hex$(hIcon) ' Sanity check.
Picture1.AutoRedraw = -1 ' Make the picture's hDC point to the
' persistent bitmap.
r% = DrawIcon(Picture1.hDC, 19, 19, hIcon)
Picture1.Refresh ' Cause Windows to paint from the persistent bitmap
' to show the icon.
End Sub
Placez le code suivant dans la section déclarations générale du formulaire :
Sub Not_Running_Way (appname As String)
Dim hInstance As Integer, handle As Integer, hIcon As Integer
Dim hWndShelledWindow As Integer
Picture1.Picture = LoadPicture("") ' Clear any previous image.
hInstance = Shell(appname, 2)
Picture2.Print "3.0 method-application not running"
Picture2.Print "hInstance= "; Hex$(hInstance) ' Check return.
r = DoEvents() ' Allow time for shell to complete.
' The following technique is from another article that explains
' how to determine when a shelled process has terminated. It is
' used here to obtain the correct handle to the window of the
' application whose icon is being extracted. The handle is needed
' to close the application after the extraction is complete.
TimeOutPeriod = 5
fTimeOut = 0 ' Set to false.
s! = Timer
Do
r = DoEvents()
hWndShelledWindow = GetActiveWindow()
' Set timeout flag if time has expired.
If Timer - s! > TimeOutPeriod Then fTimeOut = True
Loop While hWndShelledWindow = Form1.hWnd And Not fTimeOut
' If a timeout occurred, display a timeout message and terminate.
If fTimeOut Then
MsgBox "Timeout waiting for shelled application", 16
Exit Sub
End If
' Iterate through icon resource identifier values
' until you obtain a valid handle to an icon.
Do
hIcon = LoadIcon(hInstance, n&)
n& = n& + 1
Loop Until hIcon <> 0
Picture2.Print "HICON= "; Hex$(hIcon)
Picture1.AutoRedraw = -1 ' Make hDC point to persistent bitmap.
r = DrawIcon(Picture1.hDC, 19, 19, hIcon)
Picture2.Print "return from DrawIcon="; r
Picture1.Refresh ' Refresh from persistent bitmap to picture.
' Now post a message to the window to close the application.
r = PostMessage(hWndShelledWindow, WM_CLOSE, NULL, NULL)
Picture2.Print "return from PostMessage="; r
End Sub
Placez le code suivant dans l'événement Command3_Click :
Sub Command3_Click ()
' This code transfers the extracted icon's image to Picture2's
' Picture property and demonstrates that DrawIcon assigns the image
' to the hDC of Picture1, which points to the persistent bitmap
' (Image property), not to the Picture property.
Picture2.Picture = LoadPicture("") ' Clear old icon.
Picture2.currenty = 0 ' Reset coordinates for printing
' return values.
Picture2.currentx = 0
Picture2.Picture = Picture1.image ' Transfer persistent bitmap image
' to the Picture property.
End Sub
Appuyez sur ALT F, V pour enregistrer le projet. Puis appuyez sur F5 pour exécuter le programme. Cliquez sur « 3.0 méthode » pour exécuter le code qui fonctionne dans Windows version 3.0. Cliquez sur « 3.1 méthode » pour exécuter le code qui fonctionne dans Windows version 3.1. Cliquez sur Command3 pour copier l'icône dans Picture1 à Picture2 afin que l'icône est accessible que Picture2.Picture.
Les deux méthodes d'extraire la première icône du fichier. Cela peut être modifiée pour trouver les icônes par seconde ou suivants :
Stocker la valeur de n & dans la boucle faire à partir de la première extraction et connecter que comme point de départ de la recherche suivante dans Windows version 3.0.
- Or -
Paramètre troisième de la fonction ExtractIcon à un numéro d'index spécifique dans Windows version 3.1.
Vous pourriez le faire dans une boucle pour rechercher et examiner chaque icône dans le fichier.
La méthode de la version 3.0 de Windows peut prendre légèrement plus de temps pour effectuer une itération et trouver le numéro D'IDENTIFICATION icône de la ressource.
IMPORTANT : Cet article est issu du système de traduction automatique mis au point par Microsoft (http://support.microsoft.com/gp/mtdetails). Un certain nombre d?articles obtenus par traduction automatique sont en effet mis à votre disposition en complément des articles traduits en langue française par des traducteurs professionnels. Cela vous permet d?avoir accès, dans votre propre langue, à l?ensemble des articles de la base de connaissances rédigés originellement en langue anglaise. Les articles traduits automatiquement ne sont pas toujours parfaits et peuvent comporter des erreurs de vocabulaire, de syntaxe ou de grammaire (probablement semblables aux erreurs que ferait une personne étrangère s?exprimant dans votre langue !). Néanmoins, mis à part ces imperfections, ces articles devraient suffire à vous orienter et à vous aider à résoudre votre problème. Microsoft s?efforce aussi continuellement de faire évoluer son système de traduction automatique.
La version anglaise de cet article est la suivante: 88944
(http://support.microsoft.com/kb/88944/en-us/
)
L'INFORMATION CONTENUE DANS CE DOCUMENT EST FOURNIE PAR MICROSOFT SANS GARANTIE D'AUCUNE SORTE, EXPLICITE OU IMPLICITE. L'UTILISATEUR ASSUME LE RISQUE DE L'UTILISATION DU CONTENU DE CE DOCUMENT. CE DOCUMENT NE PEUT ETRE REVENDU OU CEDE EN ECHANGE D'UN QUELCONQUE PROFIT.
Exclusion de responsabilité concernant les contenus obsolètes dans la Base de connaissances
Cet article concerne des produits pour lesquels Microsoft n'offre plus de support. Il est par conséquent fourni « en l'état » et ne sera plus mis à jour.