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

📄 myprinterdlg.cpp

📁 从列表进行打印的源码,请大家下载参考对照,从中发表意见,加深修改.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MyPrinterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyPrinter.h"
#include "MyPrinterDlg.h"
#include "DlgProxy.h"
#include "newbilldlg.h"
#include "Columns.h"
#include "Column.h"
#include "MyClass.h"
#include "myadddel.h"
#include "MyDlgSetup.h"
#include "MyDlgBrowse.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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyPrinterDlg dialog

IMPLEMENT_DYNAMIC(CMyPrinterDlg, CDialog);

CMyPrinterDlg::CMyPrinterDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyPrinterDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyPrinterDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pAutoProxy = NULL;
}

CMyPrinterDlg::~CMyPrinterDlg()
{
	// If there is an automation proxy for this dialog, set
	//  its back pointer to this dialog to NULL, so it knows
	//  the dialog has been deleted.
	if (m_pAutoProxy != NULL)
		m_pAutoProxy->m_pDialog = NULL;
}

void CMyPrinterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyPrinterDlg)
	DDX_Control(pDX, IDC_EDIT_HOSPITAL, m_Edit_Hospital);
	DDX_Control(pDX, IDC_COMBO_WAREHOUSE, m_Combo_WareHouse);
	DDX_Control(pDX, IDC_COMBO_NO_, m_Combo_NO_);
	DDX_Control(pDX, IDC_DATETIMEPICKER, m_DateTime);
	DDX_Control(pDX, IDC_COMBO_HOSPITAL, m_Combo_Hospital);
	DDX_Control(pDX, IDC_STATIC_TEXT, m_Static_Text);
	DDX_Control(pDX, IDC_DATAGRID_MAIN, m_DataGrid_Main);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyPrinterDlg, CDialog)
	//{{AFX_MSG_MAP(CMyPrinterDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_COMMAND(ID_MENU_ABOUT, OnMenuAbout)
	ON_COMMAND(ID_MENU_EXIT, OnMenuExit)
	ON_COMMAND(ID_MENU_NEW, OnMenuNew)
	ON_BN_CLICKED(IDC_BUTTON_REFRESH, OnButtonRefresh)
	ON_BN_CLICKED(IDC_BUTTON_SURE, OnButtonSure)
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
	ON_COMMAND(ID_MENU_CANCEL, OnMenuCancel)
	ON_CBN_SELCHANGE(IDC_COMBO_HOSPITAL, OnSelchangeComboHospital)
	ON_NOTIFY(DTN_CLOSEUP, IDC_DATETIMEPICKER, OnCloseupDatetimepicker)
	ON_COMMAND(ID_MENU_DEL, OnMenuDel)
	ON_COMMAND(ID_MENU_ADD, OnMenuAdd)
	ON_COMMAND(ID_MENU_SETUP, OnMenuSetup)
	ON_COMMAND(ID_MENU_ADDNAME, OnMenuAddName)
	ON_COMMAND(ID_MENU_ADDPLANT, OnMenuAddPlant)
	ON_COMMAND(ID_MENU_ADDUNIT, OnMenuAddUnit)
	ON_COMMAND(ID_MENU_HISTORY, OnMenuHistory)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyPrinterDlg message handlers

