⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 visual c++网络通信程序开发指南附带的程序所有的源码。
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "Receiver.h"

#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// 自定义消息
#define WM_MAP_OPEN			WM_USER + 101
#define WM_DATA_READY		WM_USER + 102
#define WM_MAP_CLOSE		WM_USER + 103

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_MAP_OPEN, OnMapOpen)
	ON_MESSAGE(WM_MAP_CLOSE, OnMapClose)
	ON_MESSAGE(WM_DATA_READY, OnDataReady)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.cx =300;
	cs.cy =200;

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

void CMainFrame::OnMapOpen()
{
	// 打开文件映射内核对象
	m_hReceiveMap = OpenFileMapping(FILE_MAP_READ, FALSE, "DataMap");
	if (m_hReceiveMap == NULL)
		return;

	// 映射到进程的地址空间
	m_lpbReceiveBuf = (LPBYTE)MapViewOfFile(m_hReceiveMap, FILE_MAP_READ, 0, 0, 0);
	if (m_lpbReceiveBuf == NULL)
		return;
}

void CMainFrame::OnMapClose()
{
	// 撤消映像
	UnmapViewOfFile(m_lpbReceiveBuf);
	m_lpbReceiveBuf = NULL;

	// 关闭对象
	CloseHandle(m_hReceiveMap);
	m_hReceiveMap = NULL;
}

void CMainFrame::OnDataReady(WPARAM wParam, LPARAM lParam)
{
	// 接受缓存
	char RecvBuf[100];

	// 共享内存中的数据长度
	int length = (int)lParam;

	// 将数据复制到内存
	memcpy(RecvBuf, (char*)(m_lpbReceiveBuf), length);
	RecvBuf[length] = '\0';

	// 跟踪输出
	AfxMessageBox((CString)RecvBuf);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -