枚举当前打开的所有窗口

类别:VC语言 点击:0 评论:0 推荐:

一、原理
     其原理是利用GetWindowLong(hWnd,GWL_STYLE)& WS_VISIBLE对打开的窗口进行查找,并将窗口的句柄存放在一个HWND的数组中,结合EnumWindows进行窗口的枚举,从而得到响应的窗口名称;
二、主要函数介绍:
   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
);
详细资料请查看MSDN;
在此我定义了一个过程EnumWindowsProc,通过GetWindowLong来完成对打开程序窗口的枚举;通过ListBox实现程序的列举;并将每次调用的枚举结果存放在/info目录下当天日期为文件名称的.txt文件里;

四、主要实现代码:

初始化部分(全局):

HWND hwnd;
HWND m_hwndFind[100] = { 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};
int m_num = 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];

 

枚举处理的代码:

 

 CTime year,day,month,hour,minute,second;
 CStdioFile fp;
 CTime current = CTime::GetCurrentTime();
 year = current.GetYear();
 month = current.GetMonth();
 day = current.GetDay();
 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::modeNoTruncate | 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 ListBox
 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)标题名称:",i);
  strcat(m_name,m_store);
  strcat(m_name,"      类名称:");
  strcat(m_name,m_strClass);
  m_strList.AddString(m_name);
  fp.SeekToEnd();
  fp.WriteString(m_name);
  fp.WriteString("\n");
 }
 fp.SeekToEnd();
 fp.WriteString("\n\n");
 hour = current.GetHour();
 minute=current.GetMinute();
 second = current.GetSecond();
 sprintf(m_FilePath,"********************%02d时%02d分%02d秒写入***********************",hour,minute,second);
 fp.WriteString(m_FilePath);
 fp.WriteString("\n\n");
 fp.Close();

 

重新枚举处理:

 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 count 

 

枚举窗口句柄的函数:

在头文件中声明为:

static BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);

实现部分:

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;
}

调试环境:

win2000 adv server + vc6.0 + vsp5.0

 

 

 

 

 

 

本文地址:http://com.8s8s.com/it/it1164.htm