Click here to Skip to main content
15,914,111 members
Home / Discussions / C#
   

C#

 
GeneralRe: Help reading window titles and text in c sharp Pin
turbosupramk37-Jun-11 11:05
turbosupramk37-Jun-11 11:05 
GeneralRe: Help reading window titles and text in c sharp Pin
SledgeHammer017-Jun-11 11:44
SledgeHammer017-Jun-11 11:44 
AnswerRe: Help reading window titles and text in c sharp [modified] Pin
DaveyM697-Jun-11 10:14
professionalDaveyM697-Jun-11 10:14 
GeneralRe: Help reading window titles and text in c sharp Pin
turbosupramk37-Jun-11 11:00
turbosupramk37-Jun-11 11:00 
GeneralRe: Help reading window titles and text in c sharp Pin
DaveyM697-Jun-11 11:25
professionalDaveyM697-Jun-11 11:25 
GeneralRe: Help reading window titles and text in c sharp Pin
Pete O'Hanlon7-Jun-11 11:38
mvePete O'Hanlon7-Jun-11 11:38 
GeneralRe: Help reading window titles and text in c sharp Pin
DaveyM697-Jun-11 11:54
professionalDaveyM697-Jun-11 11:54 
GeneralRe: Help reading window titles and text in c sharp Pin
DaveyM697-Jun-11 12:00
professionalDaveyM697-Jun-11 12:00 
I've run a test with the code below and it works OK (a few tweaks to earlier code and an additional function).
C#
using System;
using System.Runtime.InteropServices;
using System.Text;

internal static class NativeMethods
{
    /*
BOOL WINAPI SetWindowText(
  __in      HWND hWnd,
  __in_opt  LPCTSTR lpString
); */
    public static bool SetWindowText(IntPtr hWnd)
    {
        return SetWindowText(hWnd, null);
    }
    [DllImport("User32.dll", SetLastError = true)]
    public static extern bool SetWindowText(IntPtr hWnd, string lpString);

    /*
int WINAPI GetWindowText(
  __in   HWND hWnd,
  __out  LPTSTR lpString,
  __in   int nMaxCount
); */
    public static string GetWindowText(IntPtr hWnd)
    {
        int count = GetWindowTextLength(hWnd) + 1;
        StringBuilder resultBuilder = new StringBuilder(count);
        GetWindowText(hWnd, resultBuilder, count);
        return resultBuilder.ToString();
    }
    [DllImport("User32.dll", SetLastError = true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    /*
int WINAPI GetWindowTextLength(
  __in  HWND hWnd
); */
    [DllImport("User32.dll", SetLastError = true)]
    public static extern int GetWindowTextLength(IntPtr hWnd);

    /*
BOOL WINAPI EnumWindows(
  __in  WNDENUMPROC lpEnumFunc,
  __in  LPARAM lParam
); */
    [DllImport("User32.dll", SetLastError = true)]
    public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

    /* 
BOOL CALLBACK EnumWindowsProc(
  __in  HWND hwnd,
  __in  LPARAM lParam
); */
    public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

    /*
BOOL WINAPI IsWindowVisible(
  __in  HWND hWnd
); */
    [DllImport("User32.dll", SetLastError = true)]
    public static extern bool IsWindowVisible(IntPtr hWnd);
}

C#
// List to hold hWnds
private List<IntPtr> windowHandles = new List<IntPtr>();

private bool EnumWindowsProcHandler(IntPtr hWnd, IntPtr lParam)
{
    windowHandles.Add(hWnd);
    return true;
}

C#
NativeMethods.EnumWindowsProc enumWindowsProc = 
    new NativeMethods.EnumWindowsProc(EnumWindowsProcHandler);

// Get windows
NativeMethods.EnumWindows(enumWindowsProc, IntPtr.Zero);

foreach (IntPtr hWnd in windowHandles)
{
    if (NativeMethods.IsWindowVisible(hWnd))
    {
        string caption = NativeMethods.GetWindowText(hWnd);
        if (!string.IsNullOrEmpty(caption))
        {
            Console.WriteLine(caption); // Log found & visible windows
            if (caption.EndsWith("Microsoft Visual Studio")) // Visual Studio
            {
                NativeMethods.SetWindowText(hWnd, "Changed VS Caption");
            }
        }
    }
}

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)



QuestionWhy exception is not caught in BackgroundWorker DoWork routine? Pin
Chesnokov Yuriy7-Jun-11 3:56
professionalChesnokov Yuriy7-Jun-11 3:56 
AnswerRe: Why exception is not caught in BackgroundWorker DoWork routine? Pin
Ian Shlasko7-Jun-11 4:07
Ian Shlasko7-Jun-11 4:07 
GeneralRe: Why exception is not caught in BackgroundWorker DoWork routine? Pin
Chesnokov Yuriy7-Jun-11 4:17
professionalChesnokov Yuriy7-Jun-11 4:17 
QuestionRe: Why exception is not caught in BackgroundWorker DoWork routine? Pin
Luc Pattyn7-Jun-11 4:43
sitebuilderLuc Pattyn7-Jun-11 4:43 
AnswerRe: Why exception is not caught in BackgroundWorker DoWork routine? Pin
Ian Shlasko7-Jun-11 5:12
Ian Shlasko7-Jun-11 5:12 
AnswerRe: Why exception is not caught in BackgroundWorker DoWork routine? Pin
ShadowUz7-Jun-11 21:12
ShadowUz7-Jun-11 21:12 
QuestionNotifying Network Applications Pin
NameNotYetTaken7-Jun-11 3:18
NameNotYetTaken7-Jun-11 3:18 
AnswerRe: Notifying Network Applications Pin
Dave Kreskowiak7-Jun-11 3:46
mveDave Kreskowiak7-Jun-11 3:46 
AnswerRe: Notifying Network Applications Pin
Luc Pattyn7-Jun-11 4:51
sitebuilderLuc Pattyn7-Jun-11 4:51 
GeneralRe: Notifying Network Applications Pin
NameNotYetTaken7-Jun-11 5:18
NameNotYetTaken7-Jun-11 5:18 
AnswerRe: Notifying Network Applications Pin
Luc Pattyn7-Jun-11 5:39
sitebuilderLuc Pattyn7-Jun-11 5:39 
GeneralRe: Notifying Network Applications Pin
NameNotYetTaken7-Jun-11 6:08
NameNotYetTaken7-Jun-11 6:08 
AnswerRe: Notifying Network Applications Pin
BobJanova7-Jun-11 5:27
BobJanova7-Jun-11 5:27 
QuestionMerging C# WPF projects Pin
Member 79112937-Jun-11 2:23
Member 79112937-Jun-11 2:23 
AnswerRe: Merging C# WPF projects Pin
Pete O'Hanlon7-Jun-11 2:32
mvePete O'Hanlon7-Jun-11 2:32 
AnswerRe: Merging C# WPF projects Pin
V.7-Jun-11 3:37
professionalV.7-Jun-11 3:37 
QuestionHow to fix English language to specific textbox in Arabic Input language mode? Pin
Paramu19737-Jun-11 0:29
Paramu19737-Jun-11 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.