FindWindow Find all window handles that have same caption and class
- User
FindWindow Find all window handles that have same caption and class
- Assistant
To find all window handles that have the same caption and class using the FindWindow function in C++, you would need to iterate through all the open windows and compare their captions and classes with the desired values.
Here's an example code snippet that demonstrates how you can achieve this:
#include <iostream> #include <vector> #include <string> #include <Windows.h> BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) { std::vector<HWND>* windowHandles = reinterpret_cast<std::vector<HWND>*>(lParam); // Get the length of the window caption int length = GetWindowTextLength(hWnd); if (length == 0) { return TRUE; } // Retrieve the window caption std::string caption(length + 1, '\0'); // +1 for null termination GetWindowText(hWnd, &caption[0], length + 1); // Retrieve the window class name std::string className(256, '\0'); GetClassName(hWnd, &className[0], 256); // Compare the caption and class name with desired values std::string desiredCaption = "My Window Caption"; std::string desiredClassName = "MyClassName"; if (caption == desiredCaption && className == desiredClassName) { windowHandles->push_back(hWnd); } return TRUE; } int main() { std::vector<HWND> matchingWindows; // Enumerate all windows EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&matchingWindows)); // Print the matching window handles for (const auto& windowHandle : matchingWindows) { std::cout << "Window Handle: " << windowHandle << std::endl; } return 0; } - User
C# format
- Assistant
In C#, you can use the
EnumWindowsfunction from theuser32.dlllibrary to find all window handles with the same caption and class. Here's an example code:using System; using System.Collections.Generic; using System.Runtime.InteropServices; class Program { private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); [DllImport("user32.dll")] private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam); [DllImport("user32.dll")] private static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpWindowText, int nMaxCount); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern int GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount); static void Main(string[] args) { List<IntPtr> matchingWindows = new List<IntPtr>(); // Desired caption and class name string desiredCaption = "My Window Caption"; string desiredClassName = "MyClassName"; EnumWindows((hWnd, lParam) => { System.Text.StringBuilder windowText = new System.Text.StringBuilder(256); GetWindowText(hWnd, windowText, 256); System.Text.StringBuilder className = new System.Text.StringBuilder(256); GetClassName(hWnd, className, 256); if (windowText.ToString() == desiredCaption && className.ToString() == desiredClassName) { matchingWindows.Add(hWnd); } return true; }, IntPtr.Zero); foreach (IntPtr windowHandle in matchingWindows) { Console.WriteLine("Window Handle: " + windowHandle); } } }