宝贝原来你是攻百度云:如何用EnumWindow函数得到所有窗口的句柄和标题(如果有的话)

来源:百度文库 编辑:中科新闻网 时间:2024/05/07 14:24:10
如何用EnumWindow函数得到所有窗口的句柄和标题(如果有的话):
我在VC++中用EnumWindow函数和一个回调函数实现得到所有窗口的句柄和标题,但无法实现让它们显示在Clistbox中,部分源码如下:
// test03.cpp : Defines the class behaviors for the application.
// CTest03App
BEGIN_MESSAGE_MAP(CTest03App, CWinApp)
//{{AFX_MSG_MAP(CTest03App)
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

CTest03App::CTest03App()
{
}
CTest03App theApp;
int mycount;
BOOL CTest03App::InitInstance()
{
AfxEnableControlContainer();
CTest03Dlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
}
else if (nResponse == IDCANCEL)
{
}
return FALSE;
}

BOOL CALLBACK EnumWindowsProc_1(HWND hwnd,LPARAM lparam)
{
CString text;
char lpWinTitle[256];
::GetWindowText(hwnd,lpWinTitle,256-1);
CString m_strTitle;
m_strTitle.Format("%s",lpWinTitle);
if(m_strTitle.Find("Internet Explorer")!=-1){
mycount++;
text.Format("IE的标题为\"%s\"",lpWinTitle);
CTest03Dlg::m_list.AddString(text);
AfxMessageBox(text) ;
}
return TRUE ;
}

// test03Dlg.cpp : implementation file
//

CTest03Dlg::CTest03Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CTest03Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTest03Dlg)
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTest03Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTest03Dlg)
DDX_Control(pDX, IDC_LIST1, m_list);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTest03Dlg, CDialog)
//{{AFX_MSG_MAP(CTest03Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CTest03Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE);
return TRUE;
}

void CTest03Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

HCURSOR CTest03Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CTest03Dlg::OnButton1()
{
extern int mycount;
if(::EnumWindows(EnumWindowsProc_1,0))
AfxMessageBox("没有找到IE窗口") ;
mycount=0;

}
编译后发现有下面的错误,不明白是怎么回事,还请高手指点:
D:\TEST\test03\test03.cpp(89) : error C2228: left of '.AddString' must have class/struct/union type
Error executing cl.exe.
可能是AddString数据类型不正确吧

我和你的写法不一样,可能是我还没明白你的意思
我在上面加多了4个int定义,下面加了个强制定义
我的上半部分大多用if语句完成.