📄 address.cpp
字号:
// address.cpp : implementation file
//
#include "stdafx.h"
#include "DooDads.h"
#include "address.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAddressDialog dialog
CAddressDialog::CAddressDialog(CWnd* pParent /*=NULL*/)
: CDialog(CAddressDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CAddressDialog)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_ImageList.Create(::GetSystemMetrics(SM_CXICON),
::GetSystemMetrics(SM_CYICON), TRUE, 20, 3);
m_pFootballCtrl =
m_pBaseballCtrl =
m_pBasketballCtrl = NULL;
}
CAddressDialog::~CAddressDialog()
{
if (m_pFootballCtrl != NULL)
delete m_pFootballCtrl;
if (m_pBaseballCtrl != NULL)
delete m_pBaseballCtrl;
if (m_pBasketballCtrl != NULL)
delete m_pBasketballCtrl;
}
void CAddressDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAddressDialog)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAddressDialog, CDialog)
//{{AFX_MSG_MAP(CAddressDialog)
ON_NOTIFY(NM_DBLCLK, IDS_HOCKEY, OnDblclkTeams)
ON_NOTIFY(NM_DBLCLK, IDS_FOOTBALL, OnDblclkTeams)
ON_NOTIFY(NM_DBLCLK, IDS_BASKETBALL, OnDblclkTeams)
ON_NOTIFY(NM_DBLCLK, IDS_BASEBALL, OnDblclkTeams)
ON_NOTIFY(LVN_ITEMCHANGED, IDS_HOCKEY, OnItemchangedTeams)
ON_NOTIFY(LVN_ITEMCHANGED, IDS_FOOTBALL, OnItemchangedTeams)
ON_NOTIFY(LVN_ITEMCHANGED, IDS_BASKETBALL, OnItemchangedTeams)
ON_NOTIFY(LVN_ITEMCHANGED, IDS_BASEBALL, OnItemchangedTeams)
ON_WM_DESTROY()
ON_NOTIFY(TCN_SELCHANGE, IDC_SPORT, OnSelchangeSport)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAddressDialog message handlers
// each of these arrays holds IDs string resources and icons for teams of
// four professional sports.
int nHockeyResources[] = {
IDT_BLACKHAWKS,
IDT_BLUES,
IDT_BRUINS,
IDT_CANADIENS,
IDT_CANUCKS,
IDT_CAPITALS,
IDT_DEVILS,
IDT_FLYERS,
IDT_ISLANDERS,
IDT_JETS,
IDT_KINGS,
IDT_LIGHTNING,
IDT_LEAFS,
IDT_DUCKS,
IDT_STARS,
IDT_NORDIQUES,
IDT_OILERS,
IDT_PENGUINS,
IDT_RANGERS,
IDT_REDWINGS,
IDT_WHALERS,
IDT_SHARKS,
IDT_SENATORS,
IDT_SABRES,
};
int nFootballResources[] = {
IDT_BEARS,
IDT_BENGALS,
IDT_BILLS,
IDT_BRONCOS,
IDT_BROWNS,
IDT_BUCCANEERS,
IDT_CARDINAL,
IDT_CHARGERS,
IDT_CHIEFS,
IDT_COLTS,
IDT_COWBOYS,
IDT_DOLPHINS,
IDT_EAGLES,
IDT_FALCONS,
IDT_49ERS,
IDT_GIANTS,
IDT_HOILER,
IDT_JAGUARS,
IDT_LIONS,
IDT_NYJETS,
IDT_PACKERS,
IDT_PATRIOTS,
IDT_PANTHERS,
IDT_RAIDERS,
IDT_RAMS,
IDT_REDSKINS,
IDT_SAINTS,
IDT_SEAHAWKS,
IDT_STEELERS,
IDT_VIKINGS,
};
int nBasketballResources[] = {
IDT_BUCKS,
IDT_BULLETS,
IDT_CAVS,
IDT_CELTICS,
IDT_CLIPPERS,
IDT_DENVER,
IDT_HAWKS,
IDT_HEAT,
IDT_HORNETS,
IDT_JAZZ,
IDT_SKINGS,
IDT_KNICKS,
IDT_LAKERS,
IDT_MAGIC,
IDT_MAVERICKS,
IDT_PACERS,
IDT_PISTONS,
IDT_ROCKETS,
IDT_76ERS,
IDT_SONICS,
IDT_SPURS,
IDT_SUNS,
IDT_TIMBERWOLVES,
IDT_TRAILBLAZERS,
IDT_WARRIORS
};
int nBaseballResources[] = {
IDT_AS,
IDT_ANGELS,
IDT_ASTROS,
IDT_BLUEJAYS,
IDT_BRAVES,
IDT_BREWERS,
IDT_CUBS,
IDT_DODGERS,
IDT_EXPOS,
IDT_INDIANS,
IDT_MARINERS,
IDT_MARLINS,
IDT_METS,
IDT_PADRES,
IDT_PHILLIES,
IDT_PIRATES,
IDT_TEXAS,
IDT_REDS,
IDT_REDSOX,
IDT_ROCKIES,
IDT_ROYALS,
IDT_SFGIANTS,
IDT_TIGERS,
IDT_TWINS,
IDT_WHITESOX,
IDT_YANKEES,
};
BOOL CAddressDialog::OnInitDialog()
{
CDialog::OnInitDialog();
// initially disable info button
GetDlgItem(IDOK)->EnableWindow(FALSE);
// gain a pointer to the tab control
CTabCtrl* pCtrl = (CTabCtrl*) GetDlgItem(IDC_SPORT);
// add tabs to the control and set the initial selection
AddTab(IDS_HOCKEY, pCtrl);
AddTab(IDS_FOOTBALL, pCtrl);
AddTab(IDS_BASEBALL, pCtrl);
AddTab(IDS_BASKETBALL, pCtrl);
pCtrl->SetCurSel(0);
m_nActiveTab = IDS_HOCKEY;
// call another member to copy the existing list control
// to three more instances-- one for each of four sports
m_pFootballCtrl = CopyList(IDS_HOCKEY, IDS_FOOTBALL);
m_pBaseballCtrl = CopyList(IDS_HOCKEY, IDS_BASEBALL);
m_pBasketballCtrl = CopyList(IDS_HOCKEY, IDS_BASKETBALL);
if (m_pFootballCtrl == NULL || m_pBaseballCtrl == NULL ||
m_pBasketballCtrl == NULL)
{
TRACE0("Error: Failed to copy a list control!\n");
EndDialog(IDCANCEL);
return FALSE;
}
// load up the image list and images for each control
LoadList(m_pFootballCtrl, nFootballResources, ELEMENTS(nFootballResources));
LoadList(m_pBaseballCtrl, nBaseballResources, ELEMENTS(nBaseballResources));
LoadList(m_pBasketballCtrl, nBasketballResources, ELEMENTS(nBasketballResources));
// the hockey control is a special case since it's been
// created by the template
CListCtrl* pHockeyCtrl = (CListCtrl*) GetDlgItem(IDS_HOCKEY);
pHockeyCtrl->SetImageList(&m_ImageList, LVSIL_NORMAL);
LoadList(pHockeyCtrl, nHockeyResources, ELEMENTS(nHockeyResources));
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
CListCtrl* CAddressDialog::CopyList(int nOldID, int nNewID)
{
// create a brand new list control that's exactly
// like the one we're told about
CListCtrl* pOldCtrl = (CListCtrl*) GetDlgItem(nOldID);
if (pOldCtrl == NULL)
return NULL;
// allocate the control
CListCtrl* pNewCtrl = new CListCtrl;
// we want the new control to have the same style
// but not be visible
DWORD dwStyle = pOldCtrl->GetStyle() & ~WS_VISIBLE;
// get the rect of the existing one
CRect rect;
pOldCtrl->GetWindowRect(rect);
ScreenToClient(rect);
// and go create it
if (NULL == pNewCtrl->Create(dwStyle, rect, this, nNewID))
{
TRACE1("Error: Failed to create control %d\n", nNewID);
delete pNewCtrl;
pNewCtrl = NULL;
}
else
{
// set the image list for the new control
pNewCtrl->SetImageList(&m_ImageList, LVSIL_NORMAL);
}
return pNewCtrl;
}
void CAddressDialog::AddTab(int nID, CTabCtrl* pCtrl)
{
// load a string from a resource and use it to
// set the text on a tab control
CString str;
if (str.LoadString(nID) == 0)
{
TRACE1("Error: Failed to load string: %d\n", nID);
return;
}
TC_ITEM tabItem;
tabItem.mask = TCIF_TEXT | TCIF_PARAM;
tabItem.pszText = (LPTSTR) (LPCTSTR) str;
tabItem.lParam = nID;
pCtrl->InsertItem(nID, &tabItem);
return;
}
void CAddressDialog::LoadList(CListCtrl* pList, int* pnArray, const int nSize)
{
CWinApp* pApp = AfxGetApp();
// rip through one of the arrys and
// add the images to the image contorl
// and insert new items to the list control
int nIndex;
for (nIndex = 0; nIndex < nSize; nIndex++)
{
// get the icon first
HICON hIcon;
int nImage = 0;
hIcon = pApp->LoadIcon(pnArray[nIndex]);
if (hIcon == NULL)
{
TRACE1("Warning: Couldn't get icon %d\n", pnArray[nIndex]);
}
else
{
nImage = m_ImageList.Add(hIcon);
::DeleteObject(hIcon);
}
// now, add the item to the list box
LV_ITEM itemAdder;
CString strTeamInfo;
CString strTeamName;
int nOffset;
// retrieve the name and address
// and split the address into a second string
if (!strTeamInfo.LoadString(pnArray[nIndex]))
TRACE1("Warning: LoadString() failed for %d\n", pnArray[nIndex]);
else
{
nOffset = strTeamInfo.Find(_T("\n"));
if (nOffset == -1)
{
TRACE1("Warning: Couldn't parse %d\n", pnArray[nIndex]);
}
else
{
strTeamName = strTeamInfo.Left(nOffset);
strTeamInfo = strTeamInfo.Mid(nOffset+1);
}
// put the team item in: use the image index we loaded
// and set lParam to a copy of the info string
itemAdder.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
itemAdder.pszText = strTeamName.GetBuffer(0);
itemAdder.iSubItem = 0;
itemAdder.iImage = nImage;
itemAdder.lParam = (LPARAM) new CString(strTeamInfo);
pList->InsertItem(&itemAdder);
}
}
}
void CAddressDialog::OnDblclkTeams(NMHDR* pNMHDR, LRESULT* pResult)
{
// double-clicking is just a different way to press the button
OnOK();
*pResult = 0;
}
void CAddressDialog::OnItemchangedTeams(NMHDR* pNMHDR, LRESULT* pResult)
{
// if there is a selection, we can enable the Info button
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if (pNMListView->uNewState & LVIS_SELECTED)
GetDlgItem(IDOK)->EnableWindow(TRUE);
else
GetDlgItem(IDOK)->EnableWindow(FALSE);
*pResult = 0;
}
void CAddressDialog::OnOK()
{
// when the user presses the INFO button,
// find the selected item
CListCtrl* pList;
pList = (CListCtrl*) GetDlgItem(m_nActiveTab);
ASSERT(pList != NULL);
LV_ITEM itemSelected;
int nSelected = pList->GetNextItem(-1, LVNI_SELECTED);
if (nSelected == -1)
return;
TCHAR szTeamName[80];
itemSelected.iItem = nSelected;
itemSelected.iSubItem = 0;
itemSelected.mask = LVIF_PARAM | LVIF_TEXT;
itemSelected.pszText = szTeamName;
itemSelected.cchTextMax = ELEMENTS(szTeamName);
pList->GetItem(&itemSelected);
// and pop up a message box
CString* pMessage = (CString*) itemSelected.lParam;
MessageBox(*pMessage, szTeamName);
}
void CAddressDialog::OnDestroy()
{
// when the box is destroyed, free all the copied strings
// in each of the four boxes
FreeStrings(IDS_HOCKEY);
FreeStrings(IDS_FOOTBALL);
FreeStrings(IDS_BASKETBALL);
FreeStrings(IDS_BASEBALL);
CDialog::OnDestroy();
}
void CAddressDialog::FreeStrings(int nID)
{
// rip through all the elements in a box
CListCtrl* pList;
pList = (CListCtrl*) GetDlgItem(nID);
ASSERT(pList != NULL);
LV_ITEM itemVisit;
int nCount;
int nIndex;
CString* pItem;
nCount = pList->GetItemCount();
// get the string data and free it
// set the lParam to NULL and put it back into the list
//REVIEW: overcautious
for (nIndex = 0; nIndex < nCount; nIndex++)
{
itemVisit.mask = LVIF_PARAM;
itemVisit.iSubItem = 0;
itemVisit.iItem = nIndex;
pList->GetItem(&itemVisit);
pItem = (CString*) itemVisit.lParam;
delete pItem;
itemVisit.lParam = NULL;
itemVisit.mask = LVIF_PARAM;
itemVisit.iSubItem = 0;
itemVisit.iItem = nIndex;
pList->SetItem(&itemVisit);
}
}
void CAddressDialog::OnSelchangeSport(NMHDR* pNMHDR, LRESULT* pResult)
{
// the user has clicked a different tab
CTabCtrl* pTabCtrl = (CTabCtrl*) GetDlgItem(IDC_SPORT);
// figure out what tab item is selected
// and get the lParam from it
int nItem = pTabCtrl->GetCurSel();
TC_ITEM tci;
tci.mask = TCIF_PARAM;
pTabCtrl->GetItem(nItem, &tci);
// hide the list control which was previously
// active and show the newly activated control
CWnd* pOldOne = GetDlgItem(m_nActiveTab);
CWnd* pNewOne = GetDlgItem(tci.lParam);
pOldOne->ShowWindow(SW_HIDE);
pNewOne->ShowWindow(SW_SHOW);
m_nActiveTab = tci.lParam;
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -