📄 mapiconnectview.cpp
字号:
// MAPIConnectView.cpp : implementation of the CMAPIConnectView class
//
#include "stdafx.h"
#include "MAPIConnect.h"
#include "MAPI.h"
#include "MAPIConnectDoc.h"
#include "MAPIConnectView.h"
#include "InputDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMAPIConnectView
IMPLEMENT_DYNCREATE(CMAPIConnectView, CFormView)
BEGIN_MESSAGE_MAP(CMAPIConnectView, CFormView)
//{{AFX_MSG_MAP(CMAPIConnectView)
ON_COMMAND(ID_MAIL_LOGOFF, OnMailLogoff)
ON_COMMAND(ID_MAIL_LOGON, OnMailLogon)
ON_WM_TIMER()
ON_UPDATE_COMMAND_UI(ID_MAIL_LOGOFF, OnUpdateMailLogoff)
ON_UPDATE_COMMAND_UI(ID_MAIL_LOGON, OnUpdateMailLogon)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMAPIConnectView construction/destruction
CMAPIConnectView::CMAPIConnectView()
: CFormView(CMAPIConnectView::IDD)
{
bValid = FALSE; // 初始时,未启动自动寻呼功能
//{{AFX_DATA_INIT(CMAPIConnectView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CMAPIConnectView::~CMAPIConnectView()
{
}
void CMAPIConnectView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMAPIConnectView)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BOOL CMAPIConnectView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CMAPIConnectView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
/////////////////////////////////////////////////////////////////////////////
// CMAPIConnectView printing
BOOL CMAPIConnectView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMAPIConnectView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMAPIConnectView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CMAPIConnectView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CMAPIConnectView diagnostics
#ifdef _DEBUG
void CMAPIConnectView::AssertValid() const
{
CFormView::AssertValid();
}
void CMAPIConnectView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CMAPIConnectDoc* CMAPIConnectView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMAPIConnectDoc)));
return (CMAPIConnectDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMAPIConnectView message handlers
// MAPI功能函数的声明,后面的补充说明部分有相应介绍。
ULONG (PASCAL *lpfnMAPISendMail) (LHANDLE lhSession,
ULONG ulUIParam, lpMapiMessage lpMessage,
FLAGS flFlags, ULONG ulReserved);
ULONG (PASCAL *lpfnMAPIResolveName) (LHANDLE lhSession,
ULONG ulUIParam, LPTSTR lpszName,
FLAGS ulFlags, ULONG ulReserved,
lpMapiRecipDesc FAR *lppRecip);
ULONG (FAR PASCAL *lpfnMAPILogon)(ULONG ulUIParam,
LPSTR lpszProfileName, LPSTR lpszPassword,
FLAGS flFlags, ULONG ulReserved,
LPLHANDLE lplhSession);
ULONG (FAR PASCAL *lpfnMAPILogoff)(LHANDLE lhSession,
ULONG ulUIParam, FLAGS flFlags,
ULONG ulReserved);
ULONG (FAR PASCAL *lpfnMAPIFreeBuffer)(LPVOID lpBuffer);
ULONG (FAR PASCAL *lpfnMAPIAddress)(LHANDLE lhSession,
ULONG ulUIParam, LPSTR lpszCaption,
ULONG nEditFields, LPSTR lpszLabels,
ULONG nRecips, lpMapiRecipDesc lpRecips,
FLAGS flFlags, ULONG ulReserved,
LPULONG lpnNewRecips,
lpMapiRecipDesc FAR *lppNewRecips);
ULONG (FAR PASCAL *lpfnMAPIFindNext)(LHANDLE lhSession,
ULONG ulUIParam, LPSTR lpszMessageType,
LPSTR lpszSeedMessageID, FLAGS flFlags,
ULONG ulReserved, LPSTR lpszMessageID);
ULONG (FAR PASCAL *lpfnMAPIReadMail)(LHANDLE lhSession, ULONG ulUIParam,
LPSTR lpszMessageID,
FLAGS flFlags, ULONG ulReserved,
lpMapiMessage FAR *lppMessage);
LPLHANDLE lplhSession;
// 退出邮箱菜单的响应,完成断开连接,取消定时器等操作
void CMAPIConnectView::OnMailLogoff()
{
// TODO: Add your command handler code here
KillTimer(1);
LHANDLE lhSession = *lplhSession;
lpfnMAPILogoff(lhSession, NULL, 0, 0);
}
//登陆邮箱菜单的响应,完成邮箱连接,获得寻呼台和寻呼机号的信息建立定时器,Modem联机等待
//操作
void CMAPIConnectView::OnMailLogon()
{
// TODO: Add your command handler code here
HINSTANCE hInstMail;
CInputDlg inputDlg;
if(inputDlg.DoModal() == IDOK)
{
m_strXunhutai = inputDlg.m_strHutai;
m_strXunhuji = inputDlg.m_strHuji;
hInstMail = ::LoadLibrary("MAPI32.DLL"); //动态加载MAPI32.DLL
if (hInstMail == NULL)
{
//错误代码
}
(FARPROC&) lpfnMAPISendMail = GetProcAddress(hInstMail,"MAPISendMail");
(FARPROC&) lpfnMAPIResolveName = GetProcAddress(hInstMail, "MAPIResolveName");
(FARPROC&) lpfnMAPILogon = GetProcAddress(hInstMail,"MAPILogon");
(FARPROC&) lpfnMAPILogoff = GetProcAddress(hInstMail,"MAPILogoff");
(FARPROC&) lpfnMAPIFreeBuffer = GetProcAddress(hInstMail, "MAPIFreeBuffer");
(FARPROC&) lpfnMAPIAddress = GetProcAddress(hInstMail,"MAPIAddress");
(FARPROC&) lpfnMAPIFindNext = GetProcAddress(hInstMail,"MAPIFindNext");
(FARPROC&) lpfnMAPIReadMail = GetProcAddress(hInstMail,"MAPIReadMail");
ULONG lResult = lpfnMAPILogon(0, NULL, NULL, MAPI_LOGON_UI, 0, lplhSession);
if (lResult != SUCCESS_SUCCESS)
//SUCCESS_SUCCESS在MAPI.H中被定义
{
// 错误处理
AfxMessageBox("连接错误");
}
SetTimer(1, 60000, NULL);
bValid = TRUE;
DWORD style=WS_VISIBLE|WS_CHILD;
if (!m_Com.Create(NULL,style,CRect(0,0,0,0),this,ID_COMMCTRL))
{
TRACE0("Failed to create OLE Communications Control\r\n");
return; //fail to create
}
// TODO: Add extra initialization here
m_Com.SetCommPort(1); //选择COM1
m_Com.SetInBufferSize(1024); //设置输入缓冲区的大小,Bytes
m_Com.SetOutBufferSize(512); //设置输入缓冲区的大小,Bytes
if(!m_Com.GetPortOpen()) //打开串口
{
m_Com.SetPortOpen(TRUE);
}
else
AfxMessageBox("串口1已被占用,请选择其他串口");
}
}
//定时器操作,完成对邮箱内未读邮件的检索,如果有未读邮件,则通过拨号进行自动寻呼
void CMAPIConnectView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
MapiMessage* pMessage;
memset(pMessage, 0, sizeof(MapiMessage));
char pMessageID[513];
LHANDLE lhSession = *lplhSession;
ULONG lResult;
lResult = lpfnMAPIFindNext(lhSession, NULL, NULL,
NULL, MAPI_LONG_MSGID | MAPI_UNREAD_ONLY,
0, pMessageID);
long nFlags = MAPI_SUPPRESS_ATTACH;
BOOL bNewMail = FALSE;
if(lResult == SUCCESS_SUCCESS)
{
lResult = lpfnMAPIReadMail(lhSession, NULL, pMessageID,
nFlags, 0, &pMessage);
if (lResult != SUCCESS_SUCCESS);
return;
bNewMail = TRUE;
lpfnMAPIFreeBuffer(pMessage);
lResult = lpfnMAPIFindNext(lhSession, NULL, NULL,
NULL, MAPI_LONG_MSGID | MAPI_UNREAD_ONLY,
0, pMessageID);
}
//有信件到达,拨号传呼
CString strTemp;
if(bNewMail)
{
//Modem自动等待连接
strTemp = "ATDT" + m_strXunhutai + "," + m_strXunhuji + "\r\n";
SendString(strTemp);
SendString("ATH0\r\n");
}
CFormView::OnTimer(nIDEvent);
}
// 设置"退出邮箱"菜单项是否可用
void CMAPIConnectView::OnUpdateMailLogoff(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(bValid);
}
// 设置"登录邮箱"菜单项是否可用
void CMAPIConnectView::OnUpdateMailLogon(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(!bValid);
}
// 通过串口发送指定字符串
void CMAPIConnectView::SendString(CString strTemp)
{
char TxData[100];
int Count = strTemp.GetLength();
//将字符串m_strSend中的字符存入数组TxData中
for(int i = 0; i < Count; i++)
TxData[i] = strTemp.GetAt(i);
//建立内存空间为Count的字节数组
CByteArray array;
array.RemoveAll();
array.SetSize(Count);
//将字符传从TxData中转入array中
for(i = 0; i < Count; i++)
array.SetAt(i, TxData[i]);
//发送字符串
m_Com.SetOutput(COleVariant(array));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -