www.pudn.com > WORD_useword.rar > UseWordDlg.cpp
// UseWordDlg.cpp : implementation file
//
#include "stdafx.h"
#include "UseWord.h"
#include "UseWordDlg.h"
#include "PrintInWord.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()
/////////////////////////////////////////////////////////////////////////////
// CUseWordDlg dialog
CUseWordDlg::CUseWordDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUseWordDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUseWordDlg)
// 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_blnPrinting = false;
}
void CUseWordDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUseWordDlg)
DDX_Control(pDX, IDC_PROGRESS, m_progress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUseWordDlg, CDialog)
//{{AFX_MSG_MAP(CUseWordDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_DETAIL, OnButtonDetail)
ON_WM_COPYDATA()
ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUseWordDlg message handlers
BOOL CUseWordDlg::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
//test();
// TODO: Add extra initialization here
m_progress.SetRange(0,10);
ShowSmallWindow();
return TRUE; // return TRUE unless you set the focus to a control
}
void CUseWordDlg::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 CUseWordDlg::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 CUseWordDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
UINT BeginThread(LPVOID pParam)
{
// TODO: Add extra validation here
datanode *node = new datanode;
CString strShipName ="船的中文名称";
CString strShipNameEn= "船的英文名称";
CString strDrawingName ="图纸名称";
CString strSystemName = "系统名称";
CString strSystemNameEn= "系统英文名称";
//判断打印结束是否调用文档
CUseWordDlg *dlg = (CUseWordDlg *) CDialog::FromHandle((HWND)pParam);
CPrintInWord printTest(node,strShipName,strShipNameEn,strDrawingName,strSystemName,strSystemNameEn,(HWND)pParam);
printTest.BeginPrint();
return 0;
}
void CUseWordDlg::OnOK()
{
m_startTime = CTime::GetCurrentTime();
m_endTime = CTime::GetCurrentTime();
//打印按钮变灰
((CButton*) GetDlgItem(IDOK))->EnableWindow(false);
//清空路径和时间
((CEdit*)GetDlgItem(IDC_EDIT_FILEPATH))->SetWindowText("");
((CEdit*)GetDlgItem(IDC_EDIT_USEDTIME))->SetWindowText("");
m_progress.ShowWindow(SW_SHOW);
m_progress.SetPos(0);
m_blnPrinting = true;
HWND hwd = GetSafeHwnd();
::AfxBeginThread(&BeginThread,(LPVOID)hwd,THREAD_PRIORITY_NORMAL);
}
void CUseWordDlg::OnCancel()
{
// TODO: Add extra cleanup here
if (m_blnPrinting)
{
MessageBox("正在进行打印,暂时无法退出程序!","打印进行中",MB_OK);
}
else
{
CDialog::OnCancel();
}
}
BOOL CUseWordDlg::PreTranslateMessage(MSG* pMsg)
{
static int pos = 0;
CTimeSpan m_diff;
CStatic *lblShow = (CStatic*)GetDlgItem(IDC_STATIUS);
switch(pMsg->message)
{
case WM_INITPRINTING:
lblShow->SetWindowText("正在初始化打印程序...");
pos = pos + 1;
break;
case WM_ADDTEMPLATE:
lblShow->SetWindowText("正在加载模版...");
break;
case WM_PRINTHEADER:
lblShow->SetWindowText("正在打印页眉...");
pos = pos + 1;
break;
case WM_PRINTPARTONE:
lblShow->SetWindowText("正在打印阀件...");
pos = pos + 2;
break;
case WM_PRINTPARTTWO:
lblShow->SetWindowText("正在打印附件...");
pos = pos + 2;
break;
case WM_PRINTPARTTHREE:
lblShow->SetWindowText("正在打印管件...");
pos = pos + 2;
break;
case WM_SAVEDOCUMENT:
lblShow->SetWindowText("正在形成文档并存盘...");
pos = 10;
break;
case WM_EXITPRINT:
m_endTime = CTime::GetCurrentTime();
m_diff = m_endTime - m_startTime;
SetDlgItemText(IDC_EDIT_USEDTIME,SecondToStr(&m_diff));
lblShow->SetWindowText("报表完成...");
m_blnPrinting = false;
pos = pos + 1;
((CButton*) GetDlgItem(IDOK))->EnableWindow(true);
m_progress.ShowWindow(SW_HIDE);
break;
case WM_LOADDOCUMENT:
lblShow->SetWindowText("正在调用新形成的打印文档...");
//在print类中,可能由于线程级别的原因ShellExecute不能用
break;
case WM_FINISHEDPRINT:
lblShow->SetWindowText("打印完成...");
pos = pos + 1;
break;
default:
;
}
m_progress.SetPos(pos);
if (pos == 10)
{
pos = 0;
}
return CDialog::PreTranslateMessage(pMsg);
}
void CUseWordDlg::ShowSmallWindow(bool bln)
{
CRect rectdet;
CStatic *pStatic =(CStatic*) GetDlgItem(IDC_STATIC_DETIAL);
pStatic->GetWindowRect(rectdet);
CRect rect;
this->GetWindowRect(rect);
if (bln)
{
//不显示详细信息
this->SetWindowPos(NULL,rect.left,rect.top,rect.Width(),(rect.Height() - rectdet.Height()-10),SWP_NOMOVE);
}
else
{
this->SetWindowPos(NULL,rect.left,rect.top,rect.Width(),rect.Height() + rectdet.Height()+10,SWP_NOMOVE);
}
}
void CUseWordDlg::OnButtonDetail()
{
static bool blnShowSmall = false;
ShowSmallWindow(blnShowSmall);
blnShowSmall = !blnShowSmall;
}
BOOL CUseWordDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
// TODO: Add your message handler code here and/or call default
CEdit *pEdit =(CEdit*)(GetDlgItem(IDC_EDIT_FILEPATH));
CString str =(char*)pCopyDataStruct->lpData;
long len = pCopyDataStruct->cbData;
pEdit->SetWindowText(str.Left(len));
return CDialog::OnCopyData(pWnd, pCopyDataStruct);
}
void CUseWordDlg::OnButtonOpen()
{
CString strPath;
GetDlgItemText(IDC_EDIT_FILEPATH,strPath);
CFileFind fileFind;
if (!fileFind.FindFile(strPath))
{
AfxMessageBox("没有找到文件,无法打开新形成的文档!");
fileFind.Close();
return;
}
fileFind.Close();
int len = strPath.ReverseFind('\\');
CString strFileName = strPath.Mid(len+1);
CString strFolder = strPath.Left(len);
::ShellExecute(NULL,"Open", strFileName, NULL, strFolder, SW_SHOWNORMAL);
}
//将秒转化为字符串进行显示
CString CUseWordDlg::SecondToStr(CTimeSpan *tDiff)
{
CString str;
str.Format("共用时 %d 时:%d分:%d秒",tDiff->GetHours(),tDiff->GetMinutes(),tDiff->GetSeconds());
return str;
}