104 | /* |
104 | /* |
105 | ** ReadConfig |
105 | ** ReadConfig |
106 | ** |
106 | ** |
107 | ** Loads the image from disk |
107 | ** Loads the image from disk |
108 | ** |
108 | ** |
109 | */ |
109 | */ |
110 | void CMeterImage::LoadImage(bool bLoadAlways) |
110 | void CMeterImage::LoadImage(bool bLoadAlways) |
111 | { |
111 | { |
112 | // Load the bitmap if defined |
112 | // Load the bitmap if defined |
113 | if (!m_ImageName.empty()) |
113 | if (!m_ImageName.empty()) |
114 | { |
114 | { |
115 | std::wstring filename = m_ImageName; |
115 | std::wstring filename = m_ImageName; |
116 | |
116 | |
117 | // Check extension and if it is missing, add .png |
117 | // Check extension and if it is missing, add .png |
118 | size_t pos = filename.find_last_of(L"\\"); |
118 | size_t pos = filename.find_last_of(L"\\"); |
119 | if (pos == std::wstring::npos) pos = 0; |
119 | if (pos == std::wstring::npos) pos = 0; |
120 | if (std::wstring::npos == filename.find(L'.', pos)) |
120 | if (std::wstring::npos == filename.find(L'.', pos)) |
121 | { |
121 | { |
122 | filename += L".png"; |
122 | filename += L".png"; |
123 | } |
123 | } |
124 | |
124 | |
125 | // Read the bitmap to memory so that it's not locked by GDI+ |
125 | // Read the bitmap to memory so that it's not locked by GDI+ |
126 | HANDLE fileHandle = CreateFile(filename.c_str(), GENERIC_READ, NULL, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
126 | HANDLE fileHandle = CreateFile(filename.c_str(), GENERIC_READ, NULL, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
127 | if (fileHandle != INVALID_HANDLE_VALUE) |
127 | if (fileHandle != INVALID_HANDLE_VALUE) |
128 | { |
128 | { |
129 | // Compare the timestamp and filename to check if the file has been changed (don't load if it's not) |
129 | // Compare the timestamp and filename to check if the file has been changed (don't load if it's not) |
130 | FILETIME tmpTime; |
130 | FILETIME tmpTime; |
131 | GetFileTime(fileHandle, NULL, NULL, &tmpTime); |
131 | GetFileTime(fileHandle, NULL, NULL, &tmpTime); |
132 | if (bLoadAlways || CompareFileTime(&tmpTime, &m_Modified) != 0) |
132 | if (bLoadAlways || CompareFileTime(&tmpTime, &m_Modified) != 0) |
133 | { |
133 | { |
134 | m_Modified = tmpTime; |
134 | m_Modified = tmpTime; |
135 | |
135 | |
136 | DWORD imageSize = GetFileSize(fileHandle, 0); |
136 | DWORD imageSize = GetFileSize(fileHandle, 0); |
137 | |
137 | |
138 | if (imageSize != std::wstring::npos) |
138 | if (imageSize != -1) |
139 | { |
139 | { |
140 | if (m_hBuffer) |
140 | if (m_hBuffer) |
141 | { |
141 | { |
142 | ::GlobalFree(m_hBuffer); |
142 | ::GlobalFree(m_hBuffer); |
143 | } |
143 | } |
144 | |
144 | |
145 | m_hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, imageSize); |
145 | m_hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, imageSize); |
146 | if (m_hBuffer) |
146 | if (m_hBuffer) |
147 | { |
147 | { |
148 | void* pBuffer = ::GlobalLock(m_hBuffer); |
148 | void* pBuffer = ::GlobalLock(m_hBuffer); |
149 | if (pBuffer) |
149 | if (pBuffer) |
150 | { |
150 | { |
151 | DWORD readBytes; |
151 | DWORD readBytes; |
152 | ReadFile(fileHandle, pBuffer, imageSize, &readBytes, NULL); |
152 | ReadFile(fileHandle, pBuffer, imageSize, &readBytes, NULL); |
153 | ::GlobalUnlock(m_hBuffer); |
153 | ::GlobalUnlock(m_hBuffer); |
154 | |
154 | |
155 | IStream* pStream = NULL; |
155 | IStream* pStream = NULL; |
156 | if (::CreateStreamOnHGlobal(m_hBuffer, FALSE, &pStream) == S_OK) |
156 | if (::CreateStreamOnHGlobal(m_hBuffer, FALSE, &pStream) == S_OK) |
157 | { |
157 | { |
158 | if (m_Bitmap) delete m_Bitmap; |
158 | if (m_Bitmap) delete m_Bitmap; |
159 | |
159 | |
160 | m_Bitmap = Bitmap::FromStream(pStream); |
160 | m_Bitmap = Bitmap::FromStream(pStream); |
161 | if (m_Bitmap) |
161 | if (m_Bitmap) |
162 | { |
162 | { |
163 | Status status = m_Bitmap->GetLastStatus(); |
163 | Status status = m_Bitmap->GetLastStatus(); |
164 | if(Ok != status) |
164 | if(Ok != status) |
165 | { |
165 | { |
166 | DebugLog(L"Unable to create bitmap: %s", filename.c_str()); |
166 | DebugLog(L"Unable to create bitmap: %s", filename.c_str()); |
167 | delete m_Bitmap; |
167 | delete m_Bitmap; |
168 | m_Bitmap = NULL; |
168 | m_Bitmap = NULL; |
169 | } |
169 | } |
170 | } |
170 | } |
171 | |
171 | |
172 | pStream->Release(); |
172 | pStream->Release(); |
173 | } |
173 | } |
174 | } |
174 | } |
175 | } |
175 | } |
176 | } |
176 | } |
177 | } |
177 | } |
178 | CloseHandle(fileHandle); |
178 | CloseHandle(fileHandle); |
179 | } |
179 | } |
180 | else |
180 | else |
181 | { |
181 | { |
182 | DebugLog(L"Unable to load image: %s", filename.c_str()); |
182 | DebugLog(L"Unable to load image: %s", filename.c_str()); |
183 | } |
183 | } |
184 | |
184 | |
185 | if (m_Bitmap) |
185 | if (m_Bitmap) |
186 | { |
186 | { |
187 | if (!m_DimensionsDefined) |
187 | if (!m_DimensionsDefined) |
188 | { |
188 | { |
189 | m_W = m_Bitmap->GetWidth(); |
189 | m_W = m_Bitmap->GetWidth(); |
190 | m_H = m_Bitmap->GetHeight(); |
190 | m_H = m_Bitmap->GetHeight(); |
191 | } |
191 | } |
192 | } |
192 | } |
193 | } |
193 | } |
194 | } |
194 | } |