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

📄 fractalexview.cpp

📁 自己写的一个分形程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// FractalExView.cpp : implementation of the CFractalExView class
//

#include "stdafx.h"
#include "FractalEx.h"

#include "FractalExView.h"
#include "IterationDialog.h"
#include "InfoDialog.h"
#include "JuliaDialog.h"
#include "PertDialog.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFractalExView

IMPLEMENT_DYNCREATE(CFractalExView, CFormView)

BEGIN_MESSAGE_MAP(CFractalExView, CFormView)
	//{{AFX_MSG_MAP(CFractalExView)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_PAINT()
	ON_WM_RBUTTONDOWN()
	ON_WM_SIZE()
	ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
	ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
	ON_COMMAND(ID_EDIT_REDO, OnEditRedo)
	ON_UPDATE_COMMAND_UI(ID_EDIT_REDO, OnUpdateEditRedo)
	ON_WM_DESTROY()
	ON_COMMAND(ID_ZOOM_IN, OnZoomIn)
	ON_COMMAND(ID_ZOOM_OUT, OnZoomOut)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_IN, OnUpdateZoomIn)
	ON_COMMAND(ID_ZOOM_MOVE, OnZoomMove)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_MOVE, OnUpdateZoomMove)
	ON_UPDATE_COMMAND_UI(ID_ZOOM_OUT, OnUpdateZoomOut)
	ON_WM_SETCURSOR()
	ON_COMMAND(ID_FILE_EXPORT, OnFileExport)
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_COMMAND(ID_ONIDLE, OnOnidle)
	ON_COMMAND(ID_FRACTAL_CUSTOM, OnFractalCustom)
	ON_UPDATE_COMMAND_UI(ID_FRACTAL_CUSTOM, OnUpdateFractalCustom)
	ON_COMMAND(ID_ZOOM_RESET, OnZoomReset)
	ON_COMMAND(ID_FRACTAL_OTHER, OnFractalOther)
	ON_UPDATE_COMMAND_UI(ID_FRACTAL_OTHER, OnUpdateFractalOther)
	ON_COMMAND(ID_SET_ITERATION, OnSetIteration)
	ON_COMMAND(ID_FRACTAL_VIEWINFO, OnFractalViewinfo)
	ON_UPDATE_COMMAND_UI(ID_FRACTAL_MODE_JULIA, OnUpdateFractalModeJulia)
	ON_UPDATE_COMMAND_UI(ID_FRACTAL_MODE_MAND, OnUpdateFractalModeMand)
	ON_UPDATE_COMMAND_UI(ID_FRACTAL_MODE_PERT, OnUpdateFractalModePert)
	ON_COMMAND(ID_FRACTAL_MODE_JULIA, OnFractalModeJulia)
	ON_COMMAND(ID_FRACTAL_MODE_MAND, OnFractalModeMand)
	ON_COMMAND(ID_FRACTAL_MODE_PERT, OnFractalModePert)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND_RANGE(ID_FRACTAL_FIRST, ID_FRACTAL_LAST, OnFractaltype)
	ON_UPDATE_COMMAND_UI_RANGE(ID_FRACTAL_FIRST, ID_FRACTAL_LAST, OnUpdateFractaltype)
	ON_COMMAND_RANGE(ID_COLOR_SET0, ID_COLOR_SET4, OnColorset)
	ON_UPDATE_COMMAND_UI_RANGE(ID_COLOR_SET0, ID_COLOR_SET4, OnUpdateColorset)
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_COOR, OnUpdateCoorIndicator)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFractalExView construction/destruction
CFractalExView::CFractalExView()
	: CFormView(CFractalExView::IDD), ZoomOp(((CMainFrame*)AfxGetMainWnd())->ZoomOp)
{
	//{{AFX_DATA_INIT(CFractalExView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	onceinit=false;
	mousedown=false;
	widhei=1.0;
	nUndoRect=0;
	curUndoRect=0;
	nRedoRect=0;
	pixelinfo=NULL;
	status_coor="坐标";

	CMainFrame* frame=GetMainFrame();
	cur_zoom_in[0]=frame->cur_zoom_in[0];
	cur_zoom_in[ZO_MOVE]=frame->cur_zoom_in[ZO_MOVE];
	cur_zoom_in[ZO_IN]=frame->cur_zoom_in[ZO_IN];
	cur_zoom_in[ZO_OUT]=frame->cur_zoom_in[ZO_OUT];

	CalcFractal=NULL;
	GetFractalData=NULL;
	CalcFractalPoint=NULL;
	hlib=0;
	BuildLib="";
}

CFractalExView::~CFractalExView()
{
	LoadFractalLib();
}

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

BOOL CFractalExView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	return CFormView::PreCreateWindow(cs);
}

void CFractalExView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	SetScrollSizes(MM_TEXT, CSize(0,0));

	UseColorSet(GetDocument()->colorset, false);
	UndoRect[0]=GetDocument()->myRect;
	onceinit=true;
	CreateMem();
	
	if(GetDocument()->fType==FRACTAL_CUSTOM)
	{
		BuildLib=BuildDll(GetDocument()->expression, GetDocument()->origin, "", false);
		if(BuildLib!="")
			LoadFractalLib(BuildLib);
	}
	else if(GetDocument()->fType==FRACTAL_NONE)
	{	
		if(otherfdlg.DoModal()==IDOK)
		{
			GetDocument()->ResetRect();
			LoadFractalLib(otherfdlg.SelectedLib);
			GetDocument()->fType=FRACTAL_OTHER;
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CFractalExView printing

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

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

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

void CFractalExView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CFractalExView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CFractalExView message handlers

void CFractalExView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CClientDC dc(this);
	switch(ZoomOp)
	{
	case ZO_MOVE:
		oldrect=CRect(0,0,wid2-1,hei2-1);
		dc.DrawFocusRect(&oldrect);
		oldpoint=point;
		mousedown=true;
		SetCapture();
		break;
	case ZO_IN:
		oldrect=CRect(0,0,0,0);
		oldpoint=point;
		mousedown=true;
		SetCapture();
		break;
	case ZO_OUT:
		ChangeRect(CRect(point.x-wid2, point.y-hei2, point.x+wid2, point.y+hei2)); 
		break;
	}
	
	CFormView::OnLButtonDown(nFlags, point);
}

void CFractalExView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	switch(ZoomOp)
	{
	case ZO_MOVE:
		if(mousedown)
		{
			CClientDC dc(this);
			dc.DrawFocusRect(&oldrect);
			ReleaseCapture();
			mousedown=false;
			oldrect=CRect(-oldrect.left,-oldrect.top,(wid2-1)*2-oldrect.right,(hei2-1)*2-oldrect.bottom);
			if(nFlags==8)
			{
				NewFractal(CalcSelRect(oldrect));
			}
			else
			{
				ChangeRect(oldrect);
			}
		}
		break;
	case ZO_IN:
		if(mousedown)
		{
			CClientDC dc(this);
			dc.DrawFocusRect(&oldrect);
			ReleaseCapture();
			mousedown=false;

			if(abs(point.x-oldpoint.x)<5)
			{
				ChangeRect(CRect(point.x-wid2/4, point.y-hei2/4, point.x+wid2/4, point.y+hei2/4)); 
			}
			else
			{
				if(nFlags==8)
				{
					NewFractal(CalcSelRect(oldrect));
				}
				else
				{
					ChangeRect(oldrect);
				}
			}
		}
		break;
	case ZO_OUT:
		break;
	}
	
	CFormView::OnLButtonUp(nFlags, point);
}

void CFractalExView::OnMouseMove(UINT nFlags, CPoint point) 
{
	switch(ZoomOp)
	{
	case ZO_MOVE:
		if(mousedown)
		{
			CClientDC dc(this);
			int dx=point.x-oldpoint.x, dy=point.y-oldpoint.y;
			CRect rect=oldrect+CPoint(dx, dy);
			dc.DrawFocusRect(&oldrect);
			oldrect=rect;
			oldpoint=point;
			dc.DrawFocusRect(&rect);
		}
		break;
	case ZO_IN:
		if(mousedown)
		{
			CClientDC dc(this);
			CRect rect;
			if(oldpoint.x<point.x)
				if(oldpoint.y<=point.y)
					rect=CRect(oldpoint.x, oldpoint.y, point.x, (int)(oldpoint.y+(point.x-oldpoint.x)*widhei));
				else
					rect=CRect(oldpoint.x, (int)(oldpoint.y-(point.x-oldpoint.x)*widhei), point.x, oldpoint.y);
			else
				if(oldpoint.y<=point.y)
					rect=CRect(point.x, oldpoint.y, oldpoint.x, (int)(oldpoint.y-(point.x-oldpoint.x)*widhei));
				else
					rect=CRect(point.x, (int)(oldpoint.y+(point.x-oldpoint.x)*widhei), oldpoint.x, oldpoint.y);
			dc.DrawFocusRect(&oldrect);
			oldrect=rect;
			dc.DrawFocusRect(&rect);
		}
		break;
	case ZO_OUT:
		break;
	}

	if(!mousedown)
	{
		datatype a2, b2;
		a2 = point.x*scale + GetDocument()->myRect.left;
		b2 = point.y*scale + GetDocument()->myRect.top;
		PixelInfo *info=(pixelinfo+point.x+(hei2-point.y-1)*wid2);
		status_coor.Format("x=%g, y=%g, l=%d", a2, b2, info->lvl);
	}
	
	CFormView::OnMouseMove(nFlags, point);
}

void CFractalExView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	if(mousedown)
	{
		CClientDC dc(this);
		dc.DrawFocusRect(&oldrect);
		ReleaseCapture();
		mousedown=false;
	}
	else if(ZoomOp== ZO_IN)
		ChangeRect(CRect(point.x-wid2, point.y-hei2, point.x+wid2, point.y+hei2)); 
	
	CFormView::OnRButtonDown(nFlags, point);
}

BOOL CFractalExView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	if(ZoomOp>0)
	{
		SetCursor(cur_zoom_in[ZoomOp]);
		return TRUE;
	}
	else
		return CFormView::OnSetCursor(pWnd, nHitTest, message);
}

void CFractalExView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	memdib.CopyDC(&dc, 0, 0);
}

void CFractalExView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	if(onceinit)
	{
		DeleteMem();
		CreateMem();
		DrawFractal();
	}	
}

void CFractalExView::CreateMem()
{
	CClientDC dc(this);
	CRect rect;
	GetClientRect(&rect);

	wid2=rect.Width();
	hei2=rect.Height();
	pixelinfo=new PixelInfo[wid2*hei2];

	memdib.Create(wid2, hei2);
}

void CFractalExView::DeleteMem()
{
	memdib.Destroy();

	delete[] pixelinfo;
}

void CFractalExView::OnEditUndo() 
{
	if(nUndoRect>0)
	{
		if(--curUndoRect<0)
			curUndoRect+=MAX_UNDO;
		GetDocument()->myRect=UndoRect[curUndoRect];
		nUndoRect--;
		nRedoRect++;
		DrawFractal();
	}
}

void CFractalExView::OnUpdateEditUndo(CCmdUI* pCmdUI) 
{
	if(nUndoRect==0)
		pCmdUI->Enable(false);
}

void CFractalExView::OnEditRedo() 
{
	if(nRedoRect>0)
	{
		if(++curUndoRect>=MAX_UNDO)
			curUndoRect-=MAX_UNDO;
		GetDocument()->myRect=UndoRect[curUndoRect];
		nUndoRect++;
		nRedoRect--;
		DrawFractal();
	}
}

void CFractalExView::OnUpdateEditRedo(CCmdUI* pCmdUI) 
{
	if(nRedoRect==0)
		pCmdUI->Enable(false);
}

void CFractalExView::OnDestroy() 
{
	DeleteMem();
	CFormView::OnDestroy();
}

void CFractalExView::AddUndo(CRectFloat &rc)
{
	if(++nUndoRect>MAX_UNDO-1)
		nUndoRect=MAX_UNDO-1;
	nRedoRect=0;
	if(++curUndoRect>=MAX_UNDO)
		curUndoRect=0;
	UndoRect[curUndoRect]=GetDocument()->myRect;
}

