Multiple Icons into c# 2.0 WinApp - c#

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?

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!

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.

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.

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".

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

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.

Related

Localizing Xamarin Forms

I am trying to localize a Xamarin Forms app. I followed all the steps included here: https://developer.xamarin.com/guides/xamarin-forms/localization/
However, in the Android Emulator, after changing the default language, the strings do not get displayed, they remain the default language. On the device the app doesn't even launch.
Steps taken so far:
1) tried breakpoint (BP) in the main launcher, it skipped the BP and entered the main page
2) tried BP on the Localize class, it doesn't reach it
3) tried BP on the Device.OS check, skips it.
The resource files in Visual Studio are the following:
AppResources.resx
AppResources.fr.resx
AppResources.es.resx
Anyone ever succeeded localizing a Xamarin Forms app before ? I need some assistance with this as Visual Studio does not raise any exception ...
UPDATE 1 :
The resource files in Visual Studio are the following:
AppResources.resx
AppResources.fr_FR.resx
AppResources.es_ES.resx
UPDATE 2:
After recent suggestions and modifications I have the following exception: Markup extension not found.
This is disturbing as the markup extension class is there.
Doesn't work in DEBUG mode (Android only)
If the translated strings are working in your RELEASE Android builds
but not while debugging, right-click on the Android Project and select
Options > Build > Android Build and ensure that the Fast assembly
deployment is NOT ticked. This option causes problems with loading
resources and should not be used if you are testing localized apps.
SOLVED:
The problem was in the declaration of the extension. In a PCL project, the following took place:
a) the TranslateExtension class is located in a subfolder named Pages
b) initial declaration was
xmlns:languages="clr-namespace:MyApp;assembly=MyApp"
c) the following declaration solved the problem:
xmlns:languages="clr-namespace:MyApp.Pages;assembly=MyApp"
along with language code naming conventions of the .resx files as mentioned above
2021 UPDATE
All the current answers are not valid anymore! You need to add resources. Right click your xamarin forms project (Not xamarin android or ios) => Go to project properties ==> Resources ==> Add Resource.
This will create a "Properties" Folder and a resources.resx file which you can delete if you want to.
Then follow the official microsoft documentation by creating a AppResources.resx and other language files (such as AppResources.ar.resx file for arabic). All these files MUST BE in the properties folder.
Then, in your XAML page, declare the namespace as:
xmlns:res="clr-namespace:YourProject.Properties"
Then simply use:
<Label Text="{x:Static res:AppResources.Address}" >
This will use the corresponding Language files depending on what language the device is using. If the language is not supported it will use the defulat file (AppResources.res)
FIUH!!! Wasted one hour to solve this!!!

Using embedded resource icon as app icon in C#

