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\"
Related
If my directories look like this:
MyProject
MyProject
some_dir
file_I_wanna_read
here
Here.cs
How do I read file_I_wanna_read from Here.cs, without having to type the whole path?
(eg. "C:\my_user\Projects\MyProject\MyProject\some_dir\file_I_wanna_read")
I've already tried writing the path as if I were at the solution:
MyProject\some_dir\file_I_wanna_read
And also as if I were in the .csproj:
some_dir\file_I_wanna_read
But those don't work.
Here's a solution.
It relies on getting the FileYouWantToRead copied into the EXE's location where it's findable.
Create a simple, out-of-the-box WinForms app.
Right-click the project, select Add then New Folder, name the folder "SomeDir"
Right-click the new "SomeDir" folder and select Add and then New Item
In the dialog that pops up, search for "Text", pick Text File and name the file "FileIWantToRead.txt"
When the file opens, type "This is the file I want to read" and save the file
Right-click the new "FileIWantToRead.txt" file and choose properties
In the properties pane, set Build Action to Content and Copy to Output Directory to Copy Always
Go back to the form designer, drop a button and a label control on the form
Double-click the button
In the "Form1.cs" button click handler that opens, put this code:
Form1.cs button click handler:
private void button1_Click(object sender, EventArgs e)
{
var fullExeName = Assembly.GetExecutingAssembly().CodeBase;
if (fullExeName.StartsWith("file://"))
{
fullExeName = fullExeName.Substring(8);
}
var exeDir = Path.GetDirectoryName(fullExeName);
var fileName = Path.Combine(exeDir, "SomeDir", "FileIWantToRead.txt");
var content = File.ReadAllLines(fileName)?.First();
label1.Text = content ?? "Not Found";
}
The "Copy Always" action will cause your text file to get copied to the output EXE's folder. In this case, since it's within the "SomeDir" folder, it will get copied into a "SomeDir" folder under the output folder.
I get the EXE's folder from the executing assembly (i.e., the running exe) and use the various utilities in System.IO to get the right folder and file names. After clicking the button, the first line of that file will show in the label.
First you can get current directory of your program. (Read these official docs for further help)
Then you can access your file with directory related to current directory.
Your string of directory accessing should be something like this:
..\some_dir\file_I_wanna_read
(Double dots mean go one directory backward or up.)
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!
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 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.
I am trying to get the Icon of a NotifyIcon in WPF.
So I have added a .ico file to my solution in a Resources folder and set the build action to Resource.
I am trying to grab this resource in code behind like so:
var icon = (Icon) Application.Current.FindResource("/Resources/icon.ico")
This doesn't work.
In addition to this: Application.Current.Resources.Count returns 0.
EDIT
var i = new Icon(Application.GetResourceStream(new Uri("/systemtrayicon.ico", UriKind.Relative)).Stream);
With the icon in the root and the build action set to Resource.
Still not working.
EDIT AGAIN:
I needed to Clean the solution and rebuild as per: WPF throws "Cannot locate resource" exception when loading the image
This will works 100%
ni.Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,<Image Location From root>")).Stream);
Example:
notify.Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,/images/favicon.ico")).Stream);
You have to pass resourceName as a parameter to the FindResource method, not the path for the Resource. Sample code would look like:
var icon = (Icon) Application.Current.FindResource("myImage")
Please note in the above sample code "myImage" is the resource name.
Refer to Application.FindResource Method on MSDN.
You say, Application.Current.Resources.Count is Zero, that means you do not have any Resource defined in your App.xaml file.
You can add resources to App.xaml like this:
<Application.Resources>
<Image x:Key="myImage" Source="img.png" />
</Application.Resources>
It appears that your icon is an embedded resource. FindResource cannot work with embedded resources. Set BuildAction of your icon to Resource.
Refer to this MSDN page for more reading on WPF Resources.
UPDATE
Code for accessing Embedded Resources
Assembly.GetExecutingAssembly().GetManifestResourceStream("myImg.png");
However, if you had added this image to the Resources.Resx and you should simply be able to use Resources.ResourceName.
UPDATE 2
Adding resources to App.xaml or any ResourceDictionary is better, so that you can use them as Static/Dynamic resources via StaticResource or DynamicResource markup extensions.
If you do not want to add it to App.xaml resources and still want to access it, one option as I mentioned above is to add it to the Resources.Resx and use Resources.ResourceName to refer the icon/image
Another way is to create System.Drawing.Icon by yourself, sample code:
new System.Drawing.Icon(Application.GetResourceStream(new Uri("/Resources/icon.ico")));
Personally, I would go with XAML resources and add them to App.xaml or a ResourceDictionary.