Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2008
    Messaggi
    98

    [VB.NET] hDC Picturebox

    Salve,
    posto qui perchè ho dei problemi con VB.NET 2008.

    Per svariato tempo ho programmato in visual basic 6 e oggi ho deciso di cercare di convertire un progetto di vb6 in vb.net.

    Ora mi trovo di fronte a questo problema.

    Dunque, utilizzo una sub che si basa su un'API per estrarre le icone dai file e dagli eseguibili.

    Questa è la sub:

    codice:
    Private Sub GetDefaultIcon(FileName As String, Picture_hDC As Long)
    Dim TempFileName As String 'Never manipulate an input unless it doubles as an output
    Dim lngError As Long 'For receiving error numbers
    Dim lngRegKeyHandle As Long 'Stores the "handle" of the registry key that is currently open
    Dim strProgramName As String 'Stores the contents of the first registry key
    Dim strDefaultIcon As String 'Stores the contents of the second registry key
    Dim lngStringLength As Long 'Sets / Returns the length of the output string
    Dim lngIconNumber As Long 'Stores the icon number within a file
    Dim lngIcon As Long 'Stores the "Icon Handle" for the default icon
    Dim intN As Integer 'For any temporary numbers
    
    TempFileName = Right(FileName, Len(FileName) - InStrRev(FileName, ".") + 1)
    
    If LCase(TempFileName) = ".exe" Then
    strDefaultIcon = Space(260)
    lngStringLength = GetSystemDirectory(strDefaultIcon, 260)
    strDefaultIcon = Left(strDefaultIcon, lngStringLength) & "\SHELL32.DLL"
    lngIconNumber = 2
    GoTo Draw_Icon
    End If
    
    lngError = RegOpenKey(HKEY_CLASSES_ROOT, TempFileName, lngRegKeyHandle)
    If lngError Then GoTo No_Icon 'we do not even have a valid extension so lets NOT try to find an icon!
    lngStringLength = 260
    strProgramName = Space$(260) 'Make space for the incoming string
    'Get the key value:
    lngError = RegQueryValueEx(lngRegKeyHandle, vbNullString, 0, 0, strProgramName, lngStringLength)
    If lngError Then 'if there's an error then BIG TROUBLE so lets use the normal "windows" icon
    lngError = RegCloseKey(lngRegKeyHandle) 'the world may be about to end (or just an error) but we'll clean up anyway
    GoTo No_Icon
    End If
    lngError = RegCloseKey(lngRegKeyHandle) 'if this generates an error then we can't do anything about it anyway
    strProgramName = Left(strProgramName, lngStringLength - 1) 'Cut the name down to size
    
    'Use the value of the last key in the name of the next one (strProgramName)
    lngError = RegOpenKey(HKEY_CLASSES_ROOT, strProgramName & "\DefaultIcon", lngRegKeyHandle)
    If lngError Then GoTo No_Icon 'there is no icon for this extension so lets NOT try to load what doesn't exist!
    'The rest is just the same as before
    lngStringLength = 260
    strDefaultIcon = Space$(260)
    lngError = RegQueryValueEx(lngRegKeyHandle, vbNullString, 0, 0, strDefaultIcon, lngStringLength)
    If lngError Then
    lngError = RegCloseKey(lngRegKeyHandle)
    GoTo No_Icon
    End If
    lngError = RegCloseKey(lngRegKeyHandle)
    strDefaultIcon = Trim$(Left(strDefaultIcon, lngStringLength - 1))
    
    intN = InStrRev(strDefaultIcon, ",") 'Find the commer
    If intN < 1 Then GoTo No_Icon 'We MUST have an icon number and it will be after the ",": NO COMMA NO DEFAULT ICON
    lngIconNumber = Trim$(Right(strDefaultIcon, Len(strDefaultIcon) - intN)) 'What number is after the comma
    strDefaultIcon = Trim$(Left(strDefaultIcon, intN - 1)) 'We only want what's before the comma in the file name
    
    Draw_Icon:
    lngIcon = ExtractIcon(App.hInstance, strDefaultIcon, lngIconNumber) 'Extract the Icon
    If lngIcon = 1 Or lngIcon = 0 Then GoTo No_Icon 'if 1 or 0 then after all that the Icon Could not be retrieved
    
    lngError = DrawIcon(Picture_hDC, 10, 10, lngIcon) 'Draw the icon in the box
    'If that was unsucessful then we can't do anything about it now!
    lngError = DestroyIcon(lngIcon)
    'Again we can't correct any errors now
    Exit Sub
    No_Icon:
    'No icon could be found so we use the normal windows icon
    'This icon is held in shell32.dll in the system directory, Icon 0
    strDefaultIcon = Space(260)
    lngStringLength = GetSystemDirectory(strDefaultIcon, 260)
    strDefaultIcon = Left(strDefaultIcon, lngStringLength) & "\SHELL32.DLL"
    lngIconNumber = 0
    GoTo Draw_Icon
    End Sub
    Dunque da vb6 semplicemente la richiamavo così:
    Call GetDefaultIcon("C:\Percorso\file.png", Picture1.hDC)

    Ma in vb.net non posso farlo perchè hDC pare non esista.. avevo letto in giro di utenti che consigliavano di usare Panel, che ho visto sembrano essere come gli iframe di vb6, solo che non capisco come fare..

    Spero che qualcuno mi aiuti, grazie.

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2008
    Messaggi
    98
    Comunque più in generale mi servirebbe qualche esempio o suggerimento per estrarre le icone da file ed eseguibile e visualizzarle in una picture.

    In vb6 usavo quella sub che ora sto cercando di convertire in codice per vb.net, ma se qualcuno di voi mi sa consigliare qualche altra funzione ben venga. (Le api da usare presumo siano le stesse, ma essendo nuovo di vb.net il problema è l'utilizzo)

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2008
    Messaggi
    98
    Ho risolto utilizzando:

    NomePicture.Image = Drawing.Icon.ExtractAssociatedIcon(“PercorsoFile”) .ToBit

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.