Measure/DN_sample/Form2.cpp

131 lines
2.5 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Form2.cpp : implementation file
//
#include "stdafx.h"
#include "DN_sample.h"
#include "Form2.h"
// CForm2 dialog
IMPLEMENT_DYNAMIC(CForm2, CDialog)
CForm2::CForm2(CWnd* pParent /*=NULL*/)
: CDialog(CForm2::IDD, pParent),hb(0)
, m_img(nullptr)
, m_type(0)
{
Create(IDD,pParent);
}
CForm2::~CForm2()
{
}
void CForm2::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//DDX_Control(pDX, IDC_LIST1, m_listbox);
}
BEGIN_MESSAGE_MAP(CForm2, CDialog)
ON_WM_PAINT()
//ON_LBN_SELCHANGE(IDC_LIST1, &CForm2::OnLbnSelchangeList1)
END_MESSAGE_MAP()
BOOL CForm2::OnInitDialog()
{
CDialog::OnInitDialog();
ModifyStyle(WS_CAPTION, 0);
return true;
}
// CForm2 message handlers
void CForm2::OnPaint()
{
//CDialog::OnPaint();
ShowImage();
CPaintDC dc(this); // device context for painting
// 绘制边界线
CRect rectClient;
GetClientRect(&rectClient);
dc.DrawEdge(rectClient, EDGE_ETCHED, BF_RECT);
}
void CForm2::ShowOriginImg(MicroImage* img) {
m_img = img;
m_type = 0;
ShowImage();
}
void CForm2::ShowCalImg(MicroImage* img) {
m_img = img;
m_type = 1;
ShowImage();
}
void CForm2::ShowImage() {
if (m_type > 1 || !m_img) return;
if (m_type == 0 && !m_img->m_OriginData) return;
if (m_type == 1 && !m_img->m_CalData) return;
if (!hb) {
BITMAPINFOHEADER bih = { 0 };
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = m_img->m_width;
bih.biHeight = -m_img->m_height; // 负数表示图像是顶部向下的
bih.biPlanes = 1;
bih.biBitCount = 24; // RGB
bih.biCompression = BI_RGB;
bih.biSizeImage = m_img->m_width * m_img->m_height * 3; // 每个像素3字节
// 分配内存给BITMAPINFO
BITMAPINFO* bmi = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256); // 假设有256种颜色但对于24位RGB不需要RGBQUAD数组
memcpy(&bmi->bmiHeader, &bih, sizeof(BITMAPINFOHEADER));
CDC* pDC = GetDC();
uchar* data = (m_type == 0) ? m_img->m_OriginData : m_img->m_CalData;
hb = CreateDIBitmap(pDC->m_hDC, &bih, CBM_INIT, data, bmi, DIB_RGB_COLORS);
pDC->Detach();
ReleaseDC(pDC);
free(bmi);
}
GetClientRect(&r1);
CPaintDC dc(this);
BITMAP b;
GetObject(hb, sizeof(b), &b);
CDC cdc;
cdc.CreateCompatibleDC(0);
HGDIOBJ o = cdc.SelectObject(hb);
SetStretchBltMode(dc, STRETCH_HALFTONE);
dc.StretchBlt(0, 0, r1.right - r1.left, r1.bottom - r1.top, &cdc, 0, 0, b.bmWidth, b.bmHeight, SRCCOPY);
cdc.SelectObject(o);
cdc.DeleteDC();
ReleaseDC(&cdc);
}
void CForm2::ClearImg() {
if (hb != NULL) {
DeleteObject(hb);
hb = NULL;
}
}