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

📄 graypicdlg.cpp

📁 图像灰色处理 请参考<<visual c++ 工程应用和项目实践>>
💻 CPP
字号:
// GrayPicDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GrayPic.h"
#include "GrayPicDlg.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CGrayPicDlg dialog

CGrayPicDlg::CGrayPicDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGrayPicDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGrayPicDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGrayPicDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGrayPicDlg)
	DDX_Control(pDX, IDC_STAGRAY, m_StaGray);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGrayPicDlg, CDialog)
	//{{AFX_MSG_MAP(CGrayPicDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGrayPicDlg message handlers

BOOL CGrayPicDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	this->SetTimer(1,500,NULL);

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CGrayPicDlg::OnPaint() 
{
	CPaintDC dc(this);

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
}
HCURSOR CGrayPicDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CGrayPicDlg::OnOK() 
{	
	//CDialog::OnOK();
}


void CGrayPicDlg::GetRGB(COLORREF color,int &R,int &G,int &B)
{
	char cRGB[6];
	itoa(color,cRGB,16);
	char cR[2],cG[2],cB[2];	
	if(strlen(cRGB)==5)
	{
		strcpy(cB,cRGB);
		strcpy(cG,&cRGB[1]);
		strcpy(cR,&cRGB[3]);
		cB[1]='\0';
		cG[2]='\0';
	}
	else
	{
		strcpy(cB,cRGB);
		strcpy(cG,&cRGB[2]);
		strcpy(cR,&cRGB[4]);
		cB[2]='\0';
		cG[2]='\0';

	}
	R=XToI(cR);
	G=XToI(cG);
	B=XToI(cB);
}
int CGrayPicDlg::XToI(char *xNumber)
{
	char chr;
	int nNumber=0,nValue=0,nTmp=0;
	for(int i=strlen(xNumber);i>0;i--)
	{
		chr=xNumber[strlen(xNumber)-i];
		nNumber=XToTen(chr);
		int cc=pow(16,i-1);
		nTmp=nNumber*pow(16,i-1);
		nValue=nValue+nTmp;
	}
	return nValue;
}

int CGrayPicDlg::XToTen(char chr)
{
	int i;
	if(chr=='0')
		i=0;
	if(chr=='1')
		i=1;
	if(chr=='2')
		i=2;
	if(chr=='3')
		i=3;
	if(chr=='4')
		i=4;
	if(chr=='5')
		i=5;
	if(chr=='6')
		i=6;
	if(chr=='7')
		i=7;
	if(chr=='8')
		i=8;
	if(chr=='9')
		i=9;
	if(chr=='a'||chr=='A')
		i=10;
	if(chr=='b'||chr=='B')
		i=11;
	if(chr=='c'||chr=='C')
		i=12;
	if(chr=='d'||chr=='D')
		i=13;
	if(chr=='e'||chr=='E')
		i=14;
	if(chr=='f'||chr=='F')
		i=15;
	return i;
}

//DEL void CGrayPicDlg::OnButton3() 
//DEL {
//DEL }

void CGrayPicDlg::OnTimer(UINT nIDEvent) 
{
	this->KillTimer(1);
	CDC* pDC=this->m_StaGray.GetDC();
	CDC MemDC;
	CBitmap bmp;
	bmp.LoadBitmap(IDB_BITMAP1);
	BITMAP BmpStruct;
	MemDC.CreateCompatibleDC(pDC);
	MemDC.SelectObject(&bmp);
	bmp.GetBitmap(&BmpStruct);
	COLORREF color;
	int nR,nG,nB;
	float fGray;
	for(int x=0;x<BmpStruct.bmWidth;x++)
	{
		for(int y=0;y<BmpStruct.bmHeight;y++)
		{
			color=MemDC.GetPixel(x,y);
			GetRGB(color,nR,nG,nB);
			fGray= (float)nR*0.30+(float)nG*0.59+(float)nB*0.11;
			pDC->SetPixel(x,y,RGB(fGray,fGray,fGray));
		}
	}
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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