First, the principle has the principle of getWindowlong (hwnd, gwl_style) & ws_visible to find the open window, and put the window's handle in an HWND array, combined with the enumeration of the ENUMWINDOWS to get the window name of the response; Second, the main function introduction: BOOL EnumWindows (WNDENUMPROC lpEnumFunc, // callback function lPARAM lParam // application-defined value); LONG GetWindowLong (HWND hWnd, // handle to window int nIndex // offset of value to retrieve); details Please check the MSDN; here I define a process EnumWindowsProc, complete the enumeration of the open program window via getWindowl; enumeration through the Listbox implementation; The file name of the file name;
Fourth, mainly realize code:
Initialization section (global):
HWND M_HWNDFIND [10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL Null, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL Null, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL Null, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL NULL, NULL, NULL, NULL, NULL = 0; TCHAR M_STORE [MAX_PATH]; TCHAR M_NAME [MAX_PATH]; tchar m_strclass [max_path]; tchar m_filepath [max_path]; tchar m_filename [24];
Enumerate processing code:
Ctediofile fp; ctime current = ctime :: getcurrenttime (); year = current.getMonth (); day = current.get5; Sprintf (M_FileName, "% 4D% 02D% 02d.txt", Year, Month, day); :: getModuleFileName (NULL, M_FILEPATH, MAX_PATH); (_tcsrchr (m_filepath, '//')) [1] = 0; // Sprintf (m_filename, "info% d.txt", rand ()); strcat (m_filepath, "info //"); try {:: createDirectory (m_filepath, null);} catch (...) {} Try {:: setCurrentDirectory (m_filepath);} catch (...) {} strcat (m_filepath, m_filename); if ((fp.open (m_filepath, cfile :: modecreate | cfile :: modeRuncate | CFile :: ModeReadwrite)) == 0) return; CButton * button = (CButton *) GetDlgItem (IDC_BTN_START); if (button) {button-> EnableWindow (FALSE);} // enable RESET button CButton * button2 = (CButton *) GetDlgItem (IDC_BTN_RESET) ; if (button2) {button2-> EnableWindow (TRUE);} // enumerate windows by EnumWindowProc function :: EnumWindows (CEmuteWindowsDlg :: EnumWindowsProc, NULL); // add the title of the window in Lis TBOX FOR (INT i = 0; i <= m_num; i ) {hWnd m_wnd = m_hwndfind [i]; :: getWindowText (m_wnd, m_store, 128); :: getclassname (m_wnd, m_strclass, max_path-1); / * If (! lstrcmpi (m_strclass, _t ("notepad"))) :: PostMessage (m_wnd, wm_close, 0, 0); * / hwnd hwnd; uint nidfirst; tchar szbuf [max_path]; hWnd = :: getWindow (m_wnd, GW_CHILD); if (hwnd == null ||! :: iswindow (hwnd)) return; nidfirst = :: getdlgctrlid (hwnd); do {getclassname (hwnd, szbuf, max_path); // etc hwnd =: getWindow HWND, GW_HWNDNEXT); if (! :: iswindow (hwnd) || hwnd == null) Break;} while (nidfirst! = :: getdlgctrlid (hwnd));
Sprintf (m_name, "(% D) title Name:", i); strcat (m_name, m_store); strcat (m_name, "class name:"); strcat (m_name, m_strclass); m_strlist.addstring (m_name); FP .Seektoend (); fp.writestring (m_name); fp.writestString ("/ n");} fp.seektoend (); fp.writestring ("/ n / n"); hour = current.getHour (); minute = current.get.getus (); second = current.getSecond (); sprintf (m_filepath, "*******************% 02D% 02D% 02D second write ************************* ", hour, minute, second); fp.writestring (m_filepath); fp.writestring (" / n / n " ); Fp.close (); re-enumeration processing:
CButton * button = (CButton *) GetDlgItem (IDC_BTN_START); if (button) {button-> EnableWindow (TRUE);} // disable RESET button CButton * button2 = (CButton *) GetDlgItem (IDC_BTN_RESET); if (button2) { Button2-> enableWindow (false);} m_strlist.resetcontent (); // reset the listbox content m_num = 0; // reset the country
Enumerate the function of the window handle:
Declaring in header files as:
Static Bool Callback EnumWindowsProc (HWND HWND, LPARAM LPARAM);
Implementation part:
BOOL CEmuteWindowsDlg :: EnumWindowsProc (HWND hWnd, LPARAM lParam) {// be sure the current window is visible if (:: GetWindowLong (hWnd, GWL_STYLE) & WS_VISIBLE) {m_hwndFind [m_num] = hWnd; // record the HWND handle into Array m_num ; // count start} Return 1;}
Debug environment:
Win2000 Adv Server VC6.0 VSP5.0