没有结果,看来只有用钩子了

来源:互联网 发布:设ab都是n阶对称矩阵 编辑:程序博客网 时间:2024/04/30 00:21
  1. // findDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "find.h"
  5. #include "findDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CFindDlg dialog
  13. CFindDlg::CFindDlg(CWnd* pParent /*=NULL*/)
  14.     : CDialog(CFindDlg::IDD, pParent)
  15. {
  16.     //{{AFX_DATA_INIT(CFindDlg)
  17.         // NOTE: the ClassWizard will add member initialization here
  18.     //}}AFX_DATA_INIT
  19.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  20.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  21. }
  22. void CFindDlg::DoDataExchange(CDataExchange* pDX)
  23. {
  24.     CDialog::DoDataExchange(pDX);
  25.     //{{AFX_DATA_MAP(CFindDlg)
  26.         // NOTE: the ClassWizard will add DDX and DDV calls here
  27.     //}}AFX_DATA_MAP
  28. }
  29. BEGIN_MESSAGE_MAP(CFindDlg, CDialog)
  30.     //{{AFX_MSG_MAP(CFindDlg)
  31.     ON_WM_PAINT()
  32.     ON_WM_QUERYDRAGICON()
  33.     ON_BN_CLICKED(IDC_BTN_FIND, OnBtnFind)
  34.     ON_BN_CLICKED(IDC_BTN_GET, OnBtnGet)
  35.     ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  36.     ON_BN_CLICKED(IDC_BTN_CAPTURE, OnBtnCapture)
  37.     //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CFindDlg message handlers
  41. BOOL CFindDlg::OnInitDialog()
  42. {
  43.     CDialog::OnInitDialog();
  44.     // Set the icon for this dialog.  The framework does this automatically
  45.     //  when the application's main window is not a dialog
  46.     SetIcon(m_hIcon, TRUE);         // Set big icon
  47.     SetIcon(m_hIcon, FALSE);        // Set small icon
  48.     
  49.     // TODO: Add extra initialization here
  50.     InitControls();
  51.     
  52.     return TRUE;  // return TRUE  unless you set the focus to a control
  53. }
  54. // If you add a minimize button to your dialog, you will need the code below
  55. //  to draw the icon.  For MFC applications using the document/view model,
  56. //  this is automatically done for you by the framework.
  57. void CFindDlg::OnPaint() 
  58. {
  59.     if (IsIconic())
  60.     {
  61.         CPaintDC dc(this); // device context for painting
  62.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  63.         // Center icon in client rectangle
  64.         int cxIcon = GetSystemMetrics(SM_CXICON);
  65.         int cyIcon = GetSystemMetrics(SM_CYICON);
  66.         CRect rect;
  67.         GetClientRect(&rect);
  68.         int x = (rect.Width() - cxIcon + 1) / 2;
  69.         int y = (rect.Height() - cyIcon + 1) / 2;
  70.         // Draw the icon
  71.         dc.DrawIcon(x, y, m_hIcon);
  72.     }
  73.     else
  74.     {
  75.         CDialog::OnPaint();
  76.     }
  77. }
  78. // The system calls this to obtain the cursor to display while the user drags
  79. //  the minimized window.
  80. HCURSOR CFindDlg::OnQueryDragIcon()
  81. {
  82.     return (HCURSOR) m_hIcon;
  83. }
  84. void CFindDlg::OnBtnFind() 
  85. {
  86.     HWND hWnd;
  87.     if (hWnd = /*::FindWindow(NULL, _T("Log View"))*/FindWindowEx())
  88.     {
  89. //      AfxMessageBox(_T("find!"));
  90.     }
  91.     AfxMessageBox(_T("end"));
  92. }
  93. //双层递归查找子窗口与兄弟窗口//可以使用队列轻松解决此问题,但是得小心释放内存
  94. HWND CFindDlg::FindWindowEx(HWND hwndParent, HWND hwndChildAfter, LPCTSTR lpszClass, LPCTSTR lpszWindow)
  95. {
  96.     HWND hWndFind, hWnd;
  97.     if (hWndFind = ::FindWindowEx(hwndParent, hwndChildAfter, lpszClass, lpszWindow))
  98.     {
  99. //      TCHAR strTitle[256];
  100. //      ::GetWindowText(hWndFind, strTitle, 256);
  101. //      TRACE(_T("%s/n"), strTitle);
  102.         if (hWnd = ::FindWindowEx(hwndParent, hwndChildAfter, NULL, _T("Item View")))
  103.         {
  104.             m_hFindWnd = hWnd;
  105.             AfxMessageBox(_T("find!"));
  106.             return hWnd;
  107.         }
  108.         FindWindowEx(hWndFind, NULL, lpszClass, lpszWindow);
  109.         FindWindowEx(hwndParent, hWndFind, lpszClass, lpszWindow);
  110.     }
  111.     return NULL;
  112. }
  113. HWND CFindDlg::FindWindowEx()
  114. {
  115.     return FindWindowEx(NULL, NULL, NULL, NULL);
  116. }
  117. HWND CFindDlg::FindSibling(HWND hWndParent, HWND hwndChildAfter, LPCTSTR lpszClass, LPCTSTR lpszWindow)
  118. {
  119.     HWND hWnd;
  120. //  if (hWnd = FindWindowEx(hWndParent,
  121.     return NULL;
  122. }
  123. HWND CFindDlg::FindChildren(HWND hWndParent, HWND hwndChildAfter, LPCTSTR lpszClass, LPCTSTR lpszWindow)
  124. {
  125.     HWND hWnd;
  126. //  if (hWnd = FindWindowEx(hWndParent, NULL, lpszClass, lpszWindow))
  127. //      return FindWindowEx(hwndParent, hWnd, lpszWindow);
  128.     return NULL;
  129. }
  130. void CFindDlg::OnBtnGet() 
  131. {
  132. //  HWND hWnd = ::GetWindow();  
  133.     //HWND hWnd = ::FindWindow(_T("List"), NULL);
  134.     HWND hWnd = ::GetDlgItem(m_hFindWnd, 0);
  135.     if (hWnd)
  136.     {
  137.         AfxMessageBox("findx");
  138.     }
  139.     else
  140.     {
  141.         CString str;
  142.         str.Format(_T("??%d??"), ::GetLastError());
  143.         AfxMessageBox(str);
  144.     }
  145.     hWnd = ::GetWindow(hWnd, GW_CHILD);//::GetDlgItem(m_hFindWnd, 0);
  146.     if (hWnd)
  147.     {
  148.         AfxMessageBox("find");
  149.     }
  150.     else
  151.     {
  152.         CString str;
  153.         str.Format(_T("%d"), ::GetLastError());
  154.         AfxMessageBox(str);
  155.     }
  156. }
  157. void CFindDlg::OnButton2() 
  158. {
  159.     CListCtrl *pList = static_cast<CListCtrl*>(GetDlgItem(IDC_LIST2));
  160.     if (pList == NULL)
  161.     {
  162.         AfxMessageBox("not found");
  163.         return;
  164.     }
  165.     TCHAR str[256];
  166.     ::GetWindowText(pList->m_hWnd, str, 256);
  167.     CListCtrl list;
  168.     list.Attach(pList->m_hWnd);
  169.     list.GetItemText(0, 1, str, 256);
  170. /*  UINT i, uSelectedCount = pList->GetSelectedCount();
  171.     int  nItem = -1;
  172.     
  173.     // Update all of the selected items.
  174.     if (uSelectedCount > 0)
  175.     {
  176.         for (i=0;i < uSelectedCount;i++)
  177.         {
  178.             nItem = pList->GetNextItem(nItem, LVNI_SELECTED);
  179.             pList->GetItemText(nItem, 0, str, 256);
  180.             ASSERT(nItem != -1);
  181.             pList->Update(nItem); 
  182.         }
  183.     }
  184. */
  185.     AfxMessageBox(str);
  186.     list.Detach();//记住要这一句,否则会出错
  187.     HWND hWnd = ::FindWindowEx(m_hFindWnd, NULL, _T("LISTVIEW"), NULL);
  188.     if (!hWnd)
  189.     {
  190.         AfxMessageBox("sign");
  191.     }
  192. }
  193. void CFindDlg::InitControls()
  194. {
  195.     CListCtrl *pList = static_cast<CListCtrl*>(GetDlgItem(IDC_LIST2));
  196.     if (pList == NULL)
  197.     {
  198.         AfxMessageBox("not found");
  199.         return;
  200.     }
  201.     
  202.     pList->SetExtendedStyle(pList->GetExtendedStyle() | LVS_EX_FULLROWSELECT);
  203.     pList->InsertColumn(0, _T("title"), LVCFMT_LEFT, 80);
  204.     pList->InsertColumn(1, _T("comment"), LVCFMT_LEFT, 80);
  205.     pList->InsertColumn(2, _T("credit"), LVCFMT_LEFT, 80);
  206.     pList->InsertItem(0, _T("shit"));
  207.     pList->SetItemText(0, 1, _T("very good"));
  208.     pList->SetItemText(0, 2, _T("amazing"));
  209.     pList->InsertItem(1, _T("shit"));
  210.     pList->SetItemText(1, 1, _T("very good"));
  211.     pList->SetItemText(1, 2, _T("amazing"));
  212. }
  213. void CFindDlg::OnBtnCapture() 
  214. {
  215.     CString str;
  216.     CPoint pt;
  217.     GetCursorPos(&pt);
  218.     /*HWND hWnd*/CListCtrl *pList = static_cast<CListCtrl*>(WindowFromPoint(pt));
  219.     if (pList)
  220.     {
  221.         str.Format(_T("%d"), pList->m_hWnd);
  222.         AfxMessageBox(str);
  223.         return;
  224.     }
  225. //  str.Format(_T("%d"), pList->m_hWnd);
  226. //  AfxMessageBox(str);
  227. //  str = pList->GetItemText(pList->GetNextItem(-1, LVNI_SELECTED), 0);
  228. //  AfxMessageBox(str);
  229.     HWND hWnd = ::WindowFromPoint(pt);
  230.     CListCtrl list;
  231.     list.Attach(hWnd);
  232.     
  233.     AfxMessageBox(list.GetItemText(0, 1));
  234.     list.Detach();
  235. }