Windows Develop Bookmark and Share   
 index > Windows Forms General > Storing and retrieving cursor icons using an ImageList
 

Storing and retrieving cursor icons using an ImageList

Hello everyone!

Is it possible to store and retrieve cursor images using an ImageList control? (The images stored in the ImageList are bitmaps.)

Since both the cursor and icon formats work for a mouse cursor, I have pulled the bitmaps out of the ImageList control and converted them to the icon format. But that is as far as I have been able to get. Below is my attempt to get this to work.

(Also, I have tried storing the icons in the ImageList as Icons but it looks like you can only pull the icon images out of the list as bitmaps or other non-icon image types).

Private Sub Grid_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Grid.DoubleClick

1.Dim objMStream As New IO.MemoryStream()
2.Dim objIcon As Icon
3.Dim objBitmap As New
Bitmap(ImageList.Images(Grid.Columns("ImageIndex").Value))
4.Dim IP As IntPtr = objBitmap.GetHicon

5.objIcon = Icon.FromHandle(IP)
6.objIcon.Save(objMStream)
7.gobjCursor = New Cursor(objMStream) //gobjCursor is my global cursor
object 
8.DestroyIcon(IP)

End Sub

The app stops running on #7 and produces the following error: Invalid image format. The image file may be corrupt.

THE FUNCTIONALITY I AM TRYING TO ACHIEVE:
The user selects a tickmark from a list of bitmap images i.e. checkmarks, pluses, and other various symbols)poulating a grid. When the user clicks on a row, the mouse changes to the selected image. With the new mouse cursor, the user moves to and clicks on one of any various other controls populating the form (textboxes, comboboxes, datetimepickers and numericupdowns--each control has an invisible picturebox to its right). When the user clicks on one of the four "valid" controls, the picturebox is populated with the cursor's equivalent bitmap image and the cursor returns to the default arrow.

I can't embed cursor files into the app because I need to allow the app's users the ability to add their own custom tickmark bitmaps.

I would appreciate any help! Sorry about hitting the forum so often lately with questions but looking through help files, VB Books and Newsgroups has produced no answers, a weekend at work, and a now annoyed wife.  Good thing Valentines Day is coming up! :)

(I will post the corrected code here, if what I am trying to acheive is possible.)

Thanks -Charles
MigrationUser 1  Sunday, February 09, 2003 11:38 PM
First off, can you manage to load the cursor files at all?  

If so, I'd suggest just using the Tag property on whichever list control you're using and storing the cursor there.  You can then retrieve it when an item is selected.

 - mike
MigrationUser 1  Wednesday, February 12, 2003 6:28 PM
Thanks for the reply Mike!

Actually, I can achieve the functionality I am after but I just can't do it using icons as cursors.  I can easily store cursor files on a SQL Server database and then retrieve these files and store them in an array.  Then, as needed, the current cursor can be set to any of the cursor's stored in the array.

However, I have noticed that if you set your cursor to an icon file (Cursor = New Cursor("C:\...\newcursor.ico")) your cursor will display the icon in color :) --unlike cursor files which only display in black and white :( .  That is why I have been trying to go the route of storing and retrieving icon files rather than cursor files.  But, for some reason, after I save an icon to a memorystream I cannot get the new cursor object to recognize the memorystream as a valid cursor file.

Is it possible that when you set the new cursor by using a filename you can use icons and cursors, but when you set the new cursor by using a stream you can only use cursors?  (You can also set a new cursor by referencing to a handle or type but I am not too familiar with using these options.  Perhaps you can reference an icon using one of these two methods?)

--Chuck
MigrationUser 1  Wednesday, February 12, 2003 9:54 PM

You can use google to search for other answers

Custom Search

More Threads

• Windows forms automatic user signout.
• Custom datagrid control
• Screen resolution setpups within VS2008
• Need windows explorer search string that creates a file search list then excludes certain files from the list.
• Application Icon
• FileNotFoundException When Copy Local set to false
• e.Handled for Mouse Click?
• can't PropertyGrid support multi column?
• Register single and double click
• C#/VS2005 - How can I pass a listbox and it's selected index?