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 current application windows title?

how to get current application windows title?

Scheduled Pinned Locked Moved C#
helptutorialquestion
2 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.
  • A Offline
    A Offline
    a fatemeh
    wrote on last edited by
    #1

    hi every one, I want to get current application windows title and then show it? through my search,I find out that I should use getforegroundWindow(),but I don't get it how to do that ? I'd appreciate any help.

    D 1 Reply Last reply
    0
    • A a fatemeh

      hi every one, I want to get current application windows title and then show it? through my search,I find out that I should use getforegroundWindow(),but I don't get it how to do that ? I'd appreciate any help.

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      To use C++ functions you need to do some research into P/Invoke. Here's how to declare the functions you need in C#:

      // NativeMethods.cs

      using System;
      using System.Text;
      using System.Runtime.InteropServices;

      namespace ConsoleApplicationTest
      {
      internal static class NativeMethods
      {
      [DllImport("User32.dll")]
      public static extern IntPtr GetForegroundWindow();

          \[DllImport("User32.dll")\]
          public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
      }
      

      }

      Usage example:

      // Program.cs

      using System;
      using System.Text;

      namespace ConsoleApplicationTest
      {
      class Program
      {
      static void Main(string[] args)
      {
      Console.WriteLine("Press any key to get text");
      Console.ReadKey();
      IntPtr handle = NativeMethods.GetForegroundWindow();
      if (handle != IntPtr.Zero)
      {
      StringBuilder builder = new StringBuilder(255);
      NativeMethods.GetWindowText(handle, builder, builder.Capacity);
      Console.WriteLine(builder.ToString());
      }
      Console.ReadKey();
      }
      }
      }

      I have used a hardcoded max length of 255. You can use GetWindowText[^] to get the length of the text before getting the text so you won;t be wasting memory with your string builder or risk truncating if it's longer than you expect. I'll leave implementing that (which is simple) as an exercise for you.

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      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