CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    [RESOLVED] How to add a cursor resource to my app (VC++ 2010 Express)

    Well, maybe this would be easy if I had the Pro Edition with the integrated resource editor, but unfortunately I only have the Express Edition and tried to add a cursor resource from a .cur file to my app.

    What I couldn't get to work:
    • Add the .cur file as a file resource to Form1.resX (right click on Form1.resX in the project explorer etc.) and try to construct the Cursor object using the Cursor(Type ^, String ^) constructor as described on MSDN. The resource option as described on the MSDN page for VB .NET and C# doesn't exist for cl. Maybe it could actually be done that way, but then I couldn't figure out how to obtain the correct Type ^ argument for the constructor.
    • Add a reference to the .cur file in the .rc file (by editing it manually), invoking the native LoadCursor() and trying the Cursor(IntPtr) constructor.

    If I don't mess up my memory now, both of these attempts resulted in the Cursor constructor failing and thus gcnew returning nullptr.

    I actually could construct the Cursor object from the .cur file on disk, but as the app consists only of a sole .exe file up to now, I didn't want to add the extra ballast of the .cur file.

    I finally resorted to convert the .cur file to a bunch of hex literals (hidden inside the macro in the code below) and construct the Cursor from a MemoryStream constructed from the array<unsigned char>:

    Code:
    System::Void Form1::btnScreenPicker_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
    {
      array<unsigned char> ^RawData = {PICKER_CURSOR_RAW_DATA};
      static_cast<Button ^>(sender)->Cursor =
        gcnew System::Windows::Forms::Cursor(gcnew IO::MemoryStream(RawData, false));
    }
    It works, but I'm not happy with this solution. Is there really no way to do it that's more elegant/straightforward?

    TIA for any help.
    Last edited by Eri523; October 22nd, 2010 at 07:22 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to add a cursor resource to my app (VC++ 2010 Express)

    Quote Originally Posted by Eri523 View Post
    Is there really no way to do it that's more elegant/straightforward?
    Invest in yourself and purchase the 'pro' version.

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Resolved Re: How to add a cursor resource to my app (VC++ 2010 Express)

    Update

    I finally got that to work, inspired by the MSDN documentation on the ResourceManager class.

    I added the .cur file to the .resX file as a file resource and then I could use this code:

    Code:
    System::Void Form1::btnScreenPicker_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
    {
      ResourceManager ^resm = gcnew ResourceManager(Form1::typeid);
      MemoryStream ^strm = gcnew MemoryStream(static_cast<array<Byte> ^>(resm->GetObject("Picker.cur")), false);
      static_cast<Button ^>(sender)->Cursor = gcnew System::Windows::Forms::Cursor(strm);
    }
    It took some considerable fiddling to get there and still feels like a hack somehow because I couldn't use the Cursor(Type ^, String ^) constructor, but it's still a big relief as it got me rid of that bulky hex array literal.

    Maybe the pro version supports some kind of cursor type resource in the .resX file that the Express Edition doesn't, but having got that far still makes me feel really satisfied.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured