Answered by:
Win32, getting a window title from a hWnd

Question
-
If I use EnumWindows to get all windows, how would i use a hWnd to find the title of a window (so as to differentiate between windows so an action is performed to only windows with a certain title)?
Thanks in advance,
- JavawagThursday, May 10, 2007 4:57 PM
Answers
-
You'll probably want to use that in conjunction with GetWindowTextLength:
Code Snippet[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int GetWindowTextLength(HandleRef hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount);
//...
int capacity = GetWindowTextLength(new HandleRef(this, handle)) * 2;
StringBuilder stringBuilder = new StringBuilder(capacity);
GetWindowText(new HandleRef(this, handle), stringBuilder, stringBuilder.Capacity);
If all you want is the name window text of all processes, you can use Process.GetProcesses() the use Process.MainWindowTitle; but MainWindowTitle will not get the latest window text (just the text the first time it is called on a process).
Thursday, May 10, 2007 6:59 PM
All replies
-
PInvoke GetWindowTextThursday, May 10, 2007 6:11 PM
-
Sounds about right, but it didn't work. This is the code I used:
Code Snippet - GetWindowText[DllImport("User32.dll")]
public static extern string GetWindowText(int hWnd);
and I get an "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." error. Any ideas?
- JavawagThursday, May 10, 2007 6:33 PM -
Try
Code Snippet[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount);Thursday, May 10, 2007 6:42 PM -
You'll probably want to use that in conjunction with GetWindowTextLength:
Code Snippet[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int GetWindowTextLength(HandleRef hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount);
//...
int capacity = GetWindowTextLength(new HandleRef(this, handle)) * 2;
StringBuilder stringBuilder = new StringBuilder(capacity);
GetWindowText(new HandleRef(this, handle), stringBuilder, stringBuilder.Capacity);
If all you want is the name window text of all processes, you can use Process.GetProcesses() the use Process.MainWindowTitle; but MainWindowTitle will not get the latest window text (just the text the first time it is called on a process).
Thursday, May 10, 2007 6:59 PM -
Ok, this looks promising! However, I really don't know how to use StringBuilders... could you show me what I'd need to do if i put my code here?:Ok, I fiddled around, and the original suggestion worked. I'll mark that as the answer. Thanks a lot!!
frmMain.cs: (this part is repeated for every open window, using each one's hWnd)
if (Win32.GetWindowText(hWnd) == "Untitled - Notepad")
{
Win32.SendMessage(hWnd, Win32.WM_SYSCOMMAND, Win32.SC_RESTORE, 0);}
Win32.cs:
public const int SC_MINIMIZE = 0xF020;
[DllImport("User32.dll")]
public static extern int EnumWindows(EnumWindowsProc cb, int lparam);
public delegate bool EnumWindowsProc(int hWnd, int lParam);
[DllImport("User32.dll")]
public static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount);
What code would I need to use in frmMain.cs to get the name of a single form from its hWnd and check it against my test string (which, at the moment, is "Untitled - Notepad")?
- JavawagThursday, May 10, 2007 7:20 PM