1、用vc+创建直方图详解步骤和程序:打开VC程序文件新建工程中的MFC AppWizard(exe),在工程下面的框中输入工程名(假定工程名为111),点确定选多重文档,点下一个后面都点下一个直到完成确定,基本框架就完成了,下面就加代码。这时VC界面上左边框的下面有三个按钮:ClassView、ResourceView和FileView,ClassView里面是工程111的类:CAdoutDlg、CChildFrame、CMy111App、CMy111Doc、CMy111View和Globals;点ResourceView里面是资源类:Accelerator、Dialog、Icon、Menu、S
2、tring 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中的Head
3、er 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
4、 RECTHEIGHT(lpRect) (lpRect)-bottom-(lpRect)-top)#define WIDTHBYTES(bits) (bits)+31)/32*4)#define DIB_HEADER_MARKER (WORD)(MbiClrUsed; if(dwClrUsed) return (WORD)dwClrUsed; if(IS_WIN30_DIB(lpbi) wBitCount=(LPBITMAPINFOHEADER)lpbi)-biBitCount; else wBitCount=(LPBITMAPCOREHEADER)lpbi)-bcBitCount; swit
5、ch(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 (l
6、pbi+*(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 lp
7、DIB) 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 lpD
8、IBHdr; 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,
9、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(lp
10、DIBHdr), 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); :GlobalU
11、nlock(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
12、 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(PALETTEENT
13、RY)*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;ipalPalEntryi.peRed=lpbmi-bmiColorsi.rgbRed; lpPal-palPalEntryi.peGreen=lpbm
14、i-bmiColorsi.rgbGreen; lpPal-palPalEntryi.peBlue=lpbmi-bmiColorsi.rgbBlue; lpPal-palPalEntryi.peFlags=0; else lpPal-palPalEntryi.peRed=lpbmc-bmciColorsi.rgbtRed; lpPal-palPalEntryi.peGreen=lpbmc-bmciColorsi.rgbtGreen; lpPal-palPalEntryi.peBlue=lpbmc-bmciColorsi.rgbtBlue; lpPal-palPalEntryi.peFlags=0
15、; 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
16、* 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=(LPBITMAPINFO
17、HEADER):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;
18、 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
19、)+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 b
20、mfHeader; 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
21、; 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头文件中声明的函数的定义,你将自己的函数定义加到“/在此处输
22、入自己的函数定义”后面。下面加相应的消息映射函数,点Ctrl+W键,会出现MFC ClassWizard对话框,点Message Maps,在Class name下面的框中选择CMy111Doc,在Messages里面选中OnOpenDocument,然后点击Add Function按钮就加入了相应的消息映射函数,同样的方法以此加入OnSaveDocument、DeleteContents、CanCloseFrame相应的消息函数,点“确定”即可。点开111Doc.cpp文件,里面有相应的消息映射函数定义位置,在函数CMy111Doc:CMy111Doc()里输入以下代码:m_refColor
23、BKG=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;
24、 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(); EndWaitCurso
25、r(); 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(lps
26、zPathName); 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) ReportSaveLoadE
27、xception(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(!bSucce
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1