Wpf minimize all windows

Wpf minimize all windows

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Answered by:

Question

I am using WPF. I am implementing a functionality where user is able to open a modeless dialog box from a window. Now if user is minimizing the parent window the modeless dialog box is not getting minimized. I am not able to see any suitable event in the window class. Please guide me.

Answers

Which of course is fired after the state has changed.

If that was a problem then you could use P/Invoke and check the windows messages.

All replies

This should be the event that you are looking for.

You could also set the Owner property of the child window as the parent,so that child minimizes with the parent.This would still allow you to interact with the parent window.

This should be a good read

Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem

Which of course is fired after the state has changed.

If that was a problem then you could use P/Invoke and check the windows messages.

Thanks Andy. You were right. The State Changed event is getting fired. I wired up event in a wrong manner. The switch case statement solved all my probs. Thanks a ton 🙂 🙂

question

Minimize a WPF application to System Tray in C#

First of all, I want to create a WPF that can be minimized in the Windows taskbar (next to the clock).

Second, I want to change the WPF’s Incon icon when the wpf is minimized.

Thirdly, when the WPF is minimized, it should display action messages (such as Outlook when you receive an email or display a message to the user when a connection is lost).

2 Answers

If you want it in the system tray I think what you’ll have to do is make it a Windows service. I’ve only written 1 Windows Service and that was years ago, but I believe that’s what you’ll have to do.

If I’m correct about writing a Windows service, then what I would suggest you do is create a new Visual Studio solution and add two projects to it. One would be a DLL which would run as a Windows service. The second project would be a WPF project that will be your UI the user interacts with. Then you’ll have to use some messaging system to communicate between the two.

For the action messages that would mimic what Outlook does, I’ve used some WPF toast messages to accomplish that. If you Bing/Google «WPF toast popup» you’ll get lots of results.

I hope this helps.

Читайте также:  Windows presentation foundation описание

The true difficulty was that WPF on .NET Framework never has a tray icon control. But if you use the WinForms tray icon control, you just need a few lines of code to implement the feature,

You might use third party tray icon for WPF as well.

Great Tip! I usually just design an app and other people build my designed, but with this tip (lextm tip) I managed to build the extra functionality in the app in 5 minutes!

Custom window style with minimize animation

I wanted to have a customized window so followed a few tutorials which enable this by setting the window style to none, and then adding the title-bar/restore/minimize/close buttons yourself. The minimize is achieved by simply handling the click event and setting the Window-state to minimized, but this doesn’t show the minimize animation you see on Windows 7, and just instantly hides the window, which feels very odd when used with other windows that do animate, as you tend to feel the application is closing.

So, is there anyway of enabling that animation? .. it seems to be disabled when you change the WindowStyle to none.

Edit : Test code

4 Answers 4

Edited the answer after experimenting a bit.

There are two options: 1. You can change the Style just before minimising and activating the window:

This solution has one limitation — it doesn’t animate the window if you minimise it from the taskbar.

2. Minimise the Window by sending it WM_SYSCOMMAND message with SC_MINIMIZE parameter and changing the border style by hooking into the message ( HwndSource.FromHwnd(m_hWnd).AddHook(WindowProc) ).

Neither of the above methods are great, because they are just hacks. The biggest downside is that you can actually see the border reappearing for a moment when you click the button. I’d like to see what others come up with as I don’t consider this as a good answer myself.

A newer feature of .NET has solved this problem. Leave your WindowStyle=»SingleBorder» or «ThreeDBorder» Leave ResizeMode=»CanResize»

Then add this to the xaml inside the

The window will not have any of the default border, but will still allow resizing and will not cover the task bar when maximized.It will also show the minimize animation as before.

EDIT

This also works for WindowStyle=»None». It allows resizing of a borderless window as if it had a border. It also allows you to set AllowsTransparency=»True» on the window.

If you handle the WM_NCCALCSIZE message by returning 0, handle the WM_NCHITTEST message using either your own code (if you want to do manual hit-testing) or also returning 0, and set the WindowStyle to SingleBorder, the window will function like a borderless window but it will have the animations enabled.

If completely necessary, you may also need to handle the WM_GETMINMAXINFO to fix the maximize size — it clips the borders off because the window’s style is SingleBorder.

I have found another solution, if you need AllowTransparency = True. It is not beautiful, rather a bit hacky. But it is very simple and works great. This uses a empty Window, which is shortly shown when you Minimize/Maximize/Restore your Window, and it has the same position, widht, size and height as your Window. It always has the same Window State as your Window, and it does the animations, which YourWindow lacks because of WindowStyle None and AllowTransparency True. The empty Window has a Window Style SingleBorderWindow and AllowTransparency = false. (by default, so i dont need to set it manually) This is a must or it would not animate. After it has animated, it is completely hidden. You could adjust the look of the Fake Window (BackgroundColor etc. ) to YourWindow if it doesnt look good.

Читайте также:  Что надо отключать при установке windows 10

Then, you place this in your state changed event:

Finally, create this async Task in YourWindowClass. It will wait shortly and then hide the extra Window.

This will remove the hidden hack Window, so if you close the real Window, the hacky animation Window will close too. Else it wouldnt be Visible to the user because its hidden, but it will still be open and so parts of your App are open. This is a behaviour we dont want, so put this as your Closed Event:

Minimize a window in WPF?

How do you minimize a window programmatically when using windows WPF ? I can seem to find a .Resize attribute?

7 Answers 7

set WindowState = WindowState.Minimized;

You are looking for the Window.WindowState property. It is a dependancy property and when changed will set the Window.RestoreBounds property , so you can always restore to the size before the change.

See the enumeration here.

For those who had the same problem: keep in mind that if ShowInTaskbar is set to false, then WindowState.Minimized minimizes the Window into a small window title bar at the bottom left of the desktop — so it’s not really minimized.

A workaround is to set ShowInTaskbar to true, set WindowState to Minimized and then reset the ShowInTaskbar to its old value.

Use the window’s object WindowState property to programmaticly minimise a window.

Setting window state to WindowState.Normal will restore the window to it’s previous WindowsState, size and location.

Window.Normal is a bit of a misnomer. The remarks in the WindowState property and the WindowState Enumeration MSDN articles hint at WindowState.Normal actual functionality and testing confirms it.

will minimize the window for you. But be careful about timing — I accidentally set this in a MouseLeftButtonDown handler (vs MouseLeftButtonUp ), and the window would not restore.

Disable maximize button of WPF window, keeping resizing feature intact

So WPF windows only have four resize mode options: NoResize , CanMinimize , CanResize and CanResizeWithGrip . Unfortunately, the options that enable resizing also enable maximizing the window, and those that don’t are useless to me.

Is there an option to disable the maximize button while keeping the resize feature?

I’d prefer solutions that don’t involve WinAPI stuff.

Читайте также:  Терминал для линукс дебиан

6 Answers 6

WPF does not have the native capability to disable the Maximize button alone, as you can do with WinForms. You will need to resort to a WinAPI call. It’s not scary:

Disabled only Maximize:

In your window’s properties, it will give you a resizable window with no minimize or maximize buttons at the top. It’ll be square looking and the close button is also square, but at least minimize and maximize aren’t there!

P/Invoke Method

The easiest way to call unmanaged code (C++ in this case) from managed (.NET) code is to use the Platform Invocation Services, often also referred to as P/Invoke. You simply provide the compiler with a declaration of the unmanaged function and call it like you would call any other managed method. There is an unmanaged SetWindowLong method that can be used to change an attribute of a specified window. To be able to call this method from your WPF window class using P/Invoke, you simply add the following declaration to the window class:

The DllImport attribute specifies the name of the DLL that contains the method and the extern keyword tells the C# compiler that the method is implemented externally and that it won’t find any implementation or method body for it when compiling the application. The first argument to be passed to the SetWindowLong method is a handle for the window for which you want to disable any of the mentioned buttons. You can get handle for a WPF window by creating an instance of the managed WindowInteropHelper class and access its Handle property in an event handler for the window’s SourceInitialized event. This event is raised when the handle has been completely created. The second argument of the SetWindowLong method specifies the attribute or value of the window to be set, expressed as a constant integer value. When you want to change the window style, you should pass the GWL_STYLE (= -16) constant as the second argument to the method.

Finally the third argument specifies the the replacement value. There are a set of constants that you could use here:

Note however that since you are supposed to pass in a DWORD that specifies the complete value for the “property” specified by the second argument, i.e. the window style in this case, you cannot simply pass any of these constants by themselves as the third argument to the method. There is another GetWindowLong method that retrieves the current value of a specific property – again the GWL_STYLE in this case – and you can then use bitwise operators to get the correct value of the third parameter to pass to the SetWindowLong method. Below is a complete code sample of how you for example could disable the minimize button for a window in WPF:

Disabling the minimize button is then simply a matter of replacing the WS_MAXIMIZEBOX constant with the WS_MINIMIZEBOX

Оцените статью