Wpf как свернуть в трей

от admin

Русские Блоги

причина Недавно я смотрю на «Структуру данных и алгоритм — JavaScript», затем перейдите в NPMJS.ORG для поиска, я хочу найти подходящую ссылку на библиотеку и записывать его, я могу исполь.

MySQL общие операции

jdbc Транзакция: транзакция, truncate SQL заявление Transaction 100 000 хранимая процедура mysql msyql> -определить новый терминатор,Пробелов нет mysql>delimiter // mysql> -создание хранимой .

Используйте Ansible для установки и развертывания TiDB

жизненный опыт TiDB — это распределенная база данных. Настраивать и устанавливать службы на нескольких узлах по отдельности довольно сложно. Чтобы упростить работу и облегчить управление, рекомендуетс.

Последняя версия в 2019 году: использование nvm под Windows для переключения между несколькими версиями Node.js.

С использованием различных интерфейсных сред вы можете переключаться между разными версиями в любое время для разработки. Например, развитие 2018 года основано наNode.js 7x версия разработана. Тебе эт.

Шаблон проектирования — Создать тип — Заводской шаблон

Заводская модель фабрикиPattern Решать проблему: Решен вопрос, какой интерфейс использовать принципСоздайте интерфейс объекта, класс фабрики которого реализуется его подклассом, чтобы процесс создания.

Minimize to tray icon in WPF

I had to do a bit of searching so, here’s how to minimize your WPF app to a tray icon. Assume your window is called Window1 for the code below. Trap Closing, StateChanged and IsVisibleChanged (see xaml).

xaml:

<window x:Class=»MyApp.Window1″ xmlns=»http://schemas.microsoft.com/winfx/2006/xaml/presentation»
xmlns:x=»http://schemas.microsoft.com/winfx/2006/xaml»
Title=»WCFAppVisualHost» Height=»383″ Width=»680″ Closing=»OnClose» StateChanged=»OnStateChanged» IsVisibleChanged=»OnIsVisibleChanged»
>

Associated cs:

private System.Windows.Forms.NotifyIcon m_notifyIcon;

public Window1()
< // initialise code here
m_notifyIcon = new System.Windows.Forms.NotifyIcon();
m_notifyIcon.BalloonTipText = «The app has been minimised. Click the tray icon to show.»;
m_notifyIcon.BalloonTipTitle = «The App»;
m_notifyIcon.Text = «The App»;
m_notifyIcon.Icon = new System.Drawing.Icon(«TheAppIcon.ico»);
m_notifyIcon.Click += new EventHandler(m_notifyIcon_Click);
>

void OnClose(object sender, CancelEventArgs args)
<
m_notifyIcon.Dispose();
m_notifyIcon = null;
>

private WindowState m_storedWindowState = WindowState.Normal;
void OnStateChanged(object sender, EventArgs args)
<
if (WindowState == WindowState.Minimized)
<
Hide();
if(m_notifyIcon != null)
m_notifyIcon.ShowBalloonTip(2000);
>
else
m_storedWindowState = WindowState;
>
void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
<
CheckTrayIcon();
>

void m_notifyIcon_Click(object sender, EventArgs e)
<
Show();
WindowState = m_storedWindowState;
>
void CheckTrayIcon()
<
ShowTrayIcon(!IsVisible);
>

void ShowTrayIcon(bool show)
<
if (m_notifyIcon != null)
m_notifyIcon.Visible = show;
>

Share this:

Like this:

Related

34 thoughts on “ Minimize to tray icon in WPF ”

This sample is hard to follow and get working. A working sample would be much better.

For example, what files should be created here?

You need to create *.ico file with your icon. And then you need to replace “TheAppIcon.ico” in this line:

m_notifyIcon.Icon = new System.Drawing.Icon(”TheAppIcon.ico”);

with file that you created. This example works fine on my computer.

No problems here either. Thanks.

Thank You, it works

I simplyfied one method, however:

void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args) <
if (m_notifyIcon != null)
m_notifyIcon.Visible = !IsVisible;
>

thank you
it was wery helpful

You need to add a reference to System.Windows.Forms.dll in your WPF application in order to reference the NotifyIcon control

You also need to add a reference to System.Drawing.dll in your WPF application in order to utilize NotifyIcon.

Thanks for above but I am trying to out this. My system is minimised and icon on the taskbar tray. Now how to I get the tooltip when I do mouseover the minimised icon on taskbar?

Hi,
I was trying to get this working but I am getting this error:

Exception has been thrown by the target of an invocation. Error in markup file ‘Window1.xaml’

