After writing the C version of the sample application, I decided (with just a bit of proddingmy electronic mailbox was filling up with requests for an MFC version) to port CHICOAPP to MFC. The MFC version contains a few small changes: I added separate icons for the cities, and I used the same image list both for the tree view control and for the list view control in small icon view. I created all the application windows in one function, CreateAppWindows, and left sizing the windows to the WM_SIZE handler.
BOOL CMfcexpView::CreateAppWindows ()
{
static char szBuf [MAX_PATH];
HICON hIcon; // handle to an icon
int index; // index used in for loops
LV_COLUMN lvC; // list view column structure
char szText [MAX_ITEMLEN]; // place to store some text
// Ensure that the common control DLL is loaded.
InitCommonControls ();
// Create the status bar.
m_StatusBar.Create (WS_CHILD | WS_BORDER | WS_VISIBLE |
SBS_SIZEGRIP,
CRect (0, 0, 0, 0),
this,
ID_STATUSBAR);
// Set the status bar to have two parts.
int aWidths [2] = {0, 0};
m_StatusBar.SetParts (2, aWidths);
// Set the text for the status bar.
ChangeSBText (-1);
// Create the toolbar.
m_Toolbar.Create (
WS_CHILD | WS_BORDER | WS_VISIBLE | TBSTYLE_TOOLTIPS, // styles
CRect (0, 0, 0, 0),
this,
ID_TOOLBAR);
// Add the bitmaps.
m_Toolbar.AddBitmap (NUM_BITMAPS, IDB_TOOLBAR);
// Add the buttons.
m_Toolbar.AddButtons (NUM_BUTTONS, (LPTBBUTTON)&tbButtons);
// Create the CListCtrl window.
m_ListCtl.Create (
WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_REPORT | LVS_EDITLABELS,
CRect (0, 0, 0, 0), // bounding rectangle
this, // parent
ID_LISTVIEW); // ID
// Create the large icon image list.
m_ImageLarge.Create (LARGE_BITMAP_WIDTH,
LARGE_BITMAP_HEIGHT,
FALSE, // list does not include masks
NUM_BITMAPS,
10); // list won't grow
// Create the small icon image list.
m_ImageSmall.Create (SMALL_BITMAP_WIDTH,
SMALL_BITMAP_HEIGHT,
FALSE, // list does not include masks
NUM_BITMAPS,
10); // list won't grow
// Load the house icon.
hIcon = ::LoadIcon (AfxGetResourceHandle (),
MAKEINTRESOURCE (IDI_SEATTLE));
m_idxSeattle = m_ImageSmall.Add (hIcon);
m_ImageLarge.Add (hIcon);
hIcon = ::LoadIcon (AfxGetResourceHandle (),
MAKEINTRESOURCE (IDI_BELLEVUE));
m_idxBellevue = m_ImageSmall.Add (hIcon);
m_ImageLarge.Add (hIcon);
hIcon = ::LoadIcon (AfxGetResourceHandle (),
MAKEINTRESOURCE (IDI_REDMOND));
m_idxRedmond = m_ImageSmall.Add (hIcon);
m_ImageLarge.Add (hIcon);
// Associate the image lists with the list view control.
m_ListCtl.SetImageList (&m_ImageSmall, LVSIL_SMALL);
m_ListCtl.SetImageList (&m_ImageLarge, LVSIL_NORMAL);
// Now initialize the columns you will need.
// Initialize the LV_COLUMN structure.
// The mask specifies that the fmt, cx, pszText, and iSubitem
// members of the structure are valid.
lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvC.fmt = LVCFMT_LEFT; // left-align column
lvC.cx = 75; // width of column in pixels
lvC.pszText = szText;
// Add the columns.
for (index = 0; index < NUM_COLUMNS; index++)
{
lvC.iSubItem = index;
::LoadString (AfxGetResourceHandle (),
IDS_ADDRESS + index,
szText,
sizeof (szText));
if (m_ListCtl.InsertColumn (index, &lvC) == -1)
return NULL;
}
// Create the tree view control.
m_TreeCtl.Create (
WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES | TVS_HASBUTTONS
|
TVS_LINESATROOT, // styles
CRect (0, 0, 0, 0), // bounding rectangle
this, // parent
ID_TREEVIEW); // ID
// Load the bitmaps and add them to the image list.
hIcon = ::LoadIcon (AfxGetResourceHandle (),
MAKEINTRESOURCE (IDI_FORSALE));
m_idxForSale = m_ImageSmall.Add (hIcon);
hIcon = ::LoadIcon (AfxGetResourceHandle (),
MAKEINTRESOURCE (IDI_SELECTED));
m_idxSeaSel = m_ImageSmall.Add (hIcon);
hIcon = ::LoadIcon (AfxGetResourceHandle (),
MAKEINTRESOURCE (IDI_SELBELL));
m_idxBellSel = m_ImageSmall.Add (hIcon);
hIcon = ::LoadIcon (AfxGetResourceHandle (),
MAKEINTRESOURCE (IDI_REDSEL));
m_idxRedSel = m_ImageSmall.Add (hIcon);
// Associate the image list with the tree view control.
m_TreeCtl.SetImageList (TVSIL_NORMAL, &m_ImageSmall);
TV_InitTreeView ();
return TRUE;
}
As you can see in Figure 7-2, the main screen of MFCEXP is quite similar to that of CHICOAPP. One exception is the toolbar. Instead of using the built-in toolbar bitmaps, I created my own and included buttons for adding a house, removing a house, and adding a city to the listing.
Figure 7-2.