mc++ icons/resources
-
Hi, I am writing a program in MC++ (using VS .NET -- thus this post is very VS .NET specific) and am having a problem getting my main form's Icon to display properly... My application is crashing, based on the follow process: 1 - I made a custom icon for the form (myIcon.ico) 2 - Right Click on the form (in the [design]), click "Properties", then click the "..." button of the "Window Style"->Icon field... 3 - choose "myIcon.ico" from the file chooser 4 - VS .NET then produces the following relevant code (which executes inside the form's constructor):
void InitializeComponent(void) { System::Resources::ResourceManager *resources = new System::Resources::ResourceManager(__typeof(myNameSpace::Form1)); ... // // Form1 // ... this->Icon = (__try_cast< System::Drawing::Icon * >(resources->GetObject(S"$this.Icon"))); ...
If I run the program in debug mode, it will crash when attempting thethis->Icon = ...
line... If I insert the following code after InitializeComponents() executes, it works fine.this->set_Icon(new System::Drawing::Icon(new String("app.ico") ) )
However, this now has the side-effect of requiring the myIcon.ico file to be in the same directory as the executable... As I need to release this code to customers, I don't want to trust that they won't accidentally delete the icon file... Thus, I would like icon "compiled into" the executable... So my questions are: 1) Will it get "compiled into" the executable assuming the generated code ever works correctly...??? 2) Why is it crashing...??? 3) Does it have anything to do with making the icon into a resource (.rc) file...??? 4) Anyone know how I can fix this issue...??? TIA, istrasci -
Hi, I am writing a program in MC++ (using VS .NET -- thus this post is very VS .NET specific) and am having a problem getting my main form's Icon to display properly... My application is crashing, based on the follow process: 1 - I made a custom icon for the form (myIcon.ico) 2 - Right Click on the form (in the [design]), click "Properties", then click the "..." button of the "Window Style"->Icon field... 3 - choose "myIcon.ico" from the file chooser 4 - VS .NET then produces the following relevant code (which executes inside the form's constructor):
void InitializeComponent(void) { System::Resources::ResourceManager *resources = new System::Resources::ResourceManager(__typeof(myNameSpace::Form1)); ... // // Form1 // ... this->Icon = (__try_cast< System::Drawing::Icon * >(resources->GetObject(S"$this.Icon"))); ...
If I run the program in debug mode, it will crash when attempting thethis->Icon = ...
line... If I insert the following code after InitializeComponents() executes, it works fine.this->set_Icon(new System::Drawing::Icon(new String("app.ico") ) )
However, this now has the side-effect of requiring the myIcon.ico file to be in the same directory as the executable... As I need to release this code to customers, I don't want to trust that they won't accidentally delete the icon file... Thus, I would like icon "compiled into" the executable... So my questions are: 1) Will it get "compiled into" the executable assuming the generated code ever works correctly...??? 2) Why is it crashing...??? 3) Does it have anything to do with making the icon into a resource (.rc) file...??? 4) Anyone know how I can fix this issue...??? TIA, istrasciI too has similar problems tring to embed image resources into a .NET assembly. I have the standard 2003 version of Visual C++ .NET, and in the documentation they tell you put the Icon or image file into the same directory as the executable. This works, but, as you have noted, is prone to user error. The Chris Sells book: "Windows Forms Programming in C#", he describes in great detail several methods for embedding image resources into the binary. These methods do not work in Visual C++ .NET. I did, however, find this information from a sample chapter of a book by Richard Grimes, "Developing Applications with Visual Studio.NET". 8.3.4 Managed C++ and Resources Managed C++ projects allow you to add resources through the Solution Explorer or Class View window, but these will be Win32 resources. If you want to add your own .NET resources, you need to edit the project settings. Here are the steps: First you need to add an XML file to your project. To do this you should use the Add New Item dialog of Solution Explorer, and ensure that the extension of the file is .resx (the resgen utility insists that XML resource files have this extension). If you forget to give the file this extension, you will have to remove the file from the project, rename it using Windows Explorer, and add the renamed file to the project with Add Existing Item from the C++ Solution Explorer context menu. The reason is that the C++ Solution Explorer (unlike the C# Solution Explorer) does not allow you to rename a file that has been added to a project. Once you have added the .resx file to the project, you should add the bare minimum of resource file contents: the node and the three nodes I mentioned earlier: ResMimeType, Reader, and Writer. After that it makes sense to add at least one node (essentially as a template), and then you can edit the resource file using the XML designer. The next task is to add the .resx file to the build. To do this you should select properties of this file from the Solution Explorer context menu by selecting General Configuration Properties and making sure that the Tool property option selected is Custom Build Tool. You can then set the tools command line through the Custom Build Step option (Table 8.2). Table 8.2 Custom Build Step properties for an .resx file Property Value Command Line resgen $(InputFileName) $(OutDir)\$(InputName).resources Description Building .NET resources Outputs $(OutDir)\$(InputName).resources Choosing Custom Build Step will a