// MakeBarExeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MakeBarExe.h"
#include "MakeBarExeDlg.h"
#include <comdef.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()

/////////////////////////////////////////////////////////////////////////////
// CMakeBarExeDlg dialog

CMakeBarExeDlg::CMakeBarExeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMakeBarExeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMakeBarExeDlg)
	m_nV1 = 7;
	m_nV2 = 3;
	m_nV3 = 1;
	m_nHeight = 38;
	m_nColorDepth = 24;

	m_strTxt = _T("12345678");
	m_lMargin = 10;
	m_lFontSize = 0;
	m_lFontHeight = 40;
	m_lVMargin = 5;
	m_lLineCount = 9;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMakeBarExeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMakeBarExeDlg)
	DDX_Text(pDX, IDC_EDIT1, m_nV1);
	DDX_Text(pDX, IDC_EDIT2, m_nV2);
	DDX_Text(pDX, IDC_EDIT3, m_nV3);
	DDX_Text(pDX, IDC_EDIT_HEIGHT, m_nHeight);
	DDX_Text(pDX, IDC_EDIT_COLOR, m_nColorDepth);
	DDX_Text(pDX, IDC_EDIT_FONTSIZE, m_lFontSize);
	DDX_Text(pDX, IDC_EDIT_FONTHEIGHT, m_lFontHeight);
	DDX_Text(pDX, IDC_EDIT_LINECOUNT, m_lLineCount);
	DDX_Text(pDX, IDC_EDIT_LMARGIN, m_lMargin);
	DDX_Text(pDX, IDC_EDIT_TEXT, m_strTxt);
	DDX_Text(pDX, IDC_EDIT_VMARGIN, m_lVMargin);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMakeBarExeDlg, CDialog)
	//{{AFX_MSG_MAP(CMakeBarExeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMakeBarExeDlg message handlers

BOOL CMakeBarExeDlg::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
	m_bDraw = FALSE;
	SetDlgItemText(IDC_EDIT5, "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");


	m_hInst = LoadLibrary("MakePdf417.dll");
	if(!m_hInst)
	{
		MessageBox("未发现MakePdf417.dll", "讯息", MB_OK|MB_ICONERROR);
		::ExitProcess(0);
	}
	Make = (MAKE)GetProcAddress(m_hInst, "Make");
	MixText = (MIXTEXT)GetProcAddress(m_hInst, "MixText");
	SetColorDepth = (SETCOLORDEPTH)GetProcAddress(m_hInst, "SetColorDepth");
	GetColorDepth = (GETCOLORDEPTH)GetProcAddress(m_hInst, "GetColorDepth");
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMakeBarExeDlg::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 CMakeBarExeDlg::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 to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMakeBarExeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

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

	long lRet = SetColorDepth(m_nColorDepth);
	Make((BYTE*)str.GetBuffer(0), str.GetLength(), szFileName, m_nV1, m_nV2, m_nV3, m_nHeight);
	lRet = GetColorDepth();

	lRet = MixText(szFileName, szFileName, (LPSTR)(LPCSTR)m_strTxt, m_lFontSize, m_lFontHeight
		, m_lMargin, m_lVMargin, m_lLineCount);

	m_bDraw = TRUE;
	Invalidate();
}

void CMakeBarExeDlg::OnButton2() 
{
	// 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 CMakeBarExeDlg::OnButton3() 
{
	// 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 nDataLen = file.Read(szBuf, 1024);

	Make((BYTE*)szBuf, nDataLen, szFileName, m_nV1, m_nV2, m_nV3, m_nHeight);

	m_bDraw = TRUE;
	Invalidate();
	file.Close();	
}

void CMakeBarExeDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default

	FreeLibrary(m_hInst);

	CDialog::OnClose();
}
