WPF - Refer to app icon in code - wpf

i have added an icon to a WPF app in the project properties.
How do I refer to that icon so I can add it to a
NotifyIcon that I am creating for system tray.
In code that is??
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = new System.Drawing.Icon("MyIcon.ico");
Does not work.
Malcolm

System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = System.Drawing.Icon.ExtractAssociatedIcon(
System.Reflection.Assembly.GetEntryAssembly().ManifestModule.Name);
ni.Visible = true;

I think this may be what you are looking for:
How do I use an icon that is a resource in WPF?
You need to embed the icon as a resource and then you can access it from code.

Related

MahApps:Metro SplitButton Control Template

Default SplitButton works ok, but when I try to work with it's control template issues arise.
If I try to get Control Template with reflection (with ConstructorInfo) I get empty Control Template for SplitButton. If I try to 'Edit Template copy' in XAML Designer, I get copy which does not work (like ItemsSource does not bind to elements in ListBox of SplitButton as it is always empty).
My version of MahApps Metro is 1.4.3.0
Here is how I try to get Control Template of SplitButton:
MahApps.Metro.Controls.SplitButton ctl = sender as MahApps.Metro.Controls.SplitButton;
Type type = ctl.GetType();
if (type == null)
return;
// Instantiate the type.
ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes);
Control control = (Control)info.Invoke(null);
// Get the template.
ControlTemplate template = control.Template;
// Get the XAML for the template.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, settings);
XamlWriter.Save(template, writer);
The default ControlTemplate is available on GitHub: https://github.com/MahApps/MahApps.Metro/blob/336f7dfc4bda2d0eba8aa270737ca3c11d45128c/src/MahApps.Metro/MahApps.Metro/Themes/SplitButton.xaml
MahApps.Metro is open source so you can download the source code if you want to.
After updating MahApps Metro to 1.5.0 SplitButton works fine with provided Control Templates...

c# wpf - windows forms host border color

I'm creating a windows forms host to display a dashboard.
It's displaying a border, how do I remove it?
currently have:
Creating a grid.
<Grid Name="winForms" Grid.Row="1"/>
Binding it to dashboard.
// Create the interop host control.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
this.winForms.Children.Add(host);
//THIS SHIT SHOWS A DESIGNER LOL
System.Windows.Forms.UserControl uc = new System.Windows.Forms.UserControl();
DevExpress.DashboardWin.DashboardViewer dd = new DevExpress.DashboardWin.DashboardViewer();
dd.BorderStyle = System.Windows.Forms.BorderStyle.None;
dd.DashboardSource = "DashboardFiles/KPI.xml";
uc.Controls.Add(dd);
dd.Dock = System.Windows.Forms.DockStyle.Fill;
//dd.CreateRibbon();
host.Child = uc;
I thought the border belonged to the dashboard viewer but it does not.
Change the FormBorderStyle property in the design view or else you can set the Property FormBorderStyle in code behind like this :
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

WPF: put an icon on a radWindow programatically

I want to put an icon on the top left of a radWindow programatically
my code is like this
RadWindow radWindow = new RadWindow();
radWindow.Header = "The header";
radWindow.Icon = new Image()
{
Source = new BitmapImage(new Uri("../ressources/enregistrer.png", UriKind.Relative))
};
radWindow.Show();
but the icon dont show up
does anyone have an idea ?
EDIT
This is the architecture on my project:
The file from where the above code is taken is circled in red
The ressource file is circled in green
<telerik:RadWindow.Icon>
<Image Source="pack://application:,,,/ressources/enregistrer.png" Height="18"/>
</telerik:RadWindow.Icon>
// this seems to work. You will need to provide a size.
I dont know why my solution above did not work but I found a workaround like this
RadWindow radWindow = new RadWindow();
radWindow.Header = "The header";
radWindow.Icon = new Image()
{
Source = new BitmapImage(new Uri("pack://application:,,,/ressources/enregistrer.png", UriKind.RelativeOrAbsolute))
}
radWindow.Show();

How to remove the border of a Button in code?

I am developing a windows phone 7 application. I am new to windows phone 7 applications. In my application I have created a button control dynamically and added the background image to the button control as follows.
Button AlphabetButton = new Button();
AlphabetButton.Content = vAlphabet;
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("button_off.png", UriKind.Relative));
//brush.Stretch = Stretch.None;
AlphabetButton.Background = brush;
AlphabetButton.BorderBrush = new SolidColorBrush(Colors.Gray);
AlphabetButton.Margin = new Thickness(-12, -27, 0, 0);
AlphabetButton.Width = 80;
AlphabetButton.Height = 80;
I want to remove the border of the button control because with that border the image does not appear as per requirement. How to do this? Can we do this with the BorderThickness attribute of button control or is there any other way? Can you please provide me any code or link through which I can resolve the above issue? If I am doing anything wrong then please guide me.
The easiest way is to set the BorderThickness to 0 like for Example:
Button alphabetButton = new Button();
alphabetButton.BorderThickness = new Thickness(0.0);
Another option could be to set the BorderBrush to Transparent or to change the whole Style of the button and especially its ControlTemplate.
I think Button.StrokeTHickness is the correct property to adjust the borders.

Using a Windows Forms icon in WPF

I've got this WPF code which works...
Uri iconUri = new Uri("pack://application:,,,/media/images/VS.ico", UriKind.RelativeOrAbsolute);
this.Icon = BitmapFrame.Create(iconUri);
I'm using a windows forms notifyIcon control in my WPF app, and I now need to assing the Icon to it. How do I get from my WPF icon to a System.Drawing.Icon ?
I use the following method:
// Setup the new NotifyIcon
System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.Text = "Name of Application";
notifyIcon.Icon = new System.Drawing.Icon("media/images/VS.ico");
notifyIcon.Visible = true;
Make sure you add a reference to System.Drawing.
Imaging.CreateBitmapSourceFromHBitmap
I use it like:
return Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
where source is a Bitmap, which you can get by calling your Icon's .ToBitmap() method.

Resources