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

📄 bartestdlg.cpp

📁 this program show how to get the value of every pixel,and you can modify it based on this case.
💻 CPP
字号:
// bartestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "bartest.h"
#include "bartestDlg.h"
#include "bmp.h"
#include "vfw.h"
#include "math.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define MAXWIDTH   1000  //图象最大宽高
#define MAXHEIGHT  800
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

int graybmp[MAXWIDTH][MAXHEIGHT];
CBMP mybmp,mybmp1,mybmp2,mybmp3;
//BYTE gray[MAXHEIGHT][MAXWIDTH];
HDRAWDIB hdrawdib;
BITMAPINFO bmi;	
int flag;

//RECT rect;
//GetClientRect(&rect);





class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()



/////////////////////////////////////////////////////////////////////////////
// CBartestDlg dialog

CBartestDlg::CBartestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBartestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBartestDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
}

void CBartestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBartestDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBartestDlg, CDialog)
	//{{AFX_MSG_MAP(CBartestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_PICTURE, OnPicture)
	ON_BN_CLICKED(IDC_ONGRAY, OnOngray)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_ONGRAY2, OnOngray2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBartestDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	// TODO: Add extra initialization here
	flag=0;
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CBartestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CBartestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
		////////////////////////////////////////////////////
		////////////////////////////////////////////////////
			CClientDC dc(this);
			
				//画第一幅图
				int  BytesPerRow = (mybmp1.lWidth*3+3)& (~3);
				
				bmi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
				bmi.bmiHeader.biWidth=mybmp1.lWidth ;
				bmi.bmiHeader.biHeight=mybmp1.lHeight ;
				bmi.bmiHeader.biBitCount=24;
				bmi.bmiHeader.biCompression=BI_RGB;
				bmi.bmiHeader.biSizeImage=0;
				bmi.bmiHeader.biXPelsPerMeter=0;
				bmi.bmiHeader.biYPelsPerMeter=0;
				bmi.bmiHeader.biClrUsed=0;
				bmi.bmiHeader.biPlanes=1;
				bmi.bmiHeader.biClrImportant=0;	
				
				hdrawdib=DrawDibOpen();
				int i=DrawDibDraw(hdrawdib,
					dc.GetSafeHdc(),              // handle to device context
					0,           // x-coordinate of upper-left corner of                     // dest. rect.
					0,        // y-coordinate of upper-left corner of                        // dest. rect.
mybmp1.lWidth, //MAXWIDTH,//-GetSystemMetrics(SM_CXVSCROLL),        // source rectangle width
				mybmp1.lHeight,//MAXHEIGHT,//-GetSystemMetrics(SM_CXVSCROLL),       //     // source rectangle height
					&bmi.bmiHeader,// address of structure with bitmap info.
					mybmp1.buf,  // address of array with DIB bits  
					0,             // x-coordinate of lower-left corner of                        // source rect.
					0,             // y-coordinate of lower-left corner of 
					mybmp1.lWidth ,//-GetSystemMetrics(SM_CXVSCROLL),        // source rectangle width
					mybmp1.lHeight,//-GetSystemMetrics(SM_CXVSCROLL),             // source rectangle height  // number of scan lines
					0);
				DrawDibClose(hdrawdib);
				
		
	
	
////////////////////////////////////////////////////
////////////////////////////////////////////////////

	}
}

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

void CBartestDlg::OnPicture() 
{
	// TODO: Add your control notification handler code here
	//Invalidate(FALSE);
	CString filename,strError;
    
	strError = "Can't open the file:";
	CFileDialog dlg(TRUE,NULL,NULL,OFN_NOCHANGEDIR,"BMP(*.bmp)|*.bmp");	
	//dlg.SetWindowPos(&(dlg.wndTopMost)/*CWnd::wndTopMost*/,0,0,200,100,SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
	if(dlg.DoModal () != IDOK)
		return;
    filename = dlg.GetPathName ();

	if(mybmp1.OpenBMPFile((char *)(const char *)filename) !=0)
	{
		strError += filename;
		MessageBox(strError);
		return;
	}   
	Invalidate();
}	



void CBartestDlg::DrawBmp(CBMP bmp)
{

}




void CBartestDlg::OnOngray() 
{
	// TODO: Add your control notification handler code here
	CDC *pDC=GetDC();
	//	int rcol,gcol,bcol,gray;
	    int iCol,iRow;

//马赛克处理

rgb(0,0,mybmp1.lHeight,mybmp1.lWidth,3,mybmp1.pArray_R);
rgb(0,0,mybmp1.lHeight,mybmp1.lWidth,3,mybmp1.pArray_G);
rgb(0,0,mybmp1.lHeight,mybmp1.lWidth,3,mybmp1.pArray_B);

	for( iRow=0;iRow<mybmp1.lHeight;iRow++)
	   for( iCol=0;iCol<mybmp1.lWidth;iCol++)
	   {
		   pDC->SetPixel (iCol+mybmp1.lWidth,iRow,RGB(mybmp1.pArray_R[iRow][iCol],mybmp1.pArray_G[iRow][iCol],mybmp1.pArray_B[iRow][iCol]));
	   }
		   
	 

}


void CBartestDlg::OnButton2() 
{
	CDC *pDC=GetDC();
	int rcol,gcol,bcol,gray;
	int iCol,iRow;
	for( iRow=0;iRow<mybmp1.lHeight;iRow++)
	   for( iCol=0;iCol<mybmp1.lWidth;iCol++)
	   {
		   	 rcol = mybmp1.pArray_R[iRow][iCol];
			 gcol = mybmp1.pArray_G[iRow][iCol];
			 bcol = mybmp1.pArray_B[iRow][iCol];
		
		    gray=(rcol +gcol +gcol )/3;
		    graybmp[iRow][iCol]=gray;
		    pDC->SetPixel (iCol+mybmp1.lWidth,iRow,RGB(graybmp[iRow][iCol],graybmp[iRow][iCol],graybmp[iRow][iCol]));
	   }
		   
	 	// TODO: Add your control notification handler code here
	
}

void CBartestDlg::OnOngray2() 
{
	// TODO: Add your control notification handler code here
		CDC *pDC=GetDC();
	//	int rcol,gcol,bcol,gray;
	    int iCol,iRow;

//马赛克处理

rgb(0,0,mybmp1.lHeight,mybmp1.lWidth,5,mybmp1.pArray_R);
rgb(0,0,mybmp1.lHeight,mybmp1.lWidth,5,mybmp1.pArray_G);
rgb(0,0,mybmp1.lHeight,mybmp1.lWidth,5,mybmp1.pArray_B);

	for( iRow=0;iRow<mybmp1.lHeight;iRow++)
	   for( iCol=0;iCol<mybmp1.lWidth;iCol++)
	   {
		   pDC->SetPixel (iCol+2*mybmp1.lWidth,iRow,RGB(mybmp1.pArray_R[iRow][iCol],mybmp1.pArray_G[iRow][iCol],mybmp1.pArray_B[iRow][iCol]));
	   }
		   
	
}

⌨️ 快捷键说明

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