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

📄 senderview.cpp

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

#include "stdafx.h"
#include "Sender.h"

#include "SenderDoc.h"
#include "SenderView.h"

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


/////////////////////////////////////////////////////////////////////////////
// CSenderView

IMPLEMENT_DYNCREATE(CSenderView, CFormView)

BEGIN_MESSAGE_MAP(CSenderView, CFormView)
	//{{AFX_MSG_MAP(CSenderView)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_SEND, OnSend)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSenderView construction/destruction

CSenderView::CSenderView()
	: CFormView(CSenderView::IDD)
{
	//{{AFX_DATA_INIT(CSenderView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CSenderView::~CSenderView()
{
}

void CSenderView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSenderView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BOOL CSenderView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CSenderView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
}

/////////////////////////////////////////////////////////////////////////////
// CSenderView diagnostics

#ifdef _DEBUG
void CSenderView::AssertValid() const
{
	CFormView::AssertValid();
}

void CSenderView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CSenderDoc* CSenderView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSenderDoc)));
	return (CSenderDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSenderView message handlers

void CSenderView::OnDestroy() 
{
	CFormView::OnDestroy();
}

void CSenderView::OnSend() 
{
	// 自定义消息
	#define WM_MAP_OPEN			WM_USER + 101
	#define WM_DATA_READY		WM_USER + 102
	#define WM_MAP_CLOSE		WM_USER + 103

	// 在系统页文件创建文件映射内核对象
	HANDLE hRecvMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, 
								PAGE_READWRITE | SEC_COMMIT,
								0, 1000000, "DataMap");
	if (hRecvMap == NULL)
		return;

	// 映射到进程的地址空间
	LPBYTE lpData = (LPBYTE)MapViewOfFile(hRecvMap, FILE_MAP_WRITE, 0, 0, 0);
	if (lpData == NULL)
		return;

	// 通知接收程序共享内存已建立
	CWnd* pWnd = FindWindow(NULL, "接收程序");
	if (pWnd != NULL)
		pWnd->PostMessage(WM_MAP_OPEN, 0, 0);
	
	// 要发送的数据
	CString Message = "本数据从发送方发出!";

	// 将数据写入到共享内存
	memcpy(lpData, Message, Message.GetLength());

	// 通知接收方数据准备好
	pWnd->PostMessage(WM_DATA_READY, (WPARAM)0, (LPARAM)Message.GetLength());

	// 延迟1毫秒
	Sleep(1);

	// 通知接收方关闭共享内存
	pWnd->PostMessage(WM_MAP_CLOSE, 0, 0);

	// 撤消映像
	UnmapViewOfFile(lpData);

	// 关闭映像对象
	CloseHandle(hRecvMap);
}

⌨️ 快捷键说明

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