// Dialog2.cpp
// Demonstrates a dialog box containing a list box

#include <afxwin.h>
#include <string.h>
#include "dialog2.h"
#include "resource.h"

CMainWin::CMainWin()
{
        Create(NULL, "Demonstrate Dialog Boxes", WS_OVERLAPPEDWINDOW,
                   rectDefault, NULL, "DialogMenu");
}

// The application's message map
BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)
        ON_COMMAND(IDM_DIALOG, OnDialog)
END_MESSAGE_MAP()

// Process IDM_DIALOG menu item
void CMainWin::OnDialog()
{
        CSampleDialog diagOb("SampleDialog", this);
        diagOb.DoModal();  // activate the modal dialog box
}

// The SampleDialog's message map
BEGIN_MESSAGE_MAP(CSampleDialog, CDialog)
        ON_COMMAND(IDD_SELFRUIT, OnSelect)
        ON_LBN_DBLCLK(IDD_LB1, OnSelect)
END_MESSAGE_MAP()

// Initialize the dialog box
BOOL CSampleDialog::OnInitDialog()
{
        CDialog::OnInitDialog();  // call base class version
        CListBox *plistbox = (CListBox *)GetDlgItem(IDD_LB1);

        // Initialize the list box
        plistbox->AddString("Apple");
        plistbox->AddString("Peach");
        plistbox->AddString("Banana");
        plistbox->AddString("Pear");
        return TRUE;
}

// Process IDD_SELFRUIT and LBN_DBLCLK
void CSampleDialog::OnSelect()
{
        CListBox *plistbox = (CListBox *)GetDlgItem(IDD_LB1);
        char str1[80], str2[80];
        int i;

        i = plistbox->GetCurSel(); // Get index of current selection
        wsprintf(str2, " at index %d", i);   // Format it for output
        plistbox->GetText(i, str1);          // Get the item
        strcat(str1, str2);                  // Concatenate the strings
        MessageBox(str1, "Selection Made");  // Display in a message box
}

// Initialize the application
BOOL CApp::InitInstance()
{
        m_pMainWnd = new CMainWin;
        m_pMainWnd->ShowWindow(m_nCmdShow);
        m_pMainWnd->UpdateWindow();
        return TRUE;
}


// Instantiate the Application
CApp App;