///////////////////////////////////////////////////////////////////////////// // Mockup5Dlg.cpp - Demonstrate displaying documents of various types, // // e.g., txt, doc, h, cpp, jpg, bmp, vsd. Uses browser // // control with very little design effort to accomplish // // rather sophisticated display operations. // // // // Jim Fawcett, CSE691 - SW Modeling and Analysis, Fall 2000 // ///////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "Mockup5.h" #include "Mockup5Dlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } // BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMockup5Dlg dialog CMockup5Dlg::CMockup5Dlg(CWnd* pParent /*=NULL*/) : CDialog(CMockup5Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CMockup5Dlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CMockup5Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMockup5Dlg) DDX_Control(pDX, IDC_COMBO1, m_CBcontrol); DDX_Control(pDX, IDC_EXPLORER1, m_browser); DDX_CBString(pDX, IDC_COMBO1, m_CBstring); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CMockup5Dlg, CDialog) //{{AFX_MSG_MAP(CMockup5Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, OnBack) ON_BN_CLICKED(IDC_BUTTON2, OnForward) ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1) //}}AFX_MSG_MAP END_MESSAGE_MAP() // ///////////////////////////////////////////////////////////////////////////// // CMockup5Dlg message handlers //----< initialize dialog class members now that window exists >------- BOOL CMockup5Dlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 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 ///////////////////////////////////////////////////////////////// // put ext names in combo box for user selection m_CBcontrol.AddString("txt"); m_CBcontrol.AddString("jpg"); m_CBcontrol.AddString("bmp"); m_CBcontrol.AddString("doc"); m_CBcontrol.AddString("vsd"); m_CBcontrol.AddString("h"); m_CBcontrol.AddString("cpp"); m_CBcontrol.AddString("htm"); m_CBcontrol.SetCurSel(5); // choose 5th in sorted order // ///////////////////////////////////////////////////////////////// // put current ext name in comparison string int index = m_CBcontrol.GetCurSel(); m_CBcontrol.GetLBText(index,m_CBstring); m_CBstring.Insert(0,'.'); m_CBstring.MakeLower(); ///////////////////////////////////////////////////////////////// // make array of strings for all filenames in document directory // // first, save current directory char StartDir[MAX_PATH]; int len = GetCurrentDirectory(MAX_PATH,StartDir); StartDir[len] = '\0'; ///////////////////////////////////////////////////////////////// // next, set current directory to document directory const char *WorkDir = "C:\\su\\Welch Allyn\\mockup5\\documents"; if(!SetCurrentDirectory(WorkDir)) { CString temp("--- can't find document directory: "); temp += WorkDir; temp += CString("---\r\nSet CString WorkDir in "); temp += CString("Mockup5Dlg::OnInitDlg to name of document directory."); MessageBox(temp,"Run-time Error"); exit(1); } ///////////////////////////////////////////////////////////////// // next, find all the files in document directory and put names // into an array of CStrings index = 0; jpgIndex = 0, jpgMax = 0; jpgArray = new CString[100]; WIN32_FIND_DATA fd; /////////////////////////////////////// // look for first file name HANDLE fileSearch = FindFirstFile("*.*",&fd); if(INVALID_HANDLE_VALUE == fileSearch) { MessageBox("no files to display"); exit(1); } // /////////////////////////////////////// // skip directories . and .. if(CString(fd.cFileName) != "." && CString(fd.cFileName) != "..") { jpgArray[0] = CString(WorkDir) + CString("\\") + CString(fd.cFileName); jpgArray[0].MakeLower(); } else index = -1; /////////////////////////////////////// // now collect the real file names while(1) { BOOL found = FindNextFile(fileSearch,&fd); if(found) { if(CString(fd.cFileName) == "." || CString(fd.cFileName) == "..") continue; jpgMax = index; jpgArray[++index] = CString(WorkDir) + CString("\\") + CString(fd.cFileName); jpgArray[index].MakeLower(); if(index > 99) { break; } } else { FindClose(fileSearch); break; } } /////////////////////////////////////////////////////////// // display the first file matching the selected ext name jpgIndex = -1; OnForward(); SetCurrentDirectory(StartDir); return TRUE; // return TRUE unless you set the focus to a control } // //----< handle menu - only wizard code here >-------------------------- void CMockup5Dlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } //----< handle painting of window - only wizard code here >------------ // 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 CMockup5Dlg::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(); } } // //----< get cursor - only wizard code here >--------------------------- // The system calls this to obtain the cursor to display while // the user drags the minimized window. HCURSOR CMockup5Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } //---< find and display previous document if it exists >--------------- void CMockup5Dlg::OnBack() { COleVariant noArg; while(--jpgIndex >= 0) { // if not at beginning CString temp = jpgArray[jpgIndex]; // get file name int pos = temp.Find('.'); // look for ext name temp = temp.Right(temp.GetLength() - pos); // extract ext name if(temp == m_CBstring) { // if it's the right type SetWindowText((LPCTSTR)jpgArray[jpgIndex]); // put name on title bar m_browser.Navigate(jpgArray[jpgIndex],&noArg,&noArg,&noArg,&noArg); return; // then display in browser } } jpgIndex = 0; } //----< find and display next document if it exists >------------------ void CMockup5Dlg::OnForward() { COleVariant noArg; while(++jpgIndex < jpgMax) { // if not at end of list CString temp = jpgArray[jpgIndex]; // get file name int pos = temp.Find('.'); // look for ext name temp = temp.Right(temp.GetLength() - pos); // extract ext name if(temp == m_CBstring) { // if it's the right type SetWindowText((LPCTSTR)jpgArray[jpgIndex]); // put name on title bar m_browser.Navigate(jpgArray[jpgIndex],&noArg,&noArg,&noArg,&noArg); return; // then display in browser } } jpgIndex = jpgMax-1; } // //----< extract new ext name, selected in combo box >------------------ void CMockup5Dlg::OnSelchangeCombo1() { int index = m_CBcontrol.GetCurSel(); // index of selected item m_CBcontrol.GetLBText(index,m_CBstring); // text of selected item m_CBstring.Insert(0,'.'); // make into ".ext" m_CBstring.MakeLower(); // make all lower case jpgIndex = -1; // start at beginning of list }