1. 求用vc++編程實現顯示灰度直方圖的詳細步驟,越詳細越好
步驟和程序:
打開VC程序——文件——新建——工程中的MFC AppWizard(exe),在工程下面的框中輸入工程名(假定工程名為111),點確定——選多重文檔,點下一個——後面都點下一個直到完成確定,基本框架就完成了,下面就加代碼。
這時VC界面上左邊框的下面有三個按鈕:ClassView、ResourceView和FileView,ClassView裡面是工程111的類:CAdoutDlg、CChildFrame、CMy111App、CMy111Doc、CMy111View和Globals;點ResourceView裡面是資源類:Accelerator、Dialog、Icon、Menu、String Table、Toolbar和Version;點開FileView裡面是文件類:Source File、Header Files、Resource Files和ReadMe.txt。
點界面的「工程」按鈕——添加工程——新建——選C++ Source File,在文件下面的框里輸入文件名(如DIBAPI),點「結束」,這樣在FileView中的Source Files裡面就多了一個DIBAPI.cpp文件,所有的代碼都加在該文件中。再點界面的「工程」按鈕——添加工程——新建——選C/C++ Header File,在文件下面的框里輸入文件名(和前面的文件名必須一致),點「結束」,這樣在FileView中的Header Files裡面就多了一個DIBAPI.h文件,該文件是DIBAPI.cpp的頭文件。
點開DIBAPI.h文件,裡面是空白的,把如下代碼考入文件中:
//DIBAPI.h
#ifndef _INC_DIBAPI
#define _INC_DIBAPI
DECLARE_HANDLE(HDIB);
#define PALVERSION 0x300
#define IS_WIN30_DIB(lpbi) ((*(LPDWORD)(lpbi))==sizeof(BITMAPINFOHEADER))
#define RECTWIDTH(lpRect) ((lpRect)->right-(lpRect)->left)
#define RECTHEIGHT(lpRect) ((lpRect)->bottom-(lpRect)->top)
#define WIDTHBYTES(bits) (((bits)+31)/32*4)
#define DIB_HEADER_MARKER ((WORD)('M'<<8)|'B')
BOOL WINAPI PaintDIB(HDC,LPRECT,HDIB,LPRECT,CPalette* pPal);
BOOL WINAPI CreateDIBPalette(HDIB hDIB,CPalette* cPal);
LPSTR WINAPI FindDIBBits(LPSTR lpbi);
DWORD WINAPI DIBWidth(LPSTR lpDIB);
DWORD WINAPI DIBHeight(LPSTR lpDIB);
WORD WINAPI PaletteSize(LPSTR lpbi);
WORD WINAPI DIBNumColors(LPSTR lpbi);
HGLOBAL WINAPI CopyHandle(HGLOBAL h);
BOOL WINAPI SaveDIB(HDIB hDib,CFile& file);
HDIB WINAPI ReadDIBFile(CFile& file);
//在此處輸入自己的函數聲明
#endif//!_INC_DIBAPI
上面這些函數是實現圖像的讀取、存儲等圖像處理的基本功能的,你將自己需要的函數也輸入到「//在此處輸入自己的函數聲明」的下面。
點開DIBAPI.cpp文件,裡面是空白的,將如下代碼加入其中:
//DIBAPI.cpp
#include "stdafx.h"
#include "DIBAPI.h"
WORD WINAPI DIBNumColors(LPSTR lpbi)
{
WORD wBitCount;
if(IS_WIN30_DIB(lpbi))
{
DWORD dwClrUsed;
dwClrUsed=((LPBITMAPINFOHEADER)lpbi)->biClrUsed;
if(dwClrUsed)
return (WORD)dwClrUsed;
}
if(IS_WIN30_DIB(lpbi))
wBitCount=((LPBITMAPINFOHEADER)lpbi)->biBitCount;
else
wBitCount=((LPBITMAPCOREHEADER)lpbi)->bcBitCount;
switch(wBitCount)
{
case 1:
return 2;
case 4:
return 16;
case 8:
return 256;
default:
return 0;
}
}
WORD WINAPI PaletteSize(LPSTR lpbi)
{
if(IS_WIN30_DIB(lpbi))
return (WORD)(DIBNumColors(lpbi)*sizeof(RGBQUAD));
else
return (WORD)(DIBNumColors(lpbi)*sizeof(RGBTRIPLE));
}
LPSTR WINAPI FindDIBBits(LPSTR lpbi)
{
return (lpbi+*(LPDWORD)lpbi+::PaletteSize(lpbi));
}
DWORD WINAPI DIBWidth(LPSTR lpDIB)
{
LPBITMAPINFOHEADER lpbmi;
LPBITMAPCOREHEADER lpbmc;
lpbmi=(LPBITMAPINFOHEADER)lpDIB;
lpbmc=(LPBITMAPCOREHEADER)lpDIB;
if(IS_WIN30_DIB(lpDIB))
return lpbmi->biWidth;
else
return (DWORD)lpbmc->bcWidth;
}
DWORD WINAPI DIBHeight(LPSTR lpDIB)
{
LPBITMAPINFOHEADER lpbmi;
LPBITMAPCOREHEADER lpbmc;
lpbmi=(LPBITMAPINFOHEADER)lpDIB;
lpbmc=(LPBITMAPCOREHEADER)lpDIB;
if(IS_WIN30_DIB(lpDIB))
return lpbmi->biHeight;
else
return (DWORD)lpbmc->bcHeight;
}
BOOL WINAPI PaintDIB(HDC hDC,LPRECT lpDCRect,HDIB hDIB,LPRECT lpDIBRect,CPalette* pPal)
{
LPSTR lpDIBHdr;
LPSTR lpDIBBits;
BOOL bSuccess=FALSE;
HPALETTE hPal=NULL;
HPALETTE hOldPal=NULL;
if(hDIB==NULL)
return FALSE;
lpDIBHdr=(LPSTR)::GlobalLock((HGLOBAL)hDIB);
lpDIBBits=FindDIBBits(lpDIBHdr);
if(pPal!=NULL)
{
hPal=(HPALETTE)pPal->m_hObject;
hOldPal=::SelectPalette(hDC,hPal,TRUE);
}
::SetStretchBltMode(hDC,COLORONCOLOR);
if((RECTWIDTH(lpDCRect)==RECTWIDTH(lpDIBRect))&&(RECTHEIGHT(lpDCRect)==RECTHEIGHT(lpDIBRect)))
{
bSuccess=::SetDIBitsToDevice(hDC,lpDCRect->left,lpDCRect->top,RECTWIDTH(lpDCRect),RECTHEIGHT(lpDCRect),lpDIBRect->left,\
(int)DIBHeight(lpDIBHdr)-lpDIBRect->top-RECTHEIGHT(lpDIBRect),0,(WORD)DIBHeight(lpDIBHdr),\
lpDIBBits,(LPBITMAPINFO)lpDIBHdr,DIB_RGB_COLORS);
}
else
{
bSuccess=::StretchDIBits(hDC,lpDCRect->left,lpDCRect->top,RECTWIDTH(lpDCRect),RECTHEIGHT(lpDCRect),lpDIBRect->left,\
lpDIBRect->top,RECTWIDTH(lpDIBRect),RECTHEIGHT(lpDIBRect),\
lpDIBBits,(LPBITMAPINFO)lpDIBHdr,DIB_RGB_COLORS,SRCCOPY);
}
::GlobalUnlock((HGLOBAL)hDIB);
if(hOldPal)
::SelectPalette(hDC,hOldPal,TRUE);
GlobalUnlock(hDIB);
return bSuccess;
}
BOOL WINAPI CreateDIBPalette(HDIB hDIB,CPalette* pPal)
{
LPLOGPALETTE lpPal;
HANDLE hLogPal;
HPALETTE hPal=NULL;
LPSTR lpbi;
LPBITMAPINFO lpbmi;
LPBITMAPCOREINFO lpbmc;
BOOL bWinStyleDIB;
int i;
WORD wNumColors;
BOOL bResult=FALSE;
if(hDIB==NULL)
return FALSE;
lpbi=(LPSTR)::GlobalLock((HGLOBAL)hDIB);
lpbmi=(LPBITMAPINFO)lpbi;
lpbmc=(LPBITMAPCOREINFO)lpbi;
wNumColors=DIBNumColors(lpbi);
bWinStyleDIB=IS_WIN30_DIB(lpbi);
if(wNumColors!=0)
{
hLogPal=::GlobalAlloc(GHND,sizeof(LOGPALETTE)+sizeof(PALETTEENTRY)*wNumColors);
if(hLogPal==0)
{
::GlobalUnlock((HGLOBAL)hDIB);
return FALSE;
}
lpPal=(LPLOGPALETTE)::GlobalLock(hLogPal);
lpPal->palVersion=PALVERSION;
lpPal->palNumEntries=(WORD)wNumColors;
bWinStyleDIB=IS_WIN30_DIB(lpbi);
for(i=0;i<(int)wNumColors;i++)
{
if(bWinStyleDIB)
{
lpPal->palPalEntry[i].peRed=lpbmi->bmiColors[i].rgbRed;
lpPal->palPalEntry[i].peGreen=lpbmi->bmiColors[i].rgbGreen;
lpPal->palPalEntry[i].peBlue=lpbmi->bmiColors[i].rgbBlue;
lpPal->palPalEntry[i].peFlags=0;
}
else
{
lpPal->palPalEntry[i].peRed=lpbmc->bmciColors[i].rgbtRed;
lpPal->palPalEntry[i].peGreen=lpbmc->bmciColors[i].rgbtGreen;
lpPal->palPalEntry[i].peBlue=lpbmc->bmciColors[i].rgbtBlue;
lpPal->palPalEntry[i].peFlags=0;
}
}
bResult=pPal->CreatePalette(lpPal);
::GlobalUnlock((HGLOBAL)hLogPal);
::GlobalFree((HGLOBAL)hLogPal);
}
::GlobalUnlock((HGLOBAL)hDIB);
return bResult;
}
HGLOBAL WINAPI CopyHandle(HGLOBAL h)
{
if(h==NULL)
return NULL;
DWORD dwLen=::GlobalSize((HGLOBAL)h);
HGLOBAL hCopy=::GlobalAlloc(GHND,dwLen);
if(hCopy!=NULL)
{
void* lpCopy=::GlobalLock((HGLOBAL)hCopy);
void* lp=::GlobalLock((HGLOBAL)h);
memcpy(lpCopy,lp,dwLen);
::GlobalUnlock(hCopy);
::GlobalUnlock(h);
}
return hCopy;
}
BOOL WINAPI SaveDIB(HDIB hDib,CFile& file)
{
BITMAPFILEHEADER bmfHdr;
LPBITMAPINFOHEADER lpBI;
DWORD dwDIBSize;
if(!hDib)
return FALSE;
lpBI=(LPBITMAPINFOHEADER)::GlobalLock((HGLOBAL)hDib);
if(lpBI==NULL)
return FALSE;
if(!IS_WIN30_DIB(lpBI))
{
::GlobalUnlock((HGLOBAL)hDib);
return FALSE;
}
bmfHdr.bfType=DIB_HEADER_MARKER;
dwDIBSize=*(LPDWORD)lpBI+::PaletteSize((LPSTR)lpBI);
if((lpBI->biCompression==BI_RLE8)||(lpBI->biCompression==BI_RLE4))
dwDIBSize+=lpBI->biSizeImage;
else
{
DWORD dwBmBitsSize;
dwBmBitsSize=WIDTHBYTES((lpBI->biWidth)*((DWORD)lpBI->biBitCount))*lpBI->biHeight;
dwDIBSize+=dwBmBitsSize;
lpBI->biSizeImage=dwBmBitsSize;
}
bmfHdr.bfSize=dwDIBSize+sizeof(BITMAPFILEHEADER);
bmfHdr.bfReserved1=0;
bmfHdr.bfReserved2=0;
bmfHdr.bfOffBits=(DWORD)sizeof(BITMAPFILEHEADER)+lpBI->biSize+::PaletteSize((LPSTR)lpBI);
TRY
{
file.Write((LPSTR)&bmfHdr,sizeof(BITMAPFILEHEADER));
file.WriteHuge(lpBI,dwDIBSize);
}
CATCH(CFileException,e)
{
::GlobalUnlock((HGLOBAL)hDib);
THROW_LAST();
}
END_CATCH
::GlobalUnlock((HGLOBAL)hDib);
return TRUE;
}
HDIB WINAPI ReadDIBFile(CFile& file)
{
BITMAPFILEHEADER bmfHeader;
DWORD dwBitsSize;
HDIB hDIB;
LPSTR pDIB;
dwBitsSize=file.GetLength();
if(file.Read((LPSTR)&bmfHeader,sizeof(bmfHeader))!=sizeof(bmfHeader))
return NULL;
if(bmfHeader.bfType!=DIB_HEADER_MARKER)
return NULL;
hDIB=(HDIB)::GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,dwBitsSize);
if(hDIB==0)
return NULL;
pDIB=(LPSTR)::GlobalLock((HGLOBAL)hDIB);
if(file.ReadHuge(pDIB,dwBitsSize-sizeof(BITMAPFILEHEADER))!=dwBitsSize-sizeof(BITMAPFILEHEADER))
{
::GlobalUnlock((HGLOBAL)hDIB);
::GlobalFree((HGLOBAL)hDIB);
return NULL;
}
::GlobalUnlock((HGLOBAL)hDIB);
return hDIB;
}
//在此處輸入自己的函數定義
上面是DIBAPI.h頭文件中聲明的函數的定義,你將自己的函數定義加到「//在此處輸入自己的函數定義」後面。
下面加相應的消息映射函數,點Ctrl+W鍵,會出現MFC ClassWizard對話框,點Message Maps,在Class name下面的框中選擇CMy111Doc,在Messages裡面選中OnOpenDocument,然後點擊Add Function按鈕就加入了相應的消息映射函數,同樣的方法以此加入OnSaveDocument、DeleteContents、CanCloseFrame相應的消息函數,點「確定」即可。
點開111Doc.cpp文件,裡面有相應的消息映射函數定義位置,在函數CMy111Doc::CMy111Doc()里輸入以下代碼:
m_refColorBKG=0x00808080;
m_hDIB=NULL;
m_palDIB=NULL;
m_sizeDoc=CSize(1,1);
在函數CMy111Doc::~CMy111Doc()里輸入以下代碼:
if(m_hDIB!=NULL)
::GlobalFree((HGLOBAL)m_hDIB);
if(m_palDIB!=NULL)
delete m_palDIB;
在函數BOOL CMy111Doc::OnOpenDocument(LPCTSTR lpszPathName)里/ TODO: Add your specialized creation code here下面添加如下代碼:
CFile file;
CFileException fe;
if(!file.Open(lpszPathName,CFile::modeRead|CFile::shareDenyWrite,&fe))
{
ReportSaveLoadException(lpszPathName,&fe,FALSE,AFX_IDP_FAILED_TO_OPEN_DOC);
return FALSE;
}
DeleteContents();
BeginWaitCursor();
TRY
{
m_hDIB=::ReadDIBFile(file);
}
CATCH(CFileException,eLoad)
{
file.Abort();
EndWaitCursor();
ReportSaveLoadException(lpszPathName,eLoad,FALSE,AFX_IDP_FAILED_TO_OPEN_DOC);
m_hDIB=NULL;
return FALSE;
}
END_CATCH
InitDIBData();
EndWaitCursor();
if(m_hDIB==NULL)
{
CString strMsg;
strMsg="讀取圖像時出錯!可能是不支持該類型的圖像文件!";
MessageBox(NULL,strMsg,NULL,MB_ICONINFORMATION|MB_OK);
return FALSE;
}
SetPathName(lpszPathName);
SetModifiedFlag(FALSE);
在函數BOOL CMy111Doc::OnSaveDocument(LPCTSTR lpszPathName)里// TODO: Add your specialized code here and/or call the base class後面添加如下代碼:
CFile file;
CFileException fe;
if(!file.Open(lpszPathName,CFile::modeCreate|CFile::modeReadWrite|CFile::shareExclusive,&fe))
{
ReportSaveLoadException(lpszPathName,&fe,TRUE,AFX_IDP_INVALID_FILENAME);
return FALSE;
}
BOOL bSuccess=FALSE;
TRY
{
BeginWaitCursor();
bSuccess=::SaveDIB(m_hDIB,file);
file.Close();
}
CATCH(CException,eSave)
{
file.Abort();
EndWaitCursor();
ReportSaveLoadException(lpszPathName,eSave,TRUE,AFX_IDP_FAILED_TO_SAVE_DOC);
return FALSE;
}
END_CATCH
EndWaitCursor();
SetModifiedFlag(FALSE);
if(!bSuccess)
{
CString strMsg;
strMsg="無法保存BMP圖像";
MessageBox(NULL,strMsg,NULL,MB_ICONINFORMATION|MB_OK);
}
點開ClassView,右鍵點擊CMy111Doc類,點Add Member Variable出現添加成員變數對話框,在Variable Type下的框中輸入變數類型名HDIB,在Variable Name中輸入變數名m_hDIB,在Access中選中Public就在CMy111Doc中加入了公共成員變數m_hDIB;同樣加入int類型的m_nColorIndex、COLORREF類型的m_refColorBKG公共成員變數,再添加保護成員變數(在Access中選中Protected)CSize類的m_sizeDoc、CPalette*類的m_palDIB變數。右鍵點擊CMy111Doc類,點Add Member Function出現添加成員函數對話框,在Function Type下的框中輸入函數類型名void,在Function Declaration中輸入函數名InitDIBData(),在Access中選中Public就在CMy111Doc中加入了公共成員函數InitDIBData();同樣方法加入void類型的公共成員函數ReplaceHDIB(HDIB hDIB)。在111Doc.cpp中加入函數的代碼,InitDIBData()里加如下代碼:
if(m_palDIB!=NULL)
{
delete m_palDIB;
m_palDIB=NULL;
}
if(m_hDIB==NULL)
return;
LPSTR lpDIB=(LPSTR)::GlobalLock((HGLOBAL)m_hDIB);
if(::DIBWidth(lpDIB)>INT_MAX||::DIBHeight(lpDIB)>INT_MAX)
{
::GlobalUnlock((HGLOBAL)m_hDIB);
::GlobalFree((HGLOBAL)m_hDIB);
m_hDIB=NULL;
CString strMsg;
strMsg="BMP圖像太大!";
MessageBox(NULL,strMsg,NULL,MB_ICONINFORMATION|MB_OK);
return;
}
m_sizeDoc=CSize((int)::DIBWidth(lpDIB),(int)::DIBHeight(lpDIB));
::GlobalUnlock((HGLOBAL)m_hDIB);
m_palDIB=new CPalette;
if(m_palDIB==NULL)
{
::GlobalFree((HGLOBAL)m_hDIB);
m_hDIB=NULL;
return;
}
if(::CreateDIBPalette(m_hDIB,m_palDIB)==NULL)
{
delete m_palDIB;
m_palDIB=NULL;
return;
}
在函數ReplaceHDIB(HDIB hDIB)中加入如下代碼:
if(m_hDIB!=NULL)
::GlobalFree((HGLOBAL)m_hDIB);
m_hDIB=hDIB;
在CMy111doc.h的
// Attributes
public:
下面加入如下代碼:
HDIB GetHDIB() const
{
return m_hDIB;
}
CPalette* GetDocPalette() const
{
return m_palDIB;
}
CSize GetDocSize() const
{
return m_sizeDoc;
}
注意要在111Doc.h前加上#include "DIBAPI.h"語句(在class CMy111Doc:public CDocument語句前)
下面為CMy111View中的函數 void CMy111View::OnDraw(CDC* pDC)中添加如下代碼:
BeginWaitCursor();
CMy111* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
HDIB hDIB=pDoc->GetHDIB();
if(hDIB!=NULL)
{
LPSTR lpDIB=(LPSTR)::GlobalLock((HGLOBAL)hDIB);
int cxDIB=(int)::DIBWidth(lpDIB);
int cyDIB=(int)::DIBHeight(lpDIB);
::GlobalUnlock((HGLOBAL)hDIB);
CRect rcDIB;
rcDIB.top=rcDIB.left=0;
rcDIB.right=cxDIB;
rcDIB.bottom=cyDIB;
CRect rcDest;
if(pDC->IsPrinting())
{
int cxPage=pDC->GetDeviceCaps(HORZRES);
int cyPage=pDC->GetDeviceCaps(VERTRES);
int cxInch=pDC->GetDeviceCaps(LOGPIXELSX);
int cyInch=pDC->GetDeviceCaps(LOGPIXELSY);
rcDest.top=rcDest.left=0;
rcDest.bottom=(int)(((double)cyDIB*cxPage*cyInch)/((double)cxDIB*cxInch));
rcDest.right=cxPage;
int temp=cyPage-(rcDest.bottom-rcDest.top);
rcDest.bottom+=temp/2;
rcDest.top+=temp/2;
}
else
{
rcDest=rcDIB;
}
::PaintDIB(pDC->m_hDC,&rcDest,pDoc->GetHDIB(),&rcDIB,pDoc->GetDocPalette());
}
EndWaitCursor();
以上是關於圖像打開、顯示、保存的有關步驟和代碼。
在111.cpp文件的InitInstance()函數的ParseCommandLine(cmdInfo);語句下輸入語句
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
則啟動時不自動打開一個空文檔。
顯示圖像直方圖步驟:
1、 點擊ResourceView,右鍵點擊Dialog,選Insert Dialog 在屬性對話框中將ID改為ID_HIST,對話框名稱改為「直方圖」
2、 在工具欄中點「插入」-「新建類」,輸入類名,並選Base Class為CDialog,Dialog ID為ID_HIST。這樣就將對話框和類聯系起來了,在該對話框中拖入一Edit控制項,將其ID設為IDC_HISTSHOW;
3、 快捷鍵「Ctrl+W」,出現MFC ClassWizard對話框,在Messages欄中分別選WM_INITDIALOG和WM_Paint,再點擊「Add Function」,即將對話框初始化和畫圖函數加入對話框類之中。
4、 在Hist.h文件「public:」下面輸入如下變數定義:
LONG m_lCount[256];
char* m_lpDIBBits;
LONG m_lWidth;
LONG m_lHeight;
int m_iIsDraging;
CDlgIntensity(CWnd* pParent = NULL);
5、 打開Hist.cpp程序,在CHist::OnInitDialog()函數中「// TODO: Add extra initialization here」前將如下代碼拷貝進去:
unsigned char* lpSrc;
LONG i;
LONG j;
6、 在「// TODO: Add extra initialization here」後將如下代碼拷貝進去:
CWnd* pWnd=GetDlgItem(IDC_HISTSHOW);
CRect rect;
GetClientRect(rect);
ClientToScreen(&rect);
for(i=0;i<256;i++)
{
m_lCount[i]=0;
}
LONG lLineBytes;
lLineBytes=WIDTHBYTES(m_lWidth*8);
for(i=0;i<m_lHeight;i++)
{
for(j=0;j<m_lWidth;j++)
{
lpSrc=(unsigned char*)m_lpDIBBits+lLineBytes*i+j;
m_lCount[*(lpSrc)]++;
}
}
m_iIsDraging=0;
7、在CHist::OnPaint()函數「CPaintDC dc(this);」前將如下代碼拷如其中:
CString str;
LONG i;
LONG lMaxCount=0;
LONG m_iLowGray=0;
LONG m_iUpGray=255;
在CHist::OnPaint()函數「// TODO: Add your message handler code here」後將如下代碼拷如其中:
CWnd* pWnd=GetDlgItem(IDC_ HISTSHOW);
CDC* pDC=pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();
pDC->Rectangle(0,0,330,300);
CPen* pPenRed=new CPen;
pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));
CPen* pPenBlue=new CPen;
pPenBlue->CreatePen(PS_SOLID,1,RGB(0,0,255));
CPen* pPenGreen=new CPen;
pPenGreen->CreatePen(PS_SOLID,1,RGB(0,255,0));
CGdiObject* pOldPen=pDC->SelectObject(pPenRed);
pDC->MoveTo(10,10);
pDC->LineTo(10,280);
pDC->LineTo(320,280);
str.Format("0");
pDC->TextOut(10,283,str);
str.Format("50");
pDC->TextOut(60,283,str);
str.Format("100");
pDC->TextOut(110,283,str);
str.Format("150");
pDC->TextOut(160,283,str);
str.Format("200");
pDC->TextOut(210,283,str);
str.Format("255");
pDC->TextOut(265,283,str);
for(i=0;i<256;i+=5)
{
if((i&1)==0)
{
pDC->MoveTo(i+10,280);
pDC->LineTo(i+10,284);
}
else
{
pDC->MoveTo(i+10,280);
pDC->LineTo(i+10,282);
}
}
pDC->MoveTo(315,275);
pDC->LineTo(320,280);
pDC->LineTo(315,285);
pDC->MoveTo(10,10);
pDC->LineTo(5,15);
pDC->MoveTo(10,10);
pDC->LineTo(15,15);
for(i=m_iLowGray;i<=m_iUpGray;i++)
{
if(m_lCount[i]>lMaxCount)
{
lMaxCount=m_lCount[i];
}
}
pDC->MoveTo(10,25);
pDC->LineTo(14,25);
str.Format("%d",lMaxCount);
pDC->TextOut(11,26,str);
pDC->SelectObject(pPenGreen);
pDC->MoveTo(m_iLowGray+10,25);
pDC->LineTo(m_iLowGray+10,280);
pDC->MoveTo(m_iUpGray+10,25);
pDC->LineTo(m_iUpGray+10,280);
pDC->SelectObject(pPenBlue);
if(lMaxCount>0)
{
for(i=m_iLowGray;i<=m_iUpGray;i++)
{
pDC->MoveTo(i+10,280);
pDC->LineTo(i+10,281-(int)(m_lCount[i]*256/lMaxCount));
}
}
pDC->SelectObject(pOldPen);
delete pPenRed;
delete pPenBlue;
delete pPenGreen;
8、點開ResourceView中的Menu,出現IDR_MAINFRAME和 IDR_MY111TYPE,雙擊IDR_MY111TYPE就出現了程序界面,有文件、編輯、查看、窗口、幫助按鈕。點「查看」下面的虛線框右鍵——點「屬性」,輸入標題(直方圖),和ID(大寫英文,如ID_VIEW_HIST),這樣就將按鈕和程序通過ID聯系起來了,點擊該按鈕,就會執行相應的程序。下面為該按鈕添加函數:點Ctrl+W ,出現MFC ClassWizard對話框,選Class name為CMy111View,在Object IDs對應的選項中選中設置的ID(如為ID_VIEW_HIST),在Messages選項中選中COMMAND,然後點擊「Add Function」按鈕,選默認設置,就為ID_VIEW_HIST添加了相應的函數OnViewHist()。該函數在111View.cpp文件中定義,在函數void CMy111View:: OnViewHist()添加如下代碼:
// TODO: Add your command handler code here
CMy111Doc* pDoc=GetDocument();
if(pDoc->m_hDIB==NULL)
{
MessageBox("請先打開一幅256色的BMP圖像!","系統提示",MB_ICONINFORMATION|MB_OK);
return;
}
LPSTR lpDIB;
LPSTR lpDIBBits;
lpDIB=(LPSTR)::GlobalLock((HGLOBAL)pDoc->GetHDIB());
lpDIBBits=::FindDIBBits(lpDIB);
if(::DIBNumColors(lpDIB)!=256)
{
MessageBox("目前只支持查看256色點陣圖灰度直方圖!","系統提示",MB_ICONINFORMATION|MB_OK);
::GlobalUnlock((HGLOBAL)pDoc->GetHDIB());
return;
}
BeginWaitCursor();
CHist dlgPara;
dlgPara.m_lpDIBBits=lpDIBBits;
dlgPara.m_lWidth=::DIBWidth(lpDIB);
dlgPara.m_lHeight=::DIBHeight(lpDIB);
// dlgPara.m_iLowGray=0;
// dlgPara.m_iUpGray=255;
if(dlgPara.DoModal()!=IDOK)
{
return;
}
::GlobalUnlock((HGLOBAL)pDoc->GetHDIB());
EndWaitCursor();