Visual C++ Developer Center > Visual C++ Forums > Visual C++ General > ToolboxBitmapAttribute not working for custom component
Ask a questionAsk a question
 

AnswerToolboxBitmapAttribute not working for custom component

  • Sunday, August 20, 2006 3:27 PMeldiener Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have a custom component written in C++/CLI. I added a bitmap to my project as an embedded resource. I added the ToolboxBitmapAttribute just before my custom control definition in my header file, compiled my assembly, but I do not see my custom control's bitmap in the toolbox item for my custom control.

    Details:
    Namespace of custom control: MyNamespace.
    Name of custom control: MyControl
    Name of bitmap: MyNamespace.MyControl.bmp

    In my project I 'Add New Item', pick Visual C++ | Resource | Assembly Resource file, name the resource MyAssemblyResource, click Add. The resource is added to my project under the folder called Resource Files.  I double-click the resource file, then add a new resource from the existing file 'MyNamespace.MyControl.bmp'. I save my changes to 'MyAssemblyResource.resx'. I open the properties for the project, go to the Linker | Input tab, set the Configuration to All Configurations, add the 'MyAssemblyResource.resx' to the 'Embed Managed Resource File line.

    In the header file for my custom control I added the line before the custom component:
    [System::Drawing::ToolboxBitmapAttribute(MyNamespace::MyControl::typeid)]

    I saved the file, built my custom component assembly, and added it to the Toolbox. When I opened a form and went to the toolbox to add the custom component to the form, the image shows the default sprocket image for the component, not the toolbox bitmap I embedded in my assembly.

    Any ideas why this is not working in a C++/CLI project would be appreciated.




Answers

All Replies

  • Sunday, August 20, 2006 8:29 PMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    My answer in this thread might help...
  • Sunday, August 20, 2006 10:29 PMeldiener Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    There is a difference between a C++/CLI Project and a C# project. In the C++/CLI project there is no "Build Action" to "Embedded Action" in a bitmap added to a .resx file, and there is no such thing as a "Default Namespace" for the assembly.

    For the latter, I assume that the default namespace is just the name of the assembly file in C++/CLI. That is the same namespace as my control, so that did not change.

    For the former I am not even sure how to add an embedded resource in a C++/CLI project. In my case I added a new .resx file, and added my bitmap to that file. Afterwards I added the .resx file name to my "Embed Managed Resource File" in the Linker | Input. Something tells me that the name of the actual embedded resource bitmap is changed somehow from the 'namespace.control.bmp' with which I added my bitmap to my resource file. But I asked this question so I would not have to spend hours guessing at the name, and hoping if someone knows what it is.

    I will try the resfinder trick and see if that will work, but I would still actually like to know why a normal addition of my embedded resource does not work. I don't mind hacks but this should work without a hack.
  • Sunday, August 20, 2006 11:00 PMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Have you considered taking up C# programming as an alternative to your problems with C++?  The C++ IDE and the C++/CLI syntax was not nearly designed for "ease of use". They were designed to allow programmers who had previous projects in C++ to move up to .NET.  It has many short-comings, most of all (I guess) because bolting C++ on top of .NET is just not that easy to do.  And the C++ team probably spent too much time converting their compiler to parse the C++/CLI syntax instead of the crummy Managed C++ syntax.  It was worth it. 

    Meanwhile, other thingies got a lower priority.  Lack of support for the .NET style of embedding resources is a very obvious one.  The C++ IDE only support classic Windows resources.  Because that was the way it was done and that's all the old-timers needed...
  • Sunday, August 20, 2006 11:39 PMeldiener Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Your answer on a C++/CLI forum makes no sense for two reasons:

    1) C++/CLI programmers know that C++ is far superior to C#, and they are not going to waste their time arguing about it no matter what anyone believes. There is no reason to give up C++/CLI but there is a reason to ask how things work in C++/CLI projects as opposed to C#. If your answer to people is to give up C++/CLI for C#, may I suggest that you do not answer anything on this forum, as you are not helping C++/CLI programmers.

    2) I can obvously embed managed resources in C++/CLI, as I demonstrated, and it is not broken for whatever reason you like to believe. My question concerned why this embedded resource is not being found by ToolboxBitmapAttribute in my C++/CLI project. If you do not know, or can not help with suggestions, there is no reason to answer. I do appreciate your pointing me to a way to do this, as hacky as it seems, and will try it out, but I still want to understand what is going on with embedded resources and C++/CLI and how I can determine why or why not ToolboxBitmapAttribute finds its bitmap.
  • Monday, August 21, 2006 1:37 AMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Judging from the responses I get from my posts on this forum, your assertion that I shouldn't post here is not quite main-stream yet.  Let's move on, I'll ignore your questions and you'll ignore my posts.  Problem solved.  If it really bugs you, you can always complain at the Reporting Forum, "Reporting Abuse" link at the lower right corner of the page.
    •  
  • Wednesday, April 11, 2007 6:31 AMcmroanirgo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I too have stumbled at this problem. Generating a toolbox bitmap for C++ seems to be highly problematic, whereas the C# version is a snap.

    nobugz,
    considering that you label yourself a MVP, then you should be careful in evangelizing a particular language simply because that's all you know. Your comments regarding 'taking up C# programming' are naive in the extreme. eldeiner's responses have been nothing but professional.

    eldeiner has outlined a particular issue which is easily reproducible if anyone at MS decided to test it out. I also recently posted the same question (in much less detail) here. I have not had any valid responses either Sad

  • Thursday, April 12, 2007 1:43 AMcmroanirgo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have found a solution and posted it at http://syd.cognethos.com/blog/?p=26
    In a nutshell, to 'Embed' your image, go to "Project Properties -> Linker -> Input -> Embed Managed Resource File" and add your image to that.


  • Thursday, April 12, 2007 1:42 PMeldiener Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     cmroanirgo wrote:
    I have found a solution and posted it at http://syd.cognethos.com/blog/?p=26
    In a nutshell, to 'Embed' your image, go to "Project Properties -> Linker -> Input -> Embed Managed Resource File" and add your image to that.




    I had figure this out previously and apologize for not adding it to this thread myself and saving you and others time. Also see my added comment to your solution on the web page above.