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


    动态加载dll库

    h文件中添加

    1. #include "mydll.h"
    2. #ifdef UNICODE //区分字符集
    3. #define LoadLibrary LoadLibraryW
    4. #else
    5. #define LoadLibrary LoadLibraryA
    6. #endif // !UNICODE
    7. typedef double(*mydllPtr)(int, int);
    8. 类内添加:
    9. mydllPtr m_mydll;

    cpp文件中添加

    1. 初始化函数中添加:
    2. HMODULE m_loadDll = LoadLibrary(TEXT("mydll.dll"));
    3. if (m_loadDll == NULL)
    4. AfxMessageBox("mydll.dll load error.");
    5. //m_mydll对应dll库中的mydll函数
    6. m_mydll = (mydllPtr)GetProcAddress(m_loadDll, "mydll");
    7. 使用时:
    8. m_mydll(int, int);

    Mat转CImage,播放视频

    1. //函数
    2. void MatToCImage(Mat& mat, CImage& cimage)
    3. {
    4. if (0 == mat.total())
    5. {
    6. return;
    7. }
    8. int nChannels = mat.channels();
    9. if ((1 != nChannels) && (3 != nChannels))
    10. {
    11. return;
    12. }
    13. int nWidth = mat.cols;
    14. int nHeight = mat.rows;
    15. //重建cimage
    16. cimage.Destroy();
    17. cimage.Create(nWidth, nHeight, 8 * nChannels);
    18. //拷贝数据
    19. uchar* pucRow; //指向数据区的行指针
    20. uchar* pucImage = (uchar*)cimage.GetBits(); //指向数据区的指针
    21. int nStep = cimage.GetPitch(); //每行的字节数,注意这个返回值有正有负
    22. if (1 == nChannels) //对于单通道的图像需要初始化调色板
    23. {
    24. RGBQUAD* rgbquadColorTable;
    25. int nMaxColors = 256;
    26. rgbquadColorTable = new RGBQUAD[nMaxColors];
    27. cimage.GetColorTable(0, nMaxColors, rgbquadColorTable);
    28. for (int nColor = 0; nColor < nMaxColors; nColor++)
    29. {
    30. rgbquadColorTable[nColor].rgbBlue = (uchar)nColor;
    31. rgbquadColorTable[nColor].rgbGreen = (uchar)nColor;
    32. rgbquadColorTable[nColor].rgbRed = (uchar)nColor;
    33. }
    34. cimage.SetColorTable(0, nMaxColors, rgbquadColorTable);
    35. delete[]rgbquadColorTable;
    36. }
    37. for (int nRow = 0; nRow < nHeight; nRow++)
    38. {
    39. pucRow = (mat.ptr<uchar>(nRow));
    40. for (int nCol = 0; nCol < nWidth; nCol++)
    41. {
    42. if (1 == nChannels)
    43. {
    44. *(pucImage + nRow * nStep + nCol) = pucRow[nCol];
    45. }
    46. else if (3 == nChannels)
    47. {
    48. for (int nCha = 0; nCha < 3; nCha++)
    49. {
    50. *(pucImage + nRow * nStep + nCol * 3 + nCha) = pucRow[nCol * 3 + nCha];
    51. }
    52. }
    53. }
    54. }
    55. }
    56. //使用
    57. CRect rect;
    58. GetDlgItem(IDC_STATIC_SHOW)->GetClientRect(&rect);
    59. CDC* pDc = GetDlgItem(IDC_STATIC_SHOW)->GetDC();
    60. pDc->SetStretchBltMode(COLORONCOLOR);
    61. CImage image;
    62. MatToCImage(mat, image);
    63. image.Draw(pDc->m_hDC, rect);

    读ini配置文件

    1. CString m_serialPort;
    2. GetPrivateProfileString("test", "com", "", m_serialPort.GetBuffer(100), 100, "./config.ini");
    3. config.ini文件
    4. [test]
    5. com=3

    鼠标操作

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

    1. h文件类内添加:
    2. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    3. afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    4. afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
    5. cpp文件中添加:
    6. BEGIN_MESSAGE_MAP(CIRCameraDemo_chaojingDlg, CDialogEx)
    7. ...
    8. ON_WM_LBUTTONDOWN()
    9. ON_WM_LBUTTONUP()
    10. ON_WM_RBUTTONDOWN()
    11. ...
    12. END_MESSAGE_MAP()
    13. void CIRCameraDemo_chaojingDlg::OnLButtonDown(UINT nFlags, CPoint point)
    14. {
    15. // TODO: 在此添加控件通知处理程序代码
    16. CDialogEx::OnLButtonDown(nFlags, point);
    17. }
    18. void CIRCameraDemo_chaojingDlg::OnLButtonUp(UINT nFlags, CPoint point)
    19. {
    20. // TODO: 在此添加控件通知处理程序代码
    21. CDialogEx::OnLButtonUp(nFlags, point);
    22. }
    23. void CIRCameraDemo_chaojingDlg::OnRButtonDown(UINT nFlags, CPoint point)
    24. {
    25. // TODO: 在此添加控件通知处理程序代码
    26. CDialogEx::OnRButtonDown(nFlags, point);
    27. }

    在Edit控件上画框

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

    调试信息打印到输出界面

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

  • 相关阅读:
    力扣452-用最少数量的箭引爆气球——贪心算法
    UWB室内定位系统全套源码 高精度人员定位系统源码
    学C的第二十二天【深度剖析数据在内存中的存储:1. 数据类型介绍;2. 整型在内存中的存储】
    golang[ssa & callgraph] 获取调用图实战
    PDF转换工具哪个好?值得推荐的3款PDF转换软件
    【黑马头条之项目部署_持续集成Jenkins】
    前端取图片相同颜色作为遮罩或者背景
    什么是AI推理
    Win10/Win11下部署Django项目到Apache2.4的方法
    使用 Docker 搭建 Jenkins CI/CD 环境
  • 原文地址:https://blog.csdn.net/yuchunhai321/article/details/133347447