| This article will provide a source program to implement enumeration of windows in the treectrl format in a dialog box. This is the header file // Winlist. h: header file // //////////////////////////////////////// ///////////////////////////////////// // Cwinlist Dialog Class cwinlist: Public cdialog { // Construction Public: Hwnd m_hselected; Cwinlist (cwnd * pparent = NULL); // standard Constructor Static bool callback enumwindowsproc (hwnd, lparam ); Static bool callback enumchildproc (hwnd, lparam ); Void builttree (hwnd ); // Dialog data // {Afx_data (cwinlist) Enum {IDD = id_winlist }; Ctreectrl m_tree; //} Afx_data // Overrides // Classwizard generated virtual function overrides // {Afx_virtual (cwinlist) Public: Virtual bool destroywindow (); Protected: Virtual void dodataexchange (cdataexchange * PDX); // DDX/DDV Support //} Afx_virtual // Implementation Protected: Void drawflash (hwnd ); Void refresh (); // Generated message map Functions // {Afx_msg (cwinlist) Virtual bool oninitdialog (); Afx_msg void onrefresh (); Afx_msg void onselchangedtree (nmhdr * pnmhdr, lresult * presult ); //} Afx_msg Declare_message_map () PRIVATE: Cfont treefont; TV _insertstruct treeitem; Int rootpt; Htreeitem rootarray [10]; }; The following is the implementation file // Winlist. cpp: implementation file // # Include "stdafx. H" # Include "MFC. H" # Include "winlist. H" # Include "mfcapi. H" # Ifdef _ debug # Define new debug_new # UNDEF this_file Static char this_file [] = _ file __; # Endif //////////////////////////////////////// ///////////////////////////////////// // Cwinlist Dialog Cwinlist: cwinlist (cwnd * pparent/* = NULL */) : Cdialog (cwinlist: IDD, pparent) { // {Afx_data_init (cwinlist) // Note: The classwizard will add member Initialization Here //} Afx_data_init } Void cwinlist: dodataexchange (cdataexchange * PDX) { Cdialog: dodataexchange (PDX ); // {Afx_data_map (cwinlist) Ddx_control (PDX, idtree, m_tree ); //} Afx_data_map } Begin_message_map (cwinlist, cdialog) // {Afx_msg_map (cwinlist) On_bn_clicked (idrefresh, onrefresh) On_notify (tvn_selchanged, idtree, onselchangedtree) //} Afx_msg_map End_message_map () //////////////////////////////////////// ///////////////////////////////////// // Cwinlist message handlers Static cdwordarray winarray; Static unsigned long level; Static int ignore [10]; Static hwnd hprevwnd; Bool callback cwinlist: enumchildproc (hwnd, lparam) { Ignore [level] ++; If (ignore [LEVEL + 1]) Ignore [LEVEL + 1] --; Else {Winarray. Add (unsigned long) (level <16) | (unsigned Long) hwnd )); Level ++; ignore [LEVEL + 1] = 0; Enumchildwindows (hwnd, enumchildproc, (long) 0 ); Level --; } Return true; } Bool callback cwinlist: enumwindowsproc (hwnd, lparam) { Winarray. Add (unsigned long) hwnd ); Level = 1; ignore [LEVEL + 1] = 0; Enumchildwindows (hwnd, enumchildproc, (long) 0 ); Return true; } Void cwinlist: builttree (hwnd) {Char temp [90]; Char text [50]; Char name [20]; Treeitem. hparent = rootarray [rootpt]; : Getwindowtext (hwnd, text, 50 ); : Getclassname (hwnd, name, 20 ); Sprintf (temp, "[% 04x]: {% s }:% s", hwnd, name, text ); Treeitem. item. psztext = temp; Treeitem. item. lparam = (unsigned long) hwnd; Htreeitem tempitem = m_tree.insertitem (& treeitem ); Rootarray [rootpt + 1] = tempitem; } Void cwinlist: drawflash (hwnd) { HDC; Rect RC; Hpen; : Getwindowrect (hwnd, & rc ); HDC =: getwindowdc (hwnd ); : Setrop2 (HDC, r2_not ); Hpen =: createpen (ps_insideframe, 5 * getsystemmetrics (sm_cxborder), RGB (255, 0, 0 )) ; : SelectObject (HDC, Hpen ); : SelectObject (HDC, getstockobject (null_brush )); : Rectangle (HDC, RC. right-rc.left, RC. bottom-rc.top ); : Releasedc (hwnd, HDC ); : Deleteobject (Hpen ); } Void cwinlist: refresh () {Rootarray [0] = tvi_root; Rootpt = 0; Treeitem. hinsertafter = tvi_last; Treeitem. item. Mask = tvif_text | tvif_param; Winarray. removeall (); M_tree.setfont (& treefont ); M_tree.deleteallitems (); Level = 0; For (Int J = 0; j <10; j ++) Ignore [J] = 0; Enumwindows (enumwindowsproc, (long) 0 ); For (INT I = 0; I/TD> {Rootpt = (INT) (winarray [I] & 0xffff0000)> 16 ); Builttree (hwnd) (winarray [I] & 0x0000ffff )); } } Bool cwinlist: oninitdialog () { Cdialog: oninitdialog (); // Todo: add extra initialization here M_hselected = NULL; Hprevwnd = NULL; Refresh (); Return true; // return true unless you set the focus to a control // Exception: OCX property pages shold return false } Void cwinlist: onrefresh () { // Todo: add your control notification handler code here Refresh (); } Void cwinlist: onselchangedtree (nmhdr * pnmhdr, lresult * presult) { Nm_treeview * pnmtreeview = (nm_treeview *) pnmhdr; // Todo: add your control notification handler code here Hwnd = (hwnd) pnmtreeview-> itemnew. lparam; M_hselected = hwnd; If (hprevwnd) drawflash (hprevwnd ); Hprevwnd = hwnd; Drawflash (hwnd ); * Presult = 0; } Bool cwinlist: destroywindow () { // Todo: add your specialized code here and/or call the base class If (hprevwnd) drawflash (hprevwnd ); Return cdialog: destroywindow (); } //////////////////////////////////////// ///////////////////////////////////// // DLL export interface Extern "C" Hwnd far Pascal export searchwindow () { Cwinlist winlist; If (winlist. domodal () = idok) Return winlist. m_hselected; Else Return (hwnd) NULL; } |