How to get Windows explorer’s position using C#?
-
Hello, I am developing an application which triggers when there is a windows explorer window in foreground. My application triggers a window (form) which will be placed on screen near the opened windows explorer (Planning to keep it just below the search option). I am not opening the windows explorer in my code, instead user will open windows explorer and I will trigger my application whenever there is a windows explorer in foreground. Here is my code,
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;public class Form1
{private void Form1\_Load(System.Object sender, System.EventArgs e) { Timer1.Interval = 100; // timer to watch for foreground windows Timer1.Start(); } private void Timer1\_Tick(System.Object sender, System.EventArgs e) { dynamic hWnd = GetForegroundWindow(); // get window handle of foreground window // a Win7 explorer window is in foreground. if (ClassName(hWnd) == "CabinetWClass") { Timer1.Stop(); // stop this timer! using (Form2 f2 = new Form2()) { // show a popup window f2.ShowDialog(); } } } #region "API" \[DllImport("user32.dll", CharSet = CharSet.Auto)\] private static extern Int32 GetClassName(System.IntPtr hWnd, System.Text.StringBuilder lpClassName, Int32 nMaxCount); \[DllImport("user32.dll", CharSet = CharSet.Auto)\] private static extern IntPtr GetForegroundWindow(); private string ClassName(IntPtr hWnd) { StringBuilder sb = new StringBuilder(256); GetClassName(hWnd, sb, 256); return sb.ToString; } public Form1() { Load += Form1\_Load; } #endregion
}
But I am not getting anything to get the window position of foreground "windows explorer" window. Is there any way to read the position of current foreground “Windows Explorer” window using .net?
-
Hello, I am developing an application which triggers when there is a windows explorer window in foreground. My application triggers a window (form) which will be placed on screen near the opened windows explorer (Planning to keep it just below the search option). I am not opening the windows explorer in my code, instead user will open windows explorer and I will trigger my application whenever there is a windows explorer in foreground. Here is my code,
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;public class Form1
{private void Form1\_Load(System.Object sender, System.EventArgs e) { Timer1.Interval = 100; // timer to watch for foreground windows Timer1.Start(); } private void Timer1\_Tick(System.Object sender, System.EventArgs e) { dynamic hWnd = GetForegroundWindow(); // get window handle of foreground window // a Win7 explorer window is in foreground. if (ClassName(hWnd) == "CabinetWClass") { Timer1.Stop(); // stop this timer! using (Form2 f2 = new Form2()) { // show a popup window f2.ShowDialog(); } } } #region "API" \[DllImport("user32.dll", CharSet = CharSet.Auto)\] private static extern Int32 GetClassName(System.IntPtr hWnd, System.Text.StringBuilder lpClassName, Int32 nMaxCount); \[DllImport("user32.dll", CharSet = CharSet.Auto)\] private static extern IntPtr GetForegroundWindow(); private string ClassName(IntPtr hWnd) { StringBuilder sb = new StringBuilder(256); GetClassName(hWnd, sb, 256); return sb.ToString; } public Form1() { Load += Form1\_Load; } #endregion
}
But I am not getting anything to get the window position of foreground "windows explorer" window. Is there any way to read the position of current foreground “Windows Explorer” window using .net?
This probably isn't going to work quite the way you want, due to the way Windows Explorer works - it doesn't always open a new top-level process for an instance. At the moment I have two Windows Explorer processes: one with a single Main window (and the title is the folder name) and one with two sub windows (and it's title is an empty string). How I got there? Depends on how you open the window. The "one off" one was opened by an app, the other two were opened from the task bar... But you probably want to start here:
Process\[\] all = Process.GetProcesses(); foreach (Process p in all) { string name = p.ProcessName; IntPtr hWnd = p.MainWindowHandle; string title = p.MainWindowTitle; if (p.ProcessName.Contains("explorer")) { Console.WriteLine("{0}:{1}", p.ProcessName, p.MainWindowTitle); } }
The handle should give you the location of the main window (but you may need to do some delving to find "combined" windows)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
This probably isn't going to work quite the way you want, due to the way Windows Explorer works - it doesn't always open a new top-level process for an instance. At the moment I have two Windows Explorer processes: one with a single Main window (and the title is the folder name) and one with two sub windows (and it's title is an empty string). How I got there? Depends on how you open the window. The "one off" one was opened by an app, the other two were opened from the task bar... But you probably want to start here:
Process\[\] all = Process.GetProcesses(); foreach (Process p in all) { string name = p.ProcessName; IntPtr hWnd = p.MainWindowHandle; string title = p.MainWindowTitle; if (p.ProcessName.Contains("explorer")) { Console.WriteLine("{0}:{1}", p.ProcessName, p.MainWindowTitle); } }
The handle should give you the location of the main window (but you may need to do some delving to find "combined" windows)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
sorry for the confusion, I have edited the question. I facing trouble in getting window position of windows explorer.
-
Hello, I am developing an application which triggers when there is a windows explorer window in foreground. My application triggers a window (form) which will be placed on screen near the opened windows explorer (Planning to keep it just below the search option). I am not opening the windows explorer in my code, instead user will open windows explorer and I will trigger my application whenever there is a windows explorer in foreground. Here is my code,
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;public class Form1
{private void Form1\_Load(System.Object sender, System.EventArgs e) { Timer1.Interval = 100; // timer to watch for foreground windows Timer1.Start(); } private void Timer1\_Tick(System.Object sender, System.EventArgs e) { dynamic hWnd = GetForegroundWindow(); // get window handle of foreground window // a Win7 explorer window is in foreground. if (ClassName(hWnd) == "CabinetWClass") { Timer1.Stop(); // stop this timer! using (Form2 f2 = new Form2()) { // show a popup window f2.ShowDialog(); } } } #region "API" \[DllImport("user32.dll", CharSet = CharSet.Auto)\] private static extern Int32 GetClassName(System.IntPtr hWnd, System.Text.StringBuilder lpClassName, Int32 nMaxCount); \[DllImport("user32.dll", CharSet = CharSet.Auto)\] private static extern IntPtr GetForegroundWindow(); private string ClassName(IntPtr hWnd) { StringBuilder sb = new StringBuilder(256); GetClassName(hWnd, sb, 256); return sb.ToString; } public Form1() { Load += Form1\_Load; } #endregion
}
But I am not getting anything to get the window position of foreground "windows explorer" window. Is there any way to read the position of current foreground “Windows Explorer” window using .net?
srikrishnathanthri wrote:
dynamic hWnd = GetForegroundWindow();
No need to declare
hWnd
asdynamic
; just declare it asIntPtr
. You're not using any dynamic resolution[^] on the variable, so you're just slowing your code down for no reason. If you want to find the position of a window, you need to callGetWindowRect
: GetWindowRect function (Windows)[^] GetWindowRect: pinvoke.net[^][StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;public System.Drawing.Point Location { get { return new System.Drawing.Point(Left, Top); } }
}
[DllImport("user32.dll", SetLastError=true)]
private static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);private static System.Drawing.Point GetWindowLocation(IntPtr hWnd)
{
RECT lpRect;
if (!GetWindowRect(hWnd, out lpRect)) return System.Drawing.Point.Empty;
return lpRect.Location;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer