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 locate a window by title

How to locate a window by title

Scheduled Pinned Locked Moved C#
questiondata-structurestutorial
3 Posts 2 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.
  • L Offline
    L Offline
    Leif Simon Goodwin
    wrote on last edited by
    #1

    Bit of a long winded question I'm afraid. I have an app which sends data to an open document displayed by an app such as Word, using a simple paste operation. This requires that I locate the handle of the window associated with the document. The user configures the file location of the application that displays the document, and the file location of the document. Obtaining the window handle is usually quite easy using something like this:

    using (var searcher = new System.Management.ManagementObjectSearcher("SELECT ProcessId, ExecutablePath FROM Win32_Process"))
    {
    using (var results = searcher.Get())
    {
    foreach (System.Management.ManagementObject item in results)
    {
    string ExecutablePath = (string)item["ExecutablePath"];
    if (!string.IsNullOrEmpty(ExecutablePath) && (ExecutablePath == _destinationApplication.DestinationApplicationPath))
    {
    int processID = (int)(uint)item["ProcessId"];
    System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(processID);

        if ((process != null) && process.MainWindowTitle.Contains(documentTitle))
        {
          return process.MainWindowHandle;
        }
      }
    }
    

    }
    }

    Unfortunately Microsoft Word is a pain as one instance manages multiple open documents. Calling GetWindowText() on the main window handle gets the name of one document. Using Spy to look at the window tree for Word shows that none of the child windows store the name of the other document as the window text, even though I can see the name of the document displayed by a window. So, how do I locate the handle of the window in which the second document is displayed?

    L 1 Reply Last reply
    0
    • L Leif Simon Goodwin

      Bit of a long winded question I'm afraid. I have an app which sends data to an open document displayed by an app such as Word, using a simple paste operation. This requires that I locate the handle of the window associated with the document. The user configures the file location of the application that displays the document, and the file location of the document. Obtaining the window handle is usually quite easy using something like this:

      using (var searcher = new System.Management.ManagementObjectSearcher("SELECT ProcessId, ExecutablePath FROM Win32_Process"))
      {
      using (var results = searcher.Get())
      {
      foreach (System.Management.ManagementObject item in results)
      {
      string ExecutablePath = (string)item["ExecutablePath"];
      if (!string.IsNullOrEmpty(ExecutablePath) && (ExecutablePath == _destinationApplication.DestinationApplicationPath))
      {
      int processID = (int)(uint)item["ProcessId"];
      System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(processID);

          if ((process != null) && process.MainWindowTitle.Contains(documentTitle))
          {
            return process.MainWindowHandle;
          }
        }
      }
      

      }
      }

      Unfortunately Microsoft Word is a pain as one instance manages multiple open documents. Calling GetWindowText() on the main window handle gets the name of one document. Using Spy to look at the window tree for Word shows that none of the child windows store the name of the other document as the window text, even though I can see the name of the document displayed by a window. So, how do I locate the handle of the window in which the second document is displayed?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Enumerate all processes, and then each window from each proces. c# - How to enumerate all windows belonging to a particular process using .NET? - Stack Overflow[^]

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      L 1 Reply Last reply
      0
      • L Lost User

        Enumerate all processes, and then each window from each proces. c# - How to enumerate all windows belonging to a particular process using .NET? - Stack Overflow[^]

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        L Offline
        L Offline
        Leif Simon Goodwin
        wrote on last edited by
        #3

        Unfortunately that does not work as I cannot reliably identify which window belongs to the target document. And yes I do already use that method to enumerate through the child windows.

        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