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

📄 ena---mfcview.cpp

📁 开发环境:Visual C++ .net2003 功能介绍:神经网络算法实验;分Console版本和MFC版本;主要用来求解TSP问题。
💻 CPP
字号:
// ENA---MFCView.cpp : implementation of the CENAMFCView class
//

#include "stdafx.h"
#include "ENA---MFC.h"

#include "ENA---MFCDoc.h"
#include "ENA---MFCView.h"
//#include <GDI+.h>
//using namespace Gdiplus;
#include<gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CENAMFCView

IMPLEMENT_DYNCREATE(CENAMFCView, CView)

BEGIN_MESSAGE_MAP(CENAMFCView, CView)
	// 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)
	ON_WM_SIZE()
END_MESSAGE_MAP()

// CENAMFCView construction/destruction

CENAMFCView::CENAMFCView()
{
	// TODO: add construction code here
	m_pTSP = NULL;
	m_pTvRate = 1.0;
}

CENAMFCView::~CENAMFCView()
{
}

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

	return CView::PreCreateWindow(cs);
}

// CENAMFCView drawing

//bool bFind = false;
//int curX = 500;
//int curY = 500;
void CENAMFCView::OnDraw(CDC* pDC)
{
	CENAMFCDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	//this->UpdateWindow();
	// TODO: add draw code for native data here

	CRect rect;

	// 获取绘制坐标的文本框            

	//获得对话框上的picture的窗口句柄
	GetClientRect(&rect);
	// 指针

	//内存绘图

	//(1)创建可兼容内存绘图DC
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);  
	//(2)用CBitmap对象创建可兼容位图
	CBitmap memBitmap;
	CBitmap* pOldBmp = NULL;

	memBitmap.CreateCompatibleBitmap(pDC,rect.right,rect.bottom);
	//(3)将CBitmap对象选入CDC对象(memDC)中,并存储原位图,以试当前操作完成后恢复
	pOldBmp = memDC.SelectObject(&memBitmap);
	//(4)在memDC上执行所有绘图操作 
	//将原DC的内容拷贝到可兼容内存DC中
	memDC.BitBlt(rect.left,rect.top,rect.right,rect.bottom,pDC,0,0,SRCCOPY);

	//自定义绘图函数,详细见源程序
	if(m_pTSP) WSDrawTSP(&memDC);
	//m_pTSP = NULL;
	/*	DrawWave(&memDC);   */  

	//(5)在memDC上绘图完毕后,用BitBlt()函数把内存绘图拷贝到屏幕上(窗口的DC)
	pDC->BitBlt(rect.left,rect.top,rect.right,rect.bottom,&memDC,0,0,SRCCOPY); 

	//(6)恢复原位图,并释放创建的可兼容DC对象和CBitmap对象
	memDC.SelectObject(pOldBmp);
	memDC.DeleteDC();
	memBitmap.DeleteObject();	

	//if(m_pTSP) WSDrawTSP(pDC);
	//m_pTSP = NULL;

	/*if(bFind)
	{
		this->UpdateData(TRUE);
		pDC->LineTo(curX, curY);
		curX--, curY++;
		this->InvalidateRect(NULL);
		Sleep(100);		
	}
	bFind = !bFind;*/
}
////////////////////////////////////////////////////////////////////////////////////////
//用户自定义成员函数---begin
////////////////////////////////////////////////////////////////////////////////////////
void CENAMFCView::DrawCity(CDC* pDC, float x, float y)
{
	x = ToInt(m_pTvRate*x);
	y = ToInt(m_pTvRate*y);
	//CDC* pDC = this->GetDC();
	pDC->MoveTo(x, y);	
	CPen pen(PS_SOLID, 5, RGB(0x00, 0x00, 0xff));
	CPen *oldpen;
	oldpen = pDC->SelectObject(&pen);
	pDC->LineTo(x, y);	
	pDC->SelectObject(oldpen);
	//pDC->DeleteDC();
}

void CENAMFCView::DrawFindCity(CDC* pDC, float x, float y)
{
	x = ToInt(m_pTvRate*x);
	y = ToInt(m_pTvRate*y);
	//CDC* pDC = this->GetDC();
	pDC->MoveTo(x, y);	
	CPen pen(PS_SOLID, 8, RGB(0xff, 0x00, 0x00));
	CPen *oldpen;
	oldpen = pDC->SelectObject(&pen);
	pDC->LineTo(x, y);	
	pDC->SelectObject(oldpen);
	//pDC->DeleteDC();
}

