00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
#include "precomp.h"
00016
#pragma hdrstop
00017
00018 HICON
IconFromBestImage(
00019 ICONFILEHEADER *pifh,
00020 LPNEWHEADER lpnhSrc,
00021
int cxDesired,
00022
int cyDesired,
00023 UINT LR_flags);
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 int LoadStringOrError(
00038 HANDLE hModule,
00039 UINT wID,
00040 LPWSTR lpBuffer,
00041
int cchBufferMax,
00042 WORD wLangId)
00043 {
00044 HANDLE hResInfo;
00045 HANDLE hStringSeg;
00046 LPTSTR lpsz;
00047
int cch;
00048
00049
00050
00051
00052
if (lpBuffer ==
NULL) {
00053 RIPMSG0(RIP_WARNING,
"LoadStringOrError: lpBuffer == NULL");
00054
return 0;
00055 }
00056
00057
00058 cch = 0;
00059
00060
00061
00062
00063
00064
if (hResInfo =
FINDRESOURCEEXW(hModule, (LPTSTR)ULongToPtr( ((LONG)(((
USHORT)wID >> 4) + 1)) ), RT_STRING, wLangId)) {
00065
00066
00067
00068
00069 hStringSeg =
LOADRESOURCE(hModule, hResInfo);
00070
00071
00072
00073
00074
if (lpsz = (LPTSTR)
LOCKRESOURCE(hStringSeg, hModule)) {
00075
00076
00077
00078
00079
00080 wID &= 0x0F;
00081
while (
TRUE) {
00082 cch = *((
UTCHAR *)lpsz++);
00083
00084
if (wID-- == 0)
break;
00085 lpsz += cch;
00086 }
00087
00088
00089
00090
00091
if (cchBufferMax == 0) {
00092 *(LPTSTR *)lpBuffer = lpsz;
00093 }
else {
00094
00095
00096
00097
00098 cchBufferMax--;
00099
00100
00101
00102
00103
if (cch > cchBufferMax)
00104 cch = cchBufferMax;
00105
00106
00107
00108
00109 RtlCopyMemory(lpBuffer, lpsz, cch*
sizeof(WCHAR));
00110 }
00111
00112
00113
00114
00115
00116
UNLOCKRESOURCE(hStringSeg, hModule);
00117 }
00118 }
00119
00120
00121
00122
00123
if (cchBufferMax != 0) {
00124 lpBuffer[cch] = 0;
00125 }
00126
00127
return cch;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 #define BITMAPFILEHEADER_SIZE 14
00140 #define MINHEADERS_SIZE (BITMAPFILEHEADER_SIZE + sizeof(BITMAPCOREHEADER))
00141
00142 HANDLE
RtlLoadObjectFromDIBFile(
00143 LPCWSTR lpszName,
00144 LPWSTR type,
00145 DWORD cxDesired,
00146 DWORD cyDesired,
00147 UINT LR_flags)
00148 {
00149
FILEINFO fi = {
NULL,
NULL,
NULL };
00150 HANDLE hFile;
00151 HANDLE hFileMap =
NULL;
00152 HANDLE hObj =
NULL;
00153 TCHAR
szFile[
MAX_PATH];
00154 TCHAR szFile2[
MAX_PATH];
00155 LPWSTR pszFileDummy;
00156
00157
if (LR_flags & LR_ENVSUBST) {
00158
00159
00160
00161
00162
00163
00164
00165 ExpandEnvironmentStrings(lpszName, szFile2,
MAX_PATH);
00166
00167 }
else {
00168
00169 lstrcpy(szFile2, lpszName);
00170 }
00171
00172
if (SearchPath(
NULL,
00173 szFile2,
00174
NULL,
00175
MAX_PATH,
00176
szFile,
00177 &pszFileDummy) == 0) {
00178 RIPERR0(ERROR_FILE_NOT_FOUND, RIP_VERBOSE,
"");
00179
return NULL;
00180 }
00181
00182
00183
00184
00185 hFile = CreateFileW(
szFile,
00186 GENERIC_READ,
00187 FILE_SHARE_READ,
00188
NULL,
00189
OPEN_EXISTING,
00190 0,
00191
NULL);
00192
00193
if (hFile ==
INVALID_HANDLE_VALUE)
00194
goto Done;
00195
00196
00197
00198
00199 hFileMap = CreateFileMapping(hFile,
NULL, PAGE_READONLY, 0, 0,
NULL);
00200
00201
if (hFileMap ==
NULL)
00202
goto CloseDone;
00203
00204
00205
00206
00207 fi.
pFileMap = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
00208
00209
if (fi.
pFileMap ==
NULL)
00210
goto CloseDone;
00211
00212 fi.
pFileEnd = fi.
pFileMap + GetFileSize(hFile,
NULL);
00213 fi.
pFilePtr = fi.
pFileMap;
00214 fi.
pszName =
szFile;
00215
00216
try {
00217
switch(
PTR_TO_ID(
type)) {
00218
case PTR_TO_ID(RT_BITMAP): {
00219
00220 LPBITMAPFILEHEADER pBFH;
00221
UPBITMAPINFOHEADER upBIH;
00222 LPBYTE lpBits;
00223
DWORD cx;
00224
DWORD cy;
00225 WORD planes;
00226 WORD bpp;
00227
DWORD cbSizeImage = 0;
00228
DWORD cbSizeFile;
00229
DWORD cbSizeBits;
00230
00231
00232
00233
00234 pBFH = (LPBITMAPFILEHEADER)fi.
pFileMap;
00235 upBIH = (
UPBITMAPINFOHEADER)(fi.
pFileMap +
BITMAPFILEHEADER_SIZE);
00236
00237
00238
00239
00240
if (pBFH->bfType !=
BFT_BITMAP)
00241
break;
00242
00243
00244
00245
00246
00247
00248
if (upBIH->biSize ==
sizeof(BITMAPCOREHEADER)) {
00249
00250 cx = ((
UPBITMAPCOREHEADER)upBIH)->bcWidth;
00251
cy = ((
UPBITMAPCOREHEADER)upBIH)->bcHeight;
00252 bpp = ((
UPBITMAPCOREHEADER)upBIH)->bcBitCount;
00253 planes = ((
UPBITMAPCOREHEADER)upBIH)->bcPlanes;
00254
00255 }
else {
00256
00257 cx = upBIH->biWidth;
00258
cy = upBIH->biHeight;
00259 bpp = upBIH->biBitCount;
00260 planes = upBIH->biPlanes;
00261
00262
if(upBIH->biSizeImage >=
sizeof(BITMAPINFOHEADER))
00263 cbSizeImage = upBIH->biSizeImage;
00264 }
00265
00266 cbSizeFile = (
DWORD)(fi.
pFileEnd - fi.
pFileMap);
00267 cbSizeBits =
BitmapSize(cx,
cy, planes, bpp);
00268
00269
if ((!cbSizeImage && ((cbSizeFile -
MINHEADERS_SIZE) < cbSizeBits)) ||
00270 (cbSizeImage && ((cbSizeFile -
MINHEADERS_SIZE) < cbSizeImage))) {
00271
00272
break;
00273 }
00274
00275
00276
00277
00278
if ((pBFH->bfOffBits >= (
sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPCOREHEADER))) &&
00279 (pBFH->bfOffBits <= (cbSizeFile - cbSizeImage))) {
00280
00281 lpBits = ((LPBYTE)upBIH) + pBFH->bfOffBits -
sizeof(BITMAPFILEHEADER);
00282
00283 }
else {
00284
00285 lpBits =
NULL;
00286 }
00287
00288
00289
00290
00291
00292 hObj =
ConvertDIBBitmap(upBIH,
00293 cxDesired,
00294 cyDesired,
00295 LR_flags,
00296
NULL,
00297 &lpBits);
00298 }
00299
break;
00300
00301
case PTR_TO_ID(RT_CURSOR):
00302
case PTR_TO_ID(RT_ICON):
00303 {
00304 RTAG *prtag;
00305 ICONFILEHEADER *pifh;
00306
00307
00308
00309
00310 prtag = (RTAG *)fi.
pFileMap;
00311
00312
if (prtag->ckID != FOURCC_RIFF) {
00313
00314 NEWHEADER nh;
00315
00316 pifh = (ICONFILEHEADER *)fi.
pFileMap;
00317
00318
00319
00320
00321
00322
if ((pifh->iReserved != 0) ||
00323 ((pifh->iResourceType != IMAGE_ICON) &&
00324 (pifh->iResourceType != IMAGE_CURSOR)) ||
00325 (pifh->cresIcons < 1))
00326
00327
break;
00328
00329 nh.ResType = ((
type == RT_ICON) ? IMAGE_ICON : IMAGE_CURSOR);
00330 nh.ResCount = pifh->cresIcons;
00331 nh.Reserved = 0;
00332
00333
00334
00335
00336
00337
00338
00339
00340 hObj =
IconFromBestImage(pifh,
00341 &nh,
00342 cxDesired,
00343 cyDesired,
00344 LR_flags);
00345 }
else {
00346
00347
BOOL fAni;
00348
00349 hObj =
LoadCursorIconFromFileMap(&fi,
00350 &
type,
00351 cxDesired,
00352 cyDesired,
00353 LR_flags,
00354 &fAni);
00355 }
00356 }
00357
break;
00358
00359
default:
00360 UserAssert(
FALSE);
00361
break;
00362 }
00363 } except (W32ExceptionHandler(
FALSE, RIP_WARNING)) {
00364 hObj =
NULL;
00365 }
00366 CloseDone:
00367
00368
if (fi.
pFileMap !=
NULL)
00369 UnmapViewOfFile(fi.
pFileMap);
00370
00371
if (hFileMap)
00372 CloseHandle(hFileMap);
00373
00374
if (hFile && (hFile !=
INVALID_HANDLE_VALUE))
00375 CloseHandle(hFile);
00376
00377 Done:
00378
#if DBG
00379
if (hObj ==
NULL) {
00380 RIPMSG1(RIP_WARNING,
00381
"RtlLoadObjectFromDIBFile: Couldn't read resource from %ws",
00382 lpszName);
00383 }
00384
#endif
00385
00386
return hObj;
00387 }
00388
00389
00390
00391
00392
00393
00394
00395
00396 HICON
IconFromBestImage(
00397 ICONFILEHEADER *pifh,
00398 LPNEWHEADER lpnhSrc,
00399
int cxDesired,
00400
int cyDesired,
00401 UINT LR_flags)
00402 {
00403
UINT iImage;
00404
UINT iImageBest;
00405 LPNEWHEADER lpnhDst;
00406 LPRESDIR lprd;
00407 LPBYTE lpRes;
00408
DWORD cbDIB;
00409 HICON hIcon =
NULL;
00410 IMAGEFILEHEADER *pimh;
00411
00412
if (lpnhSrc->ResCount > 1) {
00413
00414
00415
00416
00417 lpnhDst = (LPNEWHEADER)
UserLocalAlloc(0,
00418
sizeof(NEWHEADER) + (lpnhSrc->ResCount *
sizeof(RESDIR)));
00419
00420
if (lpnhDst ==
NULL)
00421
goto Done;
00422
00423 *lpnhDst = *lpnhSrc;
00424 lprd = (LPRESDIR)(lpnhDst + 1);
00425
00426
00427
00428
00429
00430
for (pimh = pifh->imh, iImage=0;
00431 iImage < lpnhDst->ResCount;
00432 iImage++, lprd++, pimh++) {
00433
00434
00435
00436
00437 lprd->Icon.Width = pimh->cx;
00438 lprd->Icon.Height = pimh->cy;
00439
00440
if (lpnhDst->ResType == IMAGE_ICON)
00441 lprd->Icon.ColorCount = pimh->nColors;
00442
00443
00444
00445
00446
00447 lprd->Planes =
gpsi->Planes;
00448 lprd->BitCount =
gpsi->BitCount;
00449 lprd->BytesInRes = pimh->cbDIB;
00450
00451
00452
00453
00454 lprd->idIcon = (WORD)iImage;
00455 }
00456
00457
00458
00459
00460 iImageBest =
LookupIconIdFromDirectoryEx((
PBYTE)lpnhDst,
00461 (lpnhDst->ResType == IMAGE_ICON),
00462 cxDesired,
00463 cyDesired,
00464 LR_flags);
00465
00466
00467
00468
UserLocalFree(lpnhDst);
00469
00470 }
else {
00471 iImageBest = 0;
00472 }
00473
00474
00475
00476
00477 pimh = &pifh->imh[iImageBest];
00478 cbDIB = pimh->cbDIB;
00479
00480
00481
00482
00483
00484
00485
if (lpnhSrc->ResType == IMAGE_CURSOR)
00486 cbDIB +=
sizeof(POINTS);
00487
00488 lpRes = (LPBYTE)
UserLocalAlloc(0, cbDIB);
00489
if (lpRes ==
NULL)
00490
goto Done;
00491
00492
if (lpnhSrc->ResType == IMAGE_CURSOR)
00493 lpRes +=
sizeof(POINTS);
00494
00495 RtlCopyMemory(lpRes,
00496 ((LPBYTE)pifh) + pimh->offsetDIB,
00497 pimh->cbDIB);
00498
00499
if (lpnhSrc->ResType == IMAGE_CURSOR) {
00500
00501 lpRes -=
sizeof(POINTS);
00502 ((LPPOINTS)lpRes)->x = pimh->xHotSpot;
00503 ((LPPOINTS)lpRes)->y = pimh->yHotSpot;
00504 }
00505
00506 hIcon =
CreateIconFromResourceEx(lpRes,
00507 cbDIB,
00508 (lpnhSrc->ResType == IMAGE_ICON),
00509 0x00030000,
00510 cxDesired,
00511 cyDesired,
00512 LR_flags);
00513
00514
UserLocalFree(lpRes);
00515
00516 Done:
00517
00518
return hIcon;
00519 }