C++針對bmp格式解析實例
本文實例講述了C++針對bmp格式解析的方法,分享給大家供大家參考。具體方法如下:
寫這代碼時,容易出現(xiàn)如下錯誤:
1. 忘了on_wm_paint() 一直在界面上畫不出來
2. 正確寫法
寫成了
這里主要是用了前面一篇中的CWnd框架。
.cpp源文件如下:
#include "resource.h"
#include <afxdlgs.h >
CMyApp theApp;
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE; //必須返回TRUE,否則不會進(jìn)入消息循環(huán),界面會直接退出
}
//CMainWindow
BEGIN_MESSAGE_MAP(CMainWindow, CWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_COMMAND(IDC_OPEN, OnOpen)
END_MESSAGE_MAP()
//構(gòu)造函數(shù)
CMainWindow::CMainWindow()
{
LPCTSTR lpszClassName = ::AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW, ::LoadCursorA(NULL, IDC_ARROW), (HBRUSH)(COLOR_3DFACE+1), theApp.LoadIcon(IDI_MAIN));
CreateEx(WS_EX_CLIENTEDGE, lpszClassName, "xxx", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL);
}
//析構(gòu)函數(shù)
CMainWindow::~CMainWindow()
{
}
//消息映射函數(shù)
int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
//OutputDebugString("oncreate");
CClientDC dc(this);
m_hMemDC = ::CreateCompatibleDC(dc);
m_nWidth = 0;
m_nHeight = 0;
//設(shè)置菜單
HMENU hMenu = ::LoadMenuA(theApp.m_hInstance, (LPCSTR)IDR_MENU);
::SetMenu(m_hWnd, hMenu);
return 0;
}
void CMainWindow::OnNcDestroy( )
{
delete this;
}
void CMainWindow::OnDestroy()
{
}
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
::BitBlt(dc, 0, 0, m_nWidth, m_nHeight, m_hMemDC, 0, 0, SRCCOPY);
}
void CMainWindow::OnOpen()
{
CFileDialog dlg(TRUE);
if (IDOK != dlg.DoModal())
{
return;
}
HANDLE hFile = ::CreateFile(dlg.GetPathName(), GENERIC_READ , FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
return;
}
HANDLE hFileMap = ::CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (NULL == hFileMap)
{
return;
}
LPVOID lpBase = ::MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
if (NULL == lpBase)
{
return;
}
//從頭結(jié)構(gòu)中取三個信息:1.bfOffBits位圖數(shù)據(jù)在文件中的起始位置,2.m_nWidth圖像的寬,3.m_nHeight圖像的高
BITMAPFILEHEADER* pbitmapFileHeader;
BITMAPINFO* pbitmapInfo;
pbitmapFileHeader = (BITMAPFILEHEADER*)lpBase;
if (pbitmapFileHeader->bfType != MAKEWORD('B','M'))
{
MessageBox("not bmp");
::UnmapViewOfFile(lpBase);
::CloseHandle(hFileMap);
::CloseHandle(hFile);
}
//DWORD bfOffBits = pbitmapFileHeader->bfOffBits;
BYTE* pBits = (BYTE*)lpBase + pbitmapFileHeader->bfOffBits;
pbitmapInfo = (BITMAPINFO*)((BYTE*)lpBase + sizeof(BITMAPFILEHEADER));
m_nWidth = pbitmapInfo->bmiHeader.biWidth;
m_nHeight = pbitmapInfo->bmiHeader.biHeight;
//顯示BMP文件到內(nèi)存設(shè)備
//得客戶區(qū)DC
CClientDC dc(this);
//創(chuàng)建與客戶區(qū)DC兼容的位圖
HBITMAP hBitmap = ::CreateCompatibleBitmap(dc, m_nWidth, m_nHeight);
if (hBitmap == 0)
{
return;
}
//位圖選入內(nèi)存DC
::SelectObject(m_hMemDC, hBitmap);
//圖像數(shù)據(jù)放到建立的DC中
::SetDIBitsToDevice(m_hMemDC, 0, 0, m_nWidth, m_nHeight, 0, 0, 0, m_nHeight, pBits, pbitmapInfo, DIB_RGB_COLORS);
::InvalidateRect(m_hWnd, NULL, TRUE);
::DeleteObject(hBitmap);
::UnmapViewOfFile(lpBase);
::CloseHandle(hFileMap);
::CloseHandle(hFile);
}
.h頭文件如下:
class CMyApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
//CMainWindow
class CMainWindow:public CWnd
{
public:
CMainWindow();
~CMainWindow();
protected:
HDC m_hMemDC; //與客戶區(qū)兼容的內(nèi)存DC句柄
UINT m_nWidth; //BMP的寬度
UINT m_nHeight; //BMP的高度
//消息映射
afx_msg void OnNcDestroy( );
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDestroy( );
afx_msg void OnOpen();
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
};
希望本文所述對大家的C++程序設(shè)計有所幫助。
相關(guān)文章
C++的STL中accumulate函數(shù)的使用方法
這篇文章主要介紹了C++的STL中accumulate的使用方法,accumulate作用是累加求和即自定義類型數(shù)據(jù)處理,下文具體的操作方法需要的小伙伴可以參考一下2022-03-03va_list(),va_start(),va_arg(),va_end() 詳細(xì)解析
這些宏定義在stdarg.h中,所以用到可變參數(shù)的程序應(yīng)該包含這個頭文件.下面我們寫一個簡單的可變參數(shù)的函數(shù),該函數(shù)至少有一個整數(shù)參數(shù),第二個參數(shù)也是整數(shù),是可選的.函數(shù)只是打印這兩個參數(shù)的值2013-09-09c語言實現(xiàn)從源文件從文本到可執(zhí)行文件經(jīng)歷的過程
這篇文章主要介紹了c語言實現(xiàn)從源文件從文本到可執(zhí)行文件經(jīng)歷的過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07基于對話框程序中讓對話框捕獲WM_KEYDOWN消息的實現(xiàn)方法
下面我們將通過程序給大家演示基于對話框的應(yīng)用程序?qū)M_KEYDOWN消息的捕獲。需要的朋友可以參考下2013-05-05