?? mailslotserverview.cpp
字號(hào):
// MailslotServerView.cpp : implementation of the CMailslotServerView class
//
#include "stdafx.h"
#include "MailslotServer.h"
#include "MailslotServerDoc.h"
#include "MailslotServerView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
UINT ReadProc(LPVOID lpParam)
{
// 數(shù)據(jù)接收緩存
char buffer[256];
// 實(shí)際接收到的字節(jié)數(shù)
DWORD dwNumberOfBytesRead;
// 得到視指針
CMailslotServerView* pView = (CMailslotServerView*)lpParam;
// 持續(xù)對(duì)郵槽進(jìn)行讀取
while (ReadFile(pView->m_hMailSlot, buffer, 256, &dwNumberOfBytesRead, NULL) != 0)
{
// 顯示從郵槽接收到的數(shù)據(jù)
buffer[dwNumberOfBytesRead] = '\0';
pView->m_sMessage = CString(buffer);
pView->Invalidate();
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CMailslotServerView
IMPLEMENT_DYNCREATE(CMailslotServerView, CView)
BEGIN_MESSAGE_MAP(CMailslotServerView, CView)
//{{AFX_MSG_MAP(CMailslotServerView)
ON_COMMAND(ID_CREATE_MAILSLOT, OnCreateMailslot)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMailslotServerView construction/destruction
CMailslotServerView::CMailslotServerView()
{
// TODO: add construction code here
}
CMailslotServerView::~CMailslotServerView()
{
}
BOOL CMailslotServerView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMailslotServerView drawing
void CMailslotServerView::OnDraw(CDC* pDC)
{
CMailslotServerDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDC->TextOut(10, 10, m_sMessage);
}
/////////////////////////////////////////////////////////////////////////////
// CMailslotServerView printing
BOOL CMailslotServerView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMailslotServerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMailslotServerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMailslotServerView diagnostics
#ifdef _DEBUG
void CMailslotServerView::AssertValid() const
{
CView::AssertValid();
}
void CMailslotServerView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMailslotServerDoc* CMailslotServerView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMailslotServerDoc)));
return (CMailslotServerDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMailslotServerView message handlers
void CMailslotServerView::OnCreateMailslot()
{
// 創(chuàng)建郵槽
m_hMailSlot = CreateMailslot("\\\\.\\Mailslot\\Test", 0, MAILSLOT_WAIT_FOREVER, NULL);
if (m_hMailSlot == INVALID_HANDLE_VALUE)
{
m_sMessage.Format("創(chuàng)建郵槽失敗,失敗代碼:%d", GetLastError());
Invalidate();
return;
}
else
{
m_sMessage = "成功創(chuàng)建郵槽!";
Invalidate();
}
// 開啟數(shù)據(jù)接收線程
AfxBeginThread(ReadProc, this);
}
BOOL CMailslotServerView::DestroyWindow()
{
// 關(guān)閉郵槽句柄
CloseHandle(m_hMailSlot);
return CView::DestroyWindow();
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -