Packets3
Packets3

Reputation: 11

Window title C++ how to get the name of a randomizing title

So I'm working on a program that would detect the window name of a title that constantly changes 6 characters in the title Gamebar-592d22(master) I currently use:

Hwnd hwnd = FindWindowA(NULL, WindowTitle);

and I send mouse input via hwnd.

Upvotes: 1

Views: 293

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596256

You won't be able to use FindWindow() for this. Use EnumWindows() instead. Inside the enum callback, use GetWindowText() to get the title of the provided HWND, check if it matches the pattern you are interested in, and if so then use the HWND as needed, eg:

BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lParam)
{
    char title[24] = {0};
    GetWindowTextA(hwnd, title, 23);
    int num;
    if (sscanf(title, "Gamebar-%6x(master)", &num) == 1)
    {
        // use hwnd and lParam as needed... 
    }
    return TRUE;
}

EnumWindows(&MyEnumWindowsProc, ...);

UPDATE: For example, given your comment about sending a mouse message to coordinates within the found window, you can use the callback's LPARAM to pass information into the callback. For instance:

  1. You can pass the address of an HWND variable in the LPARAM, and if the matching window is found then assign its HWND to that variable, then you can send the message when EnumWindows() exits:
BOOL CALLBACK FindGamebarWnd(HWND hwnd, LPARAM lParam)
{
    char title[24] = {0};
    GetWindowTextA(hwnd, title, 23);
    int num;
    if (sscanf(title, "Gamebar-%6x(master)", &num) == 1)
    {
        *reinterpret_cast<HWND*>(lParam) = hwnd;
        return FALSE;
    }
    return TRUE;
}

HWND hwnd = NULL;
EnumWindows(&FindGamebarWnd, reinterpret_cast<LPARAM>(&hwnd));
if (hwnd)
    SendMessage(hwnd, WM_LBUTTONUP, 0, MAKELPARAM(pt.x, pt.y)); 
  1. You can pass the mouse coordinates in the LPARAM, then send the message from in the callback when it finds the matching window:
BOOL CALLBACK ClickGamebarWnd(HWND hwnd, LPARAM lParam)
{
    char title[24] = {0};
    GetWindowTextA(hwnd, title, 23);
    int num;
    if (sscanf(title, "Gamebar-%6x(master)", &num) == 1)
    {
        SendMessage(hwnd, WM_LBUTTONUP, 0, lParam); 
        return FALSE;
    }
    return TRUE;
}

EnumWindows(&ClickGamebarWnd, MAKELPARAM(pt.x, pt.y));

Upvotes: 2

Related Questions