How to get the title bar of the current activity in the window?

AnonyOtaku 21 Reputation points
2021-08-07T20:01:35.25+00:00

Essentially I want to get the top bar of the current application. In this instance, when I open a file with CDisplayEX. I want to get the name that shows at the very top in its entirety. I wonder if that's possible? I added an Imgur link to show what I mean. In this case, it would be "One Piece v02\One Piece v2-000.jpeg". I want this whole title and I want the title to change as I go to the next page and so on.

https://i.imgur.com/AGIHmbc.png

Thank you in advance

Developer technologies | C#
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-08-07T21:32:45.693+00:00

    Perform some experiments with a separate new project. Create a new “Console App (.NET Framework)” project, then replace the initial text of Program.cs with this code:

    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    
    namespace ConsoleApp21
    {
       class Program
       {
          [DllImport( "user32" )]
          private static extern IntPtr GetForegroundWindow( );
    
          [DllImport( "user32", EntryPoint = "SendMessageW", CharSet = CharSet.Unicode )]
          public static extern bool SendMessage( IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam );
    
          const int WM_GETTEXT = 0x000D;
    
    
          static void Main( string[] args )
          {
             string previous_title = null;
    
             for(; ; )
             {
                IntPtr hwnd = GetForegroundWindow( );
                if( hwnd == IntPtr.Zero ) continue;
    
                StringBuilder sb = new StringBuilder( 700 );
    
                SendMessage( hwnd, WM_GETTEXT, sb.Capacity, sb );
    
                string title = sb.ToString( );
    
                if( previous_title != title )
                {
                   previous_title = title;
    
                   Console.WriteLine( $"Window title: {title}" );
                }
    
                Thread.Sle\u0065p( 100 );
             }
          }
       }
    }
    

    It will display the title of current application. You can move this test window to second monitor or minimise it and restore later. Activate other windows and check that the titles are displayed by this test.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2021-08-07T20:56:56.577+00:00

    Use GetForegroundWindow

    A quick test =>

     Timer timer1 = new Timer() { Interval = 1000, Enabled = true };  
     timer1.Tick += new System.EventHandler(timer1_Tick);  
    

    with :

            private void timer1_Tick(object sender, EventArgs e)  
            {  
                StringBuilder sbBufferText = new StringBuilder(255);  
                GetWindowText(GetForegroundWindow(), sbBufferText, sbBufferText.Capacity);  
                Console.WriteLine("Foreground Window text = {0}", sbBufferText.ToString());  
            }  
      
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
            public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);  
      
            [DllImport("User32.dll", SetLastError = true)]  
            public static extern IntPtr GetForegroundWindow();  
    
    1 person found this answer helpful.

  2. AnonyOtaku 21 Reputation points
    2021-08-15T01:11:50.863+00:00

    Thank you all. I was finally able to crack it and solve my issue.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.