-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitmapObject.cpp
97 lines (70 loc) · 1.96 KB
/
BitmapObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "BitmapObject.h"
BitMapObject::BitMapObject() {
hdcMemory = NULL;
hbmNewBitMap = NULL;
hbmOldBitMap = NULL;
iWidth = 0;
iHeight = 0;
}
BitMapObject::~BitMapObject() {
//if hdcMemory hasn't yet been destroyed, do so
if (hdcMemory)
BitMapObject::Destroy();
}
void BitMapObject::Load(HDC hdcCompatible, LPCWSTR lpszFileName) {
//if hdcMemory isn't null, make it so
if (hdcMemory)
BitMapObject::Destroy();
//create memory dc
hdcMemory = CreateCompatibleDC(hdcCompatible);
//load the bitmap
hbmNewBitMap = (HBITMAP)LoadImage(NULL, lpszFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
//shove the image into the dc
hbmOldBitMap = (HBITMAP)SelectObject(hdcMemory, hbmNewBitMap);
//grab the bitmap's properties
BITMAP bmp;
GetObject(hbmNewBitMap, sizeof(BITMAP), (LPVOID)&bmp);
//grab the width and height
iWidth = bmp.bmWidth;
iHeight = bmp.bmHeight;
}
void BitMapObject::Create(HDC hdcCompatible, int width, int height) {
//if hdcMemory isnt null, make it so
if (hdcMemory)
BitMapObject::Destroy();
//create the memory dc
hdcMemory = CreateCompatibleDC(hdcCompatible);
//create the bitmap
hbmNewBitMap = CreateCompatibleBitmap(hdcCompatible, width, height);
//shove the image into the dc
hbmOldBitMap = (HBITMAP)SelectObject(hdcMemory, hbmNewBitMap);
//change the width and height
iWidth = width;
iHeight = height;
}
void BitMapObject::Destroy() {
//restore old bitmap
SelectObject(hdcMemory, hbmOldBitMap);
//delete new bitmap
DeleteObject(hbmNewBitMap);
//delete the device context
DeleteDC(hdcMemory);
//set members to 0/NULL
hdcMemory = NULL;
hbmNewBitMap = NULL;
hbmOldBitMap = NULL;
iWidth = 0;
iHeight = 0;
}
BitMapObject::operator HDC() {
//return hdcMemory
return(hdcMemory);
}
int BitMapObject::GetWidth() {
// return width
return iWidth;
}
int BitMapObject::GetHeight() {
// return height
return iHeight;
}