CreatPicture是創建picture控制元件
DrawPicture是重復繪制圖片
做的專案遠程監控的服務端
bool CMainDlg::CreatPicture(const int ip_array_subscript, const int ipone, const int iptwo, const int ipthree, const int ipfour, const char *buf)
{
//using namespace std;
std::string ip_address;
char c1[20],c2[20],c3[20],c4[20];
RECT rec;
int return1, return2;
_itoa(ipone, c1, 10);
_itoa(iptwo, c2, 10);
_itoa(ipthree, c3, 10);
_itoa(ipfour, c4, 10);
ip_address += c1;
ip_address += ".";
ip_address += c2;
ip_address += ".";
ip_address += c3;
ip_address += ".";
ip_address += c4;
rec=GetPictureCoordinate(ip_array_subscript);
//GlobalAlloc是一個Windows API函式。該函式從全域堆中分配一定數目的位元組數
//若函式呼叫成功,則回傳一個新分配的記憶體物件的句柄。
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, 4096);
//鎖定記憶體中指定的記憶體塊,并回傳一個地址值,令其指向記憶體塊的起始處
void * pData = GlobalLock(hGlobal);
//記憶體拷貝
memcpy(pData, buf, 4096);
GlobalUnlock(hGlobal);
IStream * pStream = NULL;
//CreateStreamOnHGlobal函式從指定記憶體創建流物件。
if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
{
CImage image;
if (SUCCEEDED(image.Load(pStream)))
{
//避免讀到已有物件的記憶體
if (pPictureControl[ip_array_subscript].m_hWnd == 0)
{
//如果創建圖片框失敗,則回傳0
return1 = pPictureControl[ip_array_subscript].Create(_T(""), WS_CHILD | WS_VISIBLE | SS_BITMAP, rec, this, ip_array_subscript);
if (return1 == 0)
{
array[ip_array_subscript] = ipone;
array[ip_array_subscript + 1] = iptwo;
array[ip_array_subscript + 2] = ipthree;
array[ip_array_subscript + 3] = ipfour;
pStream->Release();
GlobalFree(hGlobal);
image.Detach();
return 0;
}
}
else
{
return2 = (int)(pPictureControl[ip_array_subscript].SetBitmap(image));
}
//CStatic *pic = (CStatic *)GetDlgItem(IDC_PIC);
//pic->SetBitmap(image);
}
pStream->Release();
image.Detach();
}
GlobalFree(hGlobal);
if (return1 != 0 && return2 != NULL)
{
return 1;
}
}
bool CMainDlg::DrawPicture(const int ip_array_subscript, const int ipone, const int iptwo, const int ipthree, const int ipfour, const char *buf)
{
//using namespace std;
std::string ip_address;
int return1;
char c1[20], c2[20], c3[20], c4[20];
RECT rec;
_itoa(ipone, c1, 10);
_itoa(iptwo, c2, 10);
_itoa(ipthree, c3, 10);
_itoa(ipfour, c4, 10);
ip_address += c1;
ip_address += ".";
ip_address += c2;
ip_address += ".";
ip_address += c3;
ip_address += ".";
ip_address += c4;
rec = GetPictureCoordinate(ip_array_subscript);
//GlobalAlloc是一個Windows API函式。該函式從全域堆中分配一定數目的位元組數
//若函式呼叫成功,則回傳一個新分配的記憶體物件的句柄。
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, 4096);
//鎖定記憶體中指定的記憶體塊,并回傳一個地址值,令其指向記憶體塊的起始處
void * pData = GlobalLock(hGlobal);
//記憶體拷貝
memcpy(pData, buf, 4096);
GlobalUnlock(hGlobal);
IStream * pStream = NULL;
//CreateStreamOnHGlobal函式從指定記憶體創建流物件。
if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
{
CImage image;
if (SUCCEEDED(image.Load(pStream)))
{
if (pPictureControl[ip_array_subscript].m_hWnd != 0)
{
return1 = (int)(pPictureControl[ip_array_subscript].SetBitmap(image));
}
}
pStream->Release();
image.Detach();
}
GlobalFree(hGlobal);
if (return1!=NULL)
{
return 1;
}
return 0;
}
uj5u.com熱心網友回復:
return 0; 就可能導致資源泄漏uj5u.com熱心網友回復:
最好注釋一部分代碼,通過TRACE增加提示文字輸出,看是哪部分卡住了。
uj5u.com熱心網友回復:
為何回傳0會告成記憶體泄漏?
uj5u.com熱心網友回復:
檢查是否資源泄漏的辦法之一:在任務管理器 行程 查看 選擇列 里面選擇:記憶體使用、虛擬記憶體大小、句柄數、執行緒數、USER物件、GDI物件
讓你的程式(行程)不退出,回圈執行主流程很多遍,越多越好,比如1000000次甚至無限回圈,記錄以上各數值,再隔至少一小時,越長越好,比如一個月,再記錄以上各數值。如果以上兩組數值的差較大或隨時間流逝不斷增加,則鐵定有對應資源的資源泄漏!
搜“GDI泄露檢測”
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/103858.html
標籤:界面
