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

📄 childview.cpp

📁 这源代码是书本《visual c++.net MFC 类库应用详解》的
💻 CPP
字号:
// ChildView.cpp : CChildView 类的实现
//

#include "stdafx.h"
#include "ImageSample.h"
#include "ChildView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CChildView

CChildView::CChildView()
{
}

CChildView::~CChildView()
{
}


BEGIN_MESSAGE_MAP(CChildView, CWnd)
	ON_WM_PAINT()
	ON_COMMAND(ID_OPEN_IMAGE, OnOpenImage)
	ON_COMMAND(ID_SVAE_IMAGE, OnSvaeImage)
	ON_COMMAND(ID_CONVERTTO_BW, OnConverttoBw)
END_MESSAGE_MAP()



// CChildView 消息处理程序

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), NULL);

	return TRUE;
}

void CChildView::OnPaint() 
{
	CPaintDC dc(this); // 用于绘制的设备上下文
	
	// TODO: 在此处添加消息处理程序代码
	if (!img.IsNull())
	   img.StretchBlt(dc,0,0,img.GetWidth(),img.GetHeight(),SRCCOPY);
	// 不要为绘制消息而调用 CWnd::OnPaint()
}


void CChildView::OnOpenImage()
{
	// TODO: 在此添加命令处理程序代码
	CString strFilter;
	HRESULT hResult;
	strFilter = "BMP image|*.bmp|JPEG image|*.jpg|GIF image|*.gif|PNG image|*.png||";
	CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, strFilter);
	hResult = (int)dlg.DoModal();
	if(hResult != IDOK) return;

	img.Destroy();
	hResult = img.Load(dlg.GetFileName());
	if (FAILED(hResult)) 
	{
		CString fmt;
		fmt.Format("Load image failed:\n%x", hResult);
		::AfxMessageBox(fmt);
		return;
	}

	Invalidate();
	UpdateWindow();
}

void CChildView::OnSvaeImage()
{
	// TODO: 在此添加命令处理程序代码
	CString strFilter;
	HRESULT hResult;

	strFilter = "BMP image|*.bmp|JPEG image|*.jpg|GIF image|*.gif|PNG image|*.png||";

	CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER,strFilter);
	hResult = (int)dlg.DoModal();
	if (hResult != IDOK) return;

    // Add the appropriate extension if the user didn't type one

	CString strFileName;
	CString strExtension;

	strFileName = dlg.m_ofn.lpstrFile;


	// add the file extension if the user didn't supply one
	if (dlg.m_ofn.nFileExtension == 0) 
	{
		switch (dlg.m_ofn.nFilterIndex)
		{
		case 1:
			strExtension = "bmp";
			break;
		case 2:
			strExtension = "jpg";
			break;
		case 3:
			strExtension = "gif";
			break;
		case 4:
			strExtension = "png";
			break;
		default:
			break;
		}

		strFileName = strFileName + '.' + strExtension;

	}

	// the extension on the file name will determine the file type that is saved
	hResult = img.Save(strFileName);
	if (FAILED(hResult)) 
	{
		CString fmt;
		fmt.Format("Save image failed:\n%x", hResult);
		::AfxMessageBox(fmt);
		return;
	}
}

void CChildView::OnConverttoBw()
{
	// TODO: 在此添加命令处理程序代码
	CWaitCursor wait;

	if (!img.IsIndexed())
	{
		// the image does not use an indexed palette, so we will change each pixel to B&W (slow)
		COLORREF pixel;
		int maxY = img.GetHeight(), maxX = img.GetWidth();
		byte r,g,b,avg;
		for (int x=0; x<maxX; x++) 
		{
			for (int y=0; y<maxY; y++) 
			{
				pixel = img.GetPixel(x,y);
				r = GetRValue(pixel);
				g = GetGValue(pixel);
				b = GetBValue(pixel);
				avg = ((r + g + b)/3);
				img.SetPixelRGB(x,y,avg,avg,avg);
			}
		}

	}
	else
	{

		// the image uses an indexed palette, so we will just change the palette table entries 
		// to their B&W equivalents 
		int MaxColors = img.GetMaxColorTableEntries();
		RGBQUAD* ColorTable;
		ColorTable = new RGBQUAD[MaxColors];
        byte r,g,b;
		img.GetColorTable(0,MaxColors,ColorTable);
		for (int i=0; i<MaxColors; i++)
		{
			r=ColorTable[i].rgbRed;
			g=ColorTable[i].rgbGreen;
			b=ColorTable[i].rgbBlue;
			int avg = (r + g + b)/3;
			ColorTable[i].rgbBlue = (BYTE)avg;
			ColorTable[i].rgbGreen = (BYTE)avg;
			ColorTable[i].rgbRed = (BYTE)avg;
		}
		img.SetColorTable(0,MaxColors,ColorTable);
	
		delete(ColorTable);
	}

	Invalidate();
	UpdateWindow();
}

⌨️ 快捷键说明

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