WPF NotifyIcon problems [duplicate] - c#

This question already has an answer here:
Set image in NotifyIcon control from code behind or in XAML
(1 answer)
Closed 9 years ago.
Hi i have some parser error when im trying to add icon to NotifyIcon. This one works fine:
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = new System.Drawing.Icon("C:\\Users\\Daniel\\Documents\\Visual Studio
2012\\Projects\\Pies\\Pies\\main.ico");
But when I'm trying to do it in this way:
ni.Icon = new System.Drawing.Icon("main.ico");
It returns some strange ParserError. I added main.ico to the project. When im adding images in xaml I'm just using "/image/image.jpg" but this don't want to work in this way. Do yuo know why ?

Notice, that you are trying to use WinForms control in WPF project. And icon class from WinForms doesn't support pack URIs.
Since you want to store your image in your assembly, you can't get it as WinForms Icon easily. WPF usually works with ImageSource class and you can convert it to WF icon (How can I convert BitmapImage to Icon?), but I would call it pain.
NotifyIcon is not implemented in WPF, so I suggest to try this solution.
More detais: Can I use NotifyIcon in WPF?

Why don't you add the .ico file in your solution and point the reference to it something like this
Try doing this
ni.Icon = new System.Drawing.Icon(#"pack://application:,,,/Icon/main.ico");

Related

WPF Form loses it style after showing from another project

I have a C# application which contains 2 project, and when I set the first project which contains a Styled WPF Form as startup project, it has it's own style and everything is fine. But when I use this code to show that WPF from from a Windows Form Application, the first project's form loses its own style:
Window introForm;
introForm = new Client.MainWindow();
introForm.Show();
I have no idea why it happens
I have found my answer here:
https://stackoverflow.com/a/6042515/7147513
this fixed my problem:
var foo = new Uri("pack://application:,,,/MyProject;component/Resources/Styles.xaml", UriKind.RelativeOrAbsolute);
Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = foo });

How can I add an InkCanvas element to a Winform?

From what I understand so far, the InkCanvas element is in the WPF Framework. To use that, I need an ElementHost control to host the InkCanvas element. I've been to the MSDN links, but the example it gives talks of creating a WPF User Control Library project and so on. It's not that bad, but it seems a bit much to just add a control to a Winform. Is there a simpler way to do this, or am I trying to oversimplify this?
Thanks.
This should work:
ElementHost host = new ElementHost();
InkCanvas ic = new InkCanvas();
host.Child = ic;
Controls.Add(host);
As mentioned in comments, one needs to add the WPF assemblies as reference (WindowsBase, PresentationCore, PresentationFramework).

How do I display a messageBox on top of, or near its parent Form? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
center MessageBox in parent form
I am trying to make an C# application and I want to have my messageBox appear near the parent.
I tried:
MessageBox.Show(this,"this operation does not work");
And this doesn't work.
Assuming you're using Windows Forms, create a new class which extends System.Windows.Form to mimic MessageBox. Set the Location property based on your new classes Parent.Location property.

hosting WPF inside Windows Form giving issues - C#

I've created a .DLL in WPF. To use it in existing Windows Forms application I use ElementHost.
ElementHost eleHOst = new ElementHost();
UserWarps userWarps = new UserWarps();
eleHOst.Child = userWarps;
eleHOst.Dock = DockStyle.Fill;
UserWarps is in WPF .DLL which has been add-referenced. Now the file does lot of 3D manipulations. I'm also using Petzold.Media3D for 3D lines for wireframe modelling. Everything's working fine except that WireLines of Petzold.Media3D is not drawing any lines. If I reference the DLL from other WPF applications everything's fine, but hosting the UserControl of wpf in windows forms eliminates the lines/wireframes. Rest everything is perfect - MeshGeometry3D, Models, Visuals, functionalities etc.
Please suggest the way forward. could any alternative to ElementHost work? If it does then what is it?
Adding answer originally added by OP in question as I don't want this question to
be closed just because of that.
Petzold has mentioned here that hosting wpf in Windows forms causes the Wire frames to disappear. He also posts a work around which is very simple and worked perfectly:
NOTE: For reasons discussed in paragraph 5, these Wire classes will
not work when
you're hosting 3D in Windows Forms, or when you're trying to print a 3D scene. To make it work, try replacing the static
OnRendering method in WireBase with the following:
static void OnRendering(object sender, EventArgs args)
{
foreach (WireBaseAndUltimateParent wirebaseAndParent in listWireBases)
{
WireBase wirebase = wirebaseAndParent.wirebase;
wirebase.OnRendering();
}
}

Open WPF window in WindowsForm APP [duplicate]

This question already has answers here:
How to programmatically create a WPF window in a WinForm application
(2 answers)
Closed 5 years ago.
I added to my WindowsForm app a new WPF window called novoLogin.
After adding it, I added the system.xaml reference....debug fine.
Now I'm trying to open this new window from the existing windowsForm.
novoLogin nl = new novoLogin();
nl.show();
The compiler is giving this error:
Error 1 'WindowsFormsApplication1.novoLogin' does not contain a
definition for 'show' and no extension method 'show' accepting a first
argument of type 'WindowsFormsApplication1.novoLogin' could be found
(are you missing a using directive or an assembly reference?)
This brief article explains how you can achieve this.
If you find yourself in need to open a WPF Window from a WinForms program, this is one way to do it (works for me):
Create/Add a new project of type WPF Custom Control Library
Add a new Item of type Window (WPF)
Do your thing with the WPF Window
From your WinForms app, create and open the WPF Window
using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
var wpfwindow = new WPFWindow.Window1();
ElementHost.EnableModelessKeyboardInterop(wpfwindow);
wpfwindow.Show();
Have a look to this: http://www.mobilemotion.eu/?p=1537&lang=en
Summary:
Open the project’s manifest file (the one with the .csproj or .vbproj extension) in any text editor.
The top node usually contains several tags, one for each build configuration and a global one. In the global
node (the one without Condition attribute), search for
the sub-node or create one if it does not
exist. This node should contain two GUIDs:
FAE04EC0-301F-11D3-BF4B-00C04F79EFBC, which stands for a C# project,
and 60dc8134-eba5-43b8-bcc9-bb4bc16c2548 which stands for WPF. The
full line should look as follows:
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
(If you’re interested in details, codeproject holds a complete list of
potential project GUIDs:
http://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs)
Reload the project in Visual Studio, and open the Add New Item wizard.
Since the project is now officially classified as WPF project, this
wizard should now contain the WPF window option. By the way, since
there is no WinForms project GUID that could be overwritten, this
approach does not harm the existing project components.
I just tried this approach for a VB.NET project and it works!
Using VB.NET obviously you have to edit above lines substituting the GUID from {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} to {F184B08F-C81C-45F6-A57F-5ABD9991F28F}
I wanted to show wpf form in windowForm and there was some resource problem...
(because I used resources..). Finally I used this code in my windowsForm project:
First create a global instance of your app class like this :
WPFTest.App app;
why this is global?
because this class is singleton and you can not create more than one instance in the same AppDomain
Now for example you have a button event to show wpf form. At the button event we have :
private void button1_Click(object sender, EventArgs e)
{
if (System.Windows.Application.Current == null)
{
app = new WPFTest.App()
{
ShutdownMode = ShutdownMode.OnExplicitShutdown
};
app.InitializeComponent();
}
else
{
app = (WPFTest.App)System.Windows.Application.Current;
app.MainWindow = new WPFTest.YourWindow();
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(app.MainWindow);
app.MainWindow.Show();
}
}
note : WPFTest is name of your project and YourWindow() is window that you wanna to show

Categories

Resources