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

📄 image.cpp

📁 画一些简单的图元
💻 CPP
字号:
// Image.cpp: implementation of the CImage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Draw.h"
#include "Image.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CImage::CImage()
{
	m_pDIB=NULL;
	m_ptStart = (0,0);
	m_ptEnd = (0,0);
	m_bSelected = false;
}

CImage::~CImage()
{

}

BOOL CImage::Draw(CDC* pDC)
{
	if (m_pDIB==NULL)
	{
		return FALSE;
	}
	
	StretchDIBits(pDC->m_hDC,m_ptStart.x,m_ptStart.y,
					(m_ptEnd.x - m_ptStart.x),(m_ptEnd.y - m_ptStart.y),
					0,0,m_Width,m_Height,m_pDIBbits,
					(BITMAPINFO *)m_pBIH,BI_RGB,SRCCOPY);
	
	if(this->m_bSelected)
	{
		CRect  SelectSignRect[8];
		if(GetRect().left-4 > 0)
		{
			SelectSignRect[0].left = GetRect().left-4;
			SelectSignRect[6].left = GetRect().left-4;
			SelectSignRect[7].left = GetRect().left-4;
		}
		else
		{
			SelectSignRect[0].left = 0;
			SelectSignRect[6].left = 0;
			SelectSignRect[7].left = 0;
		}
		
		SelectSignRect[0].right = GetRect().left+4;
		SelectSignRect[6].right = GetRect().left+4;
		SelectSignRect[7].right = GetRect().left+4;
		
		if(GetRect().top-4 > 0)
		{
			SelectSignRect[0].top = GetRect().top-4;
			SelectSignRect[1].top = GetRect().top-4;
			SelectSignRect[2].top = GetRect().top-4;	
		}
		else
		{
			SelectSignRect[0].top = 0;
			SelectSignRect[1].top = 0;
			SelectSignRect[2].top = 0;
		}
		
		SelectSignRect[0].bottom = GetRect().top+4;
		SelectSignRect[1].bottom = GetRect().top+4;
		SelectSignRect[2].bottom = GetRect().top+4;
		
		SelectSignRect[2].left = GetRect().right-4;
		SelectSignRect[3].left = GetRect().right-4;
		SelectSignRect[4].left = GetRect().right-4;
		
		SelectSignRect[2].right = GetRect().right+4;
		SelectSignRect[3].right = GetRect().right+4;
		SelectSignRect[4].right = GetRect().right+4;
		
		SelectSignRect[4].top = GetRect().bottom-4;
		SelectSignRect[5].top = GetRect().bottom-4;
		SelectSignRect[6].top = GetRect().bottom-4;
		
		SelectSignRect[4].bottom = GetRect().bottom+4;
		SelectSignRect[5].bottom = GetRect().bottom+4;
		SelectSignRect[6].bottom = GetRect().bottom+4;
		
		SelectSignRect[1].left = (GetRect().left + GetRect().right)/2 - 4;
		SelectSignRect[5].left = (GetRect().left + GetRect().right)/2 - 4;
		
		SelectSignRect[1].right = (GetRect().left + GetRect().right)/2 + 4;
		SelectSignRect[5].right = (GetRect().left + GetRect().right)/2 + 4;
		
		SelectSignRect[3].top = (GetRect().top + GetRect().bottom)/2 - 4;
		SelectSignRect[7].top = (GetRect().top + GetRect().bottom)/2 - 4;
		
		SelectSignRect[3].bottom = (GetRect().top + GetRect().bottom)/2 + 4;
		SelectSignRect[7].bottom = (GetRect().top + GetRect().bottom)/2 + 4;
		
		CPen	pen(PS_DASH, 1, RGB(0,0,0));		
		pDC->SelectObject(&pen);
		
		for(int i=0;i<=7;++i)
		{
			pDC->MoveTo(SelectSignRect[i].left,SelectSignRect[i].top);
			pDC->LineTo(SelectSignRect[i].right,SelectSignRect[i].top);
			pDC->MoveTo(SelectSignRect[i].right,SelectSignRect[i].top);
			pDC->LineTo(SelectSignRect[i].right,SelectSignRect[i].bottom);
			pDC->MoveTo(SelectSignRect[i].right,SelectSignRect[i].bottom);
			pDC->LineTo(SelectSignRect[i].left,SelectSignRect[i].bottom);
			pDC->MoveTo(SelectSignRect[i].left,SelectSignRect[i].bottom);
			pDC->LineTo(SelectSignRect[i].left,SelectSignRect[i].top);
		}
		
	}
	
	return TRUE;
}

BOOL CImage::OpenImageFile(LPCSTR lpszFileName)
{
	CFile nFile;
	
	if (!nFile.Open(lpszFileName,CFile::modeRead|CFile::shareDenyNone)) 
	{
		AfxMessageBox("a broken file!");
		return FALSE;
	}
	
	BITMAPFILEHEADER BFH;
	
	if (nFile.Read(&BFH,sizeof(BITMAPFILEHEADER))!=sizeof(BITMAPFILEHEADER)) 
	{
		AfxMessageBox("not a BMP File!");
		return FALSE;
	}
	
	if (BFH.bfType !='MB') 
	{
		AfxMessageBox("not a Bmp file!");
		return FALSE;
	}
	
	DWORD dibBitSize;
	dibBitSize = nFile.GetLength() - sizeof(BITMAPFILEHEADER);
	
	BYTE *pDIB;
	pDIB = new BYTE [dibBitSize];
	
	if (nFile.Read(pDIB,dibBitSize) != dibBitSize) 
	{
		AfxMessageBox("broken bmp file!");
		return FALSE;
	}
	
	if (m_pDIB != NULL)
	{
		delete []m_pDIB;
	}
	
	m_pDIB = pDIB;
	m_pBIH = (BITMAPINFOHEADER*)pDIB;
	m_Width = m_pBIH->biWidth;
	m_Height = m_pBIH->biHeight;

	m_ptEnd.x = m_ptStart.x + m_Width;
	m_ptEnd.y = m_ptStart.y + m_Height;
	m_BitCount = m_pBIH->biBitCount;
	
	int nCount = 1<< m_BitCount;
	
	if (nCount > 256) {
		m_PaletteEntries =0;
	} else
	{
		m_PaletteEntries = nCount;
	}
	
	m_pDIBbits = &pDIB[sizeof(BITMAPINFOHEADER)+m_PaletteEntries*sizeof(RGBQUAD)];
	
	
	return TRUE;
	
}

BOOL CImage::IsPointIn(CPoint point)
{
	if((point.x >= GetRect().left && point.x <= GetRect().right)
		&& (point.y >= GetRect().top && point.y <= GetRect().bottom))
	{
		return true;
	}
	else
	{
		return false;
	}
	
}

CRect CImage::GetRect()
{
	CRect ObjectRect;
	
	if(m_ptStart.x <= m_ptEnd.x)
	{
		ObjectRect.left  = m_ptStart.x;
		ObjectRect.right = m_ptEnd.x;
	}
	else
	{
		ObjectRect.left  = m_ptEnd.x;
		ObjectRect.right = m_ptStart.x;
	}
	
	if(m_ptStart.y <= m_ptEnd.y)
	{
		ObjectRect.top    = m_ptStart.y;
		ObjectRect.bottom = m_ptEnd.y;
	}
	else
	{
		ObjectRect.top    = m_ptEnd.y;
		ObjectRect.bottom = m_ptStart.y;
	}
	
	return ObjectRect;
}

⌨️ 快捷键说明

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