【问题标题】:mfc picture control disappears after multiple runsmfc图片控件多次运行后消失
【发布时间】:2019-06-06 15:19:10
【问题描述】:

我正在使用图片控件来显示四个小的彩色 bmp。我有一个成员函数,可以将 bmp 设置为我想要的颜色。问题是,当我使用测试按钮单击功能多次运行我的功能 SetColor 时,图片控件消失并且对话框的行为很奇怪,例如拖动时没有边框。任何帮助或建议将不胜感激。

    void CRLoaderDlg::SetColor(int ColorID, int PictureID) {
    // data structure for picture/PictureID arrangement
    CStatic *pointertopicture[4];

    // check for PictureID boundary
    if (PictureID < 0 || PictureID > 3) {
        MessageBox("PictureID out of bounds", "Error", NULL);
        return; // exit function
    }

    // idc_picturecontrol is the picture control id of the dialog
    pointertopicture[0] = (CStatic*)GetDlgItem(IDC_PICTURECONTROL0);
    pointertopicture[1] = (CStatic*)GetDlgItem(IDC_PICTURECONTROL1);
    pointertopicture[2] = (CStatic*)GetDlgItem(IDC_PICTURECONTROL2);
    pointertopicture[3] = (CStatic*)GetDlgItem(IDC_PICTURECONTROL3);

    // handle for bitmap
    HBITMAP redbmp;     // color id is 0
    HBITMAP orangebmp;  // color id is 1
    HBITMAP yellowbmp;  // color id is 2
    HBITMAP greenbmp;   // color id is 3
    HBITMAP bluebmp;    // color id is 4
    HBITMAP purplebmp;  // color id is 5
    HBITMAP whitebmp;   // color id is 6
    HBITMAP blackbmp;   // color id is 7
    HBITMAP greybmp;    // color id is 8

    // set handle for bitmap with function loadimage to specified image + path (path optional)
    redbmp =    (HBITMAP)LoadImage(NULL, "sred.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    orangebmp = (HBITMAP)LoadImage(NULL, "sorange.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    yellowbmp = (HBITMAP)LoadImage(NULL, "syellow.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    greenbmp =  (HBITMAP)LoadImage(NULL, "sgreen.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    bluebmp =   (HBITMAP)LoadImage(NULL, "sblue.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    purplebmp = (HBITMAP)LoadImage(NULL, "spurple.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    whitebmp =  (HBITMAP)LoadImage(NULL, "swhite.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    blackbmp =  (HBITMAP)LoadImage(NULL, "sblack.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    greybmp =   (HBITMAP)LoadImage(NULL, "sgrey.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

    // function to set bitmap image to handle of image
    if (ColorID == 0) pointertopicture[PictureID]->SetBitmap(redbmp);
    else if (ColorID == 1) pointertopicture[PictureID]->SetBitmap(orangebmp);
    else if (ColorID == 2) pointertopicture[PictureID]->SetBitmap(yellowbmp);
    else if (ColorID == 3) pointertopicture[PictureID]->SetBitmap(greenbmp);
    else if (ColorID == 4) pointertopicture[PictureID]->SetBitmap(bluebmp);
    else if (ColorID == 5) pointertopicture[PictureID]->SetBitmap(purplebmp);
    else if (ColorID == 6) pointertopicture[PictureID]->SetBitmap(whitebmp);
    else if (ColorID == 7) pointertopicture[PictureID]->SetBitmap(blackbmp);
    else if (ColorID == 8) pointertopicture[PictureID]->SetBitmap(greybmp);
    }

    // run SetColor function 50 times
    void CRLoaderDlg::OnBnClickedButtonTest() {
        for (int i = 0; i < 50; i++) {
            for (int c = 0; c < 9; c++) {
                for (int p = 0; p < 4; p++) {
                    SetColor(c, p);
                }
            }
        }
    }

【问题讨论】:

    标签: c++ visual-studio mfc picturebox


    【解决方案1】:

    您有 GDI 资源泄漏,因为您没有删除旧位图。 使用DeleteObject 删除它们。 SetBitmap 返回旧的HBITMAP,因此如果不是NULL,您可以删除旧的HBITMAP

    如您所见,当您用完可用的 GDI 资源时,GDI 泄漏可能会导致绘制问题。

    【讨论】:

      猜你喜欢
      • 2011-04-14
      • 2012-02-08
      • 2013-06-28
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多