Win32 handle that was passed to icon is not valid or is the wrong type.
I was testing articlehttp://support.microsoft.com/kb/319340/ to see if I could use this to populate a listview2 with the icons fromDirectory.GetFiles(apath) and getDirectories. If you go into c:\windows\system32 the program will hang for about 4 mins then if you go back to windows then back to system32 you will get the error.
just-in-time (JIT) debugging instead of this dialog box.
System.InvalidOperationException: BufferedGraphicsContext cannot be disposed of because a buffer operation is currently in progress.
at System.Drawing.BufferedGraphicsContext.Dispose(Boolean disposing)
at System.Drawing.BufferedGraphicsContext.Dispose()
at System.Drawing.BufferedGraphicsContext.AllocBufferInTempManager(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle)
at System.Drawing.BufferedGraphicsContext.AllocBuffer(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle)
at System.Drawing.BufferedGraphicsContext.Allocate(IntPtr targetDC, Rectangle targetRectangle)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Imports
System.Runtime.InteropServicesImports
System.IOPublic
Class Form1PrivateStructure SHFILEINFOPublic hIconAs IntPtr' : iconPublic iIconAsInteger' : icondexPublic dwAttributesAsInteger' : SFGAO_ flags<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayNameAsString<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeNameAsStringEndStructurePrivateDeclareAnsiFunction SHGetFileInfoLib"shell32.dll" (ByVal pszPathAsString, _ByVal dwFileAttributesAsInteger,ByRef psfiAs SHFILEINFO,ByVal cbFileInfoAsInteger, _ByVal uFlagsAsInteger)As IntPtrPrivateConst SHGFI_ICON = &H100PrivateConst SHGFI_SMALLICON = &H1PrivateConst SHGFI_LARGEICON = &H0' Large iconPrivate nIndex = 0Public files()As IO.FileSystemInfoPrivateSub Form1_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Loadlistview2.LargeImageList = ImageList1
listview2.SmallImageList = ImageList1
listview2.Columns.Add(
"File Name", 250, HorizontalAlignment.Center)listview2.Columns.Add(
"Last Access", 75, HorizontalAlignment.Left)listview2.Columns.Add(
"Last Write", 75, HorizontalAlignment.Right)LogicalDrive()
EndSubPublicSub PopulateListView(ByVal apathAsString)TryDim hImgSmallAs IntPtr'The handle to the system image list.Dim hImgLargeAs IntPtr'The handle to the system image list.Dim fnameAsNew ArrayList(Directory.GetDirectories(apath))'The file name to get the icon from.Dim myNameAsString() = Directory.GetFiles(apath)Dim countAs Int32 = 0While count < myName.Lengthfname.Add(myName(count))
count += 1
EndWhileDim shinfoAs SHFILEINFOshinfo =
New SHFILEINFO()listview2.Items.Clear()
listview2.BeginUpdate()
listview2.View = View.Details
shinfo.szDisplayName =
NewString(Chr(0), 260)shinfo.szTypeName =
NewString(Chr(0), 80)'Use this to get the small icon.count = 0
While count < fname.CounthImgSmall = SHGetFileInfo(fname(count), 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON
Or SHGFI_SMALLICON)'Use this to get the large icon.'hImgLarge = SHGetFileInfo(fname(count), 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)'The icon is returned in the hIcon member of the shinfo struct.Dim myIconAs System.Drawing.IconmyIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
ImageList1.Images.Add(myIcon)
'Add icon to imageList.Dim myItemAsNew ListViewItem(fname(count).ToString)If File.Exists(fname(count))ThenmyItem.SubItems.Add(File.GetCreationTime(fname(count)))
myItem.SubItems.Add(File.GetLastAccessTime(fname(count)))
myItem.ImageIndex = nIndex
ElsemyItem.SubItems.Add(Directory.GetCreationTime(fname(count)))
myItem.SubItems.Add(Directory.GetLastAccessTime(fname(count)))
myItem.ImageIndex = nIndex
EndIflistview2.Items.Add(myItem)
'listview.Items.Add(fname(count), nIndex) 'Add file name and icon to listview.nIndex = nIndex + 1
count += 1
EndWhilefname.Clear()
listview2.EndUpdate()
Catch exAs ExceptionMessageBox.Show(ex.Message.ToString)
EndTryEndSubPublicSub LogicalDrive()Dim driveList()AsString = Directory.GetLogicalDrivesDim driveAsString =""cmb.Items.Add(
My.Computer.FileSystem.SpecialDirectories.MyDocuments)ForEach driveIn driveListcmb.Items.Add(drive)
NextDim senderAs System.Object =NothingDim eAs System.EventArgs =Nothingcmb.Text =
"d:\"'My.Computer.FileSystem.SpecialDirectories.MyDocuments'cmb_SelectedIndexChanged(sender, e)EndSubPrivateSub cmb_KeyDown(ByVal senderAsObject,ByVal eAs System.Windows.Forms.KeyEventArgs)Handles cmb.KeyDownTryIf e.KeyCode = Keys.EnterThenPopulateListView(cmb.Text)
EndIfCatch exAs ExceptionEndTryEndSubPrivateSub cmb_SelectedIndexChanged(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles cmb.SelectedIndexChangedPopulateListView(cmb.Text)
EndSubPrivateSub listview2_DoubleClick(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles listview2.DoubleClickDim itemsAsStringDim CountAs Int32 = 0'ListView2.Items.Clear()Dim listFolderItemsAs ListView.SelectedListViewItemCollection =Me.listview2.SelectedItemsDim itemAs ListViewItemForEach itemIn listFolderItemscmb.Text = item.SubItems(0).Text
cmb_SelectedIndexChanged(sender, e)
NextEndSubEnd
Class
