www.pudn.com > UltraSound.rar > MainFrm.cpp


// MainFrm.cpp : implementation of the CMainFrame class 
// 
 
#include "stdafx.h" 
#include "UltraSound.h" 
 
#include "MainFrm.h" 
#include "cderr.h" 
//--------------------------------------- 
#include "UltraSoundDoc.h" 
#include "UltraSoundView.h" 
#include "cv.h" 
//--------------------------------------- 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
CMapStringToString g_map; 
CString g_name; 
///////////////////////////////////////////////////////////////////////////// 
// CMainFrame 
 
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) 
 
 
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) 
	//{{AFX_MSG_MAP(CMainFrame) 
	ON_WM_CREATE() 
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen) 
	//}}AFX_MSG_MAP 
	ON_COMMAND(ID_VIEW_CUSTOMIZE, OnViewCustomize) 
	ON_REGISTERED_MESSAGE(BCGM_RESETTOOLBAR, OnToolbarReset) 
	ON_REGISTERED_MESSAGE(BCGM_TOOLBARMENU, OnToolbarContextMenu) 
	ON_COMMAND(ID_VIEW_WORKSPACE, OnViewWorkspace) 
	ON_UPDATE_COMMAND_UI(ID_VIEW_WORKSPACE, OnUpdateViewWorkspace) 
	ON_COMMAND(ID_VIEW_OUTPUT, OnViewOutput) 
	ON_UPDATE_COMMAND_UI(ID_VIEW_OUTPUT, OnUpdateViewOutput) 
END_MESSAGE_MAP() 
 
static UINT indicators[] = 
{ 
	ID_SEPARATOR,           // status line indicator 
	ID_SEPARATOR, 
	ID_INDICATOR_CAPS, 
	ID_INDICATOR_NUM, 
	ID_INDICATOR_SCRL, 
}; 
 
///////////////////////////////////////////////////////////////////////////// 
// CMainFrame construction/destruction 
 
CMainFrame::CMainFrame() 
{ 
	// TODO: add member initialization code here 
	 
} 
 
CMainFrame::~CMainFrame() 
{ 
} 
 
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{ 
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1) 
		return -1; 
 
	// enable Office XP look: 
	CBCGPVisualManager::SetDefaultManager (RUNTIME_CLASS (CBCGPVisualManagerXP)); 
 
	CBCGPToolBar::EnableQuickCustomization (); 
 
	if (!m_wndMenuBar.Create (this)) 
	{ 
		TRACE0("Failed to create menubar\n"); 
		return -1;      // fail to create 
	} 
 
	m_wndMenuBar.SetBarStyle(m_wndMenuBar.GetBarStyle() | CBRS_SIZE_DYNAMIC); 
 
	// Detect color depth. 256 color toolbars can be used in the 
	// high or true color modes only (bits per pixel is > 8): 
	CClientDC dc (this); 
	BOOL bIsHighColor = dc.GetDeviceCaps (BITSPIXEL) > 8; 
 
	UINT uiToolbarHotID = bIsHighColor ? IDB_TOOLBAR256 : 0; 
	UINT uiToolbarColdID = bIsHighColor ? IDB_TOOLBARCOLD256 : 0; 
 
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP 
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || 
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME, uiToolbarColdID, 0, FALSE, 0, 0, uiToolbarHotID)) 
	{ 
		TRACE0("Failed to create toolbar\n"); 
		return -1;      // fail to create 
	} 
	if (!m_wndStatusBar.Create(this) || 
		!m_wndStatusBar.SetIndicators(indicators, 
		  sizeof(indicators)/sizeof(UINT))) 
	{ 
		TRACE0("Failed to create status bar\n"); 
		return -1;      // fail to create 
	} 
 
	CImageList imagesWorkspace; 
	imagesWorkspace.Create (IDB_WORKSPACE, 16, 0, RGB (255, 0, 255)); 
	 
	if (!m_wndWorkSpace.Create (_T("Images"), this, CRect (0, 0, 200, 200), 
		TRUE, ID_VIEW_WORKSPACE, 
		WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI)) 
	{ 
		TRACE0("Failed to create Workspace bar\n"); 
		return FALSE;      // fail to create 
	} 
 
	m_wndWorkSpace.SetIcon (imagesWorkspace.ExtractIcon (0), FALSE); 
 
	if (!m_wndOutput.Create (_T("Output"), this, CRect (0, 0, 150, 150), 
		TRUE /* Has gripper */, ID_VIEW_OUTPUT, 
		WS_CHILD | WS_VISIBLE | CBRS_BOTTOM)) 
	{ 
		TRACE0("Failed to create output bar\n"); 
		return -1;      // fail to create 
	} 
 
	CString strMainToolbarTitle; 
	strMainToolbarTitle.LoadString (IDS_MAIN_TOOLBAR); 
	m_wndToolBar.SetWindowText (strMainToolbarTitle); 
	// TODO: Delete these three lines if you don't want the toolbar to 
	//  be dockable 
	m_wndMenuBar.EnableDocking(CBRS_ALIGN_ANY); 
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); 
	m_wndWorkSpace.EnableDocking(CBRS_ALIGN_ANY); 
	m_wndOutput.EnableDocking(CBRS_ALIGN_ANY); 
	EnableDocking(CBRS_ALIGN_ANY); 
	EnableAutoHideBars(CBRS_ALIGN_ANY); 
	DockControlBar(&m_wndMenuBar); 
	DockControlBar(&m_wndToolBar); 
	DockControlBar (&m_wndWorkSpace); 
	DockControlBar(&m_wndOutput); 
 
	return 0; 
} 
 
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	if( !CFrameWnd::PreCreateWindow(cs) ) 
		return FALSE; 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return TRUE; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMainFrame diagnostics 
 
#ifdef _DEBUG 
void CMainFrame::AssertValid() const 
{ 
	CFrameWnd::AssertValid(); 
} 
 
void CMainFrame::Dump(CDumpContext& dc) const 
{ 
	CFrameWnd::Dump(dc); 
} 
 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMainFrame message handlers 
 
 
void CMainFrame::OnViewCustomize() 
{ 
	//------------------------------------ 
	// Create a customize toolbars dialog: 
	//------------------------------------ 
	CBCGPToolbarCustomize* pDlgCust = new CBCGPToolbarCustomize (this, 
		TRUE /* Automatic menus scaning */ 
		); 
 
	pDlgCust->Create (); 
} 
 
LRESULT CMainFrame::OnToolbarContextMenu(WPARAM,LPARAM lp) 
{ 
	CPoint point (BCG_GET_X_LPARAM(lp), BCG_GET_Y_LPARAM(lp)); 
 
	CMenu menu; 
	VERIFY(menu.LoadMenu (IDR_POPUP_TOOLBAR)); 
 
	CMenu* pPopup = menu.GetSubMenu(0); 
	ASSERT(pPopup != NULL); 
 
	CBCGPPopupMenu* pPopupMenu = new CBCGPPopupMenu; 
	pPopupMenu->Create (this, point.x, point.y, pPopup->Detach ()); 
 
	return 0; 
} 
 
afx_msg LRESULT CMainFrame::OnToolbarReset(WPARAM /*wp*/,LPARAM) 
{ 
	// TODO: reset toolbar with id = (UINT) wp to its initial state: 
	// 
	// UINT uiToolBarId = (UINT) wp; 
	// if (uiToolBarId == IDR_MAINFRAME) 
	// { 
	//		do something with m_wndToolBar 
	// } 
 
	return 0; 
} 
 
BOOL CMainFrame::OnShowPopupMenu (CBCGPPopupMenu* pMenuPopup) 
{ 
	//--------------------------------------------------------- 
	// Replace ID_VIEW_TOOLBARS menu item to the toolbars list: 
	//--------------------------------------------------------- 
    CFrameWnd::OnShowPopupMenu (pMenuPopup); 
 
    if (pMenuPopup != NULL && 
		pMenuPopup->GetMenuBar ()->CommandToIndex (ID_VIEW_TOOLBARS) >= 0) 
    { 
		if (CBCGPToolBar::IsCustomizeMode ()) 
		{ 
			//---------------------------------------------------- 
			// Don't show toolbars list in the cuztomization mode! 
			//---------------------------------------------------- 
			return FALSE; 
		} 
 
		pMenuPopup->RemoveAllItems (); 
 
		CMenu menu; 
		VERIFY(menu.LoadMenu (IDR_POPUP_TOOLBAR)); 
 
		CMenu* pPopup = menu.GetSubMenu(0); 
		ASSERT(pPopup != NULL); 
 
		pMenuPopup->GetMenuBar ()->ImportFromMenu (*pPopup, TRUE); 
    } 
 
	return TRUE; 
} 
 
void CMainFrame::OnViewWorkspace()  
{ 
	ShowControlBar (&m_wndWorkSpace, 
					!(m_wndWorkSpace.IsVisible ()), 
					FALSE, TRUE); 
	RecalcLayout (); 
} 
 
void CMainFrame::OnUpdateViewWorkspace(CCmdUI* pCmdUI)  
{ 
	pCmdUI->SetCheck (m_wndWorkSpace.IsVisible ()); 
} 
 
void CMainFrame::OnViewOutput()  
{ 
	ShowControlBar (&m_wndOutput, 
					!(m_wndOutput.IsVisible ()), 
					FALSE, TRUE); 
	RecalcLayout (); 
} 
 
void CMainFrame::OnUpdateViewOutput(CCmdUI* pCmdUI)  
{ 
	pCmdUI->SetCheck (m_wndOutput.IsVisible ()); 
} 
 
void CMainFrame::OnFileOpen()  
{ 
	char path[50]; 
	GetCurrentDirectory(255,path); 
	const int MAX_MULTISELECT = 2000; 
 
	CFileDialog dlgFile(TRUE); 
 
	dlgFile.m_ofn.Flags |= OFN_ALLOWMULTISELECT|OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_HIDEREADONLY; 
  
	CString strFilter; 
	VERIFY(strFilter.LoadString(AFX_IDS_ALLFILTER)); 
	strFilter += '\0';   // next string please 
	strFilter += "*.*"; 
	strFilter += '\0';   // last string 
	dlgFile.m_ofn.lpstrFilter = strFilter; 
 
	dlgFile.m_ofn.lpstrTitle = "Choose Files"; 
 
	CString fileName; 
	dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH * MAX_MULTISELECT); 
	dlgFile.m_ofn.nMaxFile = _MAX_PATH * MAX_MULTISELECT; 
 
	if (dlgFile.DoModal() != IDOK)  
	{ 
		#ifdef _DEBUG 
		switch (::CommDlgExtendedError())  
		{ 
			case FNERR_BUFFERTOOSMALL:	TRACE("The buffer for a filename is too small. (This buffer is pointed to by the lpstrFile member of the structure for a common dialog box.)\n"); break; 
			case FNERR_FILENAMECODES:	TRACE("Error codes for the Open and Save As common dialog boxes. these errors are in the range 0x3000 through 0x3FFF.\n"); break; 
			case FNERR_INVALIDFILENAME: TRACE("A filename is invalid.\n"); break; 
			case FNERR_SUBCLASSFAILURE: TRACE("An attempt to subclass a list box failed because insufficient memory was available.\n"); break; 
		} 
		#endif 
		return; 
	} 
 
	BeginWaitCursor(); 
 
	CMainFrame *pMain =(CMainFrame *)AfxGetMainWnd(); 
	CDocument * pCurrentDoc =pMain->GetActiveDocument(); 
	CUltraSoundDoc* pDoc = (CUltraSoundDoc* )(pCurrentDoc); 
	CUltraSoundView *pView=(CUltraSoundView *)pMain->GetActiveView(); 
	CImageListCtrl* p_list = m_wndWorkSpace.GetList(); 
 
	if(pDoc->m_ImgNum >0) 
	{ 
		for(int i=0;im_ImgNum;i++) 
		{ 
			cvReleaseImage(&pDoc->m_pImageList[i]); 
			pDoc->m_pImageList[i] = NULL; 
		} 
	} 
	pDoc->m_ImgNum = 0; 
	 
	if(p_list->GetItemCount()>>0) 
	{ 
		p_list->DeleteAllItems(); 
		g_map.RemoveAll(); 
	} 
 
	if(pView->m_EndImg!=NULL) 
	{ 
		cvReleaseImage(&pView->m_EndImg); 
		pView->m_EndImg = NULL; 
	} 
		 
	POSITION curfilepos = dlgFile.GetStartPosition(); 
	CString curname = ""; 
	int Filecount = 0; 
 
	while (curfilepos != NULL) 
	 { 
		curname = dlgFile.GetNextPathName(curfilepos); 
		Filecount ++; 
	 } 
 
	if(Filecount <= 1) 
		return; 
 
	pDoc->m_ImgNum = Filecount; 
 
	CString m_strFolder; 
	m_strFolder = dlgFile.m_ofn.lpstrFile; 
	g_name = m_strFolder; 
	SetWindowText(g_name); 
 
	m_strFolder = m_strFolder+"\\"; 
	int PathLenth = m_strFolder.GetLength(); 
	curfilepos = dlgFile.GetStartPosition(); 
	Filecount = 0; 
	while(curfilepos!= NULL) 
	{ 
		curname = dlgFile.GetNextPathName(curfilepos); 
			 
		int FileLenth = curname.GetLength(); 
		CString name = curname.Right(FileLenth-PathLenth); 
 
		g_map.SetAt(name,curname); 
		 
		p_list->InsertItem(Filecount,name); 
		Filecount++; 
	} 
	p_list->SortItems(NULL,(LPARAM) p_list); 
 
	for(int i=0;iGetItemText(i,0); 
		CString name; 
		g_map.Lookup(str,name); 
		pDoc->m_pImageList[i] = cvLoadImage(name,-1); 
	} 
 
	EndWaitCursor(); 
}