I have a C# project. I choose the .ico file for it in property so it automatically added the .ico into the list from the "solution explorer".
I am using a "notifyIcon", and want to change the icon programmatically.
example a notifyIcon red when program is busy and green when free.
So i know i have to add a new embed resource for the second ico. but how to access to the already existing one that is the application ico?
i would like "something like"
notifyIcon1.Icon = AppName.greenico.ico; //default app ico
notifyIcon1.Icon = AppName.redico.ico; //ico ill add as embed resource i guess
is that possible? i saw some strange ExtractIco thingy... But i am sure its possible to reference straight to something already embed ain't it?
Found a really really easy solution.
First: Change "Build" properties of the two icons in "Solution Explorer" to "Embedded Ressource".
now in your code just set the two icons as variables:
public Icon greenIco = new Icon(typeof(MainFormName), "GreenIco.ico");
public Icon redIco = new Icon(typeof(MainFormName), "RedIco.ico");
then to use them, easy so:
notifyIcon1.Icon = greenIco;
as simple as that.
Hope it'll help someone else a day.
Related
As far as I know "Cursor" don't exits in UWP.
I can change cursor with this code:
Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Arrow, 0);
How do I create a custom cursor with CoreCursorType.Custom?
CoreCursorType.Custom
You should create your own cursor *.res resource file and contains the custom cursor to your project by add it to your package.manifest file. After that, you can use CoreCursorType.Custom enumeration option, and specify the Id of the custom cursor to use the custom cursor. There also have a thread in which #Azat Tazayan has introduced the detailed steps to implement it and you can refer to it:
https://social.msdn.microsoft.com/Forums/en-US/14001796-bcd5-4b9d-9d7e-13bc6ba59d2d/uwp-how-to-set-a-pointer-cursor-as-a-circle-instead-of-a-window-default-cursor?forum=wpdevelop
Here is the answer
https://learn.microsoft.com/en-us/uwp/api/windows.ui.core.corecursor?view=winrt-20348
To use a custom cursor, use the CoreCursorType.Custom enumeration
option, and specify the Id of the custom cursor. You can add a .res
resource file that contains the custom cursor to your project and
include it in your assembly with the /win32res compiler option. The Id
is the Cursor Resource ID specified in the .res file.
Personally I have used this working manual
https://social.msdn.microsoft.com/Forums/en-US/14001796-bcd5-4b9d-9d7e-13bc6ba59d2d/uwp-how-to-set-a-pointer-cursor-as-a-circle-instead-of-a-window-default-cursor?forum=wpdevelop
We need just proper .res file
For that we should do following
Create any C++ project. For example Dll, Add new C++ Dll project name it for example as ResourcesComponent.
Add a resource file to the project. [right-mouse] the project name "ResourcesComponent" => Add New Item => Visual C++ => Resource File (.rc)
Name the resource file for example Resources.rc
Add a cursor resource. Double click on Resources.rc the it will open in Resource View tab then Right click on "Resources.rc" => Add Resource => Cursor => New (Here you can modify cursor appearance by pen)
Go back to solution explorer and now you will see resource.h file where you can find its id
#define IDC_CURSOR1 101
Build dll in Release mode
In Release sub folder you can find Resource.res file
Now we have proper .res file, We should add it to our UWP project root folder.
Copy Resource.res file to UWP project root directory. (You can delete DLL project as it is no longer needed )
Unload UWP project in VS.
Rigth click on unloaded project and select edit .csproj
in first or second PropertyGroup (where is Appname, TargetPlatform, etc ) add
<PropertyGroup>
.....
<Win32Resource>Resource.res</Win32Resource>
</ PropertyGroup>
Reload project
Use following code for set cursor in your image PointerEnter event handler
Window.Current.CoreWindow.PointerCursor =
new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Custom, 101);
If you need to create a colorized *.cur file use any free tools like I did here. And just replace the source *.cur with a new one.
And if you need a small *.cur file just remember that by default its size is 32 x 32 pixels and you have draw your small image like I did.
Enjoy!
I want to add my icon as tay icon.
But this error is shown.
xaml.cs:
private System.Windows.Forms.NotifyIcon _notifyIcon;
This icon already added on resources but not working.
When I writing codes then resources file name not be shown.
See:
How can I solve this problem?
If you want to use it that way - don't add the image to the .resx file. Right click on the project -> Properties -> Resources -> Add Resource -> Add existing file...
Add the image there. If you do this you should be able to get it in the code through Properties.Resources.MyIcon
You can use Stream
Stream iconStream = Application.GetResourceStream(
new Uri( "pack://application:,,,/YourReferencedAssembly;component/YourPossibleSubFolder/YourResourceFile.ico" )).Stream;
notifyIcon.Icon = new System.Drawing.Icon(iconStream);
You just need to have an existing file, open the context menu on your folder , and then choose Add => Existing item...like this,name the folder Images
You need to change your image's property "Build Action" to Content and change the Copy to "If Newer" or "Always"
It will create a folder in bin\debug every time you build your application.
Then give your path like this #"Images\"
This question already has answers here:
Change WPF window background image in C# code
(7 answers)
Closed 7 years ago.
I'd like to change the background image of my WPF application by a button. I know how to do it using WindowsForms, but in WPF I failed.
I found a solution already, but that solution would copy my background images to the output folder next to the Application.exe
This is not really a solution that I desire. I would like to have the images stored inside my application.
Can somebody explain me detailed what I need to do [how to add the images to the program, especially the resource-properties, how to access them in C#....]. It seems like I am too stupid to set it up correctly :P
Thanks in advance
Firstly, add a Folder to your Solution (Right click -> Add -> Folder), name it something like "Resources" or something useful.
Then, simply add your desired image to the folder (Right click on folder -> Add -> Existing item).
Once it's been added, if you click on the image and open the Properties window (Alt+Enter), you'll see the Build Action is set to Resource and the Copy to Output Directory is set to Do not copy.
You can reference the image in C# using the following code:
this.Background = new BitmapImage(new Uri(#"pack://application:,,,/YourApp;component/YourFolder/YourImage.png"));
Or in XAML:
<Image Source="pack://application:,,,/YourApp;component/YourFolder/YourImage.png" ... />
Or:
<Image Source="/YourApp;component/YourFolder/YourImage.png" ... />
Try this:
this.Background = new ImageBrush(new BitmapImage(new Uri(#"pack://application:,,,/Yourapp;component/yourimage.png")));
I'm localizing the app I've built, and I have a problem here.
I added all the strings I use in my xaml in the AppResources.resx file, and I have replaced all the hard-coded strings in the xaml with a binding to the right strings in the .resx file, like this:
Text="{Binding Path=LocalizedResources.StringName, Source={StaticResource LocalizedStrings}}"
The problem is that I need to localize a couple of App Bar buttons and a few Messabe Box, so I need a way to get those strings via C#.
I read a couple of tutorials and I came up with this, as for the localized App Bar button:
ApplicationBar = new ApplicationBar();
ResourceManager rm = new ResourceManager("AppResources", typeof(CreditiInfo).Assembly);
ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/back.png", UriKind.Relative));
appBarButton.Text = rm.GetString("BackAppBar");
ApplicationBar.Buttons.Add(appBarButton);
I get a System.Resources.MissingManifestResourceException when I try to assign to the AppBar button Text the String I retrieve from the GetString method.
What am I missing?
Also, I read that when you check a new language in the WMAppManifest.xml file, Visual Studio should automatically copy the actual AppResources.resx file and rename the copy with the new current culture selected, but when I try to add a new language it doesn't work.
Do I have to copy and rename the file on my own? Will it recognize the new file when running on a phone that is set on that language?
Thank you for your help!
Sergio
Simply use the auto generated AppResources class
string resourceValue = AppResources.NameOfYourLocalizedResource;
I need to change the background image at runtime, based on which RadioButton the user clicks. I'm doing this in a WPF project in Visual Studio, and I need to put the code in the Checked event in the xaml.cs file
I have an Image control called imgBackground, with 6 images in its Source collection, which are listed in an Images folder in the Solution Explorer.
I've tried:
this.imgBackground.Source = "filename.jpg";
both with and without the quotes, and with various paths (I've tried too many different variations to list them all here) and nothing works - everything I've tried just gives an error in the editor, before I even try to build and run anything (the error given varies depending on what I'm trying at the time).
If you are using relative paths as filenames like
this.imgBackground.Source = "filename.jpg";
then these files must be in the same directory as the .exe of your program is.
One workaround would be to use absolute paths like
this.imgBackground.Source = #"C:\MyFolder\MyProject\filename.jpg";
Or, even further use the packaging mechanism of WPF or pack your images as resources into your assembly. Look at this thread.
EDIT:
For your clarification:
The Source-property demands an System.Windows.Media.ImageSource-object, which you must provide.
Do it like this:
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("filename.jpg", UriKind.Relative);
bi3.EndInit();
this.imgBackground.Source = bi3;
Please refer to this documentation here.