I added the dll’s and all seem ok. Any suggestions?

I commented out this line and the program starts.

//m_notifyIcon.Icon = new System.Drawing.Icon(“original_logo1.ico”);

When I minimize the window, there is no icon in system tray. Obviously, i did not set the icon.

How can I make it so that will work?

I got it sorted.

m_notifyIcon.Icon = new System.Drawing.Icon(@”c:\icon.ico”);

I used another icon from the c drive so i guess there was
some problem with the location of the icon.

Thanks for the code. I learned this one today. ��

this.Hide() and this.Show() seem to be problematic with multi-monitor displays.

this.Visibility = Visibility.Collapsed has worked better for me.

Hi,
Thanks for the solution but i am getting run time error .
I am getting following error :-

System.Windows.Markup.XamlParseException was unhandled
Message=”Cannot create instance of ‘Window1’ defined in assembly ‘WpfApplication1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’. Exception has been thrown by the target of an invocation. Error in markup file ‘Window1.xaml’ Line 2 Position 2.”
Source=”PresentationFramework”
LineNumber=2
LinePosition=2
StackTrace:
at System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType)
at System.Windows.Markup.XamlParseException.ThrowException(ParserContext parserContext, Int32 lineNumber, Int32 linePosition, String message, Exception innerException)
at System.Windows.Markup.BamlRecordReader.ThrowExceptionWithLine(String message, Exception innerException)
at System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(Type type, Int16 typeId, Boolean throwOnFail)
at System.Windows.Markup.BamlRecordReader.GetElementAndFlags(BamlElementStartRecord bamlElementStartRecord, Object& element, ReaderFlags& flags, Type& delayCreatedType, Int16& delayCreatedTypeId)
at System.Windows.Markup.BamlRecordReader.BaseReadElementStartRecord(BamlElementStartRecord bamlElementRecord)
at System.Windows.Markup.BamlRecordReader.ReadElementStartRecord(BamlElementStartRecord bamlElementRecord)
at System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord)
at System.Windows.Markup.BamlRecordReader.Read(Boolean singleRecord)
at System.Windows.Markup.TreeBuilderBamlTranslator.ParseFragment()
at System.Windows.Markup.TreeBuilder.Parse()
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
at System.Windows.Application.DoStartup()
at System.Windows.Application.b__0(Object unused)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at WpfApplication1.App.Main() in C:\Documents and Settings\v-robhop\My Documents\Expression\Expression Blend Projects\WpfApplication1\WpfApplication1\obj\Debug\App.g.cs:line 0
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Reflection.TargetInvocationException
Message=”Exception has been thrown by the target of an invocation.”
Source=”mscorlib”
StackTrace:
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(Type type, Int16 typeId, Boolean throwOnFail)
InnerException: System.IO.FileNotFoundException
Message=”Could not find file ‘C:\\Documents and Settings\\v-robhop\\My Documents\\Expression\\Expression Blend Projects\\WpfApplication1\\WpfApplication1\\bin\\Debug\\Windows_icon.ico’.”
Source=”mscorlib”
FileName=”C:\\Documents and Settings\\v-robhop\\My Documents\\Expression\\Expression Blend Projects\\WpfApplication1\\WpfApplication1\\bin\\Debug\\Windows_icon.ico”
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.Drawing.Icon..ctor(String fileName, Int32 width, Int32 height)
at System.Drawing.Icon..ctor(String fileName)
at WpfApplication1.Window1..ctor() in C:\Documents and Settings\v-robhop\My Documents\Expression\Expression Blend Projects\WpfApplication1\WpfApplication1\Window1.xaml.cs:line 32
InnerException:

Читать:
Сколько касаний делать на экране

Can anybody help me on this?( I am totally new to wpf and its coding �� )

I have the same error:

No se controló System.Windows.Markup.XamlParseException
Message=»No se puede crear una instancia de ‘Window1’ en el ensamblado ‘Proyecto01, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’. Se produjo una excepción en el destino de la invocación. Error en el archivo de marcado ‘Window1.xaml’ Línea 1 Posición 9.»
Source=»PresentationFramework»
LineNumber=1
LinePosition=9
(. )

How can you fixed…?

Also your Icon should be added as an “Embedded Resouce” not “Resouce”. (see properties in Visual Studio)
“Resource” is for WPF and “Embedded Resouce” is for backward compatibility with winforms. Because this component was designed for winforms it expects a winforms compatible resource.

Thanks for posting this. It helped me solve my problem. Too bad this requires loading the WinForms assembly.

“Too bad this requires loading the WinForms assembly.”… T

True, but you do what you’ve got to do under time constraints. There are so many things you can do under WinForms that you can’t under Wpf without a TON of hassle that I find it hard to want to use Wpf. I’ve used it a few times, but for business apps it just doesn’t make sense for me yet.

Thank you for this. Had to change to Embedded resource and used icofx to create a good .ico (same file with many sizes).

Thanks for the info, very helpful! We tried the same stuff, it works fine in the vs2008 or in the bin folder, but when we publish it as a stand alone application, it comes out with error when i minimize, when we debug, it shows the error, that it is not able to find the favicon.ico in some strange path. It means during publish, the project is not taking the icon file, is there any solution for the same!

Thanks in advance!

Thanks You, i have an problem in cs code !
Could you help me to solve this !
—————————–
void OnClose(object sender, CancelEventArgs Args)
<
m_notifyIcon.Dispose();
m_notifyIcon = null;
>
—————————–
Error 1 The type or namespace name ‘CancelEventArgs’ could not be found (are you missing a using directive or an assembly reference?)

I tried the same code. But there is no icon in system tray when i minimize the window.

This is very easy to use … YOU’LL LOVE IT !

Thanks, just what I was looking for. Too bad WPF seems to miss some important building pieces.

Seems MS themselves thinks this method is the way to go:

This example was v v helpful..thanks a lot…

Thanks , it is very helpfull for me

If we want to add Context menu item in this code what kind of changes have to be done in code… solve the problem

Works fine to me. Thanks for sharing this. More power!

Hi and a big Thanks to You!

I copied the sourcecode an it’s works fine.

A good Tutorial, very helpful.

Thanks in advance!!

This is having an issue on .Net 4 when there’s a Forms web browser on my WPF window. Check out my Connect issue I’ve opened about it.

It seems when there’s a browser window open it won’t let the NotifyIcon menu to open.

To stop it appearing in the taskbar use:

Also for the icon issue:

m_notifyIcon.Icon = new Icon(AppDomain.CurrentDomain.BaseDirectory + “Small.ico”);

this needs an icon file in the bin folder called small.ico, size 16×16

It worked fine for me after i followed Artur Carvalho sugestion and put the icon as embeded resource.

Note:

There is no direct way to minimize a WPF application to system tray. This does not means that we cannot do that at all.

To minimize WPF app to System Tray/add the Tray notification, imagewe just need to add the “System.Windows.Forms” and “System.Drawing” assembly references to your WPF project. Once we add the references, we can create new instance of the “NotifyIcon” class and use it in our WPF application.

After we add the references to the WPF application, we have to create a global variable for NotifyIcon.

Now we have to initiate the NotifyIcon. This can be done in the Window’s constructor. We also have to declare an Event to retrieve back the application from SystemTray.

To minimize the application to the system tray we need to handle the appropriate events. There are multiple events that can be used to achieve the output… The most appropriate will be the Form SizeChanged event.

To revive the app back we need to write a couple more lines. This time the event to be handled is NotifyIcon MouseDoubleClick or NotifyIcon MouseClick.

If you are not interested in the balloon tip, we can omit the BalloonTip and ShowBalloon lines.

Сворачивание приложения в системный трей с помощью WPF (без использования NotifyIcon)

Я закончил создание своего приложения, и теперь я хочу включить для него «сворачивание в функцию панели задач». Я прочитал хорошую статью свернуть приложение на панель задач. Я понял, что они используют класс Windows.Form.

К сожалению, я использовал справочник по WPF Windows Presentation Foundation для создания пользовательского интерфейса своих приложений. Теперь я вижу, что NotifyIcon не поддерживается в WPF. Я вижу, что на CodePlex есть библиотека с открытым исходным кодом, которая имитирует свойства NotifyIcon WPF Contrib Пока не использовал.

Теперь я нахожусь в затруднительном положении. Вот мои вопросы: —

А) Я не хочу включать стороннюю библиотеку только для одного компонента.

Б) Могу ли я сделать функцию минимизации без NotifyIcon в WPF? Если да, то как кто-то может дать мне лиды, пожалуйста?

Или, может быть, мне следует вернуть свой пользовательский интерфейс к использованию Windows Forms?

3 ответа

Если вы пересмотрите свое нежелание использовать внешний компонент, я рекомендую WPF NotifyIcon. Я использовал это. Это просто и хорошо работает.

Он не просто полагается на соответствующий компонент WinForms, но представляет собой чисто независимый элемент управления, который использует несколько функций платформы WPF для отображения расширенных всплывающих подсказок, всплывающих окон, контекстных меню и всплывающих сообщений.

Похожие статьи