1. 程式人生 > >根據程序名獲取窗體名

根據程序名獲取窗體名

#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <string>
using namespace std;
DWORD GetProcessList( );

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
    DWORD proID = (DWORD)lParam;
    char buf[500] = {0};
    GetWindowText(hWnd, buf, 499);
    DWORD proID2;
    GetWindowThreadProcessId(hWnd, &proID2);
    if(proID2 == proID)
    {
        printf("\n%s\n", buf);
    }
    return TRUE;
}

void main( )
{
    DWORD proID = GetProcessList( );
    EnumWindows(EnumWindowsProc, proID);
}

DWORD GetProcessList( )
{
    HANDLE hProcessSnap;
    PROCESSENTRY32 pe32;
    hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
    if( hProcessSnap == INVALID_HANDLE_VALUE )
    {
        return( FALSE );
    }
    pe32.dwSize = sizeof( PROCESSENTRY32 );
    if( !Process32First( hProcessSnap, &pe32 ) )
    {
        CloseHandle( hProcessSnap );  
        return( FALSE );
    }
    do
    {
        if( (string)pe32.szExeFile == "notepad.exe")
        {
            return pe32.th32ProcessID;
        }
    } while( Process32Next( hProcessSnap, &pe32 ) );
    CloseHandle( hProcessSnap );
    return( TRUE );
}