hide tiles of Windows 8.1 in c# programmatically - c#

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 { }
}
}

Related

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;
}));
});
}

Get the handle of a window that doesn't have a name [duplicate]

This question already has answers here:
Get handle of a window that has no title.. (C#)
(5 answers)
Closed 7 years ago.
I am using Findwindow() to get the IntPtr of a window name like this:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
IntPtr handle = FindWindow(null, WindowName);
if (handle != IntPtr.Zero)
{
RECT windowRectangle = GetWindowRect(handle);
}
This works when the windowname is not empty but how can i do it when the window doesn't have a name?
Using Microsoft Spy++, you can easily get advanced information on any window you have open. Open the utility, click Find, and locate the window you need to access programmatically by dragging the cursor from the search box on to the required open window. The data will appear in the search window:
You can use the Class: field value to access the window that has no title:
To access the RStudio window by class name, I'd use
var handle = FindWindow("Qt5QWindowIcon", null);
if (handle != IntPtr.Zero)
{
// TO DO
}
Refer to this code sample originally posted by Jerry Fernholz in answer to a similar question:
...
using System.Runtime.InteropServices;
using System.Diagnostics;
...
public class foo()
{
...
[DllImport ("user32")]
internal static extern int GetWindowText (int hWnd, String text, int nMaxCount);
[DllImport ("user32.dll")]
public static extern int GetWindowTextLength (int hWnd);
[DllImport ("user32.dll")]
public static extern int FindWindow (String text, String class_name);
[DllImport ("user32.dll")]
public static extern int FindWindowEx (int parent, int start, String class_name);
[DllImport ("user32.dll")]
public static extern int GetWindow (int parent, uint cmd);
public List<int> FindTitlelessWindows()
{
List<int> titleless = new List<int> ();
Process [] procs = Process.GetProcesses ();
IntPtr hWnd;
foreach (Process proc in procs)
{
hWnd = proc.MainWindowHandle;
if (hWnd != IntPtr.Zero)
{
TraverseHierarchy (hWnd.ToInt32 (), 0, titleless);
}
}
foreach (int i in titleless)
{
System.Console.WriteLine (i);
}
return titleless;
}
public void TraverseHierarchy (int parent, int child, List<int> titleless)
{
String text = "";
GetWindowText (parent, text, GetWindowTextLength (parent));
if (String.IsNullOrEmpty (text))
{
titleless.Add (parent);
}
TraverseChildern (parent, titleless);
TraversePeers (parent, child, titleless);
}
public void TraverseChildern(int handle, List<int> titleless)
{
// First traverse child windows
const uint GW_CHILD = 0x05;
int child = GetWindow (handle, GW_CHILD);
if (0 != child)
{
TraverseHierarchy (child, 0, titleless);
}
}
public void TraversePeers(int parent, int start, List<int> titleless)
{
// Next traverse peers
int peer = FindWindowEx(parent, start, "");
if (0 != peer)
{
TraverseHierarchy (parent, peer, titleless);
}
}
}

SwitchToThisWindow [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I bring my application window to the front?
I am having an issue with SwitchToThisWindow
using System;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace BringToFront
{
public partial class Form1 : Form
{
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(String className, String windowName);
[DllImport("user32.dll", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool turnOn);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
IntPtr activeWindowHandle = GetForegroundWindow();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
if (!checkBox1.Checked)
bringToFront(comboBox1.SelectedItem.ToString());
else
timer1.Enabled = true;
}
catch
{
MessageBox.Show("Please choose a Process Name");
}
}
public static void bringToFront(string title)
{
IntPtr handle = FindWindow(null, title);
if (handle == IntPtr.Zero)
{
return;
}
SwitchToThisWindow(handle, true);
}
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear();
Process[] process = Process.GetProcesses();
foreach (Process processes in process)
{
if (!String.IsNullOrEmpty(processes.MainWindowTitle))
comboBox1.Items.Add(processes.MainWindowTitle.ToString());
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Process process = Process.GetCurrentProcess();
string title = process.ProcessName.ToString();
IntPtr handle = FindWindow(null, title);
if (activeWindowHandle != handle)
bringToFront(comboBox1.SelectedItem.ToString());
if (!checkBox1.Checked)
timer1.Enabled = false;
}
}
}
As you can see I'm trying to bring the process that is selected to the front and keep it in the front by doing a timer every 5 seconds and rebringing it to the front. It works perfectly when running the application through Microsoft Visual Studios, but when I run the program as a standalone, it works how every other function like this does and only makes it flash in taskbar instead of bringing it to the front.
Why are the permissions different and is there anyway to fix this?
Via the solution by #ReedCopsy here, I suggest to make the selected handle TopMost after you've switched to that window. Using this solution, no new app can become top over the selected window.
Add the following to your code:
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_SHOWWINDOW = 0x0040;
and change your bringToFront by adding a call to SetWindowPos:
public static void bringToFront(string title)
{
IntPtr handle = FindWindow(null, title);
if (handle == IntPtr.Zero)
{
return;
}
SwitchToThisWindow(handle, true);
// Call this way:
SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}

Hide or Disable System Tray

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.

How do I create a message-only window from windows forms?

I'm trying to create a message-only window to receive window messages from an MFC library class, within a winforms application.
I've tried subclassing NativeWindow, and in the constructor requesting a window handle like this:
CreateParams cp = new CreateParams();
cp.Parent = (IntPtr)HWND_MESSAGE;
this.CreateHandle(cp);
but I get a Win32Exception thrown with the message "Error creating window handle". How do I create a message-only window from windows forms? Is using NativeWindow the right approach?
Try that :
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
static IntPtr HWND_MESSAGE = new IntPtr(-3);
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
SetParent(this.Handle, HWND_MESSAGE);
}
I know this is 7.5 years old, but just in case anyone finds this, I thought I would respond. I used Microsoft's TimerNativeWindow code and removed the timer functionality. I ended up using this approach:
public class MyNativeWindow : NativeWindow
{
private readonly string _caption;
private const int WmClose = 0x0010;
[SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
private static readonly HandleRef HwndMessage = new HandleRef(null, new IntPtr(-3));
[DllImport("user32.dll", CharSet = CharSet.Auto)]
[ResourceExposure(ResourceScope.None)]
private static extern IntPtr PostMessage(HandleRef hwnd, int msg, int wparam, int lparam);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[ResourceExposure(ResourceScope.Process)]
private static extern int GetWindowThreadProcessId(HandleRef hWnd, out int lpdwProcessId);
[DllImport("kernel32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[ResourceExposure(ResourceScope.Process)]
private static extern int GetCurrentThreadId();
public MyNativeWindow(string caption)
{
_caption = caption;
}
public bool CreateWindow()
{
if (Handle == IntPtr.Zero)
{
CreateHandle(new CreateParams
{
Style = 0,
ExStyle = 0,
ClassStyle = 0,
Caption = _caption,
Parent = (IntPtr)HwndMessage
});
}
return Handle != IntPtr.Zero;
}
public void DestroyWindow()
{
DestroyWindow(true, IntPtr.Zero);
}
private bool GetInvokeRequired(IntPtr hWnd)
{
if (hWnd == IntPtr.Zero) return false;
int pid;
var hwndThread = GetWindowThreadProcessId(new HandleRef(this, hWnd), out pid);
var currentThread = GetCurrentThreadId();
return (hwndThread != currentThread);
}
private void DestroyWindow(bool destroyHwnd, IntPtr hWnd)
{
if (hWnd == IntPtr.Zero)
{
hWnd = Handle;
}
if (GetInvokeRequired(hWnd))
{
PostMessage(new HandleRef(this, hWnd), WmClose, 0, 0);
return;
}
lock (this)
{
if (destroyHwnd)
{
base.DestroyHandle();
}
}
}
public override void DestroyHandle()
{
DestroyWindow(false, IntPtr.Zero);
base.DestroyHandle();
}
}
I believe that you'll need to also specify a window class.
I fear that you must derive from a Form, and force the window invisible.
Another approach (in the case the class library is modifiable) is to run a message pump without a window (see Application.Run and Application.AddMessageFilter, or if you prefer pinvokes using PeekMessage & Co).
In this case you can send messages using PostThreadMessage by having the thread id which as run Application.Run, but actually you cannot synch with the application message pump thread because it doesn't wait message acknowledge.

Categories

Resources