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

📄 huiview.cpp

📁 这是书上的代码
💻 CPP
字号:
// HuiView.cpp : implementation file
//

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CHuiView

IMPLEMENT_DYNCREATE(CHuiView, CView)

CHuiView::CHuiView()
{
}

CHuiView::~CHuiView()
{
}


BEGIN_MESSAGE_MAP(CHuiView, CView)
	//{{AFX_MSG_MAP(CHuiView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHuiView drawing

void CHuiView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
	CFile cf;
    CFileException e;
	BITMAPFILEHEADER bmfh;
    BITMAPINFOHEADER bmih;
    typedef struct rgbn
	{
		BYTE red;
		BYTE green;
		BYTE blue;
		BYTE null;
	} RGBn;

	RGBn *indexRGB;
	BYTE *bmp;
	BYTE red,green,blue;
	char szFilename[80];

	_getcwd(szFilename,_MAX_PATH);
    strset(TransValue1,'\0');
	strcpy(TransValue1,"NULL");
	_chdir(szFilename);
    strcpy(szFilename,"planehd.bmp");   
	Search_Directory(szFilename);

	if (strcmp(TransValue1,"NULL")==0)
	{
		MessageBox("没有找到文件,请把planehd.bmp拷贝到可执行文件所在的子目录下!","提醒");
		return;
	}

	if (!cf.Open(TransValue1,CFile::modeRead,&e))
	{
		MessageBox("Can not open the file!","HDOpenFile");
		return;
	}

	strset(TransValue1,'\0');
	strcpy(TransValue1,"NULL");

    cf.SeekToBegin();
	cf.Read(&bmfh,sizeof(bmfh));
	cf.Read(&bmih,sizeof(bmih));
   	indexRGB = new RGBn[256];
	cf.Seek(54,CFile::begin);
	cf.ReadHuge(indexRGB,256*4);
	bmp = new BYTE[bmih.biHeight*bmih.biWidth];
    cf.Seek(1078,CFile::begin);
	if (bmih.biHeight*bmih.biWidth>64*1024)
	{
		cf.ReadHuge(bmp,bmih.biHeight*bmih.biWidth);
	}
	else
	{
		cf.Read(bmp,bmih.biHeight*bmih.biWidth);
	}
    for (int i=0; i<bmih.biHeight; i++)
	{
		for (int j=0; j<bmih.biWidth; j++)
		{
			red = indexRGB[bmp[i*bmih.biWidth+j]].red;
			green = indexRGB[bmp[i*bmih.biWidth+j]].green;
			blue = indexRGB[bmp[i*bmih.biWidth+j]].blue;
			pDC->SetPixel(j,bmih.biHeight-i,RGB(red,green,blue));
		}
	}
	pDC->TextOut(0,0,"灰度图像");
	cf.Close();
	delete indexRGB;
	delete bmp;
	ReleaseDC(pDC);
}

/////////////////////////////////////////////////////////////////////////////
// CHuiView diagnostics

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

void CHuiView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CHuiView message handlers

void CHuiView::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(TransValue1,path_search);
			 int k = 0;
			 while (k<(int)strlen(TransValue1))
			 {
				 if (TransValue1[k] == '\\')
				 {
					 for(int i=strlen(TransValue1); i>k+1; i--)
					 {
						 TransValue1[i] = TransValue1[i-1];
					 }
					 TransValue1[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(TransValue1,path_search);
				int k = 0;
				while (k<(int)strlen(TransValue1))
				{
					if (TransValue1[k] == '\\')
					{
						for(int i=strlen(TransValue1); i>k+1; i--)
						{
							TransValue1[i] = TransValue1[i-1];
						}
						TransValue1[k+1] = '\\';
						k++;
					}
					k++;
				}
			}
		}
	}
	_findclose(handle);
}

⌨️ 快捷键说明

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