void CFractalExView::ChangeRect(CRect &SelRect)
{//以选中的屏幕坐标改变图象Rect
	CClientDC dc(this);
	int maxi=2;
	CRect temp;
	CDC memdc;
	memdc.CreateCompatibleDC(&dc);
	CBitmap bm;
	bm.CreateCompatibleBitmap(&dc, wid2, hei2);
	CBitmap* oldbm=memdc.SelectObject(&bm);
	memdc.BitBlt(0, 0, wid2, hei2, &dc, 0, 0, SRCCOPY);
	for(int i=maxi-1;i>=0;i--)
	{
		temp=CRect(SelRect.left*i/maxi, SelRect.top*i/maxi, wid2-(wid2-SelRect.Width())*i/maxi, hei2-(hei2-SelRect.Height())*i/maxi);
		dc.StretchBlt(temp.left, temp.top, temp.right, temp.bottom, &memdc, SelRect.left, SelRect.top, SelRect.Width(), SelRect.Height(), SRCCOPY);
//		dc.DrawFocusRect(&temp);
	}

	memdc.SelectObject(oldbm);
	bm.DeleteObject();
	memdc.DeleteDC();
	
	CRectFloat rc=CalcSelRect(SelRect);
	GetDocument()->myRect=rc;
	AddUndo(rc);

	DrawFractal();
}

CRectFloat CFractalExView::CalcSelRect(CRect& selRect)
{//把鼠标选中的屏幕坐标转为图象坐标
	CRectFloat rc;
	rc.left=scale*selRect.left+GetDocument()->myRect.left;
	rc.top=scale*selRect.top+GetDocument()->myRect.top;
	rc.right=scale*selRect.right+GetDocument()->myRect.left;
	rc.bottom=scale*selRect.bottom+GetDocument()->myRect.top;
	return rc;
}

void CFractalExView::OnUpdateZoomIn(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(ZoomOp==ZO_IN);
}
void CFractalExView::OnUpdateZoomOut(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(ZoomOp==ZO_OUT);
}
void CFractalExView::OnUpdateZoomMove(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(ZoomOp==ZO_MOVE);
}
void CFractalExView::OnZoomIn() 
{
	if(ZoomOp==ZO_IN)
		ZoomOp=ZO_NONE;
	else
		ZoomOp=ZO_IN;
}
void CFractalExView::OnZoomOut() 
{
	if(ZoomOp==ZO_OUT)
		ZoomOp=ZO_NONE;
	else
		ZoomOp=ZO_OUT;
}
void CFractalExView::OnZoomMove() 
{
	if(ZoomOp==ZO_MOVE)
		ZoomOp=ZO_NONE;
	else
		ZoomOp=ZO_MOVE;
}

void CFractalExView::OnZoomReset() 
{
	GetDocument()->ResetRect();
	DrawFractal();
}

void CFractalExView::OnFileExport() 
{
	CString strFile, strFilter("24位位图(*.bmp)|*.bmp||"); 
	CFileDialog fdlg(FALSE, ".bmp", NULL, OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY, strFilter); 
	if( fdlg.DoModal() == IDOK ) 
	{
		strFile = fdlg.GetPathName();
		SaveMemdc(strFile);
	} 
}

void CFractalExView::SaveMemdc(CString filename)//把图象放入文件
{/***
	HBITMAP hbitmap=(HBITMAP)bm.GetSafeHandle();
	HDC hdc;
	int ibits; 
	WORD wbitcount; 
	DWORD dwpalettesize=0, dwbmbitssize, dwdibsize, dwwritten;
	BITMAP bitmap; 
	BITMAPFILEHEADER bmfhdr; 
	BITMAPINFOHEADER bi; 
	LPBITMAPINFOHEADER lpbi; 
	HANDLE fh, hdib, hpal,holdpal=NULL;
	hdc = memdc.GetSafeHdc();
	ibits = GetDeviceCaps(hdc, BITSPIXEL) * GetDeviceCaps(hdc, PLANES);
	DeleteDC(hdc);
	if (ibits <= 1)
		wbitcount = 1;
	else if (ibits <=4)
		wbitcount = 4;
	else if (ibits <= 8)
		wbitcount = 8;
	else if (ibits <= 24)
		wbitcount = 24;
	if (wbitcount <= 8)
	dwpalettesize = (1 << wbitcount) * sizeof(RGBQUAD);

⌨️ 快捷键说明

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