点击下载
#include "stdafx.h"
#include "Married.h"
#include "MarriedDlg.h"
#include<time.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CMarriedDlg 对话框
CMarriedDlg::CMarriedDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMarriedDlg::IDD, pParent)
, BoyPath(_T(""))
, GirlPath(_T(""))
, SavePic(FALSE)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
pBkImage=new CImage();
pBkImage->Load(_T("结婚证书.bmp"));
pBoyPhoto=new CImage();
pGirlPhoto=new CImage();
srand((unsigned)time( NULL ));
ISBN=rand();
ISBN+=10000;
}
void CMarriedDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMarriedDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_EN_CHANGE(IDC_EDIT_BOYNAME, &CMarriedDlg::OnEnChangeEditBoyname)
ON_EN_CHANGE(IDC_EDIT_GIRLNAME, &CMarriedDlg::OnEnChangeEditGirlname)
ON_EN_CHANGE(IDC_EDIT_BOYAGE, &CMarriedDlg::OnEnChangeEditBoyage)
ON_EN_CHANGE(IDC_EDIT_GIRLAGE, &CMarriedDlg::OnEnChangeEditGirlage)
ON_BN_CLICKED(IDC_BUTTON_BOYPHOTO, &CMarriedDlg::OnBnClickedButtonBoyphoto)
ON_BN_CLICKED(IDC_BUTTON_GIRLPTOTO, &CMarriedDlg::OnBnClickedButtonGirlptoto)
ON_BN_CLICKED(IDC_BUTTON_SAVE, &CMarriedDlg::OnBnClickedButtonSave)
// ON_BN_CLICKED(IDC_BUTTON1, &CMarriedDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
// CMarriedDlg 消息处理程序
BOOL CMarriedDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
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);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
CRect rc;
GetClientRect(rc);
this->ClientToScreen(rc);
GetDlgItem(IDC_BOYPHOTO)->GetClientRect(BoyPhoto);
GetDlgItem(IDC_BOYPHOTO)->ClientToScreen(BoyPhoto);
GetDlgItem(IDC_GIRLPHOTO)->GetClientRect(GirlPhoto);
GetDlgItem(IDC_GIRLPHOTO)->ClientToScreen(GirlPhoto);
BoyPhoto-=rc.TopLeft();
GirlPhoto-=rc.TopLeft();
///////////////////////////////////////////
::CSimpleArray<GUID> aguidFileTypes;
HRESULT hr=pBoyPhoto->GetExporterFilterString(strFilter,aguidFileTypes);
if(FAILED(hr))
{
MessageBox(_T("装入文件过滤器操作失败"),_T("信息提示"),MB_OK);
return FALSE;
}
strFilter=_T("All Files(*.*)|*.*|")+strFilter;
///////////////////////////////////////////////////
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CMarriedDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CMarriedDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
pBkImage->Draw(GetDC()->m_hDC,0,0);
/////////////////////////////////////////
CString str;
str.Format(_T("%d"),ISBN);
GetDlgItem(IDC_ISBN)->SetWindowTextW(str);
///////////////////////////
SYSTEMTIME st;
::GetLocalTime(&st);
CTime t;
t = CTime::CTime(st);
str=t.Format(_T("%Y"));
GetDlgItem(IDC_YEAR)->SetWindowTextW(str);
str=t.Format(_T("%m"));
GetDlgItem(IDC_MONTH)->SetWindowTextW(str);
str=t.Format(_T("%d"));
GetDlgItem(IDC_DAY)->SetWindowTextW(str);
//////////////////////////////////////////
if(!BoyPath.IsEmpty())
{
pBoyPhoto->Draw(GetDC()->m_hDC,this->BoyPhoto);
}
if(!GirlPath.IsEmpty())
{
pGirlPhoto->Draw(GetDC()->m_hDC,this->GirlPhoto);
}
ChangeBoyName();
ChangeGirlName();
ChangeBoyAge();
ChangeGirlAge();
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMarriedDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CMarriedDlg::OnEnChangeEditBoyname()
{
ChangeBoyName();
}
void CMarriedDlg::OnEnChangeEditGirlname()
{
ChangeGirlName();
}
void CMarriedDlg::OnEnChangeEditBoyage()
{
ChangeBoyAge();
}
void CMarriedDlg::OnEnChangeEditGirlage()
{
ChangeGirlAge();
}
void CMarriedDlg::OnBnClickedButtonBoyphoto()
{
HRESULT hr;
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,strFilter);
if(dlg.DoModal()!=IDOK)
return;
this->BoyPath=dlg.GetPathName();
pBoyPhoto->Destroy();//每一次打开的时候确保Image没有内容
hr=pBoyPhoto->Load(BoyPath);
if(FAILED(hr))
{
MessageBox(_T("装入图像失败"),_T("信息提示"),MB_OK);
return;
}
}
void CMarriedDlg::OnBnClickedButtonGirlptoto()
{
HRESULT hr;
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,strFilter);
if(dlg.DoModal()!=IDOK)
return;
this->GirlPath=dlg.GetPathName();
pGirlPhoto->Destroy();//每一次打开的时候确保Image没有内容
hr=pGirlPhoto->Load(GirlPath);
if(FAILED(hr))
{
MessageBox(_T("装入图像失败"),_T("信息提示"),MB_OK);
return;
}
}
void CMarriedDlg::OnBnClickedButtonSave()
{
// TODO: 在此添加控件通知处理程序代码
CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY,strFilter);
if(dlg.DoModal()!=IDOK)
{
this->Invalidate();
this->UpdateWindow();
return;
}
this->Invalidate();
this->UpdateWindow();
DWORD filter_num =dlg.m_ofn.nFilterIndex-1;//适应数组
TCHAR a[][9]={_T(".bmp"),_T(".bmp"),_T(".jpg"),_T(".gif"),_T(".tif"),_T(".png")};
CString lpszPathName=dlg.GetPathName()+a[filter_num];
pSaveImage=new CImage();
GetDlgItem(IDC_DISPLAY)->GetClientRect(rcSave);
pSaveImage->Create(rcSave.Width(),rcSave.Height(),24);
::BitBlt(pSaveImage->GetDC(),0,0,rcSave.Width(),rcSave.Height(),GetDC()->m_hDC,0,0,SRCCOPY);
pSaveImage->Save(lpszPathName);
}
void CMarriedDlg::ChangeBoyName(void)
{
CString str;
GetDlgItem(IDC_EDIT_BOYNAME)->GetWindowTextW(str);
if(str.GetLength()>5)
{
MessageBox(_T("名字长度不许大于5"));
return;
}
GetDlgItem(IDC_BOYNAME)->SetWindowTextW(str);
CDC* pDC=GetDC();
pDC->SetTextColor(RGB(0,0,255));
pDC->TextOutW(96,217,str);
}
void CMarriedDlg::ChangeGirlName(void)
{
CString str;
GetDlgItem(IDC_EDIT_GIRLNAME)->GetWindowTextW(str);
if(str.GetLength()>5)
{
MessageBox(_T("名字长度不许大于5"));
return;
}
GetDlgItem(IDC_GIRLNAME)->SetWindowTextW(str);
CDC* pDC=GetDC();
pDC->SetTextColor(RGB(255,0,0));
pDC->TextOutW(193,215,str);
}
void CMarriedDlg::ChangeBoyAge(void)
{
CString str;
GetDlgItem(IDC_EDIT_BOYAGE)->GetWindowTextW(str);
if(str.GetLength()>3)
{
MessageBox(_T("年龄长度不许大于3"));
return;
}
GetDlgItem(IDC_BOYAGE)->SetWindowTextW(str);
}
void CMarriedDlg::ChangeGirlAge(void)
{
CString str;
GetDlgItem(IDC_EDIT_GIRLAGE)->GetWindowTextW(str);
if(str.GetLength()>3)
{
MessageBox(_T("年龄长度不许大于3"));
return;
}
GetDlgItem(IDC_GIRLAGE)->SetWindowTextW(str);
}
BOOL CMarriedDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam==VK_RETURN)
{
pMsg->wParam=0;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
posted on 2008-11-18 12:35
cpsprogramer 阅读(2802)
评论(4) 编辑 收藏 引用 所属分类:
VC++