www.gusucode.com > VC++自制全屏的屏幕保护模块-源码程序 > VC++自制全屏的屏幕保护模块-源码程序\code\DrawWnd.cpp

    // DrawWnd.cpp : implementation file
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "ScreenSavePrograme.h"
#include "DrawWnd.h"
/////////////////MY code starts here
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
 

#define attract  0.0001   //吸引子灵敏度  
#define MaxColor 16 //最大颜色数‘

#define  zoom 2.0  
#define MaxY  800
/////////////////my codes end here
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDrawWnd
LPCTSTR CDrawWnd::m_lpszClassName=NULL;
CDrawWnd::CDrawWnd()
{
	p=0.576;
	q=0.09;
	value=50;
}

CDrawWnd::~CDrawWnd()
{
}


BEGIN_MESSAGE_MAP(CDrawWnd, CWnd)
	//{{AFX_MSG_MAP(CDrawWnd)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CDrawWnd message handlers

BOOL CDrawWnd::Create(DWORD dwExStyle, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	    // Register a class with no cursor
	if (m_lpszClassName == NULL)
	{
    	m_lpszClassName = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,
			::LoadCursor(AfxGetResourceHandle(), 
			MAKEINTRESOURCE(IDC_NULLCURSOR)));
	}
	m_nSize=rect;
	// TODO: Add your specialized code here and/or call the base class
	return CreateEx(dwExStyle, m_lpszClassName, _T(""), dwStyle, 
		rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 
		pParentWnd->GetSafeHwnd(), NULL, NULL );
	//return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

int CDrawWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	/////////My code starts here
	int nSpeed = AfxGetApp()->GetProfileInt("Config", "Speed", 50);
	SetSpeed(nSpeed);
	// TODO: Add your specialized creation code here
	
	return 0;
}

void CDrawWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
 /*	 */
	Draw(dc);
	// TODO: Add your message handler code here


	
	// Do not call CWnd::OnPaint() for painting messages
}

void CDrawWnd::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
//	if(MapRand(2)<1)
	{
		draw=0;
		if(MapRand(1)<=0.5)
	 {
	    p=double(MapRand(2)/1.034);
		if(MapRand(1)<0.5)
	             { 
				  q=double(MapRand(2)/1.0834);
			  } 
			 
		else if(MapRand(1)>=0.5)
		{ q=double(MapRand(2)/1.0345);
				
				 q=-q;
			}
	}
	else if(MapRand(1)>0.5)
	{ 	   p=double(MapRand(2)/1.034);
	       p=-p;
		if(MapRand(1)<0.5)
	             { 
				  q=double(MapRand(2)/1.0834);
			  } 
			 
		else if(MapRand(1)>=0.5)
		{ q=double(MapRand(2)/1.0345);
				
				 q=-q;
			}
	}
	}
/*	else if(MapRand(2)>=1)
	{
	draw=1;
	a[0]=0.0+MapRand(1)/2.0;a[1]=0.85;a[2]=0.2;a[3]=-0.15;
	b[0]=0 ;b[1]=0.04+MapRand(1)/2.0;b[2]=-0.26 ;b[3]=0.28 ;
	c[0]=0 ;c[1]=-0.04 +MapRand(1)/2.0;c[2]=0.23 ;c[3]=0.26 ;
	d[0]=0.16+MapRand(1)/2.0 ;d[1]=0.85 ;d[2]=0.22 ;d[3]=0.24 ;
	e[0]=0+MapRand(1)/2.0;e[1]=0 ;e[2]=0 ;e[3]=0 ;
	f[0]=0+MapRand(1)/2.0 ;f[1]=1.6 ;f[2]=1.6 ;f[3]=0.44 ;
	pp[0]=0.01+MapRand(1)/20.0;pp[1]=0.85;pp[2]=0.07+MapRand(1)/20.0;pp[3]=0.07;
	stepx=15;
	stepy=15;
	totalsteps=32000;
	m_pColor=RGB(MapRand(250),MapRand(255),MapRand(255));
	m_N=4;
	}*/
         
   Invalidate();
//	CClientDC dc(this);
//	CDC& dc;
//	draw(dc);

	
 	CWnd::OnTimer(nIDEvent);
}

double CDrawWnd::MapRand(double nMax)
{
 
 int nRand =rand();
 double fMap=double(nRand)/RAND_MAX;
 double nRetVal=fMap*nMax+0.5f;
 return  nRetVal;

}

void CDrawWnd::Draw(CDC &dc)
{
	//
	if(draw==0)
	{
	int i,j;int k;
    double x1=MapRand(2);
	double x2=MapRand(2);
	

	scale=float(2.0*zoom/MaxY);//坐标转换

	for (i=0;i<MaxY;i++)
	{	
		for(j=0;j<MaxY;j++)
			{
				x[0]=float(scale*i-zoom);//初始值变换
				y[0]=float(zoom-scale*j);
				k=0;
				mag=0;
            	while((mag<100)&&(k<MaxColor*2)) 	
				{
 				 	x[k+1]=x[k]*x[k]-y[k]*y[k]+p;//复数的平方
			 		y[k+1]=2*x[k]*y[k]+q;

			 
					mag= x[k+1]*x[k+1]+y[k+1]*y[k+1];//复数模的平方
					k=k+1;
				}
	       if((k==MaxColor*2)||(mag>100))//发散速度不同用不同的颜色来绘制
		   {
          	dc.SetPixel(i ,(j-150),RGB(int(k*value/x1),int(k*value),int(k*value/x2)));
		   }
			}
	}
	}
	if(draw==1)
	{
	 CBrush brush(RGB(0,0,0));
	  CRect rect;
	  GetClientRect(rect);
	  dc.FillRect(&rect,&brush);
     for(i=1;i<m_N;i++)
		{
			pp[i]=pp[i]+pp[i-1];
		}
		double xj;//,m;
		xx=0;yy=0;
	//	srand(unsigned(time(NULL)));
		for(i=0;i<totalsteps;i++)
		{	 
		 // m=float(rand());
	
		  //xj=float(m/RAND_MAX);
		  xj=MapRand(1);
				if(xj<=pp[0]) k=0;
				if((xj>pp[0])&&(xj<=pp[1])) k=1;
				if((xj>pp[1])&&(xj<=pp[2])) k=2;
				if((xj>pp[2])&&(xj<=pp[3])) k=3;
				if((xj>pp[3])&&(xj<=pp[4]))k=4;
				if((xj>pp[4])&&(xj<=pp[5])) k=5;
				if((xj>pp[5])&&(xj<=pp[6]))k=6;
				if((xj>pp[6]) &&(xj<=pp[7]))k=7; 
                xx=a[k]*xx+b[k]*yy+e[k];
				yy=c[k]*xx+d[k]*yy+f[k];
				if(i>10)
				dc.SetPixel(int(MaxY*xx/stepx+MaxY/2 ) , MaxY-int(MaxY*yy/stepy+30)-100 ,m_pColor);
	
			}
	}


}

void CDrawWnd::SetSpeed(int nSpeed)
{
KillTimer(1);
VERIFY(SetTimer(1,50+500-nSpeed*5,NULL)!=0);
}