How can I check whether another application is minimized or not? For instance in a loop like this:
foreach(Process p in processes)
{
// Does a process have a window?
// If so, is it minimized, normal, or maximized
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
private struct WINDOWPLACEMENT {
public int length;
public int flags;
public int showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Rectangle rcNormalPosition;
}
if (p.MainWindowHandle != IntPtr.Zero) {
if (p.MainWindowTitle.Contains("Notepad")) {
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
GetWindowPlacement(p.MainWindowHandle, ref placement);
switch (placement.showCmd) {
case 1:
Console.WriteLine("Normal");
break;
case 2:
Console.WriteLine("Minimized");
break;
case 3:
Console.WriteLine("Maximized");
break;
}
}
}
There is no such thing as a minimized "application." The best alternative would be to check whether the application's Main Window is Iconic (minimized).
IsIconic can be used to check for the iconic state of a window. It will return 1 if a window is minimized. You can call this with process.MainWindowHandle.
If a window is minimized (in Windows Forms at least) both Location.X and Location.Y values are -32000
you can use isZoomed for maximized and isIconic for minimized by injecting user32 dll
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsZoomed(IntPtr hWnd);
Instead of enumerating Processes, you should use the native EnumWindows() function and then call IsIconic.
Related
I'm trying to disable the "fading" animation in windows which happens whenever you open or maximize/minimize a window.
Of course it can be done manually by unticking the checkbox of animate windows when minimizing and maximizing
I'm trying to do this through the SystemParametersInfo
This is my call:
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, bool pvParam,uint fWinIni);
private static UInt32 SPIF_SENDCHANGE = 0x02;
private static UInt32 SPI_SETUIEFFECTS = 0x103F;
public static void Main()
{
bool res= SystemParametersInfo(SPI_SETUIEFFECTS, 0, false, SPIF_SENDCHANGE);
}
result value is always True , so I know the function was successfully called.
But I can't see any results...Windows still keep animating any window I resize.
I compile this as AnyCPU, running as adminstrator on Windows 10.
for #cody gray this is the code (added the ref keyword to the ai paramater and converted the Marshal.Sizeof(ai) to `uint).
[StructLayout(LayoutKind.Sequential)]
public struct ANIMATIONINFO
{
public uint cbSize;
public int iMinAnimate;
};
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SystemParametersInfo(uint uiAction,
uint uiParam,
ref ANIMATIONINFO pvParam,
uint fWinIni);
public static uint SPIF_SENDCHANGE = 0x02;
public static uint SPI_SETANIMATION = 0x0049;
public static void Main()
{
ANIMATIONINFO ai=new ANIMATIONINFO();
ai.cbSize = (uint)Marshal.SizeOf(ai);
ai.iMinAnimate = 0; // turn all animation off
SystemParametersInfo(SPI_SETANIMATION, 0, ref ai, SPIF_SENDCHANGE);
}
One last question-if i would like to get back to the original state-which means i want to activate the aniamtions again,what parameter should be changed in order to do this?
You are not setting the right option when you call SystemParametersInfo. The one that controls the minimize/maximize animation effect (labeled in the UI as "Animate windows when minimizing and maximizing") is SPI_SETANIMATION.
Using it is a bit more complicated, because the pvParam parameter must point to an ANIMATIONINFO structure. It is rather pointless, because the struct only has one meaningful member, but that's the way the API was designed. Presumably, the intent years ago was for this to be a toggle for all of the shell animation effects, but for whatever reason, that didn't end up happening, and separate SPI_* values were used for each of them. You unfortunately picked the wrong one. It is a long list.
Sample code in C#:
[StructLayout(LayoutKind.Sequential)]
public struct ANIMATIONINFO
{
public uint cbSize;
public int iMinAnimate;
};
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SystemParametersInfo(uint uiAction,
uint uiParam,
ref ANIMATIONINFO pvParam,
uint fWinIni);
public static uint SPIF_SENDCHANGE = 0x02;
public static uint SPI_SETANIMATION = 0x0049;
public static void Main()
{
ANIMATIONINFO ai;
ai.cbSize = Marshal.SizeOf(ai);
ai.iMinAnimate = 0; // turn all animation off
SystemParametersInfo(SPI_SETANIMATION, 0, ai, SPIF_SENDCHANGE);
}
Note that this is a global setting, affecting all applications. It is exceedingly rare that an application would ever need to toggle this switch, unless you were making a desktop customization utility. As such, you will require administrative privileges to change this setting.
There is also the DWMWA_TRANSITIONS_FORCEDISABLED flag that you can use with the DwmSetWindowAttribute function to disable transitions when a window is hidden or shown.
The advantage of this is that it is a local solution—you can change the setting only for a single window. But it won't do anything if you are not using DWM (on older versions of Windows), and the minimize/maximize transitions may still be visible. I haven't comprehensively tested the interaction between these two options.
Hans Passant linked you to one of his answers that contains an embedded example of how to call DwmSetWindowAttribute from C#. Here are the relevant bits:
const int DWMWA_TRANSITIONS_FORCEDISABLED = 3;
[DllImport("dwmapi", PreserveSig = true))]
static extern int DwmSetWindowAttribute(IntPtr hWnd, int attr, ref int value, int attrLen);
// in the form's constructor:
// (Note: in addition to checking the OS version for DWM support, you should also check
// that DWM composition is enabled---or at least gracefully handle the function's
// failure when it is not. Instead of S_OK, it will return DWM_E_COMPOSITIONDISABLED.)
if (Environment.OSVersion.Version.Major >= 6)
{
int value = 1; // TRUE to disable
DwmSetWindowAttribute(this.Handle,
DWMWA_TRANSITIONS_FORCEDISABLED,
ref value,
Marshal.SizeOf(value));
}
Is there a way in Windows 8 to detect if the virtual keyboard is visible on screen? I use tabtip.exe
After a brief search on google , I could do something that might help you. The code below exposes a function of the windows API and uses it to know the current state of a given process. Understand state as Minimized , Maximized, Hidden or Normal.
The ProccesIsRunningNotMinimized method returns true if the program is running and is not minimized or hidden.
I do not know if this will help you, but it's a start .
public bool ProccesIsRunningNotMinimized(string exeName)
{
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (p.ProcessName.ToLower() == exeName.ToLower())
{
var placement = GetPlacement(p.MainWindowHandle);
if (placement.showCmd.ToString().ToLower() != "minimized" && placement.showCmd.ToString().ToLower() != "hide")
return true;
}
}
return false;
}
// Get the placement of the target process
private static WINDOWPLACEMENT GetPlacement(IntPtr hwnd)
{
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);
GetWindowPlacement(hwnd, ref placement);
return placement;
}
//Exposes the function of Windows API
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowPlacement(
IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
//Create a struct to receive the data
[Serializable]
[StructLayout(LayoutKind.Sequential)]
internal struct WINDOWPLACEMENT
{
public int length;
public int flags;
public ShowWindowCommands showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Rectangle rcNormalPosition;
}
internal enum ShowWindowCommands : int
{
Hide = 0,
Normal = 1,
Minimized = 2,
Maximized = 3,
}
To check if the VK is running and visible in the screen, do:
if (this.ProccesIsRunningNotMinimized("tabtip"))
{
// do something
}
This code works fine on Windows 7. On windows 8 will not work.
How do I get the window state(maximized, minimized) of another process that's running?
I'd tried by using this:
Process[] procs = Process.GetProcesses();
foreach (Process proc in procs)
{
if (proc.ProcessName == "notepad")
{
MessageBox.Show(proc.StartInfo.WindowStyle.ToString());
}
}
But if process is Maximized or Minimized,it ever returns Normal.
How to fix this?
You’ll need to use Win32 through P/Invoke for checking the state of another window. Here is some sample code:
static void Main(string[] args)
{
Process[] procs = Process.GetProcesses();
foreach (Process proc in procs)
{
if (proc.ProcessName == "notepad")
{
var placement = GetPlacement(proc.MainWindowHandle);
MessageBox.Show(placement.showCmd.ToString());
}
}
}
private static WINDOWPLACEMENT GetPlacement(IntPtr hwnd)
{
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);
GetWindowPlacement(hwnd, ref placement);
return placement;
}
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowPlacement(
IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
[Serializable]
[StructLayout(LayoutKind.Sequential)]
internal struct WINDOWPLACEMENT
{
public int length;
public int flags;
public ShowWindowCommands showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Rectangle rcNormalPosition;
}
internal enum ShowWindowCommands : int
{
Hide = 0,
Normal = 1,
Minimized = 2,
Maximized = 3,
}
Definition courtesy of pinvoke.net.
You're using proc.StartInfo, which is incorrect. It does not reflect the runtime window style of the target process. It is just startup info you can set and can then be passed on to the process when it starts up.
The C# signature is:
[DllImport("user32.dll", SetLastError=true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
You need to use p/invoke and call GetWindowLong(hWnd, GWL_STYLE), and pass proc.MainWindowHandle as the hWnd parameter.
You can check if the window is minimized/maximized by doing something like:
int style = GetWindowLong(proc.MainWindowHandle, GWL_STYLE);
if((style & WS_MAXIMIZE) == WS_MAXIMIZE)
{
//It's maximized
}
else if((style & WS_MINIMIZE) == WS_MINIMIZE)
{
//It's minimized
}
NOTE: The values for the flags (WS_MINIMIZE, etc), can be found in this page: http://www.pinvoke.net/default.aspx/user32.getwindowlong
Thanks to Kakashi for pointing our the error in testing the result.
In Windows PowerShell you can do this by following code:
Add-Type -AssemblyName UIAutomationClient
$prList = Get-Process -Name "<ProcessNamesWhichHaveWindow>"
$prList | % {
try {
$ae = [System.Windows.Automation.AutomationElement]::FromHandle($_.MainWindowHandle)
$wp = $ae.GetCurrentPattern([System.Windows.Automation.WindowPatternIdentifiers]::Pattern)
echo "Window title: $($_.MainWindowTitle)"
echo "Window visual state: $($wp.Current.WindowVisualState)"
}
catch { }
}
Two Window States (maximized / minimized) can be gotten by calling WinAPI IsIconic() / IsZoomed() like this:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, ShowWindowCommands cmdShow);
if (IsIconic(_helpWindow.MainWindowHandle)) {
ShowWindowAsync(_helpWindow.MainWindowHandle, ShowWindowCommands.SW_RESTORE);
}
Definition of enum ShowWindowCommands and other functions were taken from www.PInvoke.net
I am looking for a program that measures system idle time on Windows. I have found many codes that do this. For example,
http://www.codeproject.com/KB/cs/GetIdleTimeWithCS.aspx
However, I also want to take into account user watching movies, videos, etc. That time no input is given, but still the system is not idle.
Is there anyway to do this?
This function detects if a process is running in fullscreen in forground, and returns name of the process if found so:
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
private static extern bool
GetWindowPlacement(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;
}
public string FullscreenProcess()
{
IntPtr foreWindow = GetForegroundWindow();
// get the placement
WINDOWPLACEMENT forePlacement = new WINDOWPLACEMENT();
forePlacement.length = Marshal.SizeOf(forePlacement);
GetWindowPlacement(foreWindow, ref forePlacement);
if (forePlacement.rcNormalPosition.top == 0 && forePlacement.rcNormalPosition.left == 0 && forePlacement.rcNormalPosition.right == Screen.PrimaryScreen.Bounds.Width && forePlacement.rcNormalPosition.bottom == Screen.PrimaryScreen.Bounds.Height)
{
uint processID;
GetWindowThreadProcessId(foreWindow, out processID);
Process proc = Process.GetProcessById((int)processID);
return proc.ProcessName;
}
return null;
}
After this, we just need to match the returned process name with a set of popular media players or other processes.
Limitation is that we have assumed user plays in fullscreen.
You can get the idle time for computer using this, however your problem is how to know weather a movie is playing, you can check the processes that is currently running on the computer System.Diagnostics.Process.GetProcesses(), and then check if one of them is a known movie player. However I don't think this will achieve what you locking for either, because even if you find out that for example a Gom player is running, you still don't know if it running a video or audio or just opened without any thing actually playing...
In case you watch videos in fullscreen:
You could use code like this do determine if there is an app running in fullscreen:
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int Left;
public int Top;
public int Width;
public int Height;
}
static bool IsBigWindowRunning()
{
foreach (Process proc in Process.GetProcesses())
{
RECT rect;
var success = GetWindowRect(proc.MainWindowHandle, out rect);
if(success && (r.Left + r.Width) >= Screen.PrimaryScreen.WorkingArea.Width)
{
return true;
}
}
return false;
}
I haven't tested it well and i would definetely play around with this line, add some limits:
if((r.Left + r.Width) >= Screen.PrimaryScreen.WorkingArea.Width)
Good luck!
I need to disable the Mouse Clicks, Mouse movement for a specific windows for a Kiosk application. Is it Feasible in C# ?
I have removed the menu bar and title bar of a specific window, will that be a starting point to achieve the above requirement ? How can i achieve this requirement.
The code for removing the menu bar and title bar using window handle :
#region Constants
//Finds a window by class name
[DllImport("USER32.DLL")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
//Sets a window to be a child window of another window
[DllImport("USER32.DLL")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
//Sets window attributes
[DllImport("USER32.DLL")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
//Gets window attributes
[DllImport("USER32.DLL")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr GetMenu(IntPtr hWnd);
[DllImport("user32.dll")]
static extern int GetMenuItemCount(IntPtr hMenu);
[DllImport("user32.dll")]
static extern bool DrawMenuBar(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
//assorted constants needed
public static uint MF_BYPOSITION = 0x400;
public static uint MF_REMOVE = 0x1000;
public static int GWL_STYLE = -16;
public static int WS_CHILD = 0x40000000; //child window
public static int WS_BORDER = 0x00800000; //window with border
public static int WS_DLGFRAME = 0x00400000; //window with double border but no title
public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar
public static int WS_SYSMENU = 0x00080000; //window menu
#endregion
public static void WindowsReStyle()
{
Process[] Procs = Process.GetProcesses();
foreach (Process proc in Procs)
{
if (proc.ProcessName.StartsWith("notepad"))
{
IntPtr pFoundWindow = proc.MainWindowHandle;
int style = GetWindowLong(pFoundWindow, GWL_STYLE);
//get menu
IntPtr HMENU = GetMenu(proc.MainWindowHandle);
//get item count
int count = GetMenuItemCount(HMENU);
//loop & remove
for (int i = 0; i < count; i++)
RemoveMenu(HMENU, 0, (MF_BYPOSITION | MF_REMOVE));
//force a redraw
DrawMenuBar(proc.MainWindowHandle);
SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_SYSMENU));
SetWindowLong(pFoundWindow, GWL_STYLE, (style & ~WS_CAPTION));
}
}
}
It sounds like you are looking for EnableWindow. The description is:
Enables or disables mouse and keyboard
input to the specified window or
control. When input is disabled, the
window does not receive input such as
mouse clicks and key presses. When
input is enabled, the window receives
all input.
So you would add
[DllImport("user32.dll")]
static extern bool EnableWindow(IntPtr hWnd, bool enable);
and
EnableWindow(pFoundWindow, false);
This is equivalent to setting the Enabled property on a Windows Forms Form/Control.
You can try to override the WndProc and check for WM_MOUSE* events there. If you dont call the base WndProc for these handled events, it should work.
A point to consider here is that since yours is a kiosk application, will your special mouse handling lead to any problems for Touch screen.
To prevent keyboard input in a window in another process, you need to make a keyboard hook.
You can then check GetForegroundWindow() and suppress the input.