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

📄 dib.cpp

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

#include "stdafx.h"
#include "Draw.h"
#include "DIB.h"

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

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

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

CDIB::~CDIB()
{

}

BOOL CDIB::Draw(CDC *pDC,CPoint m_ptStart, int x2, int y2)
{
	if (m_pDIB==NULL)
	{
		return FALSE;
	}
	x2 = m_Width;
	y2 = m_Height;

	StretchDIBits(pDC->m_hDC,m_ptStart.x,m_ptStart.y,x2,y2,0,0,x2,y2,m_pDIBbits,(BITMAPINFO *)m_pBIH,BI_RGB,SRCCOPY);

	if(m_bSelected)
	{
		CRect  SelectSignRect[8];
		if(GetDIBRect().left-4 > 0)
		{
			SelectSignRect[0].left = GetDIBRect().left-4;
			SelectSignRect[6].left = GetDIBRect().left-4;
			SelectSignRect[7].left = GetDIBRect().left-4;
		}
		else
		{
			SelectSignRect[0].left = 0;
			SelectSignRect[6].left = 0;
			SelectSignRect[6].left = 0;
		}

		SelectSignRect[0].right = GetDIBRect().left+4;
		SelectSignRect[6].right = GetDIBRect().left+4;
		SelectSignRect[7].right = GetDIBRect().left+4;
		
		if(GetDIBRect().top-4 > 0)
		{
			SelectSignRect[0].top = GetDIBRect().top-4;
			SelectSignRect[1].top = GetDIBRect().top-4;
			SelectSignRect[2].top = GetDIBRect().top-4;	
		}
		else
		{
			SelectSignRect[0].top = 0;
			SelectSignRect[1].top = 0;
			SelectSignRect[2].top = 0;
		}

		SelectSignRect[0].bottom = GetDIBRect().top+4;
		SelectSignRect[1].bottom = GetDIBRect().top+4;
		SelectSignRect[2].bottom = GetDIBRect().top+4;

		SelectSignRect[2].left = GetDIBRect().right-4;
		SelectSignRect[3].left = GetDIBRect().right-4;
		SelectSignRect[4].left = GetDIBRect().right-4;

		SelectSignRect[2].right = GetDIBRect().right+4;
		SelectSignRect[3].right = GetDIBRect().right+4;
		SelectSignRect[4].right = GetDIBRect().right+4;

		SelectSignRect[4].top = GetDIBRect().bottom-4;
		SelectSignRect[5].top = GetDIBRect().bottom-4;
		SelectSignRect[6].top = GetDIBRect().bottom-4;

		SelectSignRect[4].bottom = GetDIBRect().bottom+4;
		SelectSignRect[5].bottom = GetDIBRect().bottom+4;
		SelectSignRect[6].bottom = GetDIBRect().bottom+4;

		SelectSignRect[1].left = (GetDIBRect().left + GetDIBRect().right)/2 - 4;
		SelectSignRect[5].left = (GetDIBRect().left + GetDIBRect().right)/2 - 4;

		SelectSignRect[1].right = (GetDIBRect().left + GetDIBRect().right)/2 + 4;
		SelectSignRect[5].right = (GetDIBRect().left + GetDIBRect().right)/2 + 4;

		SelectSignRect[3].top = (GetDIBRect().top + GetDIBRect().bottom)/2 - 4;
		SelectSignRect[7].top = (GetDIBRect().top + GetDIBRect().bottom)/2 - 4;

		SelectSignRect[3].bottom = (GetDIBRect().top + GetDIBRect().bottom)/2 + 4;
		SelectSignRect[7].bottom = (GetDIBRect().top + GetDIBRect().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 CDIB::OpenBMPFile(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_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 CDIB::MoveDIB(CPoint point)
{
	CPoint Oldpoint;
	Oldpoint = m_ptOldPoint;
	int altx = point.x - Oldpoint.x;
	int alty = point.y - Oldpoint.y;
	if((m_ptStart.x += altx)<=0)
	{
		m_ptStart.x = 0;
	}
	if((m_ptStart.y += alty)<=0)
	{
		m_ptStart.y = 0;
	}
	
	m_ptOldPoint = point;
	
	return true;
}

CRect CDIB::GetDIBRect()
{
	CRect  DIBRect;
	DIBRect.TopLeft() = m_ptStart;
	DIBRect.right = m_ptStart.x + m_Width;
	DIBRect.bottom = m_ptStart.y + m_Height;

	return DIBRect;
}

⌨️ 快捷键说明

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