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

📄 firewnd.cpp

📁 医学图象处理系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	// Fill in the BITMAPINFOHEADER
	lpbi = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER) + (256 * sizeof(RGBQUAD))];
	lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	lpbi->bmiHeader.biWidth = size.cx;
	lpbi->bmiHeader.biHeight = size.cy;
	lpbi->bmiHeader.biPlanes = 1;
	lpbi->bmiHeader.biBitCount = 8;
	lpbi->bmiHeader.biCompression = BI_RGB;
	lpbi->bmiHeader.biSizeImage = WIDTHBYTES((DWORD)size.cx * 8) * size.cy;
	lpbi->bmiHeader.biXPelsPerMeter = 0;
	lpbi->bmiHeader.biYPelsPerMeter = 0;
	lpbi->bmiHeader.biClrUsed = 0;
	lpbi->bmiHeader.biClrImportant = 0;

	// Fill in the color table
	UINT uUsage = DIB_RGB_COLORS;
	memcpy( lpbi->bmiColors, m_rgbPalette, sizeof(RGBQUAD) * 256 );

	m_pWinDC = new CWindowDC(this);

	HBITMAP hBitmap = CreateDIBSection( m_pWinDC->m_hDC, lpbi, uUsage, (void **)&m_pBits, NULL, 0 );

	delete [] (BYTE *)lpbi;

	ASSERT(hBitmap != NULL);

	m_Bitmap.Attach( hBitmap );

	for (int y = 0; y < size.cy; y++ )
	{
		BYTE* pRow = (m_pBits + WIDTHBYTES((DWORD)size.cx * 8) * y);
   		for (int x = 0; x < size.cx; x++ )
			*pRow++ = 16;
	}

	m_pMemDC = new CDC;
	
	VERIFY( m_pMemDC->CreateCompatibleDC(m_pWinDC) );

	m_pOldPalette = m_pWinDC->SelectPalette( &m_Palette, FALSE );
	m_pWinDC->RealizePalette();
	m_pOldBitmap = m_pMemDC->SelectObject( &m_Bitmap );

	for (int x = 0; x < size.cx; x++ )
		m_Fire[x] = 16;
}

//
//	This routine was adapted from a pascal routine written by
//	Frank Jan Sorensen Alias:Frank Patxi (fjs@lab.jt.dk)
//
//	See AboutBox for a special thank you for all his work.
void CFireWnd::RenderFlame()
{
	CSize size = GetBitmapSize();
	int xStart, xEnd, x, y;
	BYTE* pRow;
	BYTE* pNextRow;

	xStart = (size.cx - m_nSize) / 2;
	xEnd = xStart + m_nSize + 1;

	{
		pRow = m_pBits;
		for (x=0;x<size.cx;x++)
		{
			if (x < (xStart + m_nDistribution) || x >= (xEnd - m_nDistribution))
				m_Fire[x] = 16;
			*pRow++ = m_Fire[x];
		}
	}

	for (y = m_MaxBurn; y > 0; y--)
	{
		pRow = (m_pBits + WIDTHBYTES((DWORD)size.cx * 8) * y);
		pNextRow = (m_pBits + WIDTHBYTES((DWORD)size.cx * 8) * (y - 1));

		if ((rand() % 2) == 0)
		{
			for (x = 0; x < size.cx; x++)
			{
				BurnPoint(pRow, pNextRow);
				pRow++;
				pNextRow++;
			}
		}
		else
		{
			pRow += size.cx - 1;
			pNextRow += size.cx - 1;
			for (x = 0; x < size.cx; x++)
			{
				BurnPoint(pRow, pNextRow);
				pRow--;
				pNextRow--;
			}
		}
	}

	int MaxHeat = m_nMaxHeat + 16;

	if ( rand() % (400 - m_nFlammability) == 0)
	{
		int off = m_nSize - 5;
		off = rand() % off;
		off += xStart;

		for (x = off; x < off + 5; x++)
			m_Fire[x] = 239;
	}

	for (x = xStart; x < xEnd; x++)
	{
		if (m_Fire[x] < MaxHeat)
		{
			int val = rand() % m_nChaos+1;
			val -= m_nChaos / 2;
			val += m_nSpreadRate;
			val += m_Fire[x];

			if ( val > MaxHeat) 
				m_Fire[x] = MaxHeat;
			else if ( val < 16)
				m_Fire[x] = 16;
			else
				m_Fire[x] = val;
 		}
		else
			m_Fire[x] = MaxHeat;
	}

	if (m_nSmoothness > 0)
	{
		xStart += m_nSmoothness;
		xEnd -= m_nSmoothness;
		for (x = xStart; x < xEnd; x++)
		{
			int val = 0;
			for (y = x - m_nSmoothness; y < x + 1 + m_nSmoothness; y++)
				val += m_Fire[y];

			m_Fire[x] = val / (2*m_nSmoothness+1);
		}
	}
}

