VB6 to VB.NET

  • Thread starter Thread starter yxq
  • Start date Start date
Y

yxq

Hello
I will run the program in Window XP.

The VB6 code

1. moudle1 code
******************************************
Public Declare Function PickIconDlg Lib "shell32.dll" Alias "#62" (ByVal
hwndOwner As Long, _
ByVal lpstrFile As String, ByVal nMaxFile As Long, lpdwIconIndex As
Long) As Long

Public Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA"
(ByVal hInst _
As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As
Long
Public Declare Function DrawIcon Lib "user32.dll" (ByVal hDC As Long, ByVal
x As Long, _
ByVal y As Long, ByVal hIcon As Long) As Long
Public Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Long)
As Long
Public Declare Function GetSystemDirectory Lib "kernel32.dll" Alias
"GetSystemDirectoryA" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long
*******************************************

2. Form1 code
*******************************************
' When the user presses button Command1, display an icon selection dialog
box. If the
' user chooses an icon, then draw it in the corner of window Form1's client
area.
Private Sub Command1_Click()
Dim iconfile As String ' file that contains the desired icon
Dim iconindex As Long ' index of the desired icon
Dim slength As Long ' length of returned string
Dim hIcon As Long ' handle to the icon once it is extracted
Dim retval As Long ' return value

' Figure out where the System directory is.
iconfile = Space(256)
slength = GetSystemDirectory(iconfile, Len(iconfile))
iconfile = Left(iconfile, slength)

' Have the default selection be the third icon in pifmgr.dll. Include
plenty
' of extra space in the string so it can receive the user's selection.
iconfile = iconfile & "\shell32.dll" & vbNullChar & Space(256)
iconindex = 2

' Display the icon selection dialog. If Windows NT or 2000 is running,
convert
' the string to and from Unicode immediately before and after calling
the function.

iconfile = StrConv(iconfile, vbUnicode)

retval = PickIconDlg(Form1.hWnd, iconfile, Len(iconfile), iconindex)

iconfile = StrConv(iconfile, vbFromUnicode)

' Remove the terminating null and empty space from the string.
iconfile = Left(iconfile, InStr(iconfile, vbNullChar) - 1)

' If the user selected something, draw the icon on Form1.
If retval <> 0 Then
' Extract the icon from the file.
hIcon = ExtractIcon(App.hInstance, iconfile, iconindex)
' Draw it in the corner of the window.
retval = DrawIcon(Form1.hDC, 0, 0, hIcon)
' Destroy the icon to free resources.
retval = DestroyIcon(hIcon)
End If
End Sub
****************************************

I use VB.NET auto upgrade wizard to convert the code

1.Moudle1
****************************************
Public Declare Function PickIconDlg Lib "shell32.dll" Alias "#62"(ByVal
hwndOwner As Integer, ByVal lpstrFile As String, ByVal nMaxFile As Integer,
ByRef lpdwIconIndex As Integer) As Integer

Public Declare Function ExtractIcon Lib "shell32.dll" Alias
"ExtractIconA"(ByVal hInst As Integer, ByVal lpszExeFileName As String,
ByVal nIconIndex As Integer) As Integer
Public Declare Function DrawIcon Lib "user32.dll" (ByVal hDC As Integer,
ByVal x As Integer, ByVal y As Integer, ByVal hIcon As Integer) As Integer
Public Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As
Integer) As Integer
Public Declare Function GetSystemDirectory Lib "kernel32.dll" Alias
"GetSystemDirectoryA"(ByVal lpBuffer As String, ByVal nSize As Integer) As
Integer

2.Form1 code
*****************************************
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal
eventArgs As System.EventArgs) Handles Command1.Click
Dim iconfile As String ' file that contains the desired icon
Dim iconindex As Integer ' index of the desired icon
Dim slength As Integer ' length of returned string
Dim hIcon As Integer ' handle to the icon once it is extracted
Dim retval As Integer ' return value

iconfile = Space(256)
slength = GetSystemDirectory(iconfile, Len(iconfile))
iconfile = VB.Left(iconfile, slength)

iconfile = iconfile & "\shell32.dll" & vbNullChar & Space(256)
iconindex = 2

iconfile = StrConv(iconfile, vbUnicode) 'vbUnicode error

retval = PickIconDlg(Form1.DefInstance.Handle.ToInt32, iconfile,
Len(iconfile), iconindex)

iconfile = StrConv(iconfile, vbFromUnicode) 'vbFromUnicode error

iconfile = VB.Left(iconfile, InStr(iconfile, vbNullChar) - 1)

' If the user selected something, draw the icon on Form1.
If retval <> 0 Then

hIcon = ExtractIcon(VB6.GetHInstance.ToInt32, iconfile, iconindex)

