Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than user32, prefix the name with the module name and a period.
using System;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
/// <summary>
/// EnumDesktopWindows Demo - shows the caption of all desktop windows.
/// Authors: Svetlin Nakov, Martin Kulov
/// Bulgarian Association of Software Developers - http://www.devbg.org/en/
/// </summary>
public class EnumDesktopWindowsDemo
{
const int MAXTITLE = 255;
private static ArrayList mTitlesList;
private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
[DllImport("user32.dll",
EntryPoint="GetWindowText",
ExactSpelling=false,
CharSet=CharSet.Auto,
SetLastError=true)]
private static extern int _GetWindowText(IntPtr hWnd,
StringBuilder lpWindowText,
int nMaxCount);
private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
{
string title = GetWindowText(hWnd);
mTitlesList.Add(title);
return true;
}
/// <summary>
/// Returns the caption of a windows by given HWND identifier.
/// </summary>
public static string GetWindowText(IntPtr hWnd)
{
StringBuilder title = new StringBuilder(MAXTITLE);
int titleLength = _GetWindowText(hWnd, title, title.Capacity + 1);
title.Length = titleLength;
return title.ToString();
}
/// <summary>
/// Returns the caption of all desktop windows.
/// </summary>
public static string[] GetDesktopWindowsCaptions()
{
mTitlesList = new ArrayList();
EnumDelegate enumfunc = new EnumDelegate(EnumWindowsProc);
IntPtr hDesktop = IntPtr.Zero; // current desktop
bool success = _EnumDesktopWindows(hDesktop, enumfunc, IntPtr.Zero);
if (success)
{
// Copy the result to string array
string[] titles = new string[mTitlesList.Count];
mTitlesList.CopyTo(titles);
return titles;
}
else
{
// Get the last Win32 error code
int errorCode = Marshal.GetLastWin32Error();
string errorMessage = String.Format("EnumDesktopWindows failed with code {0}.", errorCode);
throw new Exception(errorMessage);
}
}
foreach (string caption in desktopWindowsCaptions)
Console.WriteLine(caption);
}
}
Sample Code (VB.net):
Const MAXTITLE As Integer = 255
Public Shared mTitlesList As ArrayList
Public Delegate Function EnumDelegate(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
Private Shared Function EnumWindowsProc(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
Dim title As String = GetWindowText(hWnd)
mTitlesList.Add(title)
Return True
End Function
Public Shared Function GetWindowText(ByVal hWnd As IntPtr) As String
Dim title As System.Text.StringBuilder = New System.Text.StringBuilder(MAXTITLE)
Dim titleLength As Integer = GetWindowText(hWnd, title, (title.Capacity + 1))
title.Length = titleLength
Return title.ToString
End Function
Public Shared Function GetDesktopWindowsCaptions() As String()
mTitlesList = New ArrayList
Dim enumfunc As EnumDelegate = New EnumDelegate(AddressOf EnumWindowsProc)
Dim hDesktop As IntPtr = IntPtr.Zero
' current desktop
Dim success As Boolean = EnumDesktopWindows(hDesktop, enumfunc, IntPtr.Zero)
If success Then
' Copy the result to string array
Dim titles() As String = New String((mTitlesList.Count) - 1) {}
mTitlesList.CopyTo(titles)
Return titles
Else
' Get the last Win32 error code
Dim errorMessage As String = String.Format("EnumDesktopWindows failed.")
Throw New Exception(errorMessage)
End If
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim desktopWindowsCaptions() As String = GetDesktopWindowsCaptions()
For Each caption As String In desktopWindowsCaptions
Console.WriteLine(caption)
Next
End Sub
Alternative Managed API:
Do you know one? Please contribute it!
The EnumDesktopWindows API
1/23/2016 4:09:36 PM - -216.166.58.66
The EnumDesktopWindows API
1/23/2016 4:09:36 PM - -216.166.58.66
ByVal is a VB keyword that specifies a variable to be passed as a parameter BY VALUE. In other words, if the function or sub changes the value of the internal variable, it does not change the value of the external variable that was passed to it.
4/25/2007 3:19:55 AM - josep1er@cmich.edu-141.209.229.179
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
ByVal is a VB keyword that specifies a variable to be passed as a parameter BY VALUE. In other words, if the function or sub changes the value of the internal variable, it does not change the value of the external variable that was passed to it.
4/25/2007 3:19:55 AM - josep1er@cmich.edu-141.209.229.179
ByVal is a VB keyword that specifies a variable to be passed as a parameter BY VALUE. In other words, if the function or sub changes the value of the internal variable, it does not change the value of the external variable that was passed to it.
4/25/2007 3:19:55 AM - josep1er@cmich.edu-141.209.229.179
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).