// Preview.cpp : implementation file
//

#include "stdafx.h"
#include "fapiao.h"
#include "Preview.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPreview dialog


CPreview::CPreview(CWnd* pParent /*=NULL*/)
	: CDialog(CPreview::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPreview)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CPreview::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPreview)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPreview, CDialog)
	//{{AFX_MSG_MAP(CPreview)
	ON_WM_PAINT()
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPreview message handlers

void CPreview::DrawReport(CRect rect, CDC *pDC, BOOL isprinted)
{
	int printx,printy;
	printx = pDC->GetDeviceCaps(LOGPIXELSX);
	printy = pDC->GetDeviceCaps(LOGPIXELSY);
	double ratex = (double)(printx)/screenx;
	double ratey = (double)(printy)/screeny;
	if(isprinted)
	{
		
		pDC->StartDoc("printinformation");	
	}
	else
	{
		ratex=1,ratey=1;
	}
	CFont font130,font150,font90;
	CPen pen1,pen2,*pOldPen;
	font130.CreatePointFont(130,_T("宋体"),pDC);
	pDC->SelectObject(&font130);
	rect.DeflateRect(0,(int)(ratey*30),0,0);
	pDC->DrawText("北京市商业企业专用发票",rect,DT_CENTER);
	font150.CreatePointFont(150,_T("宋体"),pDC);
	pDC->SelectObject(&font150);
	rect.DeflateRect(0,(int)(ratey*40),0,0);
	pDC->DrawText("发  票  联",rect,DT_CENTER);
	pen1.CreatePen (PS_SOLID,2,RGB(0,0,0));
	pOldPen=pDC->SelectObject (&pen1);
	pDC->MoveTo((int)(ratex*170),(int)(ratey*50));
	pDC->LineTo((int)(ratex*390),(int)(ratey*50));
	//画表格外边框
	pDC->Rectangle ((int)(ratex*25),(int)(ratey*140),526,300);//pDC->Rectangle ((int)(ratex*25),(int)(ratey*140),(int)(ratex*526),(int)(ratey*300));//pDC->Rectangle ((int)(ratex*25),(int)(ratey*140),(int)(ratey*526),(int)(ratey*300));
	//画粗线
	pDC->MoveTo ((int)(ratex*441),(int)(ratey*152));
	pDC->LineTo ((int)(ratex*441),(int)(ratey*260));
	pDC->MoveTo ((int)(ratex*492),(int)(ratey*152));
	pDC->LineTo ((int)(ratex*492),(int)(ratey*260));		
	//创建画笔
	pen2.CreatePen (PS_SOLID,1,RGB(0,0,0));
	//将画笔选入
	pOldPen=pDC->SelectObject (&pen2);
	pDC->MoveTo((int)(ratex*75),(int)(ratey*130));
	pDC->LineTo((int)(ratex*205),(int)(ratey*130));
	pDC->MoveTo((int)(ratex*296),(int)(ratey*130));
	pDC->LineTo((int)(ratex*400),(int)(ratey*130));
	//画横线
	for(int i=1;i<=5;i++)
	{
		pDC->MoveTo ((int)(ratex*25),(int)(ratey*140+i*24));
		pDC->LineTo ((int)(ratex*526),(int)(ratey*140+i*24));
	}
	//画竖线
	pDC->MoveTo ((int)(ratex*75),(int)(ratey*140));
	pDC->LineTo ((int)(ratex*75),(int)(ratey*236));
	pDC->MoveTo ((int)(ratex*205),(int)(ratey*140));
	pDC->LineTo ((int)(ratex*205),(int)(ratey*236));
	pDC->MoveTo ((int)(ratex*254),(int)(ratey*140));
	pDC->LineTo ((int)(ratex*254),(int)(ratey*236));
	pDC->MoveTo ((int)(ratex*296),(int)(ratey*140));
	pDC->LineTo ((int)(ratex*296),(int)(ratey*236));
	pDC->MoveTo ((int)(ratex*343),(int)(ratey*140));
	pDC->LineTo ((int)(ratex*343),(int)(ratey*236));
	pDC->MoveTo ((int)(ratex*390),(int)(ratey*140));
	pDC->LineTo ((int)(ratex*390),(int)(ratey*260));
	pDC->MoveTo ((int)(ratex*120),(int)(ratey*260));
	pDC->LineTo ((int)(ratex*120),(int)(ratey*300));
	//画金额线
	pDC->MoveTo ((int)(ratex*390),(int)(ratey*152));
	pDC->LineTo ((int)(ratex*524),(int)(ratey*152));
	pDC->MoveTo ((int)(ratex*407),(int)(ratey*152));
	pDC->LineTo ((int)(ratex*407),(int)(ratey*260));
	pDC->MoveTo ((int)(ratex*424),(int)(ratey*152));
	pDC->LineTo ((int)(ratex*424),(int)(ratey*260));
	pDC->MoveTo ((int)(ratex*458),(int)(ratey*152));
	pDC->LineTo ((int)(ratex*458),(int)(ratey*260));
	pDC->MoveTo ((int)(ratex*475),(int)(ratey*152));
	pDC->LineTo ((int)(ratex*475),(int)(ratey*260));
	pDC->MoveTo ((int)(ratex*509),(int)(ratey*152));
	pDC->LineTo ((int)(ratex*509),(int)(ratey*260));
	font90.CreatePointFont(80,_T("宋体"),pDC);
	pDC->SelectObject(&font90);
	rect.DeflateRect((int)(-460*ratex),(int)(ratey*50),0,0);//rect.DeflateRect((int)(-460*ratex),(int)(ratey*50),0,0);
	pDC->DrawText("客户名称",rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*100),0,0,0);
	pDC->DrawText(khmc,rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*350),0,0,0);
	pDC->DrawText("支票号",rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*80),0,0,0);
	pDC->DrawText(zph,rect,DT_CENTER);
	for(i=0;i<4;i++)
	{
		if(i==0)
		{
			rect.DeflateRect((int)(-40*ratex),(int)(ratey*25),0,0);
		}
		else
		{
			rect.DeflateRect((int)(-320*ratex),(int)(ratey*25),0,0);
		}
		pDC->DrawText(sarrays[i][0],rect,DT_LEFT);
		rect.DeflateRect((int)(ratex*60),0,0,0);
		pDC->DrawText(sarrays[i][1],rect,DT_LEFT);
		rect.DeflateRect((int)(ratex*130),0,0,0);
		pDC->DrawText(sarrays[i][2],rect,DT_LEFT);
		rect.DeflateRect((int)(ratex*40),0,0,0);
		pDC->DrawText(sarrays[i][3],rect,DT_LEFT);
		rect.DeflateRect((int)(ratex*40),0,0,0);
		pDC->DrawText(sarrays[i][4],rect,DT_LEFT);
		rect.DeflateRect((int)(ratex*50),0,0,0);
		pDC->DrawText(sarrays[i][5],rect,DT_LEFT);
	}
	rect.DeflateRect((int)(-500*ratex),(int)(ratey*25),0,0);
	pDC->DrawText("小     写     金     额    合     计",rect,DT_CENTER);
	rect.DeflateRect((int)(-300*ratex),(int)(ratey*30),0,0);
	pDC->DrawText("大写金额",rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*200),0,0,0);
	pDC->DrawText("拾",rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*100),0,0,0);
	pDC->DrawText("万",rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*100),0,0,0);
	pDC->DrawText("仟",rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*100),0,0,0);
	pDC->DrawText("佰",rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*100),0,0,0);
	pDC->DrawText("拾",rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*100),0,0,0);
	pDC->DrawText("元",rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*100),0,0,0);
	pDC->DrawText("角",rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*100),0,0,0);
	pDC->DrawText("分",rect,DT_CENTER);
	rect.DeflateRect((int)(-870*ratex),(int)(ratey*30),0,0);
	pDC->DrawText("开票单位（盖章）",rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*150),0,0,0);
	pDC->DrawText(kpdw,rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*200),0,0,0);
	pDC->DrawText("开票人：",rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*100),0,0,0);
	pDC->DrawText(kpr,rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*350),0,0,0);
	pDC->DrawText("年    月    日",rect,DT_CENTER);
	rect.DeflateRect((int)(-100*ratex),0,0,0);
	pDC->DrawText(darrays[0],rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*70),0,0,0);
	pDC->DrawText(darrays[1],rect,DT_CENTER);
	rect.DeflateRect((int)(ratex*70),0,0,0);
	pDC->DrawText(darrays[2],rect,DT_CENTER);
	
	rect.DeflateRect((int)(-40*ratex),(int)(-164*ratey),0,0);
	pDC->DrawText("金额",rect,DT_CENTER);
	for(i=0;i<5;i++)
	{
		if(i==0)
		{
			rect.DeflateRect((int)(ratex*13),(int)(ratey*12),0,0);
		}
		else
		{
			rect.DeflateRect((int)(-119*ratex),(int)(ratey*24),0,0);
		}
		pDC->DrawText(arrays[i][0],rect,DT_LEFT);
		rect.DeflateRect((int)(ratex*17),0,0,0);
		pDC->DrawText(arrays[i][1],rect,DT_LEFT);
		rect.DeflateRect((int)(ratex*17),0,0,0);
		pDC->DrawText(arrays[i][2],rect,DT_LEFT);
		rect.DeflateRect((int)(ratex*17),0,0,0);
		pDC->DrawText(arrays[i][3],rect,DT_LEFT);
		rect.DeflateRect((int)(ratex*17),0,0,0);
		pDC->DrawText(arrays[i][4],rect,DT_LEFT);
		rect.DeflateRect((int)(ratex*17),0,0,0);
		pDC->DrawText(arrays[i][5],rect,DT_LEFT);
		rect.DeflateRect((int)(ratex*17),0,0,0);
		pDC->DrawText(arrays[i][6],rect,DT_LEFT);
		rect.DeflateRect((int)(ratex*17),0,0,0);
		pDC->DrawText(arrays[i][7],rect,DT_LEFT);
	}
	rect.DeflateRect((int)(-28*ratex),(int)(ratey*27),0,0);
	pDC->DrawText(arrays[5][7],rect,DT_LEFT);
	rect.DeflateRect((int)(-50*ratex),0,0,0);
	pDC->DrawText(arrays[5][6],rect,DT_LEFT);
	rect.DeflateRect((int)(-50*ratex),0,0,0);
	pDC->DrawText(arrays[5][5],rect,DT_LEFT);
	rect.DeflateRect((int)(-50*ratex),0,0,0);
	pDC->DrawText(arrays[5][4],rect,DT_LEFT);
	rect.DeflateRect((int)(-50*ratex),0,0,0);
	pDC->DrawText(arrays[5][3],rect,DT_LEFT);
	rect.DeflateRect((int)(-50*ratex),0,0,0);
	pDC->DrawText(arrays[5][2],rect,DT_LEFT);
	rect.DeflateRect((int)(-50*ratex),0,0,0);
	pDC->DrawText(arrays[5][1],rect,DT_LEFT);
	rect.DeflateRect((int)(-50*ratex),0,0,0);
	pDC->DrawText(arrays[5][0],rect,DT_LEFT);
	if(isprinted)
	{
		pDC->EndDoc();
	}
}

void CPreview::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CRect m_rect;
	GetClientRect(m_rect);	
	CDC* pDC = GetDC();
	screenx = pDC->GetDeviceCaps(LOGPIXELSX);
	screeny = pDC->GetDeviceCaps(LOGPIXELSY);
	DrawReport(m_rect,GetDC(),false);
	// Do not call CDialog::OnPaint() for painting messages
}

HBRUSH CPreview::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	CBrush m_brush (RGB(255,255,255));
	
	CRect m_rect;
	GetClientRect(m_rect);
	
	pDC->FillRect(m_rect,&m_brush);
	// TODO: Return a different brush if the default is not desired
	return m_brush;
}

BOOL CPreview::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	sarrays[0][0] = "编号";
	sarrays[0][1] = "商品名称";
	sarrays[0][2] = "规格";
	sarrays[0][3] = "单位";
	sarrays[0][4] = "数量";
	sarrays[0][5] = "单价";
	arrays[0][0] = "十";
	arrays[0][1] = "万";
	arrays[0][2] = "千";
	arrays[0][3] = "百";
	arrays[0][4] = "十";
	arrays[0][5] = "元";
	arrays[0][6] = "角";
	arrays[0][7] = "分";
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPreview::hanshu()
{
	int m;
	CString	str1,str2;
	for(int i=0;i<4;i++)
	{
		harrays[i].Replace(".","");
		m = harrays[i].GetLength();
		str1 = harrays[i].Left(1);
		arrays[i+1][8-m] = str1;
		str2 = harrays[i].Right(m-1);
		m=str2.GetLength();
		while(m>0)
		{
			str1=str2.Left(1);
			arrays[i+1][8-m]= str1;
			str2=str2.Right(m-1);
			m=str2.GetLength();
		}
	}
	harrays[3].Replace("0","零");
	harrays[3].Replace("1","壹");
	harrays[3].Replace("2","贰");
	harrays[3].Replace("3","叁");
	harrays[3].Replace("4","肆");
	harrays[3].Replace("5","伍");
	harrays[3].Replace("6","陆");
	harrays[3].Replace("7","柒");
	harrays[3].Replace("8","捌");
	harrays[3].Replace("9","玖");
	m = harrays[3].GetLength();
	str1 = harrays[3].Left(2);
	arrays[5][8-m/2] = str1;
	str2 = harrays[3].Right(m-2);
	m=str2.GetLength();
	while(m>0)
	{
		str1=str2.Left(2);
		arrays[5][8-m/2]= str1;
		str2=str2.Right(m-2);
		m=str2.GetLength();
	}
}
