Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / May 2004

Tip: Looking for answers? Try searching our database.

Color Cursor & .Net

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bytz - 20 Apr 2004 17:09 GMT
I'm trying to load color cursors in .Net (c#); these cursors loaded and
worked fine under MFC.  Do I have to resort to the win32 calls or is there
a way that .Net will support them?
Marc Butenko - 20 Apr 2004 22:33 GMT
Try using the C# equivalent of the following VB.NET code:
Me.Cursor = New Cursor(Me.GetType(), "MyCursor.Cur")

Hope that helps,

Signature

Marc Butenko
mbutenko@bresnan.net

> I'm trying to load color cursors in .Net (c#); these cursors loaded and
> worked fine under MFC.  Do I have to resort to the win32 calls or is there
> a way that .Net will support them?
Marc Butenko - 20 Apr 2004 22:38 GMT
Correction:

To load from a file, it should be:
Me.Cursor = New Cursor("myCursor.cur")

Sorry,
Signature

Marc Butenko
mbutenko@bresnan.net

> Try using the C# equivalent of the following VB.NET code:
> Me.Cursor = New Cursor(Me.GetType(), "MyCursor.Cur")
[quoted text clipped - 4 lines]
> > worked fine under MFC.  Do I have to resort to the win32 calls or is there
> > a way that .Net will support them?
Bytz - 21 Apr 2004 15:35 GMT
Neither the file method nor the embedded resource method works.  Sorry,I
had tried both methods before posting; I should have mentioned that.  I'm
thinking that I'll need to load it using win32 and then create the c#
cursor from the handle, but I'm unsure of how to load the .Net resource
with a Win32 call...

"Marc Butenko" <mbutenko@state.mt.us> wrote in news:##lnM$xJEHA.3380
@TK2MSFTNGP09.phx.gbl:

> Correction:
>
> To load from a file, it should be:
> Me.Cursor = New Cursor("myCursor.cur")
>
> Sorry,
Roger Norris - 15 May 2004 16:32 GMT
> Neither the file method nor the embedded resource method works.  Sorry,I
> had tried both methods before posting; I should have mentioned that.  I'm
> thinking that I'll need to load it using win32 and then create the c#
> cursor from the handle, but I'm unsure of how to load the .Net resource
> with a Win32 call...

Just ran into this problem myself, and then noticed in the Cursor
class documentation: "The Cursor class does not support animated
cursors (.ani files) or cursors with colors other than black and
white."  And after a few more attempts, I found that the Cursor class
also has problems with 16x16 monochrome images.  Finally got it to
work with a 32x32 monochrome image.

That does make me wonder, though, why Visual Studio .NET creates a
color cursor by default when adding a new cursor file :)

If you really want a color cursor, I know a partial solution.

First, we need to import a few Win32 calls:

[DllImport("user32.dll")]
public static extern IntPtr LoadCursorFromFile( string fileName );

[DllImport("user32.dll")]
public static extern IntPtr SetCursor( IntPtr cursorHandle );

[DllImport("user32.dll")]
public static extern uint DestroyCursor( IntPtr cursorHandle );

Loading the cursor is easy:

IntPtr cursorHandle = Win32.LoadCursorFromFile( "MyCursor.cur" );

Setting the cursor is more obnoxious, because it keeps wanting to
reset itself.  I tried catching the WM_SETCURSOR message by adding a
message filter (Application.AddMessageFilter), but that didn't work -
I never received the message.  Odd, because I received tons of other
messages - mouse moving, etc...

Eventually I just added an event handler to Application.Idle:

Application.Idle += new EventHandler( cursorIdle );
...
private void cursorIdle( object sender, System.EventArgs e )
{
   SetCursor( cursorHandle );
}

But this solution isn't perfect, because the cursor still flickers
back to the default pointer in some instances - such as when you click
a mouse button.  If someone has a way around the flicker, I'd love to
hear about it.
Roger Norris - 15 May 2004 16:48 GMT
> Neither the file method nor the embedded resource method works.  Sorry,I
> had tried both methods before posting; I should have mentioned that.  I'm
> thinking that I'll need to load it using win32 and then create the c#
> cursor from the handle, but I'm unsure of how to load the .Net resource
> with a Win32 call...

Ignore my previous message!  I think I found a way to do it... haven't
encountered any bad side effects (yet), and no flicker this time.

Create a black-and-white version of the cursor and load it normally:

Cursor myCursor = new Cursor( "MonochromeCursor.cur" );

Next, use platform invoke (as shown in previous message) to load the
color version:

IntPtr colorCursorHandle = LoadCursorFromFile( "ColorCursor.cur" );

...and then we use reflection to set myCursor's private "handle" field
:)

myCursor.GetType().InvokeMember(
   "handle",
   BindingFlags.Public | BindingFlags.NonPublic |
   BindingFlags.Instance | System.Reflection.BindingFlags.SetField,
   null,
   myCursor,
   new object [] { cursorHandle } );

Finally, set the form's cursor object:

form.Cursor = myCursor;

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.