tags:

views:

1030

answers:

7

Hi,

I have small problem with my .net 2.0 winforms application. I want to embed a few different icons to app.

There are also other requirements:

  • must be automatic rebuildable by ms-build; using external gui apps for preparation isn't allowed
  • application must contain versioninfo.

After embeding multiple icons, I want to register let's say two file associations to application documents / extension files.

[Registry]
...
Root: HKCR; Subkey: "MyFileExt\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\MyApp.exe,2"

where "2" it's icon index.

I know that I need to use somehow old-style Win32 resource file.

I also found somewhere that if using Visual Studio 2005 it's possible to add 'native resource file' but it doesn't exist in 2008 anymore.

Is it possible to meet all this requirements and if yes - how?

+1  A: 

Just like in 2005, you can add the icons to the resources (via the project properties). After you do that, go to the icons you have added in the project explorer each icon has a BuildAction property of how that resource is stored and utilized.

I think what you are looking for is the EmbeddedResource value of the BuildAction property.

Sailing Judo
A: 

New WinForms App -> Expand Properties in the Solution Explorer -> Double click on Resources.resx -> Click Add Resource (The drop down also includes adding existing Resources)

These should be available to you now in the visual designer or in code using Properties.Resources.[Your Resource Name Here]

Hope that helps.

Jonathan DeMarks
A: 

When I need multiple icons I embedded the png/bmp (or whatever image type it is) as a image resource in the application. Go to Project -> Properties -> Resoruces in Visual Studio.

The in code you can do the following at run-time:

buttonPlay.Image = Properties.Resources.Navigation_Media_Pause;

or

buttonPlay.Image = Properties.Resources.Navigation_Media_Play;

Assumning that you called the 2 image resources "Navigation_Media_Pause" and "Navigation_Media_Play".

Matt Warren
A: 

Compile the application, then open the .exe with visual studio, you can then add the all the icons you want.

There is also something you can do with win32 resource files in c# but i cannot find the lik for that at the moment

Pondidum
A: 

I decided to put all icons into separate old-school plain win32 c++ *.dll. It's more convienient and I don't need to do any tricks with versioninfo/win32resources.

tomo
+1  A: 

The solution is actually quite simple, although it required that I think back to my first encounter with RC files...

In a plain text file, you can write the following

#include <windows.h>

// The following is some Win32 candy for
// -- the Windows styles in XP, Vista & 7
//    does the UAC too.
1 RT_MANIFEST "App.manifest"
// -- the versioning info, which we find usually in 
//    AssemblyInfo.cs, but we need to add this one
//    because including Win32 resources overrides the .cs
//    file!
VS_VERSION_INFO VERSIONINFO
FILEVERSION     1,0,0,0
PRODUCTVERSION  1,0,0,0
FILEFLAGSMASK   VS_FFI_FILEFLAGSMASK
FILEFLAGS       VS_FF_DEBUG
FILEOS          VOS__WINDOWS32
FILETYPE        VFT_DLL
FILESUBTYPE     VFT2_UNKNOWN
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "040904E4" // en-US/cp-1252
        BEGIN
            VALUE "CompanyName",      "My Company"
            VALUE "ProductName",      "My C# App"
            VALUE "ProductVersion",   "1.0.0.0"
        END
    END
        BLOCK "VarFileInfo"
        BEGIN
            VALUE "Translation", 0x409, 1252 // en-US in ANSI (cp-1252)
        END
    END
END

// And now the icons.
// Note that the icon with the lowest ID
// Will be used as the icon in the Explorer.
101 ICON "Icon1.ico"
102 ICON "Icon2.ico"
103 ICON "Icon3.ico"

(Details about the VERSIONINFO structure can be found in MSDN: VERSIONINFO structure)

You compile using rc, which should either be part of VS, or in the Windows Platform SDK. The result of the compilation of your .rc file is a .res file which can be included in the properties page of your C# project -- or add the following in the .csproj file itself.

<Win32ResourceFile>C:\path\to\my\resource\file.res</Win32ResourceFile>

Compile your project and look in the explorer, the info and icons should be there.

The CSC compiler also provides a /win32res switch that embeds the .res file into you app.

Hope this helps!

Minustar