📄 testsavebmpview.cpp
字号:
// testSaveBmpView.cpp : implementation of the CTestSaveBmpView class
//
#include "stdafx.h"
#include "testSaveBmp.h"
#include "testSaveBmpDoc.h"
#include "testSaveBmpView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTestSaveBmpView
IMPLEMENT_DYNCREATE(CTestSaveBmpView, CView)
BEGIN_MESSAGE_MAP(CTestSaveBmpView, CView)
//{{AFX_MSG_MAP(CTestSaveBmpView)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestSaveBmpView construction/destruction
CTestSaveBmpView::CTestSaveBmpView()
{
// TODO: add construction code here
}
CTestSaveBmpView::~CTestSaveBmpView()
{
}
BOOL CTestSaveBmpView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTestSaveBmpView drawing
void CTestSaveBmpView::OnDraw(CDC* pDC)
{
CTestSaveBmpDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect rc;
GetClientRect(&rc);
rc.left = rc.Width()/10;
rc.right = rc.Width() - rc.Width()/10;
rc.top = rc.Height()/10;
rc.bottom= rc.Height() - rc.Height()/10;
DrawCurve(pDC,rc);
pDC->TextOut(rc.left + rc.Width()/2, rc.top + rc.Height()/2, "VC实现自绘图形输出到bmp文件");
}
/////////////////////////////////////////////////////////////////////////////
// CTestSaveBmpView printing
BOOL CTestSaveBmpView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTestSaveBmpView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTestSaveBmpView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTestSaveBmpView diagnostics
#ifdef _DEBUG
void CTestSaveBmpView::AssertValid() const
{
CView::AssertValid();
}
void CTestSaveBmpView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTestSaveBmpDoc* CTestSaveBmpView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestSaveBmpDoc)));
return (CTestSaveBmpDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTestSaveBmpView message handlers
void CTestSaveBmpView::SaveAsBmp(CString filename)
{
//定义图形大小
int iWidth = 800;
int iHeight = 600;
int iPixel = 16;
//图形格式参数
LPBITMAPINFO lpbmih = new BITMAPINFO;
lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
lpbmih->bmiHeader.biWidth = iWidth;
lpbmih->bmiHeader.biHeight = iHeight;
lpbmih->bmiHeader.biPlanes = 1;
lpbmih->bmiHeader.biBitCount = iPixel;
lpbmih->bmiHeader.biCompression = BI_RGB;
lpbmih->bmiHeader.biSizeImage = 0;
lpbmih->bmiHeader.biXPelsPerMeter = 0;
lpbmih->bmiHeader.biYPelsPerMeter = 0;
lpbmih->bmiHeader.biClrUsed = 0;
lpbmih->bmiHeader.biClrImportant = 0;
//创建位图数据
HDC hdc,hdcMem;
HBITMAP hBitMap = NULL;
CBitmap *pBitMap = NULL;
CDC *pMemDC = NULL;
BYTE *pBits;
hdc = CreateIC(TEXT("DISPLAY"),NULL,NULL,NULL);
hdcMem = CreateCompatibleDC(hdc);
hBitMap = CreateDIBSection(hdcMem,lpbmih,DIB_PAL_COLORS,(void **)&pBits,NULL,0);
pBitMap = new CBitmap;
pBitMap->Attach(hBitMap);
pMemDC = new CDC;
pMemDC->Attach(hdcMem);
pMemDC->SelectObject(pBitMap);
//
CRect rc(0,0,iWidth,iHeight);
pMemDC->SetBkMode(TRANSPARENT);
//添加自绘图形
DrawCurve(pMemDC,rc);
//保存到文件并创建位图结构
BITMAPFILEHEADER bmfh;
ZeroMemory(&bmfh,sizeof(BITMAPFILEHEADER));
*((char *)&bmfh.bfType) = 'B';
*(((char *)&bmfh.bfType) + 1) = 'M';
bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfh.bfSize = bmfh.bfOffBits + (iWidth * iHeight) * iPixel / 8;
TCHAR szBMPFileName[128];
int iBMPBytes = iWidth * iHeight * iPixel / 8;
strcpy(szBMPFileName,filename);
CFile file;
if(file.Open(szBMPFileName,CFile::modeWrite | CFile::modeCreate))
{
file.Write(&bmfh,sizeof(BITMAPFILEHEADER));
file.Write(&(lpbmih->bmiHeader),sizeof(BITMAPINFOHEADER));
file.Write(pBits,iBMPBytes);
file.Close();
}
pMemDC->DeleteDC();
delete pMemDC; pMemDC = NULL;
delete pBitMap; pBitMap = NULL;
delete lpbmih; lpbmih = NULL;
}
void CTestSaveBmpView::DrawCurve(CDC *pDC, CRect rcClient)
{
//页面背景色
CBrush brushCtl;
brushCtl.CreateSolidBrush(RGB(255,255,255));
pDC->Rectangle(rcClient);
pDC->FillRect(rcClient,&brushCtl) ;
brushCtl.DeleteObject();
CPen pen;
pen.CreatePen(PS_SOLID, 1, RGB(255,0,0));
CPen *oldPen = pDC->SelectObject(&pen);
double xMin = 10.00f, xMax = 100.00f;
double yMin = 10.00f, yMax = 200.00f;
double dbX1 = (xMax- xMin)/100 + xMin;
double dbY1 = 600/dbX1 ;
for (int i=1; i<100; i++) //曲线
{
double dbX2 = (xMax- xMin)*i/100 + xMin;
double dbY2 = 600/dbX2 ;
pDC->MoveTo(int(rcClient.left+(dbX1 - xMin)*rcClient.Width()/(xMax- xMin)),
int(rcClient.bottom-(dbY1- yMin)*rcClient.Height()/(yMax- yMin)));
pDC->LineTo(int(rcClient.left+(dbX2 - xMin)*rcClient.Width()/(xMax- xMin)),
int(rcClient.bottom-(dbY2- yMin)*rcClient.Height()/(yMax- yMin)));
dbX1=dbX2;
dbY1=dbY2;
}
pDC->SelectObject(oldPen);
pen.DeleteObject();
oldPen = NULL;
}
void CTestSaveBmpView::OnLButtonDown(UINT nFlags, CPoint point)
{
CView::OnLButtonDown(nFlags, point);
}
void CTestSaveBmpView::OnRButtonDown(UINT nFlags, CPoint point)
{
CFileDialog dlg(false,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"位图文件(*.bmp)|*.bmp|",
NULL);
if (dlg.DoModal()!= IDOK) return;
CString filename = dlg.GetFileName() + ".bmp";
SaveAsBmp(filename);
CView::OnRButtonDown(nFlags, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -