- // findDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "find.h"
- #include "findDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CFindDlg dialog
- CFindDlg::CFindDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CFindDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CFindDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CFindDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CFindDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CFindDlg, CDialog)
- //{{AFX_MSG_MAP(CFindDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BTN_FIND, OnBtnFind)
- ON_BN_CLICKED(IDC_BTN_GET, OnBtnGet)
- ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
- ON_BN_CLICKED(IDC_BTN_CAPTURE, OnBtnCapture)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFindDlg message handlers
- BOOL CFindDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- InitControls();
- return TRUE; // return TRUE unless you set the focus to a control
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CFindDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- 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;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CFindDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CFindDlg::OnBtnFind()
- {
- HWND hWnd;
- if (hWnd = /*::FindWindow(NULL, _T("Log View"))*/FindWindowEx())
- {
- // AfxMessageBox(_T("find!"));
- }
- AfxMessageBox(_T("end"));
- }
- //双层递归查找子窗口与兄弟窗口//可以使用队列轻松解决此问题,但是得小心释放内存
- HWND CFindDlg::FindWindowEx(HWND hwndParent, HWND hwndChildAfter, LPCTSTR lpszClass, LPCTSTR lpszWindow)
- {
- HWND hWndFind, hWnd;
- if (hWndFind = ::FindWindowEx(hwndParent, hwndChildAfter, lpszClass, lpszWindow))
- {
- // TCHAR strTitle[256];
- // ::GetWindowText(hWndFind, strTitle, 256);
- // TRACE(_T("%s/n"), strTitle);
- if (hWnd = ::FindWindowEx(hwndParent, hwndChildAfter, NULL, _T("Item View")))
- {
- m_hFindWnd = hWnd;
- AfxMessageBox(_T("find!"));
- return hWnd;
- }
- FindWindowEx(hWndFind, NULL, lpszClass, lpszWindow);
- FindWindowEx(hwndParent, hWndFind, lpszClass, lpszWindow);
- }
- return NULL;
- }
- HWND CFindDlg::FindWindowEx()
- {
- return FindWindowEx(NULL, NULL, NULL, NULL);
- }
- HWND CFindDlg::FindSibling(HWND hWndParent, HWND hwndChildAfter, LPCTSTR lpszClass, LPCTSTR lpszWindow)
- {
- HWND hWnd;
- // if (hWnd = FindWindowEx(hWndParent,
- return NULL;
- }
- HWND CFindDlg::FindChildren(HWND hWndParent, HWND hwndChildAfter, LPCTSTR lpszClass, LPCTSTR lpszWindow)
- {
- HWND hWnd;
- // if (hWnd = FindWindowEx(hWndParent, NULL, lpszClass, lpszWindow))
- // return FindWindowEx(hwndParent, hWnd, lpszWindow);
- return NULL;
- }
- void CFindDlg::OnBtnGet()
- {
- // HWND hWnd = ::GetWindow();
- //HWND hWnd = ::FindWindow(_T("List"), NULL);
- HWND hWnd = ::GetDlgItem(m_hFindWnd, 0);
- if (hWnd)
- {
- AfxMessageBox("findx");
- }
- else
- {
- CString str;
- str.Format(_T("??%d??"), ::GetLastError());
- AfxMessageBox(str);
- }
- hWnd = ::GetWindow(hWnd, GW_CHILD);//::GetDlgItem(m_hFindWnd, 0);
- if (hWnd)
- {
- AfxMessageBox("find");
- }
- else
- {
- CString str;
- str.Format(_T("%d"), ::GetLastError());
- AfxMessageBox(str);
- }
- }
- void CFindDlg::OnButton2()
- {
- CListCtrl *pList = static_cast<CListCtrl*>(GetDlgItem(IDC_LIST2));
- if (pList == NULL)
- {
- AfxMessageBox("not found");
- return;
- }
- TCHAR str[256];
- ::GetWindowText(pList->m_hWnd, str, 256);
- CListCtrl list;
- list.Attach(pList->m_hWnd);
- list.GetItemText(0, 1, str, 256);
- /* UINT i, uSelectedCount = pList->GetSelectedCount();
- int nItem = -1;
- // Update all of the selected items.
- if (uSelectedCount > 0)
- {
- for (i=0;i < uSelectedCount;i++)
- {
- nItem = pList->GetNextItem(nItem, LVNI_SELECTED);
- pList->GetItemText(nItem, 0, str, 256);
- ASSERT(nItem != -1);
- pList->Update(nItem);
- }
- }
- */
- AfxMessageBox(str);
- list.Detach();//记住要这一句,否则会出错
- HWND hWnd = ::FindWindowEx(m_hFindWnd, NULL, _T("LISTVIEW"), NULL);
- if (!hWnd)
- {
- AfxMessageBox("sign");
- }
- }
- void CFindDlg::InitControls()
- {
- CListCtrl *pList = static_cast<CListCtrl*>(GetDlgItem(IDC_LIST2));
- if (pList == NULL)
- {
- AfxMessageBox("not found");
- return;
- }
- pList->SetExtendedStyle(pList->GetExtendedStyle() | LVS_EX_FULLROWSELECT);
- pList->InsertColumn(0, _T("title"), LVCFMT_LEFT, 80);
- pList->InsertColumn(1, _T("comment"), LVCFMT_LEFT, 80);
- pList->InsertColumn(2, _T("credit"), LVCFMT_LEFT, 80);
- pList->InsertItem(0, _T("shit"));
- pList->SetItemText(0, 1, _T("very good"));
- pList->SetItemText(0, 2, _T("amazing"));
- pList->InsertItem(1, _T("shit"));
- pList->SetItemText(1, 1, _T("very good"));
- pList->SetItemText(1, 2, _T("amazing"));
- }
- void CFindDlg::OnBtnCapture()
- {
- CString str;
- CPoint pt;
- GetCursorPos(&pt);
- /*HWND hWnd*/CListCtrl *pList = static_cast<CListCtrl*>(WindowFromPoint(pt));
- if (pList)
- {
- str.Format(_T("%d"), pList->m_hWnd);
- AfxMessageBox(str);
- return;
- }
- // str.Format(_T("%d"), pList->m_hWnd);
- // AfxMessageBox(str);
- // str = pList->GetItemText(pList->GetNextItem(-1, LVNI_SELECTED), 0);
- // AfxMessageBox(str);
- HWND hWnd = ::WindowFromPoint(pt);
- CListCtrl list;
- list.Attach(hWnd);
- AfxMessageBox(list.GetItemText(0, 1));
- list.Detach();
- }