#include "stdafx.h"
#include "NewButton.h"
#include "resource.h"
#include <opencv.hpp>
using namespace cv;
CNewButton::CNewButton()
{
PICname = "";
whetherback = false;
m_FontSize = 94;
m_FontName = "Yu Gothic UI Semibold";
}
CNewButton::~CNewButton()
{
}
//为按键按钮添加图片//
void CNewButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
CRect rect = lpDrawItemStruct->rcItem;//得到按键区域
dc.Attach(lpDrawItemStruct->hDC);//得到绘制的设备环境CDC
VERIFY(lpDrawItemStruct->CtlType == ODT_BUTTON);
CPen penback(PS_SOLID, 1, RGB(30, 30, 30));
dc.SelectObject(&penback);
CBrush brubut(RGB(m_ButCol, m_ButCol, m_ButCol));
dc.SelectObject(&brubut);
dc.FillSolidRect(rect, RGB(m_BackCol, m_BackCol, m_BackCol));
int num = 0;//0常态,1按下
if (PICname != "")
{
CString picstr;
CString A, C;
Mat I;
A = "ButtonBitmap/";
C = ".PNG";
picstr = A + PICname + C;
std::string sz2 = CT2A(picstr.GetBuffer());
I = imread( sz2, 1);
CvvImage BC1;
BC1.CopyOf(&P);
CRect BitRect(0, 0, rect.right, rect.bottom);
HDC hdc = dc.GetSafeHdc();
BC1.DrawToHDC(hdc, BitRect);
if (lpDrawItemStruct->itemState&ODS_SELECTED || whetherback)//当按下按钮时的处理
{
if (num)
{
num = 0;
}
else
{
num = 1;
}
CString backstr;
A = "ButtonBitmap/";
C = "b.png";
backstr = A + PICname + C;
std::string sz2 = CT2A(backstr.GetBuffer());
Mat I = imread(sz2, 1);
IplImage BP(I);
CvvImage BC;
BC.CopyOf(&BP);
CRect BitRect(0, 0, rect.right, rect.bottom);
HDC bhdc = dc.GetSafeHdc();
BC.DrawToHDC(bhdc, BitRect);
}
}
else{
CString picstr;
CString A, C;
Mat I;
A = "ButtonBitmap/";
C = ".png";
picstr = A + PICname + C;
std::string sz2 = CT2A(picstr.GetBuffer());
I = imread(sz2, 1);
IplImage *P;
P = NULL;
if (P)cvReleaseImage(&P);
P = &IplImage(I);
if (P == NULL)
{
}
else
{
CvvImage C;
C.CopyOf(P);
CRect BitRect(0, 0, rect.right, rect.bottom);
HDC hdc = dc.GetSafeHdc();
C.DrawToHDC(hdc, BitRect);
}
}
}
BEGIN_MESSAGE_MAP(CNewButton, CButton)
// ON_WM_MOUSEMOVE()
// ON_WM_MOUSEHOVER()
// ON_WM_MOUSELEAVE()
// ON_WM_SETCURSOR()
END_MESSAGE_MAP()
sz2为空,函数imread异常
图片读取错误,检查图片的路径和格式。