void CENAMFCView::DrawAllCity(CDC* pDC)
{
	int i;
	//CDC* pDC = this->GetDC();		
	CString l_tmpStr;
	
	for(i = 0; i < m_pTSP->m_CityNum; ++i)
	{
		if(m_pTSP->m_pCity[i].m_bFound) 
		{
			DrawFindCity(pDC, m_pTSP->m_pCity[i].x, m_pTSP->m_pCity[i].y);
		}
		else
		{
			DrawCity(pDC, m_pTSP->m_pCity[i].x, m_pTSP->m_pCity[i].y);
		}
		l_tmpStr.Format(_T("%2d"), i);
		pDC->TextOut(ToInt(m_pTvRate*m_pTSP->m_pCity[i].x+2),ToInt(m_pTvRate*m_pTSP->m_pCity[i].y+2),l_tmpStr);
		//pDC->TextOutW(ToInt(m_pTvRate*m_pTSP->m_pCity[i].x+2),ToInt(m_pTvRate*m_pTSP->m_pCity[i].y+2),l_tmpStr);
	}
	//pDC->DeleteDC();
}
void CENAMFCView::DrawTempPoint(CDC* pDC, float x, float y)
{
	x = ToInt(m_pTvRate*x);
	y = ToInt(m_pTvRate*y);
	//CDC* pDC = this->GetDC();
	pDC->MoveTo(x, y);	

	CPen pen(PS_SOLID, 4, RGB(0x00, 0xff, 0x00));
	CPen *oldpen;
	oldpen = pDC->SelectObject(&pen);
	pDC->LineTo(x, y);	
	pDC->SelectObject(oldpen);
	//pDC->DeleteDC();
}

void CENAMFCView::DrawTempPointLine(CDC* pDC, float x1, float y1, float x2, float y2)
{
	x1 = ToInt(m_pTvRate*x1);
	y1 = ToInt(m_pTvRate*y1);
	x2 = ToInt(m_pTvRate*x2);
	y2 = ToInt(m_pTvRate*y2);

	//CDC* pDC = this->GetDC();
	pDC->MoveTo(x1, y1);	

	CPen pen(PS_SOLID, 2, RGB(0x00, 0x00, 0x00));
	CPen *oldpen;
	oldpen = pDC->SelectObject(&pen);
	pDC->LineTo(x2, y2);	
	pDC->SelectObject(oldpen);	
	//pDC->DeleteDC();
}
void CENAMFCView::DrawTempPointConnect(CDC *pDC)
{
	int i;
	for(i = 0; i < m_pTSP->m_TempPointNum; ++i)
	{
		if(m_pTSP->m_pTempPoint[i].m_PointPosition.m_bFound) 
			DrawCity(pDC, m_pTSP->m_pTempPoint[i].m_PointPosition.x, 
						m_pTSP->m_pTempPoint[i].m_PointPosition.y);
		else
			DrawTempPoint(pDC, m_pTSP->m_pTempPoint[i].m_PointPosition.x, 
						m_pTSP->m_pTempPoint[i].m_PointPosition.y);

		DrawTempPointLine(pDC, m_pTSP->m_pTempPoint[i].m_PointPosition.x, 
							m_pTSP->m_pTempPoint[i].m_PointPosition.y,
							m_pTSP->m_pTempPoint[ (i+1)%m_pTSP->m_TempPointNum ].m_PointPosition.x,
							m_pTSP->m_pTempPoint[ (i+1)%m_pTSP->m_TempPointNum ].m_PointPosition.y);

		if(m_pTSP->m_pTempPoint[ (i+1)%m_pTSP->m_TempPointNum ].m_PointPosition.m_bFound) 
			DrawCity(pDC, m_pTSP->m_pTempPoint[ (i+1)%m_pTSP->m_TempPointNum ].m_PointPosition.x,
						m_pTSP->m_pTempPoint[ (i+1)%m_pTSP->m_TempPointNum ].m_PointPosition.y);
		else
			DrawTempPoint(pDC, m_pTSP->m_pTempPoint[ (i+1)%m_pTSP->m_TempPointNum ].m_PointPosition.x,
						m_pTSP->m_pTempPoint[ (i+1)%m_pTSP->m_TempPointNum ].m_PointPosition.y);
	}
}

int  CENAMFCView::ToInt(float x)
{
	return (int)(x + 0.5);
}
void CENAMFCView::WSDrawTSP(CDC *pDC)
{
	//CDC* pDC = this->GetDC();
	//LPRECT lpRect;
	//this->GetClientRect(lpRect);//不能得到当前视图的坐标范围	
	pDC->FillSolidRect(0, 0, 1000, 1000, 0xffffff);

	CString l_tmpStr;
	l_tmpStr.Format(_T("当前温度[%10.6f]当前张力参数[%10.6f]"), m_pTSP->m_Temperature, m_pTSP->m_b);
	//pDC->TextOutW(0,0,l_tmpStr);
	pDC->TextOut(0,0,l_tmpStr);

	DrawAllCity(pDC);
	DrawTempPointConnect(pDC);
	//pDC->DeleteDC();
}
void CENAMFCView::StartDraw()
{
	CDC* pDC = this->GetDC();
	OnDraw(pDC);
	pDC->DeleteDC();
}
void CENAMFCView::TextDraw(int x, int y, CString val)
{
	CDC *pDC = this->GetDC();
	//pDC->TextOutW(x, y, val);
	pDC->TextOut(x, y, val);	
	pDC->DeleteDC();
}
////////////////////////////////////////////////////////////////////////////////////////
//用户自定义成员函数---end
////////////////////////////////////////////////////////////////////////////////////////
// CENAMFCView printing

BOOL CENAMFCView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CENAMFCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CENAMFCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}


// CENAMFCView diagnostics

#ifdef _DEBUG
void CENAMFCView::AssertValid() const
{
	CView::AssertValid();
}

void CENAMFCView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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


// CENAMFCView message handlers

void CENAMFCView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
	//if(m_pTSP)	WSDrawTSP();
}

⌨️ 快捷键说明

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