BOOL CMyPrinterDlg::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);

	HRESULT hr;

	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
	HWND hwnd;
	hwnd=GetSafeHwnd();
	::SetWindowLong(hwnd,GWL_EXSTYLE,WS_EX_APPWINDOW);

	this->PrepareDB ("0");

	this->InitHospital ();
	this->m_Combo_NO_ .SetCurSel (0);
	this->m_Combo_WareHouse .SetCurSel (0);

	//检测总记录数
	try
	{
		if(p_MyRstTemp->GetState ()==adStateOpen)
			p_MyRstTemp->Close ();
		hr=p_MyRstTemp->Open (_variant_t("打印单据"),
			_variant_t((IDispatch *)p_MyCon,true),
			adOpenKeyset,
			adLockOptimistic,
			adCmdTable
			);
		if(FAILED(hr))
		{
			CWnd::MessageBox ("记录集打开失败");
		}
		if(p_MyRstTemp->GetRecordCount ()>10000)
		{
			this->MessageBox ("\n数据库中历史数据已经超过一万条,为了确保系统的正常运行,请到【查看】->【历史纪录】中删除无用信息!\n");
		}
	}
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ());
	}

	//this->GetDlgItem(IDC_DATAGRID_MAIN)->SetWindowText("坪上20051201");
	/*try
	{
		HRESULT hr=m_pConn.CreateInstance(__uuidof(Connection));
		if(FAILED(hr))
		{
			this->MessageBox(数据库初始化失败!","提示",MB_OKCANCEL|MB_ICONWARNING);
			return TRUE;
		}
		_bstr_t strConn=m_pConnStr;//get the transported connection string
		m_pRec.CreateInstance(__uuidof(Recordset));
	}
	catch(_com_error &err)
	{
		this->MessageBox(err.Description (),"提示",MB_OKCANCEL|MB_ICONWARNING);
	}

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

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

// Automation servers should not exit when a user closes the UI
//  if a controller still holds on to one of its objects.  These
//  message handlers make sure that if the proxy is still in use,
//  then the UI is hidden but the dialog remains around if it
//  is dismissed.

void CMyPrinterDlg::OnClose() 
{
	if (CanExit())
		CDialog::OnClose();
}

void CMyPrinterDlg::OnOK() 
{
	//盘点总表
	this->m_Combo_WareHouse .GetWindowText (CMyClass::s_WareHouse );
	this->m_Combo_NO_ .GetWindowText (CMyClass::s_NO_ );
	if(CMyClass::b_BeingPreview ==TRUE)
	{
		CWnd::MessageBox ("您的另一个打印预览窗口已经打开!");
		return ;
	}

	CString str;
	DWORD winVer,dwWindowsMajorVersion,dwWindowsMinorVersion;
	winVer=GetVersion();
	dwWindowsMajorVersion =  (DWORD)(LOBYTE(LOWORD(winVer)));
	dwWindowsMinorVersion =  (DWORD)(HIBYTE(LOWORD(winVer)));

	//if(winVer<0x80000000)
	//{
//		CWnd::MessageBox ("你现在用的是win NT或者 win2000");
	//}
	if(dwWindowsMajorVersion ==5L&&dwWindowsMinorVersion==1L)
	{
		CWnd::MessageBox ("本系统要求工作在win2000或者winNT下,你现在用的是winXP,可能会不稳定!");
	}
	if(dwWindowsMajorVersion==4)
	{
		if(dwWindowsMinorVersion==10L)
		{
		  CWnd::MessageBox ("本系统要求工作在win2000或者winNT下,你现在用的是win98,可能会不稳定!");
		}
		if(dwWindowsMinorVersion==90L)
		{
		  CWnd::MessageBox ("本系统要求工作在win2000或者winNT下,你现在用的是win95,可能会不稳定!");
		}
	}
	if(dwWindowsMajorVersion < 4)
	{
		CWnd::MessageBox ("本系统要求工作在win2000或者winNT下,你现在用的是win3.X,系统无法正常工作!\n\n按“确定”返回");
		return;
	}

//	PRINTDLG ptg;
//	ptg.Flags =PD_NOSELECTION|PD_PAGENUMS|PD_PRINTSETUP;

/*
	CPrintDialog printDlg(FALSE);
	if(printDlg.DoModal ()==TRUE)
	{
		//lpdevmode=printDlg.GetDevMode();
		str=printDlg.GetDeviceName();
		str+=",";
		str+=printDlg.GetDriverName();
		str+=",";
		str+=printDlg.GetPortName ();
			
	WriteProfileString("windows", "device", str); //+"打印机名,WINSPOOL,lpt1:"
    SendMessageTimeout(HWND_BROADCAST, WM_WININICHANGE, 0L, 0L, SMTO_ABORTIFHUNG, 1000, NULL); 
//		CWnd::MessageBox ("printer is OK!");
	}
	else
	{
	//	CMyclass::b_BeingPreview =FALSE;	
		return;//	CWnd::MessageBox ("printer is cancel!");
	}
*/
//------------------------------------------------------------
	// TODO: Add your specialized creation code here
	CChildFrame* pFrame = new CChildFrame;
	m_pChildFrame = pFrame;
	//m_pChildFrame->m_ConnStr=m_pConnStr;

	pFrame->Create("CChildFrame","",WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,
		           CChildFrame::rectDefault,NULL,NULL,0,NULL);

	m_pChildFrame->SetWindowText("打印预览");
//wklh12	m_pChildFrame->SetIcon(m_hIcon, FALSE);		// Set small icon
      m_pChildFrame->ShowWindow(SW_HIDE);//SHOW
        m_pChildFrame->ShowWindow(SW_SHOWMAXIMIZED);
	m_pChildFrame->UpdateWindow();

	//传递数据库指针
	pFrame->p_connect =this->p_MyCon ;
	pFrame->p_recordset =this->p_MyRst;

//--------------------------------------------------------------

	CMyPrinterApp* app=(CMyPrinterApp*)AfxGetApp();//wrlh12
	m_pChildFrame->ShowWindow(SW_SHOW);//HIDE
	m_pChildFrame->SendMessage(WM_COMMAND, ID_FILE_PRINT_PREVIEW);


//	if (CanExit())
	//	CDialog::OnOK();
}

void CMyPrinterDlg::OnCancel() 
{
	if(1==this->MessageBox ("你确定要退出吗!  ^_^","提示",MB_OKCANCEL|MB_ICONINFORMATION))
	{//2-取消按钮,1-确定按钮
		if (CanExit())
		CDialog::OnCancel();
	}
	else
		return;
		
	//this->MessageBox ("哥们,退出? 确定一下先!  ^_^");
	if (CanExit())//你确定要退出吗?
		CDialog::OnCancel();

}

BOOL CMyPrinterDlg::CanExit()
{
	// If the proxy object is still around, then the automation
	//  controller is still holding on to this application.  Leave
	//  the dialog around, but hide its UI.
	if (m_pAutoProxy != NULL)
	{
		ShowWindow(SW_HIDE);
		return FALSE;
	}

	return TRUE;
}

void CMyPrinterDlg::OnMenuAbout() 
{
	// TODO: Add your command handler code here
	CAboutDlg Aboutdlg;
	//Aboutdlg.m_pConnStr=m_pConnStr;
	Aboutdlg.DoModal();
}

void CMyPrinterDlg::OnMenuExit() 
{
	// TODO: Add your command handler code here
		
		
	if(1==this->MessageBox ("你确定要退出吗?","提示",MB_OKCANCEL|MB_ICONINFORMATION))
	{//2-取消按钮,1-确定按钮
		if (CanExit())
		CDialog::OnCancel();
	}
	else
		return;
}

int CMyPrinterDlg::PrepareDB(CString str)
{
	CString str_Path;

	str="";

	HRESULT hr;
	//初始化连接指针
	this->FindDB(str);
	this->TrimDBPath(str);

	CoInitialize(NULL);
	hr=p_MyCon.CreateInstance(_uuidof(Connection));
	if(FAILED(hr))
	{
		CWnd::MessageBox ("_ConnectionPtr初始化错误!");
		return 1;
	}

	str_Path.Format("Provider=MSDASQL;Driver={Microsoft Access Driver (*.mdb)};Dbq=%s;Uid=Admin;Pwd=",str);
	bstr_Con=_bstr_t(str_Path);
	
	//连接到数据库上
	try
	{
		p_MyCon->Open(bstr_Con,"","",NULL);

	}
	catch(_com_error &err)
	{
		CWnd::MessageBox (err.Description ());
		return 2;
	}

   //初始化recordset指针
	hr=p_MyRst.CreateInstance(_uuidof(Recordset));
	if(FAILED(hr))

⌨️ 快捷键说明

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