// VC.NetDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VC.Net.h"
#include "VC.NetDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CVCNetDlg dialog




CVCNetDlg::CVCNetDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVCNetDlg::IDD, pParent)
	, m_nV1(7)
	, m_nV2(3)
	, m_nV3(1)
	//, m_nV4(_T(""))
	, m_nV4(38)
	, m_nV5(24)
	, m_nV6(9)
	//, m_nV7(0)
	, m_nV8(0)
	, m_nV9(40)
	, m_nV10(10)
	, m_nV11(5)
	, m_nV7(_T("12345678"))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVCNetDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT2, m_nV1);
	DDX_Text(pDX, IDC_EDIT4, m_nV2);
	DDX_Text(pDX, IDC_EDIT3, m_nV3);
	//DDX_Text(pDX, IDC_EDIT11, m_nV4);
	DDX_Text(pDX, IDC_EDIT11, m_nV4);
	DDX_Text(pDX, IDC_EDIT10, m_nV5);
	DDX_Text(pDX, IDC_EDIT1, m_nV6);
	//DDX_Text(pDX, IDC_EDIT5, m_nV7);
	DDX_Text(pDX, IDC_EDIT6, m_nV8);
	DDX_Text(pDX, IDC_EDIT7, m_nV9);
	DDX_Text(pDX, IDC_EDIT8, m_nV10);
	DDX_Text(pDX, IDC_EDIT9, m_nV11);
	DDX_Text(pDX, IDC_EDIT5, m_nV7);
}

BEGIN_MESSAGE_MAP(CVCNetDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, &CVCNetDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CVCNetDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CVCNetDlg::OnBnClickedButton3)
END_MESSAGE_MAP()


// CVCNetDlg message handlers

BOOL CVCNetDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	m_bDraw = FALSE;
	SetDlgItemText(IDC_RICHEDIT21, "2004062100000014ST0005\x0d\x0a机电式");
	ZeroMemory(szFileName, MAX_PATH);
	GetModuleFileName(NULL, szFileName, MAX_PATH);
	for(int i = strlen(szFileName); i>0 ;i--)
	{
		if(szFileName[i] == '\\')
			break;
		else
			szFileName[i] = NULL;
	}
	strcat(szFileName, "temp.bmp");

	hInst = LoadLibrary("MakePDF417.dll");
	if(!hInst)
	{
		MessageBox("未发现MakePDF417.dll", "讯息", MB_OK|MB_ICONERROR);
		exit(0);
	}
	Make = (MAKE)GetProcAddress(hInst, "Make");
	MixText = (MIXTEXT)GetProcAddress(hInst, "MixText");
	SetColorDepth = (SETCOLORDEPTH)GetProcAddress(hInst, "SetColorDepth");
	GetColorDepth = (GETCOLORDEPTH)GetProcAddress(hInst, "GetColorDepth");

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CVCNetDlg::OnPaint()
{
	CDialog::OnPaint();

	if(!m_bDraw)
		return;

	CDC* pdc = GetDlgItem(IDC_IMAGE)->GetDC();
	CBitmap bitmap;
	HBITMAP m_hBmpNew =	 (HBITMAP) LoadImage(
	AfxGetInstanceHandle(),		// handle to instance
	szFileName,					// name or identifier of the image (root is where project is)
	IMAGE_BITMAP,				// image types
	0,							// desired width
	0,							// desired height
	LR_LOADFROMFILE);
	bitmap.Attach(m_hBmpNew) ;
	CDC memdc;
	memdc.CreateCompatibleDC(pdc);
	memdc.SelectObject(&bitmap) ;

	CRect rect;
	GetDlgItem(IDC_IMAGE)->GetWindowRect(&rect);
	ScreenToClient(&rect);
	
	BITMAP bmpinfo;
	bitmap.GetBitmap(&bmpinfo);
	pdc->StretchBlt(0, 0,rect.Width(),
		rect.Height(),&memdc,0,0,bmpinfo.bmWidth,bmpinfo.bmHeight,SRCCOPY);
	return ;
}

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


void CVCNetDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	CString str;
	GetDlgItemText(IDC_RICHEDIT21, str);
	if(str == "")
		return;
	if(!UpdateData())
		return;

    long lRet = SetColorDepth(m_nV5);
	Make((unsigned char*)str.GetBuffer(0), str.GetLength(), szFileName, m_nV1, m_nV2, m_nV3,m_nV4);
//	MakePdf417Barcode((BYTE*)str.GetBuffer(0), str.GetLength(), m_nV1, m_nV2, szFileName);
	lRet = GetColorDepth();

	lRet = MixText(szFileName, szFileName, (LPSTR)(LPCSTR)m_nV7, m_nV8, m_nV9
		, m_nV10, m_nV11, m_nV6);

	m_bDraw = TRUE;
	Invalidate();
}

void CVCNetDlg::OnBnClickedButton2()
{
	// TODO: Add your control notification handler code here
	char szTemp[MAX_PATH] = {NULL};
	CFileDialog  filedlg(TRUE, "bmp", "", OFN_HIDEREADONLY|OFN_FILEMUSTEXIST, "位图文件 (*.bmp)|*.bmp|所有文件 (*.*)|*.*||");
	if(IDCANCEL == filedlg.DoModal())
		return;
	CString filename = filedlg.GetPathName();
	CStdioFile file;
	if(file.Open(filename, CFile::modeRead|CFile::typeBinary) == 0)
	{
		CString str = "无法打开文件";
		str += filename;
		MessageBox(str.GetBuffer(0), "讯息", MB_ICONEXCLAMATION|MB_OK);
		return;
	}
	if(file.GetLength()>929)
	{
		CString str = "文件";
		str += filename;
		str += "的长度超过929个字节，无法生成二维条码";
		MessageBox(str.GetBuffer(0), "讯息", MB_ICONEXCLAMATION|MB_OK);
		return;
	}
	char szBuf[1024] = {NULL};
	long nLength =  file.Read(szBuf, 1024);

	if(!UpdateData(TRUE))
		return;
	SetColorDepth(m_nV5);
	Make((unsigned char*)szBuf, nLength , szFileName, m_nV1, m_nV2, m_nV3,m_nV4);
	MixText(szFileName, szFileName, (LPSTR)(LPCSTR)m_nV7, m_nV8, m_nV9
		, m_nV10, m_nV11, m_nV6);

//	MakePdf417Barcode((BYTE*)szBuf, strlen(szBuf), m_nV1, m_nV2, szFileName);
	m_bDraw = TRUE;
	Invalidate();
	file.Close();
}

void CVCNetDlg::OnBnClickedButton3()
{
	// TODO: Add your control notification handler code here
	CFileDialog  filedlg(FALSE, "bmp", "twobarcode.bmp", OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "位图文件 (*.bmp)|*.bmp|所有文件 (*.*)|*.*||");
	if(IDOK == filedlg.DoModal())
		CopyFile(szFileName, filedlg.GetPathName(), FALSE);
	Invalidate();
}

void CVCNetDlg::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	if(hInst)
		::FreeLibrary(hInst);
	CDialog::OnClose();
}