retval = DrawIcon(Form1.DefInstance.hDC, 0, 0, hIcon)
'Form1.DefInstance.hDC error

retval = DestroyIcon(hIcon)
End If
End Sub
******************************************

But there are 3 error.

Can anyone help me?

Thanks
 
yxq said:
I will run the program in Window XP.

[...}

But there are 3 error.

Can anyone help me?

Maybe - if you could tell us which errors occur where?
 
Ok, I added the code to a VB6 project now and ran the Wizard in VB.NET.
Right, there are three errors, but there are also hints starting with
"UPGRADE_ISSUE". Did you read the hints?

Now, concerning vbUnicode/vbFromUnicode: All Strings in (VB).NET are Unicode
strings.
The argument lpstrFile for PickIconDlg is an in and _out_ argument. This
means you have to pass a stringbuilder instead because strings are
"immutable". After calling the function, get the returned file by using the
Stringbuilder's ToString method.

To convert the hDC issue, you must use a graphics object instead.


Also, replace
iconfile = Space(256)
slength = GetSystemDirectory(iconfile, Len(iconfile))
iconfile = VB.Left(iconfile, slength)
by
iconfile = Environment.GetFolderPath( _
Environment.SpecialFolder.System _
)


You must also declare PickIconDlg as an "Auto" or "Unicode" function because
the string pointed to by the lpstrfile argument must be unicode. Also
declare hwndOwner as Intptr. The same with the first argument for
ExtractIcon.


So, the resulting code is:
Module Module1

Public Declare Unicode Function PickIconDlg _
Lib "shell32.dll" Alias "#62" ( _
ByVal hwndOwner As IntPtr, _
ByVal lpstrFile As System.Text.StringBuilder, _
ByVal nMaxFile As Integer, _
ByRef lpdwIconIndex As Integer) _
As Integer

Public Declare Function ExtractIcon _
Lib "shell32.dll" Alias "ExtractIconA" ( _
ByVal hInst As IntPtr, _
ByVal lpszExeFileName As String, _
ByVal nIconIndex As Integer) _
As Integer

Public Declare Function DestroyIcon _
Lib "user32.dll" (ByVal hIcon As Integer) _
As Integer
End Module


'Form:
Private Sub Command1_Click( _
ByVal eventSender As System.Object, _
ByVal eventArgs As System.EventArgs) _
Handles Command1.Click

Dim iconfile As String ' file that contains the desired icon
Dim iconindex As Integer ' index of the desired icon
Dim slength As Integer ' length of returned string
Dim retval As Integer ' return value
Dim sb As System.Text.StringBuilder

iconfile = Environment.GetFolderPath( _
Environment.SpecialFolder.System _
)
iconfile = iconfile & "\shell32.dll"
iconindex = 2

sb = New System.Text.StringBuilder(iconfile, 500)
retval = PickIconDlg( _
Form1.DefInstance.Handle, sb, sb.Length, iconindex _
)
iconfile = sb.ToString

If retval <> 0 Then
Dim g As Graphics
Dim ico As Icon
Dim hIcon As IntPtr ' handle to the icon once it is extracted
hIcon = New IntPtr( _
ExtractIcon(VB6.GetHInstance, iconfile, iconindex) _
)
ico = Icon.FromHandle(New IntPtr(hIcon))
g = Form1.DefInstance.CreateGraphics
g.DrawIcon(ico, 0, 0)
g.Dispose()
ico.Dispose()
retval = DestroyIcon(hIcon)
End If
End Sub
 
Me again...

sorry, the previous code does not work. I shouldn't make changes in OE. ;-(

Change the return type of ExtractIcon to IntPtr:

Public Declare Function ExtractIcon _
Lib "shell32.dll" Alias "ExtractIconA" ( _
ByVal hInst As IntPtr, _
ByVal lpszExeFileName As String, _
ByVal nIconIndex As Integer) _
As IntPtr

Also change the call to PickIconDlg:

retval = PickIconDlg( _
Form1.DefInstance.Handle, sb, sb.Capacity, iconindex _
)


Also:
If retval <> 0 Then
Dim g As Graphics
Dim ico As Icon
Dim hIcon As IntPtr ' handle to the icon once it is extracted
hIcon = ExtractIcon(VB6.GetHInstance, iconfile, iconindex)
ico = Icon.FromHandle(hIcon)
g = Form1.DefInstance.CreateGraphics
g.DrawIcon(ico, 0, 0)
g.Dispose()
retval = DestroyIcon(hIcon)
End If
(no need to call ico.Dispose when created by Icon.FromHandle. Only
DestroyIcon must be called)


Sorry again.
 
Back
Top