I'm trying to figure a way to specify my C# application to use an embedded resource (icon) as my application icon (the one displayed for taskbar, task switch, etc).
Yet, as I already embedded my icon in a resource, I can manage to use it in my forms but not with my application yet.
In fact, if someone could first tell me where I'll be able to set this (which file) by hand instead of using the VS2010 GUI to specify it, that'd be a great start!
Yet, my icon is available in these 2 ways :
{{Namespace}}.Properties.Resources.c_name.ico and in {{Namespace}}.Refs.c_name.ico (as a property of a public static class inside a class library).
Windows is an unmanaged operating system that doesn't know beans about managed resources. The only icon it can display is one that's embedded as an unmanaged resource. You can see what unmanaged resources look like in Visual Studio. Use File + Open + File and pick a .NET exe file (won't work in Express). A .NET program should have three of them, an icon, a manifest that declares the program compatible with UAC and a Version resource. This is all done automatically by the compiler, note for example how the Version resource properties match the attributes you set in AssemblyInfo.cs
The IDE makes it simple to set the icon for a program, you use Project + Properties, Application tab, Icon setting. Just pick the same .ico file as you embedded as a managed resource. Forget about trying to share, it cannot work by design.
Simply set
Icon=yourNamespace.Properties.Resources.yourEmbeddedIconName;
System.Drawing.Icon.FromHandle(Properties.Resources.EliminarNota.GetHicon());

Using the same icon for .exe and Form

In my WinForms app, if I set an icon for my app and an icon for my Form, the icon appears twice in my executable. Is it possible to avoid this?
(This question has been asked before, here, but the answers didn't seem to solve the problem. vanmelle's answer appears to extract only one icon (e.g., 16x16), Sunlight's answer extracts only the 32x32, and lc's answer doesn't solve the problem: there is still a duplicated icon in the executable.)
If it's not possible to accomplish this task, why is this? What is it about using the same icon for an executable and a Form that's so hard in WinForms?
This is an inevitable consequence of running managed code on a completely unmanaged operating system. Windows Explorer only knows how to read unmanaged resources. You can see what they look like, use File + Open + File in Visual Studio and select your .exe. You'll typically see three resource groups listed there:
RT_MANIFEST, contains the manifest that tells Windows that your program is Vista aware
Version, contains the file version resource with values derived from your AssemblyInfo.cs file. You see its content when you use Properties + Details tab in Explorer. Note how the super important [AssemblyVersion] isn't visible in Vista and up
Icon, contains the icon you added.
This unmanaged resource data is separate from the managed resources you added. Managed resources are compiled into the assembly manifest. Unmanaged resources are stored in the .rsrc section of the image file. You can override the auto-generated version with the /win32res command line option. Which requires a .res file, a binary file that is generated from a .rc resource script by the rc.exe resource compiler. An age old Windows SDK tool.
This may change some day, the super-secret Midori project is a rumored to focus on a managed operating system. For now, we'll have to make do with the glue.

Multiple icons in executable in VS2010

I have Googled and found multiple ways of adding multiple icons into the executable, but they all seem to work for VS 2003-2005-2008, nothing for VS2010. I have not tried the Win32 resource with /win32res because I do not know how to use it (can't figure to get a good Google result for that either).
Any simple suggestion?
I've just created a simple tool to do exactly this without having to mess with .res files. It's a tiny utility which you can use as part of your Post-Build event and lets you add all icons files in a particular folder to your assembly. If we assume that you have a icons folder under your main project folder you can add the following post-build event:
C:\path\to\InsertIcons.exe $(TargetPath) $(ProjectDir)icons
A further description and a download can be found at http://einaregilsson.com/add-multiple-icons-to-a-dotnet-application/
This works for me:
http://www.codeproject.com/Tips/160885/How-to-Embed-Multiple-Icons-and-Color-Animated-Cur.aspx
If you're using visual studio 2012:
For C#.NET Here I found a good solution for this problem for c# projects as an example. But it only works in my C# projects
Create a new "Native Resource Template" from the File | New dialog box.
In project properties(project->application->resources) there is option to choose resource file (.res) rather than "Icon and manifest" which is selected by default (This option is visible only to C# projects!).
For VB.Net projects this link (Also mentioned here by Waldo) can be more helpful because in my visual 2012 there is no option to select/browse Native Resource Template(.res) files but you could manually change project definition file for vb.net project as described to compile project win a native win32 resource file:
Open your project file in notepad (*.vbProj) and add the following block:
<PropertyGroup>
<Win32Resource>assemblyWin32.res</Win32Resource>
</PropertyGroup>
The Code Project article explains how to create a "assemblyWin32.res" file.
https://www.codeproject.com/Tips/160885/%2fTips%2f160885%2fHow-to-Embed-Multiple-Icons-and-Color-Animated-Cur

How to Use Scintilla .NET in C# Project?

I am attempting to use Scintilla .NET in a project (I want a good editor + syntax highlighting, etc). Unfortunately, when I reference the binaries in my project, I can't seem to actually use the Scintilla controls. I get two different errors.
The first happens when adding
SciLexer.dll as a reference to my
project. I get a message that says:
A reference to SciLexer.dll could
not be added. Please make sure the
file is accessible, and that it is a
valid assembly or COM component.
The second error occurs when I try to use the controls from ScintillaNET.dll. If I try to drag the component into a form, I get the following message: Failed to create component Scintilla. The error message follows: System.ComponentModel.Win32Exception: %1 is not a valid Win32 application at ScintillaNet.Scintilla.get_CreateParams()
Any help with either of these problems would be much appreciated. For the record, I am running Visual Studio 2008 running on a 64-bit Windows 7 platform.
Thanks.
It's been a little while since I used Scintilla, but my understanding is that SciLexer.dll is an entirely native DLL, which is why you can't add a reference to it from Visual Studio.
Instead, you should 'arrange' for SciLexer.dll to appear in the right directory at runtime so that it can be loaded by ScintillaNET.dll. The safest way to do this would be to add SciLexer.dll to your Visual Studio project as a plain file (not as a reference), then change the properties on the file to have it copied to the output directory when your project is built.
Given that you're on 64-bit, I expect that you'll need to build your app specifically as 32-bit and not AnyCPU. As I say, it's been a while since I did this, but when I did, Scintilla only had 32-bit binaries.
I got round the issue by copying the files to C:\Windows\SysWOW64
Got the idea from #weston's post.
I use ScintillaNET (C# port). It is easy to use, but it is a memory hog.
https://github.com/jacobslusser/ScintillaNET
Click on RUN and select MyComputer Right click on MyComputer Go to Properties then
Click on Advanced System Settings .
After that select Advanced and Click on Environment Variables.
Here we can add the value and path.
Value : what ever you like " Eg : Scintilla
Value path : where the downloaded dll kept ." EG: D:\prathap\DLL\
Download Path :https://scintillanet.codeplex.com/releases
Select the downloads from right hand side on the screen.
Then in the application add the Scintill Dll as a reference and other 2 dll as existing files.
Right click on project ADD -- Addexisting files -- then select the files from local system and make sure that you keep the 2 files "Copy to Output directory = Copy Always.
Then run the application it will work.
i added reference to Scintilla.dll instead of SciLexer.dll - and it worked

Categories