childview.cpp

来自「本文件包包含了大量用VC++开发的应用程序界面」· C++ 代码 · 共 180 行

CPP
180
字号
// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "List.h"
#include "ChildView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CAPITOL States[] = 
    {
    "Colorado", "Denver",
    "Texas", "Austin",
    "Alabama", "Montgomery",
    "Alaska", "Juneau",
    "Oregon", "Salem",
    "New Hampshire", "Montpelier",
    "Virginia", "Roanoke",
    "Arizona", "Tucson",
    "Idaho", "Boise",
    "Minnesota", "St. Paul",
	"Tennessee", "Nashville",
	"Arkansas", "Little Rock",
	"Delaware", "Dover",
	"New York", "Albany",
	"Pennsylvania", "Harrisburg",
	"Ohio", "Columbus",
	"Louisiana", "Baton Rouge",
	"Rhode Island", "Providence",
	"Michigan", "Lansing",
	"Utah", "Salt Lake City",
	"Oklahoma", "Oklahoma City",
	"Wyoming", "Casper",
	"Canada", "Ottawa"
    };

/////////////////////////////////////////////////////////////////////////////
// CChildView

CChildView::CChildView()
{
	m_nSortBy = 0;
    SortArray ();
}

CChildView::~CChildView()
{
}


BEGIN_MESSAGE_MAP(CChildView,CWnd )
    //{{AFX_MSG_MAP(CChildView)
    ON_WM_PAINT()
	ON_COMMAND(ID_SORT_BYCAPITOL, OnSortBycapitol)
	ON_COMMAND(ID_SORT_BYSTATE, OnSortBystate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
    if (!CWnd::PreCreateWindow(cs))
        return FALSE;

    cs.dwExStyle |= WS_EX_CLIENTEDGE;
    cs.style &= ~WS_BORDER;
    cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
        ::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);

    return TRUE;
}

void CChildView::OnPaint() 
{
CFont font;
CPoint  point(0, 0);
int     tabs[2] = {130, 200};
char    szText[_MAX_PATH];

    CPaintDC dc(this); // device context for painting
    font.CreatePointFont (100, "Times New Roman Bold");
    dc.SelectObject (&font);

    LOGFONT lf;
    memset (&lf, '\0', sizeof (LOGFONT));
    font.GetLogFont (&lf);
    CAPITOL state = m_States.GetHead ();
    POSITION pos = m_States.GetHeadPosition ();
    while (pos != NULL)
    {
        state = m_States.GetNext (pos);
        sprintf (szText, "%s\t%s", state.State, state.Capitol);
        dc.TabbedTextOut (point.x, point.y, szText, 2, tabs, 0);
        point.y += abs(lf.lfHeight);
    } 

    // TODO: Add your message handler code here
    
    // Do not call CWnd::OnPaint() for painting messages
}

void CChildView::SortArray()
{
POSITION pos;

	if (!m_States.IsEmpty ())
	{
		while ((pos = m_States.GetHeadPosition ()) != NULL)
			m_States.RemoveHead ();
	}
    for (int i = 0; i < (sizeof(States)/sizeof(CAPITOL)); ++i)
    {
        if (m_States.IsEmpty())
        {
            m_States.AddHead (States[i]);
            continue;
        }
        CAPITOL next;
        pos = m_States.GetHeadPosition ();
        next = m_States.GetTail ();
        for (next = m_States.GetHead(); pos != NULL; next = m_States.GetNext (pos))
        {
			if (CompareStrings (next, States[i]) > 0)
				break;
        }
        if (pos == NULL)
        {
			if (CompareStrings (States[i], next) < 0)
            {
                pos = m_States.GetTailPosition ();
                m_States.InsertBefore (pos, States[i]);
            }
            else
                m_States.AddTail (States[i]);
            continue;
        }
        m_States.GetPrev (pos);
        m_States.InsertBefore (pos, States[i]);
    }
}

int CChildView::CompareStrings(CAPITOL & c1, CAPITOL & c2)
{
char *str1, *str2;

	switch (m_nSortBy)
	{
		case 0:
			str1 = c1.State;
			str2 = c2.State;
			break;
		case 1:
			str1 = c1.Capitol;
			str2 = c2.Capitol;
			break;
	}
	return (strcmp (str1, str2));
}

void CChildView::OnSortBycapitol() 
{
	m_nSortBy = 1;
    SortArray ();
	Invalidate ();
}

void CChildView::OnSortBystate() 
{
	m_nSortBy = 0;
    SortArray ();
	Invalidate ();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?