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

📄 displaybmpview.cpp

📁 这是书上的代码
💻 CPP
字号:
// DisplayBMPView.cpp : implementation of the CDisplayBMPView class
//

#include "stdafx.h"
#include "DisplayBMP.h"

#include "io.h"
#include "direct.h"
#include "string.h"

#include "DisplayBMPDoc.h"
#include "DisplayBMPView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
char TransValue[_MAX_PATH];//全局变量,用来传递文件路径
/////////////////////////////////////////////////////////////////////////////
// CDisplayBMPView

IMPLEMENT_DYNCREATE(CDisplayBMPView, CView)

BEGIN_MESSAGE_MAP(CDisplayBMPView, CView)
	//{{AFX_MSG_MAP(CDisplayBMPView)
	ON_COMMAND(ID_MENUCONVERT, OnMenuconvert)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDisplayBMPView construction/destruction

CDisplayBMPView::CDisplayBMPView()
{
	// TODO: add construction code here

}

CDisplayBMPView::~CDisplayBMPView()
{
}

BOOL CDisplayBMPView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
    
    strcpy(TransValue,"NULL");//初始化文件路径变量
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDisplayBMPView drawing

void CDisplayBMPView::OnDraw(CDC* pDC)
{
	CDisplayBMPDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CWnd *m_pMainWnd;
    m_pMainWnd = AfxGetMainWnd();
	m_pMainWnd->SetWindowText("显示图片");//改变窗口标题

	CFile cf;//文件变量,用来对文件操作
	CFileException e;//出错处理
    BITMAPFILEHEADER bmfh;//BMP文件头变量
	BITMAPINFOHEADER bmih;//BMP文件信息变量
    RGBTRIPLE *rgb;//24位真彩色数据格式
	char szFilename[80];//文件名变量

	_getcwd(szFilename,_MAX_PATH);//获取当前工作目录
  
	_chdir(szFilename);//进入当前工作目录
    strcpy(szFilename,"plane24.bmp");   
	Search_Directory(szFilename);//在当前工作目录中,搜索需要的文件
	if (strcmp(TransValue,"NULL")==0)//对搜索的结果进行处理
	{
		MessageBox("没有找到文件,请把plane24.bmp拷贝到可执行文件所在的子目录下!","提醒");
		return;
	}
 
	if (!cf.Open(TransValue,CFile::modeRead, &e))//找到文件后,打开文件
	{
		MessageBox("Can not open the file!","24Open File");
		return;
	}
//	TRACE("%s\n",TransValue);
	strset(TransValue,'\0');//找到文件后,对文件路径名进行清零
	strcpy(TransValue,"NULL");//和初始化处理
//	TRACE("%s\n",TransValue);

	cf.SeekToBegin();
	cf.Read(&bmfh,sizeof(bmfh));//读取文件头
    cf.Read(&bmih,sizeof(bmih));//读取文件信息头
	rgb = new RGBTRIPLE[bmih.biWidth*bmih.biHeight];
	cf.SeekToBegin();
	cf.Seek(54,CFile::begin);
	//读取文件数据
	if (cf.GetLength()>64*1024)
	{
		cf.ReadHuge(rgb,bmih.biWidth*bmih.biHeight*3);//
	}
	else
	{
		cf.Read(rgb,bmih.biWidth*bmih.biHeight);
	}
	//在屏幕上打点显示图像
	for (int i=0; i<bmih.biHeight;i++)
	{
		for (int j=0; j<bmih.biWidth; j++)
		{
        	pDC->SetPixel(j,bmih.biHeight-i,RGB(rgb[i*bmih.biWidth+j].rgbtRed,rgb[i*bmih.biWidth+j].rgbtGreen,rgb[i*bmih.biWidth+j].rgbtBlue));
		}
	}
	cf.Close();//关闭文件
	delete rgb;//释放内存
	pDC->TextOut(0,0,"24位真彩色图!");
	ReleaseDC(pDC);
}

/////////////////////////////////////////////////////////////////////////////
// CDisplayBMPView printing

BOOL CDisplayBMPView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CDisplayBMPView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CDisplayBMPView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CDisplayBMPView diagnostics

#ifdef _DEBUG
void CDisplayBMPView::AssertValid() const
{
	CView::AssertValid();
}

void CDisplayBMPView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CDisplayBMPDoc* CDisplayBMPView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDisplayBMPDoc)));
	return (CDisplayBMPDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDisplayBMPView message handlers

//递归搜索需要的文件
void CDisplayBMPView::Search_Directory(char *szFilename)
{
    long handle;//判断是文件夹还是文件
	struct _finddata_t filestruct;//文件结构信息
	char path_search[_MAX_PATH];//文件路径名

	handle = _findfirst("*",&filestruct);
	if (handle == -1)
		return ;
	if (::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY)
	{
        if (filestruct.name[0] != '.')
		{
			_chdir(filestruct.name);
			Search_Directory(szFilename);
			_chdir("..");
		}
	}
	else
	{
		if (!stricmp(filestruct.name,szFilename))
		{
             _getcwd(path_search,_MAX_PATH);
			 strcat(path_search,"\\");
			 strcat(path_search,filestruct.name);
			 strcpy(TransValue,path_search);
			 //文件路径名处理,使其能在程序处理中能用
			 int k = 0;
			 while (k<(int)strlen(TransValue))
			 {
				 if (TransValue[k] == '\\')
				 {
					 for(int i=strlen(TransValue); i>k+1; i--)
					 {
						 TransValue[i] = TransValue[i-1];
					 }
					 TransValue[k+1] = '\\';
					 k++;
				 }
				 k++;
			 }
		}
	}
	while (!(_findnext(handle,&filestruct)))
	{
		if (::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY)
		{
			if (*filestruct.name != '.')
			{
				_chdir(filestruct.name);
				Search_Directory(szFilename);
				_chdir("..");
			}
		}
		else
		{
			if (!stricmp(filestruct.name,szFilename))
			{
				_getcwd(path_search,_MAX_PATH);
				strcat(path_search,"\\");
				strcat(path_search,filestruct.name);
				strcpy(TransValue,path_search);
				//文件路径名处理,使其能在程序处理中能用
				int k = 0;
				while (k<(int)strlen(TransValue))
				{
					if (TransValue[k] == '\\')
					{
						for(int i=strlen(TransValue); i>k+1; i--)
						{
							TransValue[i] = TransValue[i-1];
						}
						TransValue[k+1] = '\\';
						k++;
					}
					k++;
				}
			}
		}
	}
	_findclose(handle);
}
//转换24位真彩色文件到黑白灰度BMP图片
void CDisplayBMPView::OnMenuconvert() 
{
	// TODO: Add your command handler code here
	CDC *pDC = GetDC();
	CFile cf;
	CFileException e;
    BITMAPFILEHEADER bmfh;
	BITMAPINFOHEADER bmih;
    RGBTRIPLE *rgb;
	char szFilename[80];

	_getcwd(szFilename,_MAX_PATH);
  
	_chdir(szFilename);
    strcpy(szFilename,"plane24.bmp");   
	Search_Directory(szFilename);
	if (strcmp(TransValue,"NULL")==0)
	{
		MessageBox("没有找到文件,请把plane24.bmp拷贝到可执行文件所在的子目录下!","提醒");
		return;
	}
 
	if (!cf.Open(TransValue,CFile::modeRead, &e))
	{
		MessageBox("Can not open the file!","24Open File");
		return;
	}
	TRACE("%s\n",TransValue);
	strset(TransValue,'\0');
	strcpy(TransValue,"NULL");
	TRACE("%s\n",TransValue);

	cf.SeekToBegin();
	cf.Read(&bmfh,sizeof(bmfh));
    cf.Read(&bmih,sizeof(bmih));
	rgb = new RGBTRIPLE[bmih.biWidth*bmih.biHeight];
	cf.SeekToBegin();
	cf.Seek(54,CFile::begin);
	if (cf.GetLength()>64*1024)
	{
		cf.ReadHuge(rgb,bmih.biWidth*bmih.biHeight*3);
	}
	else
	{
		cf.Read(rgb,bmih.biWidth*bmih.biHeight);
	}

	for (int i=0; i<bmih.biHeight;i++)
	{
		for (int j=0; j<bmih.biWidth; j++)
		{
			BYTE color = (rgb[i*bmih.biWidth+j].rgbtRed + rgb[i*bmih.biWidth+j].rgbtGreen + rgb[i*bmih.biWidth+j].rgbtBlue)/3;
			pDC->SetPixel(j,bmih.biHeight-i,RGB(color,color,color));
		}
	}
	cf.Close();
	delete rgb;
	pDC->TextOut(0,0,"24位真彩色位图转换成的灰度图像!");
	ReleaseDC(pDC);

}

⌨️ 快捷键说明

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