?? 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 + -