سؤال

There is an externally running program that i need the capability to resize. The kicker for me is that part of the title is the version and other specific information related to that instance. I know the substring that should be consistent across versions. I have attempted the Findwindow() function, which works well if you have the exact wording of the title, but not when you only have a portion. I have also tried EnumWindows, but i believe that has the same limitations (i didn't have much luck with it). I feel the simplest thing i could do (if possible) would be to get the window handle from the image name in order to do my resizing. Ideas?

هل كانت مفيدة؟

المحلول

Here's a working piece of code I just tested on MSVS 2010 that works perfectly:

#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <windows.h>


BOOL CALLBACK FindWindowBySubstr(HWND hwnd, LPARAM substring)
{
    const DWORD TITLE_SIZE = 1024;
    TCHAR windowTitle[TITLE_SIZE];

    if (GetWindowText(hwnd, windowTitle, TITLE_SIZE))
    {
        //_tprintf(TEXT("%s\n"), windowTitle);
        // Uncomment to print all windows being enumerated
        if (_tcsstr(windowTitle, LPCTSTR(substring)) != NULL)
        {
            // We found the window! Stop enumerating.
            return false;
        }
    }
    return true; // Need to continue enumerating windows
}

int main() 
{
    const TCHAR substring[] = TEXT("Substring");
    EnumWindows(FindWindowBySubstr, (LPARAM)substring);
}

نصائح أخرى

EnumWindows was meant specifically for this. You create your own callback function to pass to EnumWindows, and it will call your callback function for each window it enumerates and pass it the hwnd of the window. You can call GetWindowText inside of your callback function to get the window title and search that text like any other. What problem did you have with that code? Why don't you post it?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top