?? mailslotclientview.cpp
字號:
// MailslotClientView.cpp : implementation of the CMailslotClientView class
//
#include "stdafx.h"
#include "MailslotClient.h"
#include "MailslotClientDoc.h"
#include "MailslotClientView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMailslotClientView
IMPLEMENT_DYNCREATE(CMailslotClientView, CView)
BEGIN_MESSAGE_MAP(CMailslotClientView, CView)
//{{AFX_MSG_MAP(CMailslotClientView)
ON_COMMAND(ID_LINK, OnLink)
ON_COMMAND(ID_SEND, OnSend)
ON_COMMAND(ID_CLOSE, OnClose)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMailslotClientView construction/destruction
CMailslotClientView::CMailslotClientView()
{
// TODO: add construction code here
}
CMailslotClientView::~CMailslotClientView()
{
}
BOOL CMailslotClientView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMailslotClientView drawing
void CMailslotClientView::OnDraw(CDC* pDC)
{
CMailslotClientDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDC->TextOut(10, 10, m_sMessage);
}
/////////////////////////////////////////////////////////////////////////////
// CMailslotClientView printing
BOOL CMailslotClientView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMailslotClientView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMailslotClientView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMailslotClientView diagnostics
#ifdef _DEBUG
void CMailslotClientView::AssertValid() const
{
CView::AssertValid();
}
void CMailslotClientView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMailslotClientDoc* CMailslotClientView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMailslotClientDoc)));
return (CMailslotClientDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMailslotClientView message handlers
void CMailslotClientView::OnLink()
{
// 打開郵槽
m_hMailSlot = CreateFile("\\\\*\\Mailslot\\Test",
GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (m_hMailSlot == INVALID_HANDLE_VALUE)
{
m_sMessage.Format("打開郵槽失敗,失敗代碼:%d", GetLastError());
Invalidate();
return;
}
else
{
m_sMessage = "成功打開郵槽!";
Invalidate();
}
}
void CMailslotClientView::OnSend()
{
// 要發送的數據
CString sMessage = "Hello World!";
// 實際發送出去的字節數
DWORD dwBytesWritten;
// 向郵槽寫入數據
int ret = WriteFile(m_hMailSlot, sMessage, sMessage.GetLength(), &dwBytesWritten, NULL);
if (ret == 0)
{
m_sMessage.Format("發送數據失敗,失敗代碼:%d", GetLastError());
Invalidate();
}
else
{
m_sMessage.Format("成功發送數據,數據長度:%d字節", dwBytesWritten);
Invalidate();
}
}
void CMailslotClientView::OnClose()
{
// 關閉郵槽句柄
CloseHandle(m_hMailSlot);
m_sMessage = "關閉與服務器的連接";
Invalidate();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -