Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
3.27/5 (3 votes)
Hi all,

Is there is a way to block minimizing of my application to system tray. I have to develop an application that will allow the user to view the prupose of that Application, which mean the application will remain open in my desktop all time for that it should not be minimized. But he can set order of that control as like windows Media player or like Task Manager. is there is any option to this in WPF using C#. let me know any link you have.
Posted

Hi,

App.xaml:
C#
<Window x:Class="WpfApplicationNonMinimizable.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Topmost="True" 
        ResizeMode="NoResize">
    <Grid>
        
    </Grid>
</Window>


App.xaml.cs:
C#
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace WpfApplicationNonMinimizable
{
    public partial class MainWindow : Window
    {
        public const int SW_SHOWNORMAL = 1;
        public const int SW_SHOWMINIMIZED = 2;

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

        [DllImport("user32.dll")]
        public static extern bool SetWindowPlacement(IntPtr hWnd,
                           ref WINDOWPLACEMENT lpwndpl);

        public struct POINTAPI
        {
            public int x;
            public int y;
        }

        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        public struct WINDOWPLACEMENT
        {
            public int length;
            public int flags;
            public int showCmd;
            public POINTAPI ptMinPosition;
            public POINTAPI ptMaxPosition;
            public RECT rcNormalPosition;
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            try
            {
                System.IntPtr app_hwnd = new WindowInteropHelper(this).Handle;
                WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
                GetWindowPlacement(app_hwnd, ref wp);
                wp.showCmd = (wp.showCmd == SW_SHOWMINIMIZED ? SW_SHOWNORMAL : wp.showCmd);                
                SetWindowPlacement(app_hwnd, ref wp);
            }
            catch
            {
            }
        }
    }
}


I was inspired by the following article:
http://www.codeproject.com/Articles/29560/Set-Window-Action-Minimize-Maximize

and of course the source code of Snoop.
https://snoopwpf.codeplex.com/

If this works for you, don't forget to vote.

Happy Coding,
Stephan
 
Share this answer
 
v7
Comments
Gold$Coin 27-Nov-14 21:57pm    
actually i was not as clear in my question because minimize includes when we click on show desktop button in right bottom corner. is there is any solution ? it should look like http://snoopwpf.codeplex.com/ tool
TheRealSteveJudge 28-Nov-14 4:17am    
An answer can only be as good as the question asked.
Meanwhile I found a working solution for you.
Please have look at the updated solution.
Gold$Coin 30-Nov-14 22:36pm    
ofcourse, that my bad i forgot to included the Show desktop option. sorry about that.
TheRealSteveJudge 1-Dec-14 2:50am    
No Problem! Your're welcome.
I hope my solution works as you expect.
Gold$Coin 2-Dec-14 6:02am    
yup thanks a lot :)
Hi,

Use the window's object Window State property. And try setting it to Maximized.
And use ResizeMode so deny resizing.

I havent used this, but thought of suggesting. It might work.
 
Share this answer
 
Comments
TheRealSteveJudge 27-Nov-14 4:20am    
My vote of 1:

Some suggestions into the right direction
but actually confusing.
Gold$Coin 27-Nov-14 21:56pm    
actually i was not as clear in my question because minimize includes when we click on show desktop button in right bottom corner. is there is any solution ? it should look like http://snoopwpf.codeplex.com/ tool
Hi Set Form property ResizeMode=>NoResize and WindowStyle=>ToolWindow
 
Share this answer
 
Comments
TheRealSteveJudge 27-Nov-14 4:18am    
My vote of 4.

ResizeMode="NoResize" is correct.
whereas WindowStyle="ToolWindow" just gives a smaller title bar.
Gold$Coin 27-Nov-14 21:57pm    
actually i was not as clear in my question because minimize includes when we click on show desktop button in right bottom corner. is there is any solution ? it should look like http://snoopwpf.codeplex.com/ tool

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900