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

📄 fractalview.cpp

📁 visual c++ 实例编程
💻 CPP
字号:

//
// FractalView.cpp : implementation of the CFractalView class
//

#include "stdafx.h"
#include "Fractal.h"

#include "FractalDoc.h"
#include "mainfrm.h"
#include "FractalView.h"
#include "adjustFractal.h"
#include "mainfrm.h"

#include "math.h"
#define PI 3.14159265358

#define MY_UPDATESTATUSBAR_MSG "Update Status Bar"
#define ML_DRAWING 1
#define ML_DONE 2

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

struct DrawInfo{
	HWND hWnd;
	CFractalDoc *pDoc;
	CRect rectClient;
};

static DrawInfo g_Environment;
static BOOL g_bDraw=false;
static UINT DrawFractal(LPVOID pPar);

HDC hdc;
HPEN hPen, hOldPen;

UINT UpdateMsg = RegisterWindowMessage(MY_UPDATESTATUSBAR_MSG);

UINT CFractalView::m_AdjustMsg=
	RegisterWindowMessage(MY_ADJUST_MSG);
/////////////////////////////////////////////////////////////////////////////
// CFractalView

IMPLEMENT_DYNCREATE(CFractalView, CView)

BEGIN_MESSAGE_MAP(CFractalView, CView)
	//{{AFX_MSG_MAP(CFractalView)
	ON_WM_MOUSEMOVE()
	ON_COMMAND(ID_VIEW_REFRESH, OnViewRefresh)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_REGISTERED_MESSAGE(m_AdjustMsg, OnAdjustBar)
	ON_REGISTERED_MESSAGE(UpdateMsg, OnUpdateStautsBar)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFractalView construction/destruction

CFractalView::CFractalView():m_pDrawThread(0){}
CFractalView::~CFractalView(){}
BOOL CFractalView::PreCreateWindow(CREATESTRUCT& cs){return CView::PreCreateWindow(cs);}

/////////////////////////////////////////////////////////////////////////////
// CFractalView diagnostics

#ifdef _DEBUG
void CFractalView::AssertValid() const{CView::AssertValid();}
void CFractalView::Dump(CDumpContext& dc) const{CView::Dump(dc);}

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

/////////////////////////////////////////////////////////////////////////////
// CFractalView message handlers

