⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch7demo4dlg.cpp

📁 零基础学Visual.C....教案PPT.随书光盘-452M.zip
💻 CPP
字号:
// Ch7Demo4Dlg.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CCh7Demo4Dlg dialog

CCh7Demo4Dlg::CCh7Demo4Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCh7Demo4Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCh7Demo4Dlg)
	m_Bookname = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCh7Demo4Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCh7Demo4Dlg)
	DDX_Control(pDX, IDC_LIST1, m_list);
	DDX_Text(pDX, IDC_EDIT1, m_Bookname);
	DDV_MaxChars(pDX, m_Bookname, 20);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCh7Demo4Dlg, CDialog)
	//{{AFX_MSG_MAP(CCh7Demo4Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CHECK, OnCheck)
	ON_BN_CLICKED(IDC_MODIFY, OnModify)
	ON_BN_CLICKED(IDC_REMOVEALL, OnRemoveall)
	ON_LBN_DBLCLK(IDC_LIST1, OnDblclkList1)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCh7Demo4Dlg message handlers

BOOL CCh7Demo4Dlg::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
	
	// TODO: Add extra initialization here
	GetDlgItem(IDC_DELETE)->EnableWindow(false);//删除按钮无效
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCh7Demo4Dlg::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 CCh7Demo4Dlg::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 CCh7Demo4Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCh7Demo4Dlg::OnCheck() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	if(m_Bookname.IsEmpty())										//如果用户名为空
	{
		AfxMessageBox("书名不能为空!");
		return;
	}
	if(m_list.FindString(-1,(LPCTSTR)m_Bookname)==CB_ERR)//判断列表框中是否有该书名
	{
		m_list.AddString(m_Bookname);					//加入列表框
		m_Bookname="";
		UpdateData(false);
	}
	else
	{
		AfxMessageBox("该书名在列表框中已经存在!");
	}
	
}

void CCh7Demo4Dlg::OnModify() 
{	
}

void CCh7Demo4Dlg::OnRemoveall() 
{
	// TODO: Add your control notification handler code here
	if(m_list.GetCount())//列表框不为空
	{
		m_list.ResetContent();								//清空列表框
	}
	
}

void CCh7Demo4Dlg::OnDblclkList1() 
{
	// TODO: Add your control notification handler code here
	int nselect=m_list.GetCurSel();						//获取当前列表框选项的索引
	CString name;
	m_list.GetText(nselect,name);							//列表框选项的文本
	AfxMessageBox("当前选择的图书为:"+name);	
}

void CCh7Demo4Dlg::OnDelete() 
{
	// TODO: Add your control notification handler code here
	int nselect=m_list.GetCurSel();						//获取当前列表框选项的索引
	if(nselect!=-1)
	{
		m_list.DeleteString(nselect);							//删除列表框选项
	}
	GetDlgItem(IDC_DELETE)->EnableWindow(false);//删除按钮无效
}

void CCh7Demo4Dlg::OnSelchangeList1() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_DELETE)->EnableWindow(true);//删除按钮生效
}

void CCh7Demo4Dlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	char szName[40];
	CString strFilter,strFileName,strText;
	strFilter="BOOKINFO Files(*.*)|*.*||";
	CFileDialog dlg(FALSE, NULL, NULL, OFN_EXPLORER|OFN_HIDEREADONLY|
		OFN_ENABLESIZING|OFN_FILEMUSTEXIST,strFilter);
	if(dlg.DoModal() == IDOK )								//显示保存文件对话框
	{	
		strFileName=dlg.GetPathName();//获取文件名
		CFile m_file(strFileName,CFile::modeCreate|CFile::modeWrite);		//构造CFile对象
		if(m_list.GetCount()==0)
		{
			AfxMessageBox("数据为空!");
			return;
		}
		for(int i=0;i<m_list.GetCount();i++)
		{
			m_list.GetText(i,strText);						//获取列表框数据
			strcpy(szName,strText);
		//	m_file.Seek(sizeof(szName)*i,CFile::begin);						//定位文件指针
			m_file.Write(&szName,sizeof(szName));							//写文件数据
		}
		m_file.Close();										//关闭文件
	}

	
}

void CCh7Demo4Dlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
	CString strFilter,strFileName;
	char szName[40];								//存储从文件读取的数据
	strFilter="BOOKINFO Files(*.*)|*.*||";
	CFileDialog dlg(TRUE, NULL, NULL, OFN_EXPLORER|OFN_HIDEREADONLY|
		OFN_ENABLESIZING|OFN_FILEMUSTEXIST,strFilter);
	if(dlg.DoModal() == IDOK )								//显示打开文件对话框
	{
		int nvalue= MessageBox("现有数据如果没有保存将丢失,是否继续?","提示",4+32+256);
		if(nvalue==6)//用户按下了"是"按钮
		{
			m_list.ResetContent();								//清空列表框
			CString fileName=dlg.GetPathName();
			CFile m_file(fileName,CFile::modeRead);				//构造CFile对象
			int i=0;
			m_file.SeekToBegin();
			while(m_file.Read(&szName,sizeof(szName))!=0)					//读出的数据不为空
			{	
				m_list.AddString((CString)szName);
			}
			m_file.Close();									//关闭文件
		}
		else
		{
			return;										//返回
		}	
	}	

	
}

⌨️ 快捷键说明

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