ex040202dlg.cpp

来自「深入浅出Visual C++入门进阶与应用实例 随书光盘 作者 何志丹」· C++ 代码 · 共 251 行

CPP
251
字号
// Ex040202Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Ex040202.h"
#include "Ex040202Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CEx040202Dlg dialog

CEx040202Dlg::CEx040202Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEx040202Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEx040202Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEx040202Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEx040202Dlg)
	DDX_Control(pDX, IDC_STU_LIST, m_stuList);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEx040202Dlg, CDialog)
	//{{AFX_MSG_MAP(CEx040202Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SELECT, OnSelect)
	ON_BN_CLICKED(IDC_ICON, OnIcon)
	ON_BN_CLICKED(IDC_LIST, OnList)
	ON_BN_CLICKED(IDC_REPORT, OnReport)
	ON_BN_CLICKED(IDC_SMALL_ICON, OnSmallIcon)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEx040202Dlg message handlers

BOOL CEx040202Dlg::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
	
	//增加列头
	m_stuList.InsertColumn(0,"姓名",LVCFMT_LEFT,100);
	m_stuList.InsertColumn(1,"特长",LVCFMT_LEFT,100);

	//增加内容
	int nItem = m_stuList.InsertItem(0,"张三",2);
	m_stuList.SetItemText(nItem,1,"热爱自然");
	nItem = m_stuList.InsertItem(1,"李四",1);
	m_stuList.SetItemText(nItem,1,"热爱科学");
	nItem = m_stuList.InsertItem(2,"王五",0);
	m_stuList.SetItemText(nItem,1,"热爱运动");

	//读取图标信息
	{
		CBitmap bt ;
		bt.LoadBitmap(IDB_ICON);
		m_imageList.Create(32,32,ILC_COLOR24,3,1);
		m_imageList.Add(&bt,RGB(216,233,236));//将RGB(216,233,236)变成透明色
	}
	{
		CBitmap bt ;
		bt.LoadBitmap(IDB_SMALL_ICON);
		m_smallImageList.Create(16,16,ILC_COLOR24,3,1);
		m_smallImageList.Add(&bt,RGB(216,233,236));//将RGB(216,233,236)变成透明色
	}

	//设置图像
	m_stuList.SetImageList(&m_imageList,LVSIL_NORMAL );
	m_stuList.SetImageList(&m_smallImageList,LVSIL_SMALL );
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEx040202Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CEx040202Dlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CEx040202Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CEx040202Dlg::OnSelect() 
{
	CString strMess = "你选中了如下学生.他们的姓名及爱好是   \n------------ \n";	
	POSITION pos = m_stuList.GetFirstSelectedItemPosition();
	if(NULL != pos )
	{
		while(NULL != pos )
		{
			int nItem = m_stuList.GetNextSelectedItem(pos);
			strMess += m_stuList.GetItemText(nItem,0) ;//取姓名
			strMess += "\t";
			strMess += m_stuList.GetItemText(nItem,1) ;//取爱好
			strMess += "\n";
		}
		AfxMessageBox(strMess);
	}	
}

void CEx040202Dlg::SetStyle(DWORD uStyle)
{
	LONG   lStyle;     
	lStyle = GetWindowLong(m_stuList.m_hWnd,GWL_STYLE);//获取当前窗口类型   
    lStyle &= ~LVS_TYPEMASK;   //清除显示方式位      
	lStyle |= uStyle;   //设置显示方式    
	SetWindowLong(m_stuList.m_hWnd,GWL_STYLE,lStyle);//设置窗口类型  
}

void CEx040202Dlg::OnIcon() 
{
	SetStyle(LVS_ICON);//图标	
}

void CEx040202Dlg::OnList() 
{
	SetStyle(LVS_LIST);//列表	
}

void CEx040202Dlg::OnReport() 
{
	SetStyle(LVS_REPORT);//详细资料	
}

void CEx040202Dlg::OnSmallIcon() 
{
	SetStyle(LVS_SMALLICON);//小图标
}

⌨️ 快捷键说明

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