void CFireWnd::PaintFlame(CDC* pDC)
{
	if (m_Palette.m_hObject != NULL  && m_Bitmap.m_hObject != NULL)
	{
		CSize size = GetBitmapSize();

		if (pDC != NULL)
		{
			CDC dc;
			CPalette *pOldPalette;

			pOldPalette = pDC->SelectPalette( &m_Palette, FALSE );
			pDC->RealizePalette();
			VERIFY( dc.CreateCompatibleDC(pDC) );
			CBitmap* pOldBitmap = dc.SelectObject( &m_Bitmap );
			pDC->BitBlt( 0, 0, size.cx, size.cy, &dc, 0, 0, SRCCOPY );
			dc.SelectObject(pOldBitmap);
			pDC->SelectPalette( pOldPalette, TRUE );
		}
		else
		{
			m_pWinDC->BitBlt( 0, 0, size.cx, size.cy, m_pMemDC, 0, 0, SRCCOPY );
		}
	}
}

void CFireWnd::SetMaxBurn(int nMax)
{
	if (m_Palette.m_hObject != NULL  && m_Bitmap.m_hObject != NULL)
	{
		CSize size = GetBitmapSize();

		m_MaxBurn = (int)(size.cy * (nMax/100.0));

		int y;
		for (y = (size.cy - 1); y > m_MaxBurn; y--)
		{
			BYTE* pRow = (m_pBits + WIDTHBYTES((DWORD)size.cx * 8) * y);
	   		for (int x = 0; x < size.cx; x++)
				*pRow++ = 16;
		}
	}
}

int CFireWnd::GetMaxBurn()
{
	return m_MaxBurn;
}

/////////////////////////////////////////////////////////////////////////////
// CFireWnd message handlers

void CFireWnd::OnDestroy() 
{
	CStatic::OnDestroy();
	
	if (m_pMemDC != NULL)
	{
		m_pMemDC->SelectObject(m_pOldBitmap);
		delete m_pMemDC;
		m_pMemDC = NULL;
		m_pOldBitmap = NULL;
	}

	if (m_pWinDC != NULL)
	{
		m_pWinDC->SelectObject(m_pOldPalette);
		delete m_pWinDC;
		m_pWinDC = NULL;
		m_pOldPalette = NULL;
	}

	if (m_Bitmap.m_hObject != NULL)
		m_Bitmap.DeleteObject();

	if (m_Palette.m_hObject != NULL)
		m_Palette.DeleteObject();

	if (m_Fire != NULL)
	{
		delete m_Fire;
		m_Fire = NULL;
	}
}

void CFireWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	PaintFlame(&dc);
}

void CFireWnd::OnPaletteChanged(CWnd* pFocusWnd) 
{
	if (pFocusWnd == this || IsChild(pFocusWnd))
		return;

	OnQueryNewPalette();
}

BOOL CFireWnd::OnQueryNewPalette() 
{
	if (GetPalette() == NULL)
		return FALSE;

	{
		CClientDC dc(this);
		CPalette* pOldPalette = dc.SelectPalette(&m_Palette,
			GetCurrentMessage()->message == WM_PALETTECHANGED);
		UINT nChanged = dc.RealizePalette();
		dc.SelectPalette(pOldPalette, TRUE);

		if (nChanged == 0)	// no change to our mapping
			return FALSE;
	}

	// some changes have been made; invalidate
	Invalidate(FALSE);

	return TRUE;
}

⌨️ 快捷键说明

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