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

📄 loaddialog.cpp

📁 This file (the project file) contains information at the project level and is used to build a sing
💻 CPP
字号:
// LoadDialog.cpp : implementation file
//

#include "stdafx.h"
#include "轧板标号检测演示.h"
#include "LoadDialog.h"

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

/////////////////////////////////////////////////////////////////////////////
// LoadDialog dialog


LoadDialog::LoadDialog(CWnd* pParent /*=NULL*/)
	: CDialog(LoadDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(LoadDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void LoadDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(LoadDialog)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(LoadDialog, CDialog)
	//{{AFX_MSG_MAP(LoadDialog)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_TIMER()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// LoadDialog message handlers

int LoadDialog::OpenBmpFile(int Type)
{
	
	CString FileName="loadbmp.bmp";
	//读取文件数据
	if (!ReadBmpFile(FileName,0))
		return 0;
	RECT *lpRect;
	lpRect=new RECT;
	this->GetWindowRect(lpRect);	
	this->MoveWindow(lpRect->left,lpRect->top,nWidth+20,nHeight+50); 
	delete lpRect;
	this->Invalidate(true);
	return 1;
}



int LoadDialog::ReadBmpFile(CString FileName, int Type)
{
//读取位图文件,返回图形的长、宽、24色像素的指针
	CFile BmpFile;
	BYTE *m_lpBit;
	if (!BmpFile.Open(FileName,CFile::modeRead,NULL))
		return 0; //读取文件失败
	int FileLen=BmpFile.GetLength();
	BYTE *lpFile=NULL;
	lpFile= new BYTE[FileLen];
	//读取文件中全部数据失败
	if(!BmpFile.Read (lpFile,FileLen))
		{ 
			BmpFile.Close();
			return 0;
	} 
	BmpFile.Close(); 
	//文件的头结构指定类型、大小等
	BITMAPFILEHEADER *lpBitmapFileHeader;
	lpBitmapFileHeader=(BITMAPFILEHEADER *)	lpFile;
	//从结构中得到文件的大小
	int filelength= lpBitmapFileHeader->bfSize ;
	//从结构中得到图像数据(不包括调色板数据)在文件中开始地址 
	int DataOffBits=lpBitmapFileHeader->bfOffBits;
	//类型是否为'b'+'m' 
	if (lpBitmapFileHeader->bfType!='B'+'M'*256)  return 0;
	BITMAPINFOHEADER *lpBitmapInfoHeader;
	//得到BITMAPINFOHEADER(BITMAPINFO) 结构的开始地址
	lpBitmapInfoHeader=(BITMAPINFOHEADER *)(lpFile + sizeof(BITMAPFILEHEADER));  
	//unsigned int PointColorNum=1<<(lpBitmapInfoHeader->biBitCount);
	unsigned int PointColorNum=0 ;//图像色彩 真色彩超过12000
	switch(lpBitmapInfoHeader->biBitCount)
	{	case 1://表示图像单色 
			PointColorNum=2;break;
		case 4://表示图像16色 
			PointColorNum=16;break;
		case 8://表示图像256色 
			PointColorNum=256;break;
		case 24://表示图像真色彩 
			PointColorNum=1<<(lpBitmapInfoHeader->biBitCount);break;
		default:
			PointColorNum=1<<(lpBitmapInfoHeader->biBitCount);// 真色彩
	}
	if(lpBitmapInfoHeader->biCompression!=BI_RGB)
	{
		::MessageBox(this->m_hWnd,"有压缩数据,代号:" + (char)(lpBitmapInfoHeader->biCompression),"不能显示为图像",MB_OK);
		return(0);
	}
	unsigned int PaletteSize;//得到调色板的大小,可能有压缩
	if(lpBitmapInfoHeader->biClrUsed==0)//位图中实际使用的颜色数,因为压缩所以和PointColorNum不同
		PaletteSize=PointColorNum;//biClrUsed=0 本位图使用最大的颜色数(biBitCount定)
	else
//		if (lpBitmapInfoHeader->biBitCount<16)
		PaletteSize=lpBitmapInfoHeader->biClrUsed;//颜色数从biClrUsed得到

	if (lpBitmapInfoHeader->biBitCount >=16)PaletteSize=0;//颜色表有0项目

	PaletteSize=PaletteSize*sizeof(RGBQUAD);
	//有关数据定义
	//BYTE *m_lpBit;//像素每点数值的指针
	nWidth=( lpBitmapInfoHeader->biWidth);
	nHeight=( lpBitmapInfoHeader->biHeight);
	nByteWidth= nWidth*3;//真色彩24位3字节显示一点
	if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4);//位图文件每行比需要是4的倍数
	m_lpBit = lpFile+DataOffBits;
	
	if (lpBitmapInfoHeader->biBitCount <24)//有调色板显示<16或=16时则转化成24色真色彩
	{	
		RGBQUAD *pPalette=(RGBQUAD *)(lpFile + sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER));
		int nLen=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nByteWidth*nHeight;
		//BYTE *lpTemp=lpFile;
		lpFile=new BYTE[nLen];//改变lpFile的地址使m_lpBit和lpTempBit不重叠
		BITMAPFILEHEADER bmh;
		BITMAPINFOHEADER bmi;
		bmh.bfType='B'+'M'*256;
		bmh.bfSize=nLen;
		bmh.bfReserved1=0;
		bmh.bfReserved2=0;
		bmh.bfOffBits=54;//14+40=sizeof(BITMAPINFOHEADER)+sizeof(BITMAPFILEHEADER)
		bmi.biSize=sizeof(BITMAPINFOHEADER);//定值 40
		bmi.biWidth=nWidth;
		bmi.biHeight=nHeight;
		bmi.biPlanes=1;//平面数一定为1
		bmi.biBitCount=24;//转化为真色彩24位
		bmi.biCompression=BI_RGB;//不压缩
		bmi.biSizeImage=0;//BI_RGB类型一定为0
		bmi.biXPelsPerMeter=0;
		bmi.biYPelsPerMeter=0;
		bmi.biClrUsed=0;//颜色数
		bmi.biClrImportant=0;//所有颜色都是重要色
		
		memset(lpFile,0,nLen);//数据赋值0
		memcpy(lpFile,&bmh,sizeof(BITMAPFILEHEADER));
		memcpy(lpFile+sizeof(BITMAPFILEHEADER),&bmi,sizeof(BITMAPINFOHEADER));
	
		BYTE *lpTempBit=lpFile+sizeof(BITMAPINFOHEADER)+sizeof(BITMAPFILEHEADER);
		int x,y;
		unsigned int nBWidth,OldPoint,byteNO,BitNo,Palette,NewPoint;
		nBWidth=nWidth;//*lpBitmapInfoHeader->biBitCount;//一行字节代表的像素点数
		if (nBWidth%(32/lpBitmapInfoHeader->biBitCount)) //文件中实际应该有的字节大小是4的倍数
			nBWidth+=(32/lpBitmapInfoHeader->biBitCount)-(nBWidth%(32/lpBitmapInfoHeader->biBitCount));

		for(y=0;y<nHeight;y++)//图像从左下坐标开始
		{
			for(x=0;x<nWidth;x++)
			{
				OldPoint=y*nBWidth+x;
				switch(lpBitmapInfoHeader->biBitCount)
					{case 1://表示图像单色 
							//if (nBWidth%32) nBWidth+=32-(nBWidth%32);//图像中每行的字节数为4的倍数
							//OldPoint=y*nBWidth+x;
							byteNO=int(OldPoint / 8);
							BitNo=7-(OldPoint % 8);
							Palette=m_lpBit[byteNO];	
							Palette=(Palette>>BitNo) ;
							Palette=(Palette	& 0X01);
							break;
			
					case 4://表示图像16色 
							//if (nBWidth%8) nBWidth+=8-(nBWidth%8);//图像中每行的字节数为4的倍数
							//OldPoint=y*nBWidth+x;
							byteNO=int(OldPoint / 2) ;
							BitNo=(OldPoint % 2)*4;
							Palette=m_lpBit[byteNO];
							Palette=((Palette>>BitNo) & 0x0f);
							break;
					case 8://表示图像256色 
							//if (nBWidth%4) nBWidth+=4-(nBWidth%4);//图像中每行的字节数为4的倍数
							//OldPoint=y*nBWidth+x;
							Palette= m_lpBit[OldPoint];
							break;
					case 16://16位 两字节一点,5bit为一种色彩,blue(低),green(中),red(高),最高5bit无意义
							byteNO=OldPoint / 2;
							if (!(OldPoint%2))
							{
								NewPoint=y*nByteWidth+x*3;//24色
								BYTE Color[2],GetColor;
								Color[0]=m_lpBit[byteNO];
								Color[1]=m_lpBit[byteNO+1];								
								lpTempBit[NewPoint]=(Color[0] & 0x1f)  ;
								GetColor=((Color[0] >>5) & 0x07) +((Color[1]<<3) & 0x18);
								lpTempBit[NewPoint+1]=GetColor  ;
								GetColor=((Color[1]>>2) & 0x1f);
								lpTempBit[NewPoint+2]= GetColor;
							}
					}
				if ((lpBitmapInfoHeader->biBitCount) < 16)
				{
					if (Palette*4 > PaletteSize)  Palette=0;
					NewPoint=y*nByteWidth+x*3;//24色
					lpTempBit[NewPoint]=(pPalette[Palette].rgbBlue) ;
					lpTempBit[NewPoint+1]=(pPalette[Palette].rgbGreen ) ;
					lpTempBit[NewPoint+2]=(pPalette[Palette].rgbRed  ) ;
				}
			}
		}
	}
	m_BmpByte = lpFile+ sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
	return 1;
}

void LoadDialog::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	if (m_BmpByte!=NULL){
		BITMAPINFOHEADER bmi;
		bmi.biSize=sizeof(BITMAPINFOHEADER);
		bmi.biWidth=nWidth;
		bmi.biHeight=nHeight;
		bmi.biPlanes=1;
		bmi.biBitCount=24;//24色
		bmi.biCompression=BI_RGB;//不压缩
		bmi.biSizeImage=0;//定数值
		bmi.biXPelsPerMeter=0;
		bmi.biYPelsPerMeter=0;
		bmi.biClrUsed=0;
		bmi.biClrImportant=0;
		StretchDIBits(dc.m_hDC,5,5,nWidth,nHeight,0,0,nWidth,nHeight,
		m_BmpByte,(BITMAPINFO *)&bmi,DIB_RGB_COLORS,SRCCOPY);
	}
}



BOOL LoadDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	this->SetTimer(1,1000,0); 
	OpenBmpFile(0);
	return TRUE;  // return TRUE u)nless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
/////////////////////////////////////////////////////////////////////////////
// LoadDialog dialog


void LoadDialog::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CDialog::OnLButtonDown(nFlags, point);
	::Sleep(1000);
	this->OnOK(); 	

}

void LoadDialog::OnTimer(UINT nIDEvent) 
{
#define ALLTIME 5
	this->FlashWindow (true);
	static time=0;
	CString Wincap;
	Wincap.Format("欢迎进入检测演示程序 %d",ALLTIME-time);
	this->SetWindowText(Wincap);
	if (time>=ALLTIME) this->OnOK();
	time++;
	CDialog::OnTimer(nIDEvent);
}

void LoadDialog::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	m_hIcon=::AfxGetApp()->LoadStandardCursor(IDC_WAIT); 
	SetCursor(m_hIcon); 
	
	CDialog::OnMouseMove(nFlags, point);
}

⌨️ 快捷键说明

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