📄 clientview.cpp
字号:
// ClientView.cpp : implementation of the CClientView class
//
#include "stdafx.h"
#include "Client.h"
#include "ClientDoc.h"
#include "ClientView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClientView
IMPLEMENT_DYNCREATE(CClientView, CView)
BEGIN_MESSAGE_MAP(CClientView, CView)
//{{AFX_MSG_MAP(CClientView)
ON_COMMAND(ID_CONNECT, OnConnect)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CClientView construction/destruction
CClientView::CClientView()
{
// TODO: add construction code here
}
CClientView::~CClientView()
{
}
BOOL CClientView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CClientView drawing
void CClientView::OnDraw(CDC* pDC)
{
CClientDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
pDC->TextOut(10, 10, m_sMessage);
}
/////////////////////////////////////////////////////////////////////////////
// CClientView printing
BOOL CClientView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CClientView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CClientView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CClientView diagnostics
#ifdef _DEBUG
void CClientView::AssertValid() const
{
CView::AssertValid();
}
void CClientView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CClientDoc* CClientView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CClientDoc)));
return (CClientDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CClientView message handlers
void CClientView::OnConnect()
{
// 要发送的数据
CString Message = "[测试数据,由客户机发出]";
// 发送的是数据长度
DWORD WriteNum;
// 等待与服务器的连接
if (WaitNamedPipe("\\\\.\\Pipe\\Test", NMPWAIT_WAIT_FOREVER) == FALSE)
{
// 显示信息
m_sMessage = "等待连接失败!";
Invalidate();
return;
}
// 打开已创建的管道句柄
HANDLE hPipe = CreateFile("\\\\.\\Pipe\\Test", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hPipe == INVALID_HANDLE_VALUE)
{
// 显示信息
m_sMessage = "管道打开失败!";
Invalidate();
return;
}
else
{
// 显示信息
m_sMessage = "成功打开管道!";
Invalidate();
}
// 向管道写入数据
if (WriteFile(hPipe, Message, Message.GetLength(), &WriteNum, NULL) == FALSE)
{
// 显示信息
m_sMessage = "数据写入管道失败!";
Invalidate();
}
else
{
// 显示信息
m_sMessage = "数据成功写入管道!";
Invalidate();
}
// 关闭管道句柄
CloseHandle(hPipe);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -