Para expertos en APIs. Solo 3 lineas...

07/10/2003 - 21:07 por magustinapaz | Informe spam
(perdon, escribo a este foro PORQUE HAY MUCHISIMAS mas
posibilidades de encontrar a gente que sepa trabajar con
Handles, Device context, hDC, punteros, etc..)

Me faltan solo 3 lineas para terminar de adaptar un
pequeño procedimiento de VB6 a VB.NET. Este procedimiento
usa 3 funciones API de Windows.
Por el poco conocimiento que tengo a este nivel me ha sido
imposible terminarlo.
Debido a los cambios en el diseño del control PictureBox
no se como seguir.


Adjunto el codigo necesario. Las 3 lineas que me faltan
adaptar estan como comentario ', y marcadas con una flecha.
Las 3 lineas lo que hacen es terminar de obtener el icono
asociado a un determinado tipo de archivo.


Un saludo y gracias





Imports System.Runtime.InteropServices

Module Module1
Private Const HKEY_CLASSES_ROOT As Integer = &H80000000
Private Const MAX_PATH_LENGTH As Integer = 260
Private Const GOOD_RETURN_CODE As Integer = 0
Private Const STARTS_WITH_A_PERIOD As Integer = 46
Private Const SH_USEFILEATTRIBUTES As Integer = &H10
Private Const SH_TYPENAME As Long = &H400
Private Const SH_SHELLICONSIZE As Integer = &H4
Private Const SH_SYSICONINDEX As Integer = &H4000
Private Const SH_DISPLAYNAME As Integer = &H200
Private Const SH_EXETYPE As Integer = &H2000
Private Const BASIC_SH_FLAGS As Integer = SH_TYPENAME
Or SH_SHELLICONSIZE Or SH_SYSICONINDEX Or SH_DISPLAYNAME
Or SH_EXETYPE
Private Const SH_LARGEICON As Integer = &H0
Private Const SH_SMALLICON As Integer = &H1
Private Const ILD_TRANSPARENT As Integer = &H1
Private Const FILE_ATTRIBUTE_NORMAL As Integer = &H80

<StructLayout(LayoutKind.Sequential)> Public Structure
FILETIME
Public dwLowDateTime As Integer
Public dwHighDateTime As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
SHFILEINFO
Public hIcon As Integer
Public iIcon As Integer
Public dwAttributes As Integer
<MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH_LENGTH)> Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:€)

Public szTypeName As String


End Structure


Private Declare Function RegEnumKeyEx Lib "advapi32"
Alias "RegEnumKeyExA" (ByVal hkey As Integer, ByVal
dwIndex As Integer, ByVal lpName As String, ByRef lpcbName
As Integer, ByVal lpReserved As Integer, ByVal lpClass As
String, ByRef lpcbClass As Integer, <MarshalAs
(UnmanagedType.Struct)> ByRef lpftLastWriteTime As
FILETIME) As Integer
Private Declare Function SHGetFileInfo Lib "shell32"
Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal
dwFileAttributes As Integer, <MarshalAs
(UnmanagedType.Struct)> ByRef psfi As SHFILEINFO, ByVal
cbFileInfo As Integer, ByVal uFlags As Integer) As Integer
Private Declare Function ImageList_Draw
Lib "comctl32.dll" (ByVal himl As Integer, ByVal i As
Integer, ByVal hDCDest As Integer, ByVal x As Integer,
ByVal y As Integer, ByVal flags As Integer) As Integer



Private Item As System.Windows.Forms.ListViewItem
Private bResize As Boolean
Private iPos As Integer
Private vValue As Object
Private sValue As String
Private sKey As String
Private sImageList1Key As String
<MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH_LENGTH)> Public Subkey As String
<MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH_LENGTH)> Public KeyClass As String





Public Sub Prueba(ByVal form As Form1)
Dim Index As Integer
Dim TheTime As FILETIME
TheTime.dwHighDateTime = 0
TheTime.dwLowDateTime = 0
Dim Icon As Integer
Dim Icon2 As Integer
Dim Info As SHFILEINFO
Dim FileTypeName As String
Dim FileExtension As String
Subkey = New String(" "c, MAX_PATH_LENGTH)
KeyClass = New String(" "c, MAX_PATH_LENGTH)
Dim g As System.Drawing.Graphics

Do While RegEnumKeyEx(HKEY_CLASSES_ROOT, Index,
Subkey, MAX_PATH_LENGTH, 0, KeyClass, MAX_PATH_LENGTH,
TheTime) = GOOD_RETURN_CODE
If Asc(Subkey) = STARTS_WITH_A_PERIOD Then
Icon2 = SHGetFileInfo(Subkey,
FILE_ATTRIBUTE_NORMAL, Info, Len(Info),
SH_USEFILEATTRIBUTES Or BASIC_SH_FLAGS Or SH_LARGEICON)
Icon = SHGetFileInfo(Subkey,
FILE_ATTRIBUTE_NORMAL, Info, Len(Info),
SH_USEFILEATTRIBUTES Or BASIC_SH_FLAGS Or SH_SMALLICON)
FileTypeName = TrimNull(Info.szTypeName)
FileExtension = TrimNull(Subkey)
FileExtension = Right(FileExtension, Len
(FileExtension) - 1)
form.ListBox1.Items.Add(TrimNull(Subkey)
+ " - " + Info.szTypeName)
1)-> psmall.Picture = LoadPicture()
2)-> Call ImageList_Draw(Icon, Info.iIcon,
psmall.hDC, 0, 0, ILD_TRANSPARENT)
3)-> psmall.Picture = psmall.Image
sImageList1Key = "#" & FileExtension & "#"

If FileExtension = "" Then FileExtension
= "*"

End If
Index = Index + 1

Loop
End Sub
Private Function TrimNull(ByVal startstr As String) As
String
iPos = InStr(startstr, Chr(0))
If iPos > 0 Then
TrimNull = Left$(startstr, iPos - 1)
Exit Function
End If
TrimNull = startstr
End Function

End Module

Preguntas similare

Leer las respuestas

#1 Alejandro Mezcua
08/10/2003 - 09:21 | Informe spam
Hola.

No he probado tu código, pero por lo que parece has llegado a obtener el
handle del icono (Win32) y quieres pasarlo a algún control de Windows Forms.
Existe la posibilidad de crear iconos GDI+ (de WinForms) a partir de un
handle de Win32 utilizando la función (método) FromHandle de la clase Icon
del Framework, que creo que te será más fácil de usar.

Por otro lado, creo que estas mirando en el registro para obtener el archivo
en el que está definido el icono según la extensión de un archivo concreto
¿no?. Si tu idea es obtener el icono de un archivo concreto (es decir, sabes
su nombre) no es necesario que mires en el registro, puedes usar la función
SHGetFileInfo pasándole directamente el nombre del archivo del que quieres
extraer el icono, con lo que tu código se reducirá bastante, aunque lo que
haces también es correcto.

Un saludo,

Alejandro Mezcua
MVP .NET
Zaltor Soluciones Informáticas

"magustinapaz" wrote in message
news:0d9301c38d06$50c9f9c0$

(perdon, escribo a este foro PORQUE HAY MUCHISIMAS mas
posibilidades de encontrar a gente que sepa trabajar con
Handles, Device context, hDC, punteros, etc..)

Me faltan solo 3 lineas para terminar de adaptar un
pequeño procedimiento de VB6 a VB.NET. Este procedimiento
usa 3 funciones API de Windows.
Por el poco conocimiento que tengo a este nivel me ha sido
imposible terminarlo.
Debido a los cambios en el diseño del control PictureBox
no se como seguir.


Adjunto el codigo necesario. Las 3 lineas que me faltan
adaptar estan como comentario ', y marcadas con una flecha.
Las 3 lineas lo que hacen es terminar de obtener el icono
asociado a un determinado tipo de archivo.


Un saludo y gracias





Imports System.Runtime.InteropServices

Module Module1
Private Const HKEY_CLASSES_ROOT As Integer = &H80000000
Private Const MAX_PATH_LENGTH As Integer = 260
Private Const GOOD_RETURN_CODE As Integer = 0
Private Const STARTS_WITH_A_PERIOD As Integer = 46
Private Const SH_USEFILEATTRIBUTES As Integer = &H10
Private Const SH_TYPENAME As Long = &H400
Private Const SH_SHELLICONSIZE As Integer = &H4
Private Const SH_SYSICONINDEX As Integer = &H4000
Private Const SH_DISPLAYNAME As Integer = &H200
Private Const SH_EXETYPE As Integer = &H2000
Private Const BASIC_SH_FLAGS As Integer = SH_TYPENAME
Or SH_SHELLICONSIZE Or SH_SYSICONINDEX Or SH_DISPLAYNAME
Or SH_EXETYPE
Private Const SH_LARGEICON As Integer = &H0
Private Const SH_SMALLICON As Integer = &H1
Private Const ILD_TRANSPARENT As Integer = &H1
Private Const FILE_ATTRIBUTE_NORMAL As Integer = &H80

<StructLayout(LayoutKind.Sequential)> Public Structure
FILETIME
Public dwLowDateTime As Integer
Public dwHighDateTime As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> Public Structure
SHFILEINFO
Public hIcon As Integer
Public iIcon As Integer
Public dwAttributes As Integer
<MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH_LENGTH)> Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:€)
Public szTypeName As String


End Structure


Private Declare Function RegEnumKeyEx Lib "advapi32"
Alias "RegEnumKeyExA" (ByVal hkey As Integer, ByVal
dwIndex As Integer, ByVal lpName As String, ByRef lpcbName
As Integer, ByVal lpReserved As Integer, ByVal lpClass As
String, ByRef lpcbClass As Integer, <MarshalAs
(UnmanagedType.Struct)> ByRef lpftLastWriteTime As
FILETIME) As Integer
Private Declare Function SHGetFileInfo Lib "shell32"
Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal
dwFileAttributes As Integer, <MarshalAs
(UnmanagedType.Struct)> ByRef psfi As SHFILEINFO, ByVal
cbFileInfo As Integer, ByVal uFlags As Integer) As Integer
Private Declare Function ImageList_Draw
Lib "comctl32.dll" (ByVal himl As Integer, ByVal i As
Integer, ByVal hDCDest As Integer, ByVal x As Integer,
ByVal y As Integer, ByVal flags As Integer) As Integer



Private Item As System.Windows.Forms.ListViewItem
Private bResize As Boolean
Private iPos As Integer
Private vValue As Object
Private sValue As String
Private sKey As String
Private sImageList1Key As String
<MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH_LENGTH)> Public Subkey As String
<MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=MAX_PATH_LENGTH)> Public KeyClass As String





Public Sub Prueba(ByVal form As Form1)
Dim Index As Integer
Dim TheTime As FILETIME
TheTime.dwHighDateTime = 0
TheTime.dwLowDateTime = 0
Dim Icon As Integer
Dim Icon2 As Integer
Dim Info As SHFILEINFO
Dim FileTypeName As String
Dim FileExtension As String
Subkey = New String(" "c, MAX_PATH_LENGTH)
KeyClass = New String(" "c, MAX_PATH_LENGTH)
Dim g As System.Drawing.Graphics

Do While RegEnumKeyEx(HKEY_CLASSES_ROOT, Index,
Subkey, MAX_PATH_LENGTH, 0, KeyClass, MAX_PATH_LENGTH,
TheTime) = GOOD_RETURN_CODE
If Asc(Subkey) = STARTS_WITH_A_PERIOD Then
Icon2 = SHGetFileInfo(Subkey,
FILE_ATTRIBUTE_NORMAL, Info, Len(Info),
SH_USEFILEATTRIBUTES Or BASIC_SH_FLAGS Or SH_LARGEICON)
Icon = SHGetFileInfo(Subkey,
FILE_ATTRIBUTE_NORMAL, Info, Len(Info),
SH_USEFILEATTRIBUTES Or BASIC_SH_FLAGS Or SH_SMALLICON)
FileTypeName = TrimNull(Info.szTypeName)
FileExtension = TrimNull(Subkey)
FileExtension = Right(FileExtension, Len
(FileExtension) - 1)
form.ListBox1.Items.Add(TrimNull(Subkey)
+ " - " + Info.szTypeName)
1)-> psmall.Picture = LoadPicture()
2)-> Call ImageList_Draw(Icon, Info.iIcon,
psmall.hDC, 0, 0, ILD_TRANSPARENT)
3)-> psmall.Picture = psmall.Image
sImageList1Key = "#" & FileExtension & "#"

If FileExtension = "" Then FileExtension
= "*"

End If
Index = Index + 1

Loop
End Sub
Private Function TrimNull(ByVal startstr As String) As
String
iPos = InStr(startstr, Chr(0))
If iPos > 0 Then
TrimNull = Left$(startstr, iPos - 1)
Exit Function
End If
TrimNull = startstr
End Function

End Module
email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaRespuesta Tengo una respuesta
Search Busqueda sugerida