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

📄 objwnd.cpp

📁 根据菲涅耳衍射原理编写的光学衍射模拟程序
💻 CPP
字号:
// Objwnd.cpp : implementation file
//

#include "stdafx.h"
#include "Optical.h"
#include "Objwnd.h"
#include "OpticalDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CObjwnd

CObjwnd::CObjwnd()
{
	pmdc=NULL;
	opix=NULL;
}

CObjwnd::~CObjwnd()
{
	if (pmdc)
		delete pmdc;
	if (opix)
		delete []opix;
}


BEGIN_MESSAGE_MAP(CObjwnd, CWnd)
	//{{AFX_MSG_MAP(CObjwnd)
	ON_WM_ERASEBKGND()
	ON_WM_PAINT()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CObjwnd message handlers

BOOL CObjwnd::CreateWnd(CWnd *pparent, RECT &rect, BOOL bstatic)
{
	BOOL res=Create(NULL,"",WS_CHILD|WS_VISIBLE,rect,pparent,0xffff);
	pdlg=(COpticalDlg*)pparent->GetParent();

	if (!pdlg || res==FALSE)
		return FALSE;

	ww=rect.right-rect.left;
	hh=rect.bottom-rect.top;
	bsta=bstatic;
	bmpid=0;

	pmdc=new CDC;
	pmdc->CreateCompatibleDC(pdlg->GetDC());
	if (!bsta)
		bmp.CreateCompatibleBitmap(pdlg->GetDC(),ww,hh);
	else bmp.LoadBitmap(IDB_BITMAP1);
	pmdc->SelectObject(&bmp);
	if (!bsta)
		pmdc->FillSolidRect(&rect,RGB(0,0,0));

	opix=new Complex[ww*hh];

	int h=hh/2;
	int w=ww/2;
	DWORD rgb;
	for (int y=-h;y<h;y++)
		for (int x=-w;x<w;x++)
		{
			opix[x+w+(y+h)*2*w].im=0.0;
			if (!bsta)
			{
				opix[x+w+(y+h)*2*w].re=0.0;
				continue;
			}
			rgb=(DWORD)pmdc->GetPixel(x+w,y+h);
			opix[x+w+(y+h)*2*w].re=sqrt(rgb&0x000000FF);
		}

	return res;
}

BOOL CObjwnd::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	return FALSE;
}

void CObjwnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	RECT r;
	GetClientRect(&r);
	dc.BitBlt(0,0,r.right,r.bottom,pmdc,0,0,SRCCOPY);
	// Do not call CWnd::OnPaint() for painting messages
}

void CObjwnd::ReDrawDC(int rate)
{
	RECT rect;
	GetClientRect(&rect);
	int ww=rect.right/2;
	int hh=rect.bottom/2;
	int xx,yy,i;
	DWORD rgbE;

	for (yy=-hh;yy<hh;yy++)
		for (xx=-ww;xx<ww;xx++)
		{
			i=xx+ww+2*(yy+hh)*ww;
			rgbE=(opix[i].re*opix[i].re+opix[i].im*opix[i].im)*rate/100;
			if (rgbE>255) rgbE=255;
			pmdc->SetPixel(xx+ww,yy+hh,RGB(rgbE,0,0));
		}

	Invalidate();
}

void CObjwnd::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CWnd::OnLButtonUp(nFlags, point);
	if (!bsta || pdlg->bon) return;

	bmpid=(bmpid+1)%BMPMAX;
	bmp.DeleteObject();
	bmp.LoadBitmap(IDB_BITMAP1+bmpid);
	pmdc->SelectObject(&bmp);

	int h=hh/2;
	int w=ww/2;
	DWORD rgb;
	for (int y=-h;y<h;y++)
		for (int x=-w;x<w;x++)
		{
			rgb=(DWORD)pmdc->GetPixel(x+w,y+h);
			opix[x+w+(y+h)*2*w].re=sqrt(rgb&0x000000FF);
			opix[x+w+(y+h)*2*w].im=0.0;
		}
	Invalidate();
}

void CObjwnd::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CWnd::OnRButtonUp(nFlags, point);
}

⌨️ 快捷键说明

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