开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
2 Star 4 Fork 4

精易科技/eapi

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
eapi
/
cppCode
/
eapi_33_GetIconFromResource.cpp
eapi
/
cppCode
/
eapi_33_GetIconFromResource.cpp
eapi_33_GetIconFromResource.cpp 39.73 KB
一键复制 编辑 原始数据 按行查看 历史
AlongsCode 提交于 2023年02月09日 00:42 +08:00 . 部分重构
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
#include "..\include_eapi_header.h"
#include <algorithm>
/*开源库icon.h*/
namespace eapi_openlib_icon {
#pragma region 开源库代码
#pragma warning(disable :28183)
#pragma warning(disable :6387)
#pragma warning(disable :6385)
#pragma warning(disable :6386)
#pragma warning(disable :6011)
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#pragma pack( push )
#pragma pack( 2 )
typedef struct
{
BYTE bWidth;
BYTE bHeight;
BYTE bColorCount;
BYTE bReserved;
WORD wPlanes;
WORD wBitCount;
DWORD dwBytesInRes;
WORD nID;
} MEMICONDIRENTRY, * LPMEMICONDIRENTRY;
typedef struct
{
WORD idReserved;
WORD idType;
WORD idCount;
MEMICONDIRENTRY idEntries[1];
} MEMICONDIR, * LPMEMICONDIR;
#pragma pack( pop )
typedef struct
{
BYTE bWidth;
BYTE bHeight;
BYTE bColorCount;
BYTE bReserved;
WORD wPlanes;
WORD wBitCount;
DWORD dwBytesInRes;
DWORD dwImageOffset;
} ICONDIRENTRY, * LPICONDIRENTRY;
typedef struct
{
WORD idReserved;
WORD idType;
WORD idCount;
ICONDIRENTRY idEntries[1];
} ICONDIR, * LPICONDIR;
typedef struct
{
UINT Width, Height, Colors;
LPBYTE lpBits;
DWORD dwNumBytes;
LPBITMAPINFO lpbi;
LPBYTE lpXOR;
LPBYTE lpAND;
} ICONIMAGE, * LPICONIMAGE;
typedef struct
{
BOOL bHasChanged;
CHAR szOriginalICOFileName[MAX_PATH];
CHAR szOriginalDLLFileName[MAX_PATH];
UINT nNumImages;
ICONIMAGE IconImages[1];
} ICONRESOURCE, * LPICONRESOURCE;
typedef struct
{
DWORD dwBytes;
DWORD dwOffset;
} RESOURCEPOSINFO, * LPRESOURCEPOSINFO;
typedef struct
{
LPCSTR szFileName;
HINSTANCE hInstance;
LPSTR lpID;
} EXEDLLICONINFO, * LPEXEDLLICONINFO;
#define MAX_ICON_WIDTH 128
#define MIN_ICON_WIDTH 16
#define MAX_ICON_HEIGHT MAX_ICON_WIDTH
#define MIN_ICON_HEIGHT MIN_ICON_WIDTH
class MyIcon
{
public:
MyIcon();
virtual ~MyIcon();
public:
LPICONRESOURCE ReadIconFromICOFile(LPCSTR szFileName);
BOOL WriteIconToICOFile(LPICONRESOURCE lpIR, LPCSTR szFileName);
HICON MakeIconFromResource(LPICONIMAGE lpIcon);
LPICONRESOURCE ReadIconFromEXEFile(LPCSTR szFileName, LPCSTR lpID);
BOOL IconImageToClipBoard(LPICONIMAGE lpii);
BOOL IconImageFromBMPFile(LPCSTR szFileName, LPICONIMAGE lpii, BOOL bStretchToFit);
BOOL IconImageToBMPFile(LPCSTR szFileName, LPICONIMAGE lpii);
private:
UINT ReadICOHeader(HANDLE hFile);
BOOL AdjustIconImagePointers(LPICONIMAGE lpImage);
BOOL WriteICOHeader(HANDLE hFile, UINT nNumEntries);
DWORD CalculateImageOffset(LPICONRESOURCE lpIR, UINT nIndex);
BOOL DIBToIconImage(LPICONIMAGE lpii, LPBYTE lpDIB, BOOL bStretch);
};
#if _MSC_VER > 1000
#pragma once
#endif
#define WIDTHBYTES(bits) ((((bits) + 31)>>5)<<2)
class MyDib
{
public:
MyDib();
virtual ~MyDib();
public:
LPSTR FindDIBBits(LPSTR lpbi);
WORD DIBNumColors(LPSTR lpbi);
WORD PaletteSize(LPSTR lpbi);
DWORD BytesPerLine(LPBITMAPINFOHEADER lpBMIH);
LPBYTE ConvertDIBFormat(LPBITMAPINFO lpSrcDIB, UINT nWidth, UINT nHeight, UINT nColors, BOOL bStretch);
void SetMonoDIBPixel(LPBYTE pANDBits, DWORD dwWidth, DWORD dwHeight, DWORD x, DWORD y, BOOL bWhite);
LPBYTE ReadBMPFile(LPCSTR szFileName);
BOOL WriteBMPFile(LPCSTR szFileName, LPBYTE lpDIB);
private:
BOOL CopyColorTable(LPBITMAPINFO lpTarget, LPBITMAPINFO lpSource);
};
MyDib::MyDib()
{
}
MyDib::~MyDib()
{
}
LPSTR MyDib::FindDIBBits(LPSTR lpbi)
{
return (lpbi + *(LPDWORD)lpbi + PaletteSize(lpbi));
}
WORD MyDib::DIBNumColors(LPSTR lpbi)
{
WORD wBitCount;
DWORD dwClrUsed;
dwClrUsed = ((LPBITMAPINFOHEADER)lpbi)->biClrUsed;
if (dwClrUsed)
return (WORD)dwClrUsed;
wBitCount = ((LPBITMAPINFOHEADER)lpbi)->biBitCount;
switch (wBitCount)
{
case 1: return 2;
case 4: return 16;
case 8: return 256;
default:return 0;
}
return 0;
}
WORD MyDib::PaletteSize(LPSTR lpbi)
{
return (DIBNumColors(lpbi) * sizeof(RGBQUAD));
}
DWORD MyDib::BytesPerLine(LPBITMAPINFOHEADER lpBMIH)
{
return WIDTHBYTES(lpBMIH->biWidth * lpBMIH->biPlanes * lpBMIH->biBitCount);
}
LPBYTE MyDib::ConvertDIBFormat(LPBITMAPINFO lpSrcDIB, UINT nWidth, UINT nHeight, UINT nbpp, BOOL bStretch)
{
LPBITMAPINFO lpbmi = NULL;
LPBYTE lpSourceBits, lpTargetBits, lpResult;
HDC hDC = NULL, hSourceDC, hTargetDC;
HBITMAP hSourceBitmap, hTargetBitmap, hOldTargetBitmap, hOldSourceBitmap;
DWORD dwSourceBitsSize, dwTargetBitsSize, dwTargetHeaderSize;
// Allocate and fill out a BITMAPINFO struct for the new DIB
// Allow enough room for a 256-entry color table, just in case
dwTargetHeaderSize = sizeof(BITMAPINFO) + (256 * sizeof(RGBQUAD));
lpbmi = (LPBITMAPINFO)malloc(dwTargetHeaderSize);
lpbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
lpbmi->bmiHeader.biWidth = nWidth;
lpbmi->bmiHeader.biHeight = nHeight;
lpbmi->bmiHeader.biPlanes = 1;
lpbmi->bmiHeader.biBitCount = nbpp;
lpbmi->bmiHeader.biCompression = BI_RGB;
lpbmi->bmiHeader.biSizeImage = 0;
lpbmi->bmiHeader.biXPelsPerMeter = 0;
lpbmi->bmiHeader.biYPelsPerMeter = 0;
lpbmi->bmiHeader.biClrUsed = 0;
lpbmi->bmiHeader.biClrImportant = 0;
// Fill in the color table
if (!CopyColorTable(lpbmi, (LPBITMAPINFO)lpSrcDIB))
{
free(lpbmi);
return NULL;
}
// Gonna use DIBSections and BitBlt() to do the conversion, so make 'em
hDC = GetDC(NULL);
hTargetBitmap = CreateDIBSection(hDC, lpbmi, DIB_RGB_COLORS, (void**)&lpTargetBits, NULL, 0);
hSourceBitmap = CreateDIBSection(hDC, lpSrcDIB, DIB_RGB_COLORS, (void**)&lpSourceBits, NULL, 0);
hSourceDC = CreateCompatibleDC(hDC);
hTargetDC = CreateCompatibleDC(hDC);
// Flip the bits on the source DIBSection to match the source DIB
dwSourceBitsSize = lpSrcDIB->bmiHeader.biHeight * BytesPerLine(&(lpSrcDIB->bmiHeader));
dwTargetBitsSize = lpbmi->bmiHeader.biHeight * BytesPerLine(&(lpbmi->bmiHeader));
memcpy(lpSourceBits, FindDIBBits((LPSTR)lpSrcDIB), dwSourceBitsSize);
// Select DIBSections into DCs
hOldSourceBitmap = (HBITMAP)SelectObject(hSourceDC, hSourceBitmap);
hOldTargetBitmap = (HBITMAP)SelectObject(hTargetDC, hTargetBitmap);
// Set the color tables for the DIBSections
if (lpSrcDIB->bmiHeader.biBitCount <= 8)
SetDIBColorTable(hSourceDC, 0, 1 << lpSrcDIB->bmiHeader.biBitCount, lpSrcDIB->bmiColors);
if (lpbmi->bmiHeader.biBitCount <= 8)
SetDIBColorTable(hTargetDC, 0, 1 << lpbmi->bmiHeader.biBitCount, lpbmi->bmiColors);
// If we are asking for a straight copy, do it
if ((lpSrcDIB->bmiHeader.biWidth == lpbmi->bmiHeader.biWidth) && (lpSrcDIB->bmiHeader.biHeight == lpbmi->bmiHeader.biHeight))
{
BitBlt(hTargetDC, 0, 0, lpbmi->bmiHeader.biWidth, lpbmi->bmiHeader.biHeight, hSourceDC, 0, 0, SRCCOPY);
}
else
{
// else, should we stretch it?
if (bStretch)
{
SetStretchBltMode(hTargetDC, COLORONCOLOR);
StretchBlt(hTargetDC, 0, 0, lpbmi->bmiHeader.biWidth, lpbmi->bmiHeader.biHeight, hSourceDC, 0, 0, lpSrcDIB->bmiHeader.biWidth, lpSrcDIB->bmiHeader.biHeight, SRCCOPY);
}
else
{
// or just take the upper left corner of the source
BitBlt(hTargetDC, 0, 0, lpbmi->bmiHeader.biWidth, lpbmi->bmiHeader.biHeight, hSourceDC, 0, 0, SRCCOPY);
}
}
// Clean up and delete the DCs
SelectObject(hSourceDC, hOldSourceBitmap);
SelectObject(hSourceDC, hOldTargetBitmap);
DeleteDC(hSourceDC);
DeleteDC(hTargetDC);
ReleaseDC(NULL, hDC);
// Flush the GDI batch, so we can play with the bits
GdiFlush();
// Allocate enough memory for the new CF_DIB, and copy bits
lpResult = (LPBYTE)malloc(dwTargetHeaderSize + dwTargetBitsSize);
memcpy(lpResult, lpbmi, dwTargetHeaderSize);
memcpy(FindDIBBits((LPSTR)lpResult), lpTargetBits, dwTargetBitsSize);
// final cleanup
DeleteObject(hTargetBitmap);
DeleteObject(hSourceBitmap);
free(lpbmi);
return lpResult;
}
BOOL MyDib::CopyColorTable(LPBITMAPINFO lpTarget, LPBITMAPINFO lpSource)
{
// What we do depends on the target's color depth
switch (lpTarget->bmiHeader.biBitCount)
{
// 8bpp - need 256 entry color table
case 8:
if (lpSource->bmiHeader.biBitCount == 8)
{ // Source is 8bpp too, copy color table
memcpy(lpTarget->bmiColors, lpSource->bmiColors, 256 * sizeof(RGBQUAD));
return TRUE;
}
else
{ // Source is != 8bpp, use halftone palette
HPALETTE hPal;
HDC hDC = GetDC(NULL);
PALETTEENTRY pe[256];
UINT i;
hPal = CreateHalftonePalette(hDC);
ReleaseDC(NULL, hDC);
GetPaletteEntries(hPal, 0, 256, pe);
DeleteObject(hPal);
for (i = 0; i < 256; i++)
{
lpTarget->bmiColors[i].rgbRed = pe[i].peRed;
lpTarget->bmiColors[i].rgbGreen = pe[i].peGreen;
lpTarget->bmiColors[i].rgbBlue = pe[i].peBlue;
lpTarget->bmiColors[i].rgbReserved = pe[i].peFlags;
}
return TRUE;
}
break; // end 8bpp
// 4bpp - need 16 entry color table
case 4:
if (lpSource->bmiHeader.biBitCount == 4)
{ // Source is 4bpp too, copy color table
memcpy(lpTarget->bmiColors, lpSource->bmiColors, 16 * sizeof(RGBQUAD));
return TRUE;
}
else
{ // Source is != 4bpp, use system palette
HPALETTE hPal;
PALETTEENTRY pe[256];
UINT i;
hPal = (HPALETTE)GetStockObject(DEFAULT_PALETTE);
GetPaletteEntries(hPal, 0, 16, pe);
for (i = 0; i < 16; i++)
{
lpTarget->bmiColors[i].rgbRed = pe[i].peRed;
lpTarget->bmiColors[i].rgbGreen = pe[i].peGreen;
lpTarget->bmiColors[i].rgbBlue = pe[i].peBlue;
lpTarget->bmiColors[i].rgbReserved = pe[i].peFlags;
}
return TRUE;
}
break; // end 4bpp
// 1bpp - need 2 entry mono color table
case 1:
lpTarget->bmiColors[0].rgbRed = 0;
lpTarget->bmiColors[0].rgbGreen = 0;
lpTarget->bmiColors[0].rgbBlue = 0;
lpTarget->bmiColors[0].rgbReserved = 0;
lpTarget->bmiColors[1].rgbRed = 255;
lpTarget->bmiColors[1].rgbGreen = 255;
lpTarget->bmiColors[1].rgbBlue = 255;
lpTarget->bmiColors[1].rgbReserved = 0;
break; // end 1bpp
// no color table for the > 8bpp modes
case 32:
case 24:
case 16:
default:
return TRUE;
break;
}
return TRUE;
}
void MyDib::SetMonoDIBPixel(LPBYTE pANDBits, DWORD dwWidth, DWORD dwHeight, DWORD x, DWORD y, BOOL bWhite)
{
DWORD ByteIndex;
BYTE BitNumber;
// Find the byte on which this scanline begins
ByteIndex = (dwHeight - y - 1) * WIDTHBYTES(dwWidth);
// Find the byte containing this pixel
ByteIndex += (x >> 3);
// Which bit is it?
BitNumber = (BYTE)(7 - (x % 8));
if (bWhite)
// Turn it on
pANDBits[ByteIndex] |= (1 << BitNumber);
else
// Turn it off
pANDBits[ByteIndex] &= ~(1 << BitNumber);
}
LPBYTE MyDib::ReadBMPFile(LPCSTR szFileName)
{
HANDLE hFile;
BITMAPFILEHEADER bfh;
DWORD dwBytes;
LPBYTE lpDIB = NULL, lpTemp = NULL;
WORD wPaletteSize = 0;
DWORD dwBitsSize = 0;
// Open the file
if ((hFile = CreateFileA(szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
{
return NULL;
}
// Read the header
if ((!ReadFile(hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwBytes, NULL)) || (dwBytes != sizeof(BITMAPFILEHEADER)))
{
CloseHandle(hFile);
return NULL;
}
// Does it look like a BMP file?
if ((bfh.bfType != 0x4d42) || (bfh.bfReserved1 != 0) || (bfh.bfReserved2 != 0))
{
CloseHandle(hFile);
return NULL;
}
// Allocate some memory
if ((lpDIB = (LPBYTE)malloc(sizeof(BITMAPINFO))) == NULL)
{
CloseHandle(hFile);
return NULL;
}
// Read in the BITMAPINFOHEADER
if ((!ReadFile(hFile, lpDIB, sizeof(BITMAPINFOHEADER), &dwBytes, NULL)) || (dwBytes != sizeof(BITMAPINFOHEADER)))
{
CloseHandle(hFile);
free(lpDIB);
return NULL;
}
if (((LPBITMAPINFOHEADER)lpDIB)->biSize != sizeof(BITMAPINFOHEADER))
{
CloseHandle(hFile);
free(lpDIB);
return NULL;
}
// How big are the elements?
wPaletteSize = PaletteSize((LPSTR)lpDIB);
dwBitsSize = ((LPBITMAPINFOHEADER)lpDIB)->biHeight * BytesPerLine((LPBITMAPINFOHEADER)lpDIB);
// realloc to account for the total size of the DIB
if ((lpTemp = (LPBYTE)realloc(lpDIB, sizeof(BITMAPINFOHEADER) + wPaletteSize + dwBitsSize)) == NULL)
{
CloseHandle(hFile);
free(lpDIB);
return NULL;
}
lpDIB = lpTemp;
// If there is a color table, read it
if (wPaletteSize != 0)
{
if ((!ReadFile(hFile, ((LPBITMAPINFO)lpDIB)->bmiColors, wPaletteSize, &dwBytes, NULL)) || (dwBytes != wPaletteSize))
{
CloseHandle(hFile);
free(lpDIB);
return NULL;
}
}
// Seek to the bits
// checking against 0 in case some bogus app didn't set this element
if (bfh.bfOffBits != 0)
{
if (SetFilePointer(hFile, bfh.bfOffBits, NULL, FILE_BEGIN) == 0xffffffff)
{
CloseHandle(hFile);
free(lpDIB);
return NULL;
}
}
// Read the image bits
if ((!ReadFile(hFile, FindDIBBits((LPSTR)lpDIB), dwBitsSize, &dwBytes, NULL)) || (dwBytes != dwBitsSize))
{
CloseHandle(hFile);
free(lpDIB);
return NULL;
}
// clean up
CloseHandle(hFile);
return lpDIB;
}
BOOL MyDib::WriteBMPFile(LPCSTR szFileName, LPBYTE lpDIB)
{
HANDLE hFile;
BITMAPFILEHEADER bfh;
DWORD dwBytes, dwBytesToWrite;
LPBITMAPINFOHEADER lpbmih;
// Open the file
if ((hFile = CreateFileA(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
{
return FALSE;
}
bfh.bfType = 0x4d42;
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + PaletteSize((LPSTR)lpDIB);
bfh.bfSize = (bfh.bfOffBits + ((LPBITMAPINFOHEADER)lpDIB)->biHeight * BytesPerLine((LPBITMAPINFOHEADER)lpDIB)) / 4;
// Write the header
if ((!WriteFile(hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwBytes, NULL)) || (dwBytes != sizeof(BITMAPFILEHEADER)))
{
CloseHandle(hFile);
return FALSE;
}
lpbmih = (LPBITMAPINFOHEADER)lpDIB;
// lpbmih->biHeight /= 2;
dwBytesToWrite = bfh.bfOffBits + (lpbmih->biHeight * BytesPerLine(lpbmih));
if ((!WriteFile(hFile, lpDIB, dwBytesToWrite, &dwBytes, NULL)) || (dwBytes != dwBytesToWrite))
{
CloseHandle(hFile);
return FALSE;
}
lpbmih->biHeight *= 2;
CloseHandle(hFile);
return TRUE;
}
MyDib* pDib;
MyIcon::MyIcon()
{
}
MyIcon::~MyIcon()
{
}
HICON MyIcon::MakeIconFromResource(LPICONIMAGE lpIcon)
{
HICON hIcon = NULL;
// Sanity Check
if (lpIcon == NULL)
return NULL;
if (lpIcon->lpBits == NULL)
return NULL;
// Let the OS do the real work :)
hIcon = CreateIconFromResourceEx(lpIcon->lpBits, lpIcon->dwNumBytes, TRUE, 0x00030000,
(*(LPBITMAPINFOHEADER)(lpIcon->lpBits)).biWidth, (*(LPBITMAPINFOHEADER)(lpIcon->lpBits)).biHeight / 2, 0);
// It failed, odds are good we're on NT so try the non-Ex way
if (hIcon == NULL)
{
// We would break on NT if we try with a 16bpp image
if (lpIcon->lpbi->bmiHeader.biBitCount != 16)
{
hIcon = CreateIconFromResource(lpIcon->lpBits, lpIcon->dwNumBytes, TRUE, 0x00030000);
}
}
return hIcon;
}
/* End MakeIconFromResource() **********************************************/
/****************************************************************************
*
* FUNCTION: ReadIconFromICOFile
*
* PURPOSE: Reads an Icon Resource from an ICO file
*
* PARAMS: LPCSTR szFileName - Name of the ICO file
*
* RETURNS: LPICONRESOURCE - pointer to the resource, NULL for failure
*
*
\****************************************************************************/
LPICONRESOURCE MyIcon::ReadIconFromICOFile(LPCSTR szFileName)
{
LPICONRESOURCE lpIR = NULL, lpNew = NULL;
HANDLE hFile = NULL;
LPRESOURCEPOSINFO lpRPI = NULL;
UINT i;
DWORD dwBytesRead;
LPICONDIRENTRY lpIDE = NULL;
// Open the file
if ((hFile = CreateFileA(szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "ͼ���ļ���ɦ����!", szFileName, MB_OK);
return NULL;
}
// Allocate memory for the resource structure
if ((lpIR = (LPICONRESOURCE)malloc(sizeof(ICONRESOURCE))) == NULL)
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "ͼ������ڴ�ɦ����!", szFileName, MB_OK);
CloseHandle(hFile);
return NULL;
}
// Read in the header
if ((lpIR->nNumImages = ReadICOHeader(hFile)) == (UINT)-1)
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "��ͼ���ļ�ͷɦ����!", szFileName, MB_OK);
CloseHandle(hFile);
free(lpIR);
return NULL;
}
// Adjust the size of the struct to account for the images
if ((lpNew = (LPICONRESOURCE)realloc(lpIR, sizeof(ICONRESOURCE) + ((lpIR->nNumImages - 1) * sizeof(ICONIMAGE)))) == NULL)
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "ͼ���ڴ����·������!", szFileName, MB_OK);
CloseHandle(hFile);
free(lpIR);
return NULL;
}
lpIR = lpNew;
// Store the original name
lstrcpyA(lpIR->szOriginalICOFileName, szFileName);
lstrcpyA(lpIR->szOriginalDLLFileName, "");
// Allocate enough memory for the icon directory entries
if ((lpIDE = (LPICONDIRENTRY)malloc(lpIR->nNumImages * sizeof(ICONDIRENTRY))) == NULL)
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "ͼ���ļ��ڴ����·������!", szFileName, MB_OK);
CloseHandle(hFile);
free(lpIR);
return NULL;
}
// Read in the icon directory entries
if (!ReadFile(hFile, lpIDE, lpIR->nNumImages * sizeof(ICONDIRENTRY), &dwBytesRead, NULL))
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "��ͼ���ļ�ɦ����!", szFileName, MB_OK);
CloseHandle(hFile);
free(lpIR);
return NULL;
}
if (dwBytesRead != lpIR->nNumImages * sizeof(ICONDIRENTRY))
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "��ͼ���ļ�ɦ����!", szFileName, MB_OK);
CloseHandle(hFile);
free(lpIR);
return NULL;
}
// Loop through and read in each image
for (i = 0; i < lpIR->nNumImages; i++)
{
// Allocate memory for the resource
if ((lpIR->IconImages[i].lpBits = (LPBYTE)malloc(lpIDE[i].dwBytesInRes)) == NULL)
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "ͼ���ڴ����·������!", szFileName, MB_OK);
CloseHandle(hFile);
free(lpIR);
free(lpIDE);
return NULL;
}
lpIR->IconImages[i].dwNumBytes = lpIDE[i].dwBytesInRes;
// Seek to beginning of this image
if (SetFilePointer(hFile, lpIDE[i].dwImageOffset, NULL, FILE_BEGIN) == 0xFFFFFFFF)
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "ͼ�����ݶ�λɦ����!", szFileName, MB_OK);
CloseHandle(hFile);
free(lpIR);
free(lpIDE);
return NULL;
}
// Read it in
if (!ReadFile(hFile, lpIR->IconImages[i].lpBits, lpIDE[i].dwBytesInRes, &dwBytesRead, NULL))
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "��ͼ�����ݳ���!", szFileName, MB_OK);
CloseHandle(hFile);
free(lpIR);
free(lpIDE);
return NULL;
}
if (dwBytesRead != lpIDE[i].dwBytesInRes)
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "��ͼ�����ݳ���!", szFileName, MB_OK);
CloseHandle(hFile);
free(lpIDE);
free(lpIR);
return NULL;
}
// Set the internal pointers appropriately
if (!AdjustIconImagePointers(&(lpIR->IconImages[i])))
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "ͼ���ڲ���ʽת������!", szFileName, MB_OK);
CloseHandle(hFile);
free(lpIDE);
free(lpIR);
return NULL;
}
}
// Clean up
free(lpIDE);
free(lpRPI);
CloseHandle(hFile);
return lpIR;
}
/* End ReadIconFromICOFile() **********************************************/
/****************************************************************************
*
* FUNCTION: AdjustIconImagePointers
*
* PURPOSE: Adjusts internal pointers in icon resource struct
*
* PARAMS: LPICONIMAGE lpImage - the resource to handle
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL MyIcon::AdjustIconImagePointers(LPICONIMAGE lpImage)
{
// Sanity check
if (lpImage == NULL)
return FALSE;
// BITMAPINFO is at beginning of bits
lpImage->lpbi = (LPBITMAPINFO)lpImage->lpBits;
// Width - simple enough
lpImage->Width = lpImage->lpbi->bmiHeader.biWidth;
// Icons are stored in funky format where height is doubled - account for it
lpImage->Height = (lpImage->lpbi->bmiHeader.biHeight) / 2;
// How many colors?
lpImage->Colors = lpImage->lpbi->bmiHeader.biPlanes * lpImage->lpbi->bmiHeader.biBitCount;
// XOR bits follow the header and color table
lpImage->lpXOR = (LPBYTE)pDib->FindDIBBits((LPSTR)lpImage->lpbi);
// AND bits follow the XOR bits
lpImage->lpAND = lpImage->lpXOR + (lpImage->Height * pDib->BytesPerLine((LPBITMAPINFOHEADER)(lpImage->lpbi)));
return TRUE;
}
/* End AdjustIconImagePointers() *******************************************/
/****************************************************************************
*
* FUNCTION: ReadICOHeader
*
* PURPOSE: Reads the header from an ICO file
*
* PARAMS: HANDLE hFile - handle to the file
*
* RETURNS: UINT - Number of images in file, -1 for failure
*
*
\****************************************************************************/
UINT MyIcon::ReadICOHeader(HANDLE hFile)
{
WORD Input;
DWORD dwBytesRead;
// Read the 'reserved' WORD
if (!ReadFile(hFile, &Input, sizeof(WORD), &dwBytesRead, NULL))
return (UINT)-1;
// Did we get a WORD?
if (dwBytesRead != sizeof(WORD))
return (UINT)-1;
// Was it 'reserved' ? (ie 0)
if (Input != 0)
return (UINT)-1;
// Read the type WORD
if (!ReadFile(hFile, &Input, sizeof(WORD), &dwBytesRead, NULL))
return (UINT)-1;
// Did we get a WORD?
if (dwBytesRead != sizeof(WORD))
return (UINT)-1;
// Was it type 1?
if (Input != 1)
return (UINT)-1;
// Get the count of images
if (!ReadFile(hFile, &Input, sizeof(WORD), &dwBytesRead, NULL))
return (UINT)-1;
// Did we get a WORD?
if (dwBytesRead != sizeof(WORD))
return (UINT)-1;
// Return the count
return Input;
}
/* End ReadICOHeader() ****************************************************/
/****************************************************************************
*
* FUNCTION: GetIconFromInstance
*
* PURPOSE: Callback for enumerating resources in a DLL/EXE
*
* PARAMS: HINSTANCE hInstance - Instance handle for this module
* LPSTR nIndex - Resource index
*
* RETURNS: HICON - Handle to the icon, NULL for failure
*
*
\****************************************************************************/
/* End GetIconFromInstance() ***********************************************/
/****************************************************************************
*
* FUNCTION: ReadIconFromEXEFile
*
* PURPOSE: Load an Icon Resource from a DLL/EXE file
*
* PARAMS: LPCSTR szFileName - name of DLL/EXE file
* LPCSTR lpID - Index of DLL/EXE file
*
* RETURNS: LPICONRESOURCE - pointer to icon resource
*
*
\****************************************************************************/
LPICONRESOURCE MyIcon::ReadIconFromEXEFile(LPCSTR szFileName, LPCSTR lpID)
{
LPICONRESOURCE lpIR = NULL, lpNew = NULL;
HINSTANCE hLibrary;
EXEDLLICONINFO EDII;
// Load the DLL/EXE - NOTE: must be a 32bit EXE/DLL for this to work
if ((hLibrary = LoadLibraryExA(szFileName, NULL, LOAD_LIBRARY_AS_DATAFILE)) == NULL)
{
// Failed to load - abort
//MessageBox(AfxGetMainWnd()->m_hWnd, "װ���ļ�ɦ���� - ��ѡ��һ��WIN32��DLL��EXE�ļ�!", szFileName, MB_OK);
return NULL;
}
// Store the info
EDII.szFileName = szFileName;
EDII.hInstance = hLibrary;
// Ask the user, "Which Icon?"
if (lpID != NULL)
{
HRSRC hRsrc = NULL;
HGLOBAL hGlobal = NULL;
LPMEMICONDIR lpIcon = NULL;
UINT i;
// Find the group icon resource
if ((hRsrc = FindResourceA(hLibrary, lpID, MAKEINTRESOURCEA((ULONG_PTR)(RT_ICON)+DIFFERENCE))) == NULL)
{
FreeLibrary(hLibrary);
return NULL;
}
if ((hGlobal = LoadResource(hLibrary, hRsrc)) == NULL)
{
FreeLibrary(hLibrary);
return NULL;
}
if ((lpIcon = (LPMEMICONDIR)LockResource(hGlobal)) == NULL)
{
FreeLibrary(hLibrary);
return NULL;
}
// Allocate enough memory for the images
if ((lpIR = (LPICONRESOURCE)malloc(sizeof(ICONRESOURCE) + ((lpIcon->idCount - 1) * sizeof(ICONIMAGE)))) == NULL)
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "�ڴ�������!", szFileName, MB_OK);
FreeLibrary(hLibrary);
return NULL;
}
// Fill in local struct members
lpIR->nNumImages = lpIcon->idCount;
lstrcpyA(lpIR->szOriginalDLLFileName, szFileName);
lstrcpyA(lpIR->szOriginalICOFileName, "");
// Loop through the images
for (i = 0; i < lpIR->nNumImages; i++)
{
// Get the individual image
if ((hRsrc = FindResource(hLibrary, MAKEINTRESOURCE(lpIcon->idEntries[i].nID), RT_ICON)) == NULL)
{
free(lpIR);
FreeLibrary(hLibrary);
return NULL;
}
if ((hGlobal = LoadResource(hLibrary, hRsrc)) == NULL)
{
free(lpIR);
FreeLibrary(hLibrary);
return NULL;
}
// Store a copy of the resource locally
lpIR->IconImages[i].dwNumBytes = SizeofResource(hLibrary, hRsrc);
lpIR->IconImages[i].lpBits = (LPBYTE)malloc(lpIR->IconImages[i].dwNumBytes);
memcpy(lpIR->IconImages[i].lpBits, LockResource(hGlobal), lpIR->IconImages[i].dwNumBytes);
// Adjust internal pointers
if (!AdjustIconImagePointers(&(lpIR->IconImages[i])))
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "ת����ͼ���ڲ���ʽɦ����!", szFileName, MB_OK);
free(lpIR);
FreeLibrary(hLibrary);
return NULL;
}
}
}
FreeLibrary(hLibrary);
return lpIR;
}
/* End ReadIconFromEXEFile() ************************************************/
/****************************************************************************
*
* FUNCTION: WriteICOHeader
*
* PURPOSE: Writes the header to an ICO file
*
* PARAMS: HANDLE hFile - handle to the file
* UINT nNumEntries - Number of images in file
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL MyIcon::WriteICOHeader(HANDLE hFile, UINT nNumEntries)
{
WORD Output;
DWORD dwBytesWritten;
// Write 'reserved' WORD
Output = 0;
if (!WriteFile(hFile, &Output, sizeof(WORD), &dwBytesWritten, NULL))
return FALSE;
// Did we write a WORD?
if (dwBytesWritten != sizeof(WORD))
return FALSE;
// Write 'type' WORD (1)
Output = 1;
if (!WriteFile(hFile, &Output, sizeof(WORD), &dwBytesWritten, NULL))
return FALSE;
// Did we write a WORD?
if (dwBytesWritten != sizeof(WORD))
return FALSE;
// Write Number of Entries
Output = (WORD)nNumEntries;
if (!WriteFile(hFile, &Output, sizeof(WORD), &dwBytesWritten, NULL))
return FALSE;
// Did we write a WORD?
if (dwBytesWritten != sizeof(WORD))
return FALSE;
return TRUE;
}
/* End WriteICOHeader() ****************************************************/
/****************************************************************************
*
* FUNCTION: CalculateImageOffset
*
* PURPOSE: Calculates the file offset for an icon image
*
* PARAMS: LPICONRESOURCE lpIR - pointer to icon resource
* UINT nIndex - which image?
*
* RETURNS: DWORD - the file offset for that image
*
*
\****************************************************************************/
DWORD MyIcon::CalculateImageOffset(LPICONRESOURCE lpIR, UINT nIndex)
{
DWORD dwSize;
UINT i;
// Calculate the ICO header size
dwSize = 3 * sizeof(WORD);
// Add the ICONDIRENTRY's
dwSize += lpIR->nNumImages * sizeof(ICONDIRENTRY);
// Add the sizes of the previous images
for (i = 0; i < nIndex; i++)
dwSize += lpIR->IconImages[i].dwNumBytes;
// we're there - return the number
return dwSize;
}
/* End CalculateImageOffset() ***********************************************/
/****************************************************************************
*
* FUNCTION: WriteIconToICOFile
*
* PURPOSE: Writes the icon resource data to an ICO file
*
* PARAMS: LPICONRESOURCE lpIR - pointer to icon resource
* LPCSTR szFileName - name for the ICO file
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL MyIcon::WriteIconToICOFile(LPICONRESOURCE lpIR, LPCSTR szFileName)
{
HANDLE hFile;
UINT i;
DWORD dwBytesWritten;
// open the file
if ((hFile = CreateFileA(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "�ļ�����ɦ����!", szFileName, MB_OK);
return FALSE;
}
// Write the header
if (!WriteICOHeader(hFile, lpIR->nNumImages))
{
//MessageBox(AfxGetMainWnd()->m_hWnd, "дͼ���ļ�ͷɦ����!", szFileName, MB_OK);
CloseHandle(hFile);
return FALSE;
}
// Write the ICONDIRENTRY's
for (i = 0; i < lpIR->nNumImages; i++)
{
ICONDIRENTRY ide;
// Convert internal format to ICONDIRENTRY
ide.bWidth = lpIR->IconImages[i].Width;
ide.bHeight = lpIR->IconImages[i].Height;
ide.bReserved = 0;
ide.wPlanes = lpIR->IconImages[i].lpbi->bmiHeader.biPlanes;
ide.wBitCount = lpIR->IconImages[i].lpbi->bmiHeader.biBitCount;
if ((ide.wPlanes * ide.wBitCount) >= 8)
ide.bColorCount = 0;
else
ide.bColorCount = 1 << (ide.wPlanes * ide.wBitCount);
ide.dwBytesInRes = lpIR->IconImages[i].dwNumBytes;
ide.dwImageOffset = CalculateImageOffset(lpIR, i);
// Write the ICONDIRENTRY out to disk
if (!WriteFile(hFile, &ide, sizeof(ICONDIRENTRY), &dwBytesWritten, NULL))
return FALSE;
// Did we write a full ICONDIRENTRY ?
if (dwBytesWritten != sizeof(ICONDIRENTRY))
return FALSE;
}
// Write the image bits for each image
for (i = 0; i < lpIR->nNumImages; i++)
{
DWORD dwTemp = lpIR->IconImages[i].lpbi->bmiHeader.biSizeImage;
// Set the sizeimage member to zero
lpIR->IconImages[i].lpbi->bmiHeader.biSizeImage = 0;
// Write the image bits to file
if (!WriteFile(hFile, lpIR->IconImages[i].lpBits, lpIR->IconImages[i].dwNumBytes, &dwBytesWritten, NULL))
return FALSE;
if (dwBytesWritten != lpIR->IconImages[i].dwNumBytes)
return FALSE;
// set it back
lpIR->IconImages[i].lpbi->bmiHeader.biSizeImage = dwTemp;
}
CloseHandle(hFile);
return FALSE;
}
/* End WriteIconToICOFile() **************************************************/
/****************************************************************************
*
* FUNCTION: IconImageToClipBoard
*
* PURPOSE: Copies an icon image to the clipboard in CF_DIB format
*
* PARAMS: LPICONIMAGE lpii - pointer to icon image data
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL MyIcon::IconImageToClipBoard(LPICONIMAGE lpii)
{
HANDLE hGlobal;
LPSTR lpBits;
// Open the clipboard
if (OpenClipboard(0))
{
// empty it
if (EmptyClipboard())
{
if (lpii->dwNumBytes == NULL)
return false;
// Make a buffer to send to clipboard
hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, lpii->dwNumBytes);
lpBits = (LPSTR)GlobalLock(hGlobal);
// Copy the bits to the buffer
memcpy(lpBits, lpii->lpBits, lpii->dwNumBytes);
// Adjust for funky height*2 thing
((LPBITMAPINFOHEADER)lpBits)->biHeight /= 2;
GlobalUnlock(hGlobal);
// Send it to the clipboard
SetClipboardData(CF_DIB, hGlobal);
CloseClipboard();
return TRUE;
}
}
return FALSE;
}
BOOL MyIcon::DIBToIconImage(LPICONIMAGE lpii, LPBYTE lpDIB, BOOL bStretch)
{
LPBYTE lpNewDIB;
// Sanity check
if (lpDIB == NULL)
return FALSE;
// Let the DIB engine convert color depths if need be
lpNewDIB = pDib->ConvertDIBFormat((LPBITMAPINFO)lpDIB, lpii->Width, lpii->Height, lpii->Colors, bStretch);
// Now we have a cool new DIB of the proper size/color depth
// Lets poke it into our data structures and be done with it
// How big is it?
lpii->dwNumBytes = sizeof(BITMAPINFOHEADER) // Header
+ pDib->PaletteSize((LPSTR)lpNewDIB) // Palette
+ lpii->Height * pDib->BytesPerLine((LPBITMAPINFOHEADER)lpNewDIB) // XOR mask
+ lpii->Height * WIDTHBYTES(lpii->Width); // AND mask
// If there was already an image here, free it
if (lpii->lpBits != NULL)
free(lpii->lpBits);
// Allocate enough room for the new image
if ((lpii->lpBits = (LPBYTE)malloc(lpii->dwNumBytes)) == NULL)
{
free(lpii);
return FALSE;
}
// Copy the bits
memcpy(lpii->lpBits, lpNewDIB, sizeof(BITMAPINFOHEADER) + pDib->PaletteSize((LPSTR)lpNewDIB));
// Adjust internal pointers/variables for new image
lpii->lpbi = (LPBITMAPINFO)(lpii->lpBits);
lpii->lpbi->bmiHeader.biHeight *= 2;
lpii->lpXOR = (LPBYTE)pDib->FindDIBBits((LPSTR)(lpii->lpBits));
memcpy(lpii->lpXOR, pDib->FindDIBBits((LPSTR)lpNewDIB), lpii->Height * pDib->BytesPerLine((LPBITMAPINFOHEADER)lpNewDIB));
lpii->lpAND = lpii->lpXOR + lpii->Height * pDib->BytesPerLine((LPBITMAPINFOHEADER)lpNewDIB);
memset(lpii->lpAND, 0, lpii->Height * WIDTHBYTES(lpii->Width));
// Free the source
free(lpNewDIB);
return TRUE;
}
/* End DIBToIconImage() ***************************************************/
/****************************************************************************
*
* FUNCTION: IconImageFromBMPFile
*
* PURPOSE: Creates an icon image from a BMP file
*
* PARAMS: LPCSTR szFileName - Filename for BMP file
* LPICONIMAGE lpii - pointer to icon image data
* BOOL bStretchToFit - TRUE to stretch, FALSE to take
* the upper left corner of the DIB
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL MyIcon::IconImageFromBMPFile(LPCSTR szFileName, LPICONIMAGE lpii, BOOL bStretchToFit)
{
LPBYTE lpDIB = NULL;
BOOL bRet = FALSE;
if ((lpDIB = pDib->ReadBMPFile(szFileName)) == NULL)
return FALSE;
// Convert it to an icon image
bRet = DIBToIconImage(lpii, lpDIB, bStretchToFit);
free(lpDIB);
return bRet;
}
/* End IconImageFromBMPFile() ********************************************/
/****************************************************************************
*
* FUNCTION: IconImageToBMPFile
*
* PURPOSE: Creates BMP file from an icon image
*
* PARAMS: LPCSTR szFileName - Filename for BMP file
* LPICONIMAGE lpii - pointer to icon image data
*
* RETURNS: BOOL - TRUE for success, FALSE for failure
*
*
\****************************************************************************/
BOOL MyIcon::IconImageToBMPFile(LPCSTR szFileName, LPICONIMAGE lpii)
{
return pDib->WriteBMPFile(szFileName, (LPBYTE)lpii->lpbi);
}
#pragma endregion
#pragma region 易需要的代码
inline std::string TakeUnifiedExtName(std::string paht) {
std::string result(paht);
std::transform(paht.begin(), paht.end(), result.begin(), tolower);
if (!result.empty())
{
result = result.substr(result.rfind(".") + 1);
}
return result;
}
typedef struct ICONINDEXMAT
{
DWORD TargetIndex;
DWORD Count;
char* IndexId;
}*LPICONINDEXMAT;
char* Mystrdup(const char* str)
{
int len = 0;
const char* pstr = str;
while (*pstr++ != '0円')
{
len++;
}
char* pdest = (char*)malloc(len + 1);
if (!pdest)
{
return nullptr;
}
int word_num = len / sizeof(size_t);
int byte_num = len % sizeof(size_t);
const size_t* wsrc = (size_t*)str;
size_t* wdest = (size_t*)pdest;
while (word_num--)
{
*wdest++ = *wsrc++;
}
const char* bsrc = (char*)wsrc;
char* bdest = (char*)wdest;
while (byte_num--)
{
*bdest++ = *bsrc++;
}
pdest[len] = '0円';
return pdest;
}
BOOL CALLBACK MyEnumProcedure(HMODULE hModule, LPCSTR lpType, LPSTR lpName, LONG_PTR lParam)
{
LPICONINDEXMAT MatchObj = (LPICONINDEXMAT)lParam;//减少全局变量使用
if (++MatchObj->Count == MatchObj->TargetIndex)
{
if (IS_INTRESOURCE(lpName) == FALSE) {
MatchObj->IndexId = Mystrdup(lpName);//拷贝构造,因为ID有可能时整数,所以没办法用string,难受!!
}
else
{
MatchObj->IndexId = (char*)(int)lpName;
}
}
return 1;
}
//释放存在些问题,拷贝以后返回的函数并没有去释放,考虑运行周期和命令使用频率,拷贝的字符串内存长度也很小,不会出问题
int GetIconFromResource(std::string PathName, bool Extract2ClipBoard, std::string SaveName, DWORD _Index) {
LPICONRESOURCE ObjIcon = NULL;
MyIcon pIcon;
ICONINDEXMAT MatchObj = { _Index,0,(char*)-1 };
std::string ExtName = TakeUnifiedExtName(PathName);
if (ExtName == "ico")
{
ObjIcon = pIcon.ReadIconFromICOFile(PathName.c_str());
++MatchObj.Count;
}
else {
if (ExtName == "exe" || ExtName == "dll" || ExtName == "icl")
{
HMODULE hLibModule = LoadLibraryExA(PathName.c_str(), 0, LOAD_LIBRARY_AS_DATAFILE);
if (hLibModule)
{
if (!EnumResourceNamesA(hLibModule, MAKEINTRESOURCEA((ULONG_PTR)(RT_ICON)+DIFFERENCE), (ENUMRESNAMEPROCA)MyEnumProcedure, (LONG_PTR)&MatchObj))
{
FreeLibrary(hLibModule);
return 0;
}
FreeLibrary(hLibModule);
if (MatchObj.IndexId != (char*)-1)
{
ObjIcon = pIcon.ReadIconFromEXEFile(PathName.c_str(), MatchObj.IndexId);
}
}
}
}
if (!ObjIcon)
{
return 0;
}
if (MatchObj.Count > 0 && MatchObj.TargetIndex <= MatchObj.Count)
{
std::string SaveExtName = TakeUnifiedExtName(SaveName);
if (Extract2ClipBoard)//到剪辑板
{
if (ObjIcon)
{
pIcon.IconImageToClipBoard(ObjIcon->IconImages);
}
}
if (!SaveName.empty()) {//到文件
if (SaveExtName == "ico" && ObjIcon) {
pIcon.WriteIconToICOFile(ObjIcon, SaveName.c_str());
}
else if (SaveExtName == "bmp" && ObjIcon)
{
pIcon.IconImageToBMPFile(SaveName.c_str(), ObjIcon->IconImages);
}
}//?是否需要到字节集??
}
if (ObjIcon)
{
if (ObjIcon->nNumImages > 0)//读入的图标数量大于0
{
for (UINT i = 0; i < ObjIcon->nNumImages; i++)
{
free(ObjIcon->IconImages[i].lpBits);
}
}
free(ObjIcon);
}
return MatchObj.Count;
}//
#pragma endregion
}
// 调用格式: SDT_INT 提取资源文件图标, 命令说明: "从指定的资源文件中提取图标,并保存为ICO、BMP文件或复制到剪贴板。成功返回图标数量;失败返回0。"
// 参数<1>: 资源文件名 SDT_TEXT, 参数说明: "要提取图标的资源文件名。扩展名为EXE、DLL、ICL、ICO。"
// 参数<2>: [是否提取到剪辑板 SDT_INT], 参数说明: "0为假其余为真,兼容之前易库,默认为假"
// 参数<3>: [保存文件名 SDT_TEXT], 参数说明: "如需要提取到文件则填写提取的图标要保存的文件名。扩展名为ICO、BMP。,默认为空"
// 参数<4>: [图标序号 SDT_INT], 参数说明: "当资源文件中包含多个图标时,表示要提取图标的序号。"
EAPI_EXTERN_C void eapi_GetIconFromResource_33_eapi(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
std::string FileNmae, SaveFileName;
BOOL ToClipBar = FALSE;
DWORD Index = 1;
if (pArgInf->m_pText) {
FileNmae = pArgInf->m_pText;
}
if (pArgInf[1].m_dtDataType && pArgInf[1].m_int != 0)
{
ToClipBar = TRUE;
}
if (pArgInf[2].m_dtDataType && pArgInf[2].m_pText)
{
SaveFileName = pArgInf[2].m_pText;
}
if (pArgInf[3].m_dtDataType && pArgInf[3].m_int >= 0)
{
Index = pArgInf[3].m_int;
}
pRetData->m_int = eapi_openlib_icon::GetIconFromResource(FileNmae, ToClipBar, SaveFileName, Index);
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

易语言支持库 - 应用接口支持库 欢迎易友fork提交代码完成支持库重构改造。
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/JYtechnology/eapi.git
git@gitee.com:JYtechnology/eapi.git
JYtechnology
eapi
eapi
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /