Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to get Windows explorer’s position using C#?

How to get Windows explorer’s position using C#?

Scheduled Pinned Locked Moved C#
csharpdatabasejsontutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    srikrishnathanthri
    wrote on last edited by
    #1

    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?

    OriginalGriffO Richard DeemingR 2 Replies Last reply
    0
    • S srikrishnathanthri

      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?

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      S 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        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...

        S Offline
        S Offline
        srikrishnathanthri
        wrote on last edited by
        #3

        sorry for the confusion, I have edited the question. I facing trouble in getting window position of windows explorer.

        1 Reply Last reply
        0
        • S srikrishnathanthri

          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?

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          srikrishnathanthri wrote:

          dynamic hWnd = GetForegroundWindow();

          No need to declare hWnd as dynamic; just declare it as IntPtr. 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 call GetWindowRect: 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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups