我正在使用图片控件显示四个小的彩色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);
}
}
}
}
最佳答案
您有GDI资源泄漏,因为您不删除旧的位图。
用DeleteObject
删除它们。SetBitmap
返回旧的HBITMAP
的原因是,如果不是HBITMAP
,则可以删除旧的NULL
。
当您失去可用的GDI资源时,发生GDI泄漏可能会导致绘画问题。
关于c++ - 多次运行后,mfc图片控件消失,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54150969/