mfc 动态加载dll库,Mat转CImage,读ini配置文件,鼠标操作,在edit控件上画框,调试信息打印

动态加载dll库

h文件中添加

#include "mydll.h"
#ifdef UNICODE  //区分字符集
#define LoadLibrary  LoadLibraryW
#else
#define LoadLibrary  LoadLibraryA
#endif // !UNICODE

typedef double(*mydllPtr)(int, int);


类内添加:
mydllPtr m_mydll; 

cpp文件中添加

初始化函数中添加:

HMODULE m_loadDll = LoadLibrary(TEXT("mydll.dll"));
if (m_loadDll == NULL)
	AfxMessageBox("mydll.dll load error.");

//m_mydll对应dll库中的mydll函数
m_mydll = (mydllPtr)GetProcAddress(m_loadDll, "mydll");


使用时:
m_mydll(int, int);

Mat转CImage,播放视频

//函数
void MatToCImage(Mat& mat, CImage& cimage)
{
	if (0 == mat.total())
	{
		return;
	}
	int nChannels = mat.channels();
	if ((1 != nChannels) && (3 != nChannels))
	{
		return;
	}
	int nWidth = mat.cols;
	int nHeight = mat.rows;
	//重建cimage
	cimage.Destroy();
	cimage.Create(nWidth, nHeight, 8 * nChannels);
	//拷贝数据
	uchar* pucRow;									//指向数据区的行指针
	uchar* pucImage = (uchar*)cimage.GetBits();		//指向数据区的指针
	int nStep = cimage.GetPitch();					//每行的字节数,注意这个返回值有正有负
	if (1 == nChannels)								//对于单通道的图像需要初始化调色板
	{
		RGBQUAD* rgbquadColorTable;
		int nMaxColors = 256;
		rgbquadColorTable = new RGBQUAD[nMaxColors];
		cimage.GetColorTable(0, nMaxColors, rgbquadColorTable);
		for (int nColor = 0; nColor < nMaxColors; nColor++)
		{
			rgbquadColorTable[nColor].rgbBlue = (uchar)nColor;
			rgbquadColorTable[nColor].rgbGreen = (uchar)nColor;
			rgbquadColorTable[nColor].rgbRed = (uchar)nColor;
		}
		cimage.SetColorTable(0, nMaxColors, rgbquadColorTable);
		delete[]rgbquadColorTable;
	}
	for (int nRow = 0; nRow < nHeight; nRow++)
	{
		pucRow = (mat.ptr(nRow));
		for (int nCol = 0; nCol < nWidth; nCol++)
		{
			if (1 == nChannels)
			{
				*(pucImage + nRow * nStep + nCol) = pucRow[nCol];
			}
			else if (3 == nChannels)
			{
				for (int nCha = 0; nCha < 3; nCha++)
				{
					*(pucImage + nRow * nStep + nCol * 3 + nCha) = pucRow[nCol * 3 + nCha];
				}
			}
		}
	}

}


//使用
CRect rect;
GetDlgItem(IDC_STATIC_SHOW)->GetClientRect(&rect);
CDC* pDc = GetDlgItem(IDC_STATIC_SHOW)->GetDC();
pDc->SetStretchBltMode(COLORONCOLOR);
CImage image;
MatToCImage(mat, image);
image.Draw(pDc->m_hDC, rect); 

读ini配置文件

CString m_serialPort;
GetPrivateProfileString("test", "com", "", m_serialPort.GetBuffer(100), 100, "./config.ini");


config.ini文件
[test]
com=3

鼠标操作

左键按下弹起,右键按下弹起

h文件类内添加:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);

cpp文件中添加:

BEGIN_MESSAGE_MAP(CIRCameraDemo_chaojingDlg, CDialogEx)
    ...
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDOWN()
    ...
END_MESSAGE_MAP()

void CIRCameraDemo_chaojingDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: 在此添加控件通知处理程序代码

	CDialogEx::OnLButtonDown(nFlags, point);
}

void CIRCameraDemo_chaojingDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加控件通知处理程序代码

	CDialogEx::OnLButtonUp(nFlags, point);
}

void CIRCameraDemo_chaojingDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加控件通知处理程序代码

	CDialogEx::OnRButtonDown(nFlags, point);
}

在Edit控件上画框

CDC* pDc = GetDlgItem(IDC_STATIC_SHOW)->GetDC();
CBrush *pOldBrush = (CBrush*)pDc->SelectStockObject(NULL_BRUSH);
CPen *pen = new CPen(PS_SOLID, 5, RGB(255, 0, 0));
CPen *pOldPen = pDc->SelectObject(pen);
pDc->Rectangle(CRect(m_startPoint, m_stopPoint));
pDc->SelectObject(pOldPen);
pDc->SelectObject(pOldBrush);
delete pen;

调试信息打印到输出界面

TRACE("temp= %d\n", temp);

你可能感兴趣的:(Windows,mfc,c++)