Hide or Disable System Tray - c#

I am working in .Net Windows Application. My requirement is, through my code i want to disable or hide the SYSTEM TRAY...
I am not having any idea regarding this..Kindly guide me.

If you need to show/hide Windows taskbar you can use this:
public class Taskbar
{
[DllImport( "user32.dll" )]
private static extern int FindWindow( string className, string windowText );
[DllImport( "user32.dll" )]
private static extern int ShowWindow( int hwnd, int command );
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;
protected static int Handle
{
get { return FindWindow( "Shell_TrayWnd", "" ); }
}
private Taskbar() { }
public static void Show()
{
ShowWindow( Handle, SW_SHOW );
}
public static void Hide()
{
ShowWindow( Handle, SW_HIDE );
}
}
So you can use
Taskbar.Show() to show taskbar and
Taskbar.Hide() to hide it.

Related

how can minimize and maximize window folders in C#

I'm trying to minimize window folders and applications with voice ( speech recognition ) and this is my code :
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
private struct POINTAPI
{
public int x;
public int y;
}
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
private struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public POINTAPI ptMinPosition;
public POINTAPI ptMaxPosition;
public RECT rcNormalPosition;
}
private void button1_Click(object sender, EventArgs e)
{
WindowAction_MinimizeNotepad();
}
void WindowAction_MinimizeNotepad()
{
System.IntPtr app_hwnd;
WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
app_hwnd = FindWindow("chrome", null);
GetWindowPlacement(app_hwnd, ref wp);
wp.showCmd = 2;
SetWindowPlacement(app_hwnd, ref wp);
}
i know how to use that code with button but i don't know how to use it with speech recognition so, how can i minimize windows with voice ? thanks.
First You need to Register for speech recognition event like:
On Page load
// Register handler for the SpeechRecognized event.
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(srEvent_SpeechRecognized);
Then create a speech recognition event handler, Like :
void srEvent_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
You may want to have a look at Microsoft.Speech.Recognition.SpeechRecognitionEngine.
Using this you can subscribe to events and proceed with whatever action you want to perform on those events.
Get more information at https://msdn.microsoft.com/en-us/library/hh378426(v=office.14).aspx

Make WPF Window Immune to Show Desktop (Prevent Hide)

I have a WPF window that is supposed to be a "desktop gadget".
My users are asking for a way to prevent it from disappearing when they hit "Show Desktop".
Making the window always topmost works but some of my users do not want the window always on top.
Short of running a timer every x seconds to activate the window, is there a proper way to achieve this?
I ended up developing my own solution.
I scoured the internet for weeks trying to find an answer so I'm kind of proud of this one.
So what we do is use pinvoke to create a hook for the EVENT_SYSTEM_FOREGROUND window event.
This event triggers whenever the foreground window is changed.
Now what I noticed is when the "Show Desktop" command is issued, the WorkerW window class becomes foreground.
Note this WorkerW window is not the desktop and I confirmed the hwnd of this WorkerW window is not the Desktop hwnd.
So what we do is whenever the WorkerW window becomes the foreground, we set our "WPF Gadget Window" to be topmost!
Whenever a window other the WorkerW window becomes the foreground, we remove topmost from our "WPF Gadget Window".
If you want to take it a step further, you can uncomment out the part where I check if the new foreground window is also "PROGMAN", which is the Desktop window.
However, this will lead to your window becoming topmost if the user clicks their desktop on a different monitor. In my case, I did not want this behavior, but I figured some of you might.
Confirmed to work in Windows 10. Should work in older versions of Windows.
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
namespace YourNamespace
{
internal static class NativeMethods
{
[DllImport("user32.dll")]
internal static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, ShowDesktop.WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
[DllImport("user32.dll")]
internal static extern bool UnhookWinEvent(IntPtr hWinEventHook);
[DllImport("user32.dll")]
internal static extern int GetClassName(IntPtr hwnd, StringBuilder name, int count);
}
public static class ShowDesktop
{
private const uint WINEVENT_OUTOFCONTEXT = 0u;
private const uint EVENT_SYSTEM_FOREGROUND = 3u;
private const string WORKERW = "WorkerW";
private const string PROGMAN = "Progman";
public static void AddHook(Window window)
{
if (IsHooked)
{
return;
}
IsHooked = true;
_delegate = new WinEventDelegate(WinEventHook);
_hookIntPtr = NativeMethods.SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _delegate, 0, 0, WINEVENT_OUTOFCONTEXT);
_window = window;
}
public static void RemoveHook()
{
if (!IsHooked)
{
return;
}
IsHooked = false;
NativeMethods.UnhookWinEvent(_hookIntPtr.Value);
_delegate = null;
_hookIntPtr = null;
_window = null;
}
private static string GetWindowClass(IntPtr hwnd)
{
StringBuilder _sb = new StringBuilder(32);
NativeMethods.GetClassName(hwnd, _sb, _sb.Capacity);
return _sb.ToString();
}
internal delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
private static void WinEventHook(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
if (eventType == EVENT_SYSTEM_FOREGROUND)
{
string _class = GetWindowClass(hwnd);
if (string.Equals(_class, WORKERW, StringComparison.Ordinal) /*|| string.Equals(_class, PROGMAN, StringComparison.Ordinal)*/ )
{
_window.Topmost = true;
}
else
{
_window.Topmost = false;
}
}
}
public static bool IsHooked { get; private set; } = false;
private static IntPtr? _hookIntPtr { get; set; }
private static WinEventDelegate _delegate { get; set; }
private static Window _window { get; set; }
}
}
You can use "StateChanged" event of the window. It fires when "WindowState" property changes. You can use this event and maximize the window when the state changed to minimized.
UPDATE
Try this code:
private async void Window_StateChanged_1(object sender, EventArgs e)
{
await MaximizeWindow(this);
}
public Task MaximizeWindow(Window window)
{
return Task.Factory.StartNew(() =>
{
this.Dispatcher.Invoke((Action)(() =>
{
Thread.Sleep(100);
window.WindowState = System.Windows.WindowState.Maximized;
}));
});
}

Hide TaskBar in WinForms Application [duplicate]

This question already has answers here:
How do I make a WinForms app go Full Screen
(10 answers)
Closed 9 years ago.
How can I hide the Windows taskbar when I run my C# WinForms application?
I tried some code, but it opens in maximized view with the taskbar.
Do you have any sample code or suggestions?
Just add this class into your project .it works as you expected.
using System;
using System.Runtime.InteropServices;
public class Taskbar
{
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
[DllImport("user32.dll")]
public static extern int FindWindowEx(int parentHandle, int childAfter, string className, int windowTitle);
[DllImport("user32.dll")]
private static extern int GetDesktopWindow();
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;
protected static int Handle
{
get
{
return FindWindow("Shell_TrayWnd", "");
}
}
protected static int HandleOfStartButton
{
get
{
int handleOfDesktop = GetDesktopWindow();
int handleOfStartButton = FindWindowEx(handleOfDesktop, 0, "button", 0);
return handleOfStartButton;
}
}
private Taskbar()
{
// hide ctor
}
public static void Show()
{
ShowWindow(Handle, SW_SHOW);
ShowWindow(HandleOfStartButton, SW_SHOW);
}
public static void Hide()
{
ShowWindow(Handle, SW_HIDE);
ShowWindow(HandleOfStartButton, SW_HIDE);
}
}
USAGE:
Taskbar.Hide();
You need to set WinForms application from property like below
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
You need to use P/INVOKE
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;
int hwnd = FindWindow("Shell_TrayWnd","");
ShowWindow(hwnd,SW_HIDE);
I hope that helps

hide tiles of Windows 8.1 in c# programmatically

I have to extend a terminal server software in order to work with windows 8.1.
The scenario is a follows:
Two PCs: on one runs the client software on the other one runs the server.
The operating system of the server is Windows 8.1
When the user presses a button on the client PC it opens an RDP connection via virtual channel to the server PC.
There has to be a logon and the tiles have to be hidden and also the server part of the software hast to be startet.
In order to hide the normal desktop under earlier versions of windows we used the following commands:
// For Windows Vista and Windows 7 hide the Status-Bar and all Desktop-Icons
int a_hWndTaskBar = FindWindow( "Shell_TrayWnd", null );
int a_hWndStart = FindWindow( "Button", "Start" );
int a_hWndDesktop = FindWindow( "Progman", null );
bool a_bResult = false;
try
{
a_bResult = SetWindowPos( a_hWndTaskBar, 0, 0, 0, 0, 0, SWP_HIDEWINDOW );
a_bResult = SetWindowPos( a_hWndStart, 0, 0, 0, 0, 0, SWP_HIDEWINDOW );
a_bResult = ShowWindow( a_hWndDesktop, SW_HIDE );
}
catch( Exception e )
{
MessageBox.Show( e.Message );
}
What do I have to do in order to achieve this with windows 8.1?
Regards
Markus
Here: Works for me:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Constants.UI
{
public class Taskbar
{
[DllImport("user32.dll")]// For Windows Mobile, replace user32.dll with coredll.dll
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;
protected static int Handle
{
get
{
return HandlePtr.ToInt32();
}
}
protected static IntPtr HandlePtr
{
get
{
return FindWindow("Shell_TrayWnd", "");
}
}
protected static int StartHandle
{
get
{
int hStart = FindWindow("Button", "Start").ToInt32();
if (hStart == 0)
{
hStart = FindWindowEx(HandlePtr, IntPtr.Zero, "Start", null).ToInt32(); //windows 8
}
return hStart;
}
}
private Taskbar()
{
// hide ctor
}
static object lockAccess = new object();
public static void Show()
{
try
{
lock (lockAccess)
{
ShowWindow(Handle, SW_SHOW);
ShowWindow(StartHandle, SW_SHOW);
}
}
catch { }
}
public static void Hide()
{
try
{
lock (lockAccess)
{
ShowWindow(Handle, SW_HIDE);
ShowWindow(StartHandle, SW_HIDE);
}
}
catch { }
}
}

How to prevent a window from minimizing?

How can I prevent WPF window from minimizing when users clicks on show desktop button?
This link will help you : Get the minimize box click of a WPF window
you need to catch the event and handle it yourself.
Edit : This method will alert you once the state is changed, so it might not be the "best" solution but it could work.
Windows are not minimized when "Show Desktop" is issued. Instead the "WorkerW" and "Desktop" windows are brought to the foreground.
I ended up developing my own solution.
I scoured the internet for weeks trying to find an answer so I'm kind of proud of this one.
So what we do is use pinvoke to create a hook for the EVENT_SYSTEM_FOREGROUND window event.
This event triggers whenever the foreground window is changed.
Now what I noticed is when the "Show Desktop" command is issued, the WorkerW window class becomes foreground.
Note this WorkerW window is not the desktop and I confirmed the hwnd of this WorkerW window is not the Desktop hwnd.
So what we do is whenever the WorkerW window becomes the foreground, we set our "WPF Gadget Window" to be topmost!
Whenever a window other the WorkerW window becomes the foreground, we remove topmost from our "WPF Gadget Window".
If you want to take it a step further, you can uncomment out the part where I check if the new foreground window is also "PROGMAN", which is the Desktop window.
However, this will lead to your window becoming topmost if the user clicks their desktop on a different monitor. In my case, I did not want this behavior, but I figured some of you might.
Confirmed to work in Windows 10. Should work in older versions of Windows.
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
namespace YourNamespace
{
internal static class NativeMethods
{
[DllImport("user32.dll")]
internal static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, ShowDesktop.WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
[DllImport("user32.dll")]
internal static extern bool UnhookWinEvent(IntPtr hWinEventHook);
[DllImport("user32.dll")]
internal static extern int GetClassName(IntPtr hwnd, StringBuilder name, int count);
}
public static class ShowDesktop
{
private const uint WINEVENT_OUTOFCONTEXT = 0u;
private const uint EVENT_SYSTEM_FOREGROUND = 3u;
private const string WORKERW = "WorkerW";
private const string PROGMAN = "Progman";
public static void AddHook(Window window)
{
if (IsHooked)
{
return;
}
IsHooked = true;
_delegate = new WinEventDelegate(WinEventHook);
_hookIntPtr = NativeMethods.SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _delegate, 0, 0, WINEVENT_OUTOFCONTEXT);
_window = window;
}
public static void RemoveHook()
{
if (!IsHooked)
{
return;
}
IsHooked = false;
NativeMethods.UnhookWinEvent(_hookIntPtr.Value);
_delegate = null;
_hookIntPtr = null;
_window = null;
}
private static string GetWindowClass(IntPtr hwnd)
{
StringBuilder _sb = new StringBuilder(32);
NativeMethods.GetClassName(hwnd, _sb, _sb.Capacity);
return _sb.ToString();
}
internal delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
private static void WinEventHook(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
if (eventType == EVENT_SYSTEM_FOREGROUND)
{
string _class = GetWindowClass(hwnd);
if (string.Equals(_class, WORKERW, StringComparison.Ordinal) /*|| string.Equals(_class, PROGMAN, StringComparison.Ordinal)*/ )
{
_window.Topmost = true;
}
else
{
_window.Topmost = false;
}
}
}
public static bool IsHooked { get; private set; } = false;
private static IntPtr? _hookIntPtr { get; set; }
private static WinEventDelegate _delegate { get; set; }
private static Window _window { get; set; }
}
}
You can change your window's parent to not be affected by Show Desktop. (as stated here: Window "on desktop")
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += MainWindowLoaded;
}
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
private void MainWindowLoaded(object sender, RoutedEventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
var ProgmanHwnd = FindWindowEx(FindWindowEx(FindWindow("Progman", "Program Manager"), IntPtr.Zero, "SHELLDLL_DefView",""), IntPtr.Zero,"SysListView32", "FolderView");
SetParent(hwnd, ProgmanHwnd);
}
}

Categories

Resources