I am developing an application called WeatherBar. Its main functionality is based on its interaction with the Windows 7 taskbar — it changes the icon depending on the weather conditions in a specific location.
The icons I am using in the application are all stored in a compiled native resource file (.res) — I am using it instead of the embedded resource manifest for icons only. By default, I modify the Icon property of the main form to change the icons accordingly and it works fine, as long as the icon is not pinned to the taskbar. When it gets pinned, the icon in the taskbar automatically switches to the default one for the executable (with index 0 in the resource file).
After doing a little bit of research, I figured that a way to change the icon would be changing the shortcut icon (as all pinned applications are actually shortcuts stored in the user folder). But it didn't work.
I assume that I need to change the icon for the executable, and therefore use UpdateResource, but I am not entirely sure about this. My executable is not digitally signed, so it shouldn't be an issue modifying it.
What would be the way to solve this issue?
If you want to do this programatically, I would start by looking at the Portable Executable file format (Wikipedia entry). The resources section (.rsrc, see section 6.9) should contain the icon. Using this information, you can write a tool to modify the icon.
If you just want to quickly change an icon in an existing file, you might be able to hack it up in the Visual Studio resource editor. I tested this with a file by deleting the old icon and adding a new one. The .exe icon changed in Explorer to the new icon, and the new icon appeared on the Start menu when I dragged it there.
-- Edit --
Yes, I agree that using UpdateResource is a good approach. Here is an example I found of using C++ functions to do so, and a P/Invoke signature for UpdateResource and FindResource.
private void button1_Click(object sender, EventArgs e)
{
String path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
String name = "test";
Shell32.Shell shl = new Shell32.ShellClass();
// Optional code to create the shortcut
System.IO.StreamWriter sw = new System.IO.StreamWriter(path + #"\" + name + ".lnk", false);
sw.Close();
// End optional code
Shell32.Folder dir = shl.NameSpace(path);
Shell32.FolderItem itm = dir.Items().Item(name + ".lnk");
Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink;
// Optional code to create the shortcut
lnk.Path = Environment.GetFolderPath(Environment.SpecialFolder.System)
+ #"\notepad.exe";
lnk.Description = "nobugz was here";
lnk.Arguments = #"c:\sample.txt";
lnk.WorkingDirectory = #"c:\";
// End optional code
lnk.SetIconLocation(Environment.GetFolderPath(Environment.SpecialFolder.System)
+ "cmd.exe", 1);
lnk.Save(null);
}
This was taken from http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/9e23a82c-8bed-4b96-8b9a-4c2b6136a622/
It may help.
I decided to implement a workaround - the icon will change in the thumbnail for the window (it is possible in Windows 7). If the icon is unpinned, the user can see the icon changing. In case it is pinned, the thumbnail will change according to the current weather conditions.
Seems to me like the structure of pinned icons (being a shortcut, in fact) doesn't allow dynamic icon change. If I am wrong, I am open for comments and ideas on this.
Related
I download and use CommonOpenFileDialog from NuGet to open the folder selection window.
Since the functions are generally similar, you can think of using OpenFileDialog.
I saved the path of the folder that opened last in a text file and loaded it at the next run to run the folder selection window through CommonOpenFileDialog.Initial Directory.
It works well so far, but what I want to is that from the moment the folder selection window opens, the folder is already selected.
Since I have to check before opening the folder, it is possible to implement it right away without a folder selection window through the path, but it is an unwanted way.
I would appreciate it if you could give me advice.
I upload a part of the code I wrote for reference.
Thank you.
string readTexts = null;
if (fi.Exists)
readTexts = File.ReadAllText(fi.FullName);
folder_dialog.InitialDirectory = readTexts;
if (folder_dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
OpenTextBox.Text = selected_path = folder_dialog.FileName;
WritePath(fi, System.IO.Path.GetDirectoryName(selected_path));
}
System.Windows.Forms.FolderBrowserDialog dlg = new FolderBrowserDialog();
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle);
System.Windows.Forms.DialogResult result = dlg.ShowDialog(win);
I have used this to get the folder dialog box but now i need to disable right click in that folder dialog box, so that i can prevent deletion of folders from there..enter code here
Creating a custom folderDialog box is the last option i want to take..
So, Could some one suggest any possible solution for this without custom folderDialog.
You can't. The class cannot be inherited so you can't override any of the settings. There are no events to hook into.
So you have a couple options:
Roll Your Own
Use the file system to lock down your user environment.
Buy a third party control that has this functionality.
We opted for option 2, because the end users did not need to use "normal" windows apps/file locations on our RDP server, they just needed to run our application. The Organizational Unit (OU) they are added to applies permissions that they only had access to the folders we wanted them to have access to. They can't see any of the normal items you would see when the dialog is shown, but can create folders, save items, load items from the folders we give them permission to use.
Ravindra,
Since Delete in the ContextMenu is a windows feature, you would have to modify the registry settings.
In essence you have to modify/delete the Delete registry entry & after your code executes you must restore it.
You can find the registry entry under: HKEY_CLASSES_ROOT. (You would indeed take some time to figure out this entry).
Ex:
System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog();
//Get key for New menu option in Context menu.
RegistryKey key = Registry.ClassesRoot.OpenSubKey(#"Directory\Background\shellex\ContextMenuHandlers\New",true);
//Set it to blank.
key.SetValue("", "");
fd.ShowDialog();
//Restore the value.
key.SetValue("", "{D969A300-E7FF-11d0-A93B-00A0C90F2719}");`
I'm setting the HKEY_CLASSES_ROOT\.ext\DefaultIcon to the path of my application and it works...for the most part.
// create the HKEY_CLASSES_ROOT\extension
RegistryKey k = Registry.ClassesRoot.CreateSubKey(".spr");
k.SetValue("", "SpaceRead"); // set the default to the program name
RegistryKey di = k.CreateSubKey("DefaultIcon");
di.SetValue("", Application.ExecutablePath);
di.Close();
k.Close();
My problem is that my icon is showing up 'inside a white page' instead of only showing my icon (and losing the page)
here's an example. The first icon is mine, the second one is what I want.
My .ico file has all the standard resolutions in it, but I don't think that's the problem since even small icon views show the 'page' background.
Is there some custom icon rendering going on that I don't know about?
BTW, this is a Windows 7 x64 machine.
I want to get a list of information (with filename and real theme name) of the installed AERO themes in a Windows 7 OS.
Also I need to change the current theme to one of the installed themes, but without using process.start(ThemeFile) because the personalization panel opens when I try that...
I know here is the installed theme files to get the filenames: "C:\Windows\Resources\Themes"
But I'm asking if exist a better way than listing the file content of that dir 'cause I need the real names too, and I want to know how to change the current theme to other without opening the personalization panel.
An example of this... I have only two Aero themes on my OS.
The filenames are:
Aero.theme
Concave_seven.theme
But the theme names wich appears in the personalization panel are:
Windows 7
Concave 7
I want to retreive the filenames and real names to store it in a listbox to change the current theme for the desired theme.
Update,
What I've tried...
' Load theme names
For Each Theme As System.IO.FileInfo In New System.IO.DirectoryInfo(Environment.GetEnvironmentVariable("windir") & "\Resources\Themes").GetFiles("*theme")
ComboBox1.Items.Add(Theme.ToString.Substring(0, Theme.ToString.Length - 6))
Next
' Change theme
' Process.start(ThemeFilename)
' rundll32.exe Shell32.dll,Control_RunDLL desk.cpl desk,#Themes /Action:OpenTheme /File:"C:\Windows\Resources\Themes\aero.theme"
Well like anybody knew how to change the current theme without opening the theme selector with rundll32 etc... then here we go my personal trick (which is not on google and anywhere, it's pure luck).
First step: disable DWMCOMPOSITION for example disabling the "Themes" service.
Second step: add a regkey to specify the desired new theme, specify the msstyles file, not the .theme file.
I do this with a personal func but you can get the idea:
Reg_Set_Value("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ThemeManager", "DllName", "C:\windows\resources\themes\aero\aero.msstyles", Microsoft.Win32.RegistryValueKind.String)
Third step: Re-enable the DWM composition ("Themes" service).
Voilá!
UPDATE:
Also I noticed need to change this value to 0 before enabling the theme to ensure all the color schemes are updated:
Reg_Set_Value("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ThemeManager", "LoadedBefore", "0", Microsoft.Win32.RegistryValueKind.String)
Before going much further i'll mention I have tried solutions in following:
How do I set the icon for my application in visual studio 2008?
Set application icon from resources in VS 05
I am trying to set an icon for my application.
AFAIK, I need potentially 3 images?
1 image is the actual image in explorer when clicking on the .exe (thumbnail for the exe)
1 image (tiny) in the top left corner (16 x 16? Not entirely sure)
1 image in the start menu dock, to the left of the app (maybe 32x32? again not sure)
So thats fine.
Now I have selected an Icon. How do I use it in one of above situations?
I have tried adding it in resources, nothing seems to happen. Following that first SO solution,
"First go to Resource View (from menu: View --> Other Window --> Resource View). Then in Resource View navigate through resources, if any. If there is already a resource of Icon type, added by Visual Studio, then open and edit it. Otherwise right-click and select Add Resource, and then add a new icon."
The resource view is empty, and I cannot right click in this view.
If I right click on the solution > properties > resources > I can add the icon image, but it doesn't show in either of the locations listed above. (or anywhere that I can see)
1) How do I set the application icon for a WPF Application?
Assuming you use VS Express and C#.
The icon is set in the project properties page. To open it right click on the project name in the solution explorer. in the page that opens, there is an Application tab, in this tab you can set the icon.
#742's answer works pretty well, but as outlined in the comments when running from the VS debugger the generic icon is still shown.
If you want to have your icon even when you're pressing F5, you can add in the Main Window:
<Window x:Class="myClass"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Icon="./Resources/Icon/myIcon.png">
where you indicate the path to your icon (the icon can be *.png, *.ico.)
(Note you will still need to set the Application Icon or it'll still be the default in Explorer).
Note: (replace file.ico with your actual icon filename)
Add the icon to the project with build action of "Resource".
In the Project Properties, set the Application Icon to file.ico
In the main Window XAML set: Icon=".\file.ico" on the Window
After getting a XamlParseException with message: 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' with the given solutions, setting the icon programmatically worked for me. This is how I did it:
Put the icon in a folder <icon_path> in the project directory
Mimic the folder path <icon_path> in the solution
Add a new item (your icon) in the solution folder you created
Add the following code in the WPF window's code behind:
Icon = new BitmapImage(new Uri("<icon_path>", UriKind.Relative));
Please inform me if you have any difficulties implementing this solution so I can help.
You can try this also:
private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
Uri iconUri = new Uri(#"C:\Apps\R&D\WPFNavigation\WPFNavigation\Images\airport.ico", UriKind.RelativeOrAbsolute);
(this.Parent as Window).Icon = BitmapFrame.Create(iconUri);
}