void CFractalView::OnMouseMove(UINT nFlags, CPoint point) 
{
	strLocation.Format("X:%u,Y:%u",point.x,point.y);
	((CMainFrame *)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(2,strLocation);
	
	CView::OnMouseMove(nFlags, point);
}

void DrawBranch(int x, int y, double Length, int StartAngle, HDC hdc,CFractalDoc* pDoc)
{
	
	double x1,y1,nx,ny,count;
	double nLength;
	
	x1=x+Length*cos(StartAngle*PI/180);
	y1=y-Length*sin(StartAngle*PI/180);
	::MoveToEx(hdc,(int)x,(int)y,0);
	if(pDoc->m_bDrawPixel)
		::SetPixel(hdc,(int)x1,(int)y1,pDoc->m_color);
	else
		::LineTo(hdc,(int)x1,(int)y1);
	if(Length<pDoc->m_last_branch_length)
		return;
	nLength=Length;
	nx=x;
	ny=y;
	for(count=0;count<pDoc->m_branch_num;count++)
	{
		nx+=nLength*(1-pDoc->m_cut_rate)*cos(StartAngle*PI/180);
		ny-=nLength*(1-pDoc->m_cut_rate)*sin(StartAngle*PI/180);
		DrawBranch((int)nx,(int)ny,nLength*(1-pDoc->m_cut_rate),StartAngle+(int)pDoc->m_left_branch_angle,hdc,pDoc);
		DrawBranch((int)nx,(int)ny,nLength*(1-pDoc->m_cut_rate),StartAngle-(int)pDoc->m_right_branch_angle,hdc,pDoc);
		nLength*=pDoc->m_cut_rate;
	}
	
}

UINT DrawFractal(LPVOID pPar)
{
	g_bDraw=true;

	DrawInfo* pDrawInfo=(DrawInfo*)pPar;

	::SendMessage(pDrawInfo->hWnd,UpdateMsg, ML_DRAWING,0);	

	if(hdc)::ReleaseDC( pDrawInfo->hWnd, hdc );
	hdc = ::GetDC( pDrawInfo->hWnd );
	::GetClientRect(pDrawInfo->hWnd,&(pDrawInfo->rectClient));
	
	if(hPen)::DeleteObject( hPen );
	hPen =::CreatePen( PS_SOLID, pDrawInfo->pDoc->m_linewidth,pDrawInfo->pDoc->m_color);
	hOldPen = (HPEN) ::SelectObject( hdc, hPen );

	::InvalidateRect( pDrawInfo->hWnd,&pDrawInfo->rectClient,TRUE );
	::UpdateWindow( pDrawInfo->hWnd );

	DrawBranch(pDrawInfo->rectClient.left+pDrawInfo->rectClient.Width()/2,
		pDrawInfo->rectClient.bottom,
		pDrawInfo->pDoc->m_branch_length,90,
		hdc,pDrawInfo->pDoc);
	
	::SelectObject( hdc, hOldPen );
	::DeleteObject( hPen );
	::ReleaseDC( pDrawInfo->hWnd, hdc );
	
	::SendMessage(pDrawInfo->hWnd,UpdateMsg, ML_DONE,0);	

	g_bDraw=false;
	
	return 0;
}

void CFractalView::OnViewRefresh() 
{
	if(g_bDraw){
		::TerminateThread(m_pDrawThread->m_hThread,0);
		g_bDraw=false;
	}
	::SendMessage(AfxGetMainWnd()->m_hWnd,WM_SHOWWINDOW,0,0);

	Invalidate();	
}

LRESULT CFractalView::OnUpdateStautsBar(WPARAM wParam, LPARAM lParam)
{
	switch (wParam)
	{
		case ML_DRAWING:
			((CMainFrame *)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(1,"正在绘图...");
			break;
		case ML_DONE:
			((CMainFrame *)AfxGetMainWnd())->m_wndStatusBar.SetPaneText(1,"绘图完毕");
			break;
	}
	return TRUE;
}

LRESULT CFractalView::OnAdjustBar(WPARAM wParam, LPARAM lParam)
{
	CFractalDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);	

	CAdjustFractal *pAdjustBox=((CMainFrame*)AfxGetMainWnd())->m_pAdjustBox;

	switch (wParam)
	{
	case ML_APPLY:
		pDoc->m_branch_length=pAdjustBox->m_iBranchLength;
		pDoc->m_branch_num=pAdjustBox->m_iBranchNum;
		pDoc->m_cut_rate=pAdjustBox->m_dblCutRate;
		pDoc->m_last_branch_length=pAdjustBox->m_iLastBranchLength;
		pDoc->m_left_branch_angle=pAdjustBox->m_iLeftAngle;
		pDoc->m_right_branch_angle=pAdjustBox->m_iRightAngle;
		pDoc->m_linewidth=pAdjustBox->m_iLineWidth;
		pDoc->m_bDrawPixel=pAdjustBox->m_bDrawPixel;
		pDoc->m_color=pAdjustBox->m_Color;

		pDoc->SetModifiedFlag();
		
		SendMessage(WM_COMMAND,ID_VIEW_REFRESH);

		break;
	}
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CFractalView drawing

void CFractalView::OnDraw(CDC* pDC) 
{
 	CFractalDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	
	if(!g_bDraw){
		g_Environment.hWnd = m_hWnd;
		g_Environment.pDoc=pDoc;
		
		m_pDrawThread=AfxBeginThread(DrawFractal,(LPVOID*)&g_Environment);
	}
}



⌨️ 快捷键说明

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