📄 memorytestgraph.cpp
字号:
//---------------------------------------------------------------------------
//
// MemoryTestGraph.cpp: CMemoryTestGraph Class Implementation
//
//---------------------------------------------------------------------------
#include "StdAfx.h"
#include "MemoryTestGraph.h"
// CMemoryTestGraph Implementation
CMemoryTestGraph::CMemoryTestGraph()
: m_hWnd(NULL)
, m_hDC(NULL), m_hCompatDC(NULL)
, m_hBitmap(NULL), m_hOldBitmap(NULL)
, m_hPen(NULL), m_hOldPen(NULL)
, m_hFont(NULL), m_hOldFont(NULL)
{
ZeroMemory(&m_bi, sizeof(BITMAPINFO));
m_clRect.left = 0;
m_clRect.top = 0;
m_clRect.right = 0;
m_clRect.bottom = 0;
}
HRESULT __fastcall CMemoryTestGraph::Connect(HWND hWnd)
{
// Check Window Handle
if ((! hWnd) || (! ::IsWindow(hWnd)))
{
return E_INVALIDARG;
}
m_hWnd = hWnd;
// Get Window Handle DC
m_hDC = ::GetDC(m_hWnd);
if (! m_hDC)
{
return E_FAIL;
}
// Create Compatible DC
m_hCompatDC = ::CreateCompatibleDC(m_hDC);
if (! m_hCompatDC)
{
return E_FAIL;
}
// Create and select Font
m_hFont = ::CreateFont(14, 0, 0, 0, 0,
FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_TT_PRECIS,
CLIP_DEFAULT_PRECIS, PROOF_QUALITY,
DEFAULT_PITCH, "Arial");
if (! m_hFont)
{
return HRESULT_FROM_WIN32(::GetLastError());
}
m_hOldFont = HFONT(::SelectObject(m_hCompatDC, m_hFont));
if (! m_hOldFont)
{
return E_FAIL;
}
// Initialize the Bitmap for the first time
return Resize();
}
HRESULT __fastcall CMemoryTestGraph::Disconnect()
{
// Unselect and delete Font
::SelectObject(m_hCompatDC, m_hOldFont);
::DeleteObject(m_hFont);
m_hFont = NULL;
m_hOldFont = NULL;
// Delete Pen
DeletePen();
// Delete Bitmap
DeleteBitmap();
// Delete Compatible DC
::DeleteDC(m_hCompatDC);
m_hCompatDC = NULL;
// Release Window Handle DC
::ReleaseDC(m_hWnd, m_hDC);
m_hDC = NULL;
m_hWnd = NULL;
m_clRect.left = 0;
m_clRect.top = 0;
m_clRect.right = 0;
m_clRect.bottom = 0;
return S_OK;
}
HRESULT __fastcall CMemoryTestGraph::Resize()
{
// Get Window Client area
RECT newRect;
::GetClientRect(m_hWnd, &newRect);
// Has client area resized?
if ((newRect.right == m_clRect.right) &&
(newRect.bottom == m_clRect.bottom))
{
return S_FALSE;
}
m_clRect = newRect;
DeleteBitmap();
// Prepare DIB data
m_bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_bi.bmiHeader.biWidth = m_clRect.right;
m_bi.bmiHeader.biHeight = m_clRect.bottom;
m_bi.bmiHeader.biPlanes = 1;
m_bi.bmiHeader.biBitCount = 24;
m_bi.bmiHeader.biCompression = BI_RGB;
m_bi.bmiHeader.biSizeImage = ((m_bi.bmiHeader.biWidth + 7) >> 3)
* m_bi.bmiHeader.biHeight
* m_bi.bmiHeader.biBitCount;
HDC hMainDC = ::GetDC(NULL);
m_bi.bmiHeader.biXPelsPerMeter = ::GetDeviceCaps(hMainDC, HORZRES)
/ ::GetDeviceCaps(hMainDC, HORZSIZE);
m_bi.bmiHeader.biYPelsPerMeter = ::GetDeviceCaps(hMainDC, VERTRES)
/ ::GetDeviceCaps(hMainDC, VERTSIZE);
::ReleaseDC(NULL, hMainDC);
m_bi.bmiHeader.biClrUsed = 0;
m_bi.bmiHeader.biClrImportant = 0;
// Create and select DIB
m_hBitmap = ::CreateDIBitmap(m_hDC, &m_bi.bmiHeader, 0, NULL, NULL, DIB_RGB_COLORS);
m_hOldBitmap = HBITMAP(::SelectObject(m_hCompatDC, m_hBitmap));
return S_OK;
}
HRESULT __fastcall CMemoryTestGraph::CreateBMPFile(LPCSTR pszFile)
{
LPBYTE lpBits = LPBYTE(malloc(m_bi.bmiHeader.biSizeImage));
if (! lpBits)
{
return E_OUTOFMEMORY;
}
// Retrieve the bits from the DIB.
if (! ::GetDIBits(m_hDC, m_hBitmap, 0, m_bi.bmiHeader.biHeight,
LPVOID(lpBits), &m_bi, DIB_RGB_COLORS))
{
return HRESULT_FROM_WIN32(::GetLastError());
}
// Create the BMP file
FILE* fp = fopen(pszFile, "wb");
if (! fp)
return E_FAIL;
/*
HANDLE hf = ::CreateFile(pszFile,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hf == INVALID_HANDLE_VALUE)
{
return HRESULT_FROM_WIN32(::GetLastError());
}
*/
BITMAPFILEHEADER hdr; // bitmap file-header
hdr.bfType = 0x4D42; // "BM"
// Compute the size of the entire file
hdr.bfSize = sizeof(BITMAPFILEHEADER)
+ m_bi.bmiHeader.biSize
+ m_bi.bmiHeader.biSizeImage;
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
// Compute the offset to the bitmap bits
hdr.bfOffBits = sizeof(BITMAPFILEHEADER)
+ m_bi.bmiHeader.biSize;
// Copy the BITMAPFILEHEADER into the BMP file
fwrite((const void *)&hdr, sizeof(hdr), 1, fp);
/*
DWORD dwTmp;
if (! ::WriteFile(hf,
LPCVOID(&hdr),
sizeof(BITMAPFILEHEADER),
&dwTmp, NULL))
{
return HRESULT_FROM_WIN32(::GetLastError());
}
*/
// Copy the BITMAPINFOHEADER into the file
fwrite((const void *)&m_bi.bmiHeader, sizeof(BITMAPINFOHEADER), 1, fp);
/*
if (! ::WriteFile(hf,
LPCVOID(&m_bi.bmiHeader),
sizeof(BITMAPINFOHEADER),
&dwTmp, NULL))
{
return HRESULT_FROM_WIN32(::GetLastError());
}
*/
// Copy the bitmap bits into the BMP file
fwrite((const void *)lpBits, size_t(m_bi.bmiHeader.biSizeImage), 1, fp);
/*
if (! ::WriteFile(hf,
LPCVOID(lpBits),
m_bi.bmiHeader.biSizeImage,
&dwTmp,
NULL))
{
return HRESULT_FROM_WIN32(::GetLastError());
}
*/
// Close the BMP file
fclose(fp);
/*
if (! ::CloseHandle(hf))
{
return HRESULT_FROM_WIN32(::GetLastError());
}
*/
// Free memory
free((void *)lpBits);
return S_OK;
}
inline void CMemoryTestGraph::DeleteBitmap()
{
// Unselect and delete old DIB
if (m_hOldBitmap)
{
::SelectObject(m_hCompatDC, m_hOldBitmap);
m_hOldBitmap = NULL;
}
if (m_hBitmap)
{
::DeleteObject(m_hBitmap);
m_hBitmap = NULL;
}
}
inline void CMemoryTestGraph::DeletePen()
{
// Unselect and delete Pen
if (m_hOldPen)
{
::SelectObject(m_hCompatDC, m_hOldPen);
m_hOldPen = NULL;
}
if (m_hPen)
{
::DeleteObject(m_hPen);
m_hPen = NULL;
}
}
void __fastcall CMemoryTestGraph::Clear(BOOL bWhiteBkgnd)
{
HBRUSH hBrush;
if (bWhiteBkgnd)
{
hBrush = ::CreateSolidBrush(RGB(255, 255, 255));
}
else
{
hBrush = ::CreateSolidBrush(RGB(0, 0, 0));
}
::FillRect(m_hCompatDC, &m_clRect, hBrush);
::DeleteObject(hBrush);
}
void __fastcall CMemoryTestGraph::SetTextColor(BOOL bWhiteBkgnd)
{
// Set text attributes
::SetBkMode(m_hCompatDC, TRANSPARENT);
if (bWhiteBkgnd)
{
::SetTextColor(m_hCompatDC, RGB(0, 0, 0));
// ::SetBkColor(m_hCompatDC, RGB(255, 255, 255));
}
else
{
::SetTextColor(m_hCompatDC, RGB(255, 255, 255));
// ::SetBkColor(m_hCompatDC, RGB(0, 0, 0));
}
}
void __fastcall CMemoryTestGraph::SetPenParameters(COLORREF clr, int width)
{
// Delete old Pen
DeletePen();
// Create and select Pen
m_hPen = ::CreatePen(PS_SOLID, width, clr);
m_hOldPen = HPEN(::SelectObject(m_hCompatDC, m_hPen));
}
void __fastcall CMemoryTestGraph::MoveTo(int X, int Y)
{
::MoveToEx(m_hCompatDC, X, Y, NULL);
}
void __fastcall CMemoryTestGraph::LineTo(int X, int Y)
{
::LineTo(m_hCompatDC, X, Y);
}
void __fastcall CMemoryTestGraph::Rectangle(int nLeft, int nTop, int nRight, int nBottom)
{
HBRUSH m_hBrush = HBRUSH(::GetStockObject(HOLLOW_BRUSH));
HBRUSH m_hOldBrush = HBRUSH(::SelectObject(m_hCompatDC, m_hBrush));
::Rectangle(m_hCompatDC, nLeft, nTop, nRight, nBottom);
::SelectObject(m_hCompatDC, m_hOldBrush);
// @WARNING: There's no need to delete Stock Objects
}
void __fastcall CMemoryTestGraph::TextExtent(LPCTSTR lpString, LPSIZE lpSize)
{
::GetTextExtentPoint32(m_hCompatDC, lpString, strlen(lpString), lpSize);
}
void __fastcall CMemoryTestGraph::TextOut(int X, int Y, LPCTSTR lpString)
{
::TextOut(m_hCompatDC, X, Y, lpString, strlen(lpString));
}
void __fastcall CMemoryTestGraph::Paint()
{
// Present the graph
::BitBlt(m_hDC, 0, 0, m_clRect.right, m_clRect.bottom, m_hCompatDC, 0, 0, SRCCOPY);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -