我想取得CMDIFrameWnd內的所有已打開子窗口的標示,這個標示可以是窗口句柄,也可以是窗口對象的指針,總之什么都行。不知改如何做?
11 个解决方案
BOOL EnumChildWindows(
HWND hWndParent, // handle to parent window
WNDENUMPROC lpEnumFunc, // callback function
LPARAM lParam // application-defined value
);
其實,我的目的是:類似VC++的工作區,雙擊工作區目錄樹里的一個文件時,這個文件對應的窗口只打開一次,如果該窗口已打開后,窗口激活。
std::vector<HWND> vecAllChildWnds; //窗口數組
EnumChildWindows( this, EnumChildProc,(LPARAM)(&vecAllChildWnds) );
定義一個callback函數(非類的成員函數)
BOOL CALLBACK EnumChildProc(
HWND hwnd, // handle to child window
LPARAM lParam // application-defined value
)
{
//把hwnd添加到你的窗口容器中
((std::vector<HWND>*)lParam) ->push_back(hwnd);
}
如果是常規的文檔/視圖/框架結構的話可以這樣:
CObList lstFrames;
CDocManager *pDocManager = AfxGetApp()->m_pDocManager;
POSITION pos = pDocManager->GetFirstDocTemplatePosition();
while(pos)
{
CDocTemplate* pTemplate = (CDocTemplate*)pDocManager->GetNextDocTemplate(pos);
{
POSITION pos = pTemplate->GetFirstDocPosition();
while (pos)
{
CDocument* pDoc = pTemplate->GetNextDoc(pos);
{
POSITION pos = pDoc->GetFirstViewPosition();
while(pos)
{
CView *pView = pDoc->GetNextView(pos);
CFrameWnd *pFrame = pView->GetParentFrame();
if(lstFrames.Find(pFrame) == NULL)
{
lstFrames.AddTail(pFrame);
}
}
}
}
}
}
pos = lstFrames.GetHeadPosition();
while(pos)
{
CFrameWnd *pFrame = (CFrameWnd*)lstFrames.GetNext(pos);
CString sTitle;
pFrame->GetWindowText(sTitle);
AfxMessageBox(sTitle);
}
不是常規的文檔/視圖/框架結構
GetFirstDocPosition每次都是NULL
EnumChildWindows(m_hWnd,EnumChildProc,(LPARAM)(&vecAllChildWnds));
報錯如下:
error C2664: 'EnumChildWindows' : cannot convert parameter 2 from 'bool (struct HWND__ *,long)' to 'int (__stdcall *)(struct HWND__ *,long)'
None of the functions with this name in scope match the target type