VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 628|回复: 2

HICON保存为ICO文件

[复制链接]

2

主题

6

帖子

7.00

积分

新手上路

Rank: 1

积分
7.00
发表于 2020-2-7 03:30:01 | 显示全部楼层 |阅读模式
以下的代码是通过HICON保存为ICO文件
但是只能保存为16色的,这是为什么?谢谢哪:)

void SaveIco(HICON hIconToSave, LPCTSTR sIconFileName)
{
if(hIconToSave==NULL || sIconFileName==NULL)
return;
PICTDESC picdesc;
picdesc.cbSizeofstruct = sizeof(PICTDESC);
picdesc.picType = PICTYPE_ICON ;
picdesc.icon.hicon = hIconToSave;
IPicture* pPicture=NULL;
OleCreatePictureIndirect(&picdesc, IID_IPicture, TRUE,(VOID**)&pPicture);
LPSTREAM pStream;
CreateStreamOnHGlobal(NULL,TRUE,&pStream);
LONG size;
HRESULT hr=pPicture->SaveAsFile(pStream,TRUE,&size);
char pathbuf[1024];
strcpy(pathbuf,sIconFileName);
CFile iconfile;
iconfile.Open(pathbuf, CFile::modeCreate|CFile::modeWrite);
LARGE_INTEGER li;
li.HighPart =0;
li.LowPart =0;
ULARGE_INTEGER ulnewpos;
pStream->Seek( li,STREAM_SEEK_SET,&ulnewpos);
ULONG uReadCount = 1;
while(uReadCount>0)
{
pStream->Read(pathbuf,sizeof(pathbuf),&uReadCount);
if(uReadCount>0)
iconfile.Write(pathbuf,uReadCount);
}
pStream->Release();
iconfile.Close();
}
回复

使用道具 举报

0

主题

1

帖子

2.00

积分

新手上路

Rank: 1

积分
2.00
发表于 2020-7-14 10:30:02 | 显示全部楼层
#include <windows.h>

#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst,unsigned long reason, void* lpReserved)
{
    return 1;
}

struct TCursorOrIcon
{
    WORD Reserved;
    WORD wType;
    WORD Count;
};

struct TIconRec
{
    BYTE Width;
    BYTE Height;
    WORD Colors;
    WORD Reserved1;
    WORD Reserved2;
    long DIBSize;
    long DIBOffset;
};

static void InitBmpInfoHeader(HBITMAP Bitmap,BITMAPINFOHEADER& BI, int nBit)
{
    int Bytes;
    DIBSECTION DS;

    DS.dsBmih.biSize = 0;
    Bytes = GetObject(Bitmap, sizeof(DS), &DS);
    if (Bytes>=sizeof(DS.dsBm)+sizeof(DS.dsBmih) && DS.dsBmih.biSize>=sizeof(DS.dsBmih))
        memcpy(&BI, &DS.dsBmih, sizeof(BITMAPINFOHEADER));
    else {
        memset(&BI, 0, sizeof(BI));
        BI.biSize = sizeof(BI);
        BI.biWidth = DS.dsBm.bmWidth;
        BI.biHeight = DS.dsBm.bmHeight;
        BI.biBitCount = DS.dsBm.bmPlanes
            * DS.dsBm.bmBitsPixel;
    }
    if (nBit!=0) BI.biBitCount = nBit;
    if (BI.biBitCount <= 8)
        BI.biClrUsed = 1<<BI.biBitCount;
    BI.biPlanes = 1;
    if (BI.biClrImportant > BI.biClrUsed)
        BI.biClrImportant = BI.biClrUsed;
    if (BI.biSizeImage == 0)
        BI.biSizeImage = ((BI.biWidth*BI.biBitCount+31) / 32) * 4 * BI.biHeight;
}

static void GetDIBSizes(HBITMAP Bitmap,DWORD& InfoSize, DWORD& ImageSize,int nBit)
{
    BITMAPINFOHEADER BI;

    InitBmpInfoHeader(Bitmap, BI, nBit);
    InfoSize = sizeof(BITMAPINFOHEADER);
    if (BI.biBitCount > 8) {
        if (BI.biCompression&BI_BITFIELDS)
            InfoSize += 12;
    }
    else
        InfoSize += sizeof(RGBQUAD) * (BI.biClrUsed!=0 ? BI.biClrUsed :(1 << BI.biBitCount));
    ImageSize = BI.biSizeImage;
}

static void GetDIB(HBITMAP Bitmap,BITMAPINFO* BmpInfo, void* Bits, int nBit)
{
    HDC DC;
    DC = CreateCompatibleDC(NULL);
    InitBmpInfoHeader(Bitmap, BmpInfo->bmiHeader, nBit);
    GetDIBits(DC, Bitmap, 0, BmpInfo->bmiHeader.biHeight, Bits,BmpInfo, DIB_RGB_COLORS);
    DeleteDC(DC);
}




extern "C" __declspec(dllexport) BOOL _stdcall SaveIcon(HICON Icon, LPCSTR FileName, int nBit)
{
    BOOL ret;
    UINT nColor;
    long Length;
    void *MonoBits;
    void *ColorBits;
    DWORD dwWrite;
    HANDLE hFile;
    TIconRec List;
    ICONINFO IconInfo;
    TCursorOrIcon CI;
    DWORD MonoInfoSize;
    DWORD ColorInfoSize;
    DWORD MonoBitsSize;
    DWORD ColorBitsSize;
    BITMAPINFO *MonoInfo;
    BITMAPINFO *ColorInfo;

    switch(nBit) {
        case 0:
        case 1:
        case 4:
        case 8:
        case 16:
        case 24:
        case 32: break;
        default: return FALSE;
    }
    if (Icon==NULL || !FileName)
        return FALSE;

    hFile = CreateFile(FileName,GENERIC_WRITE, FILE_SHARE_READ,0, CREATE_ALWAYS, 0, 0);
    if (hFile==INVALID_HANDLE_VALUE)
        return false;
    memset(&CI, 0, sizeof(CI));
    memset(&List, 0, sizeof(List));
    GetIconInfo(Icon, &IconInfo);

    GetDIBSizes(IconInfo.hbmMask,MonoInfoSize, MonoBitsSize, 1);
    GetDIBSizes(IconInfo.hbmColor,ColorInfoSize, ColorBitsSize,nBit);
    MonoInfo = (BITMAPINFO*)malloc(MonoInfoSize);
    ColorInfo = (BITMAPINFO*)malloc(ColorInfoSize);
    MonoBits = malloc(MonoBitsSize);
    ColorBits = malloc(ColorBitsSize);
    GetDIB(IconInfo.hbmMask, MonoInfo,MonoBits, 1);
    GetDIB(IconInfo.hbmColor, ColorInfo,ColorBits, nBit);
    CI.wType = 0x10001;  CI.Count = 1;
    ret &= WriteFile(hFile, &CI, sizeof(CI),&dwWrite, NULL);
    List.Width = ColorInfo->bmiHeader.biWidth;
    List.Height = ColorInfo->bmiHeader.biHeight;
    List.Colors = ColorInfo->bmiHeader.biPlanes * ColorInfo->bmiHeader.biBitCount;
    List.DIBSize = ColorInfoSize + ColorBitsSize + MonoBitsSize;
    List.DIBOffset = sizeof(CI) + sizeof(List);
    ret &= WriteFile(hFile, &List, sizeof(List), &dwWrite, NULL);
    ColorInfo->bmiHeader.biHeight *= 2;
    ret &= WriteFile(hFile, ColorInfo,ColorInfoSize, &dwWrite, NULL);
    ret &= WriteFile(hFile, ColorBits,ColorBitsSize, &dwWrite, NULL);
    ret &= WriteFile(hFile, MonoBits,MonoBitsSize, &dwWrite, NULL);
    free(ColorInfo);  
        free(MonoInfo);
    free(ColorBits);  
        free(MonoBits);
    DeleteObject(IconInfo.hbmColor);
    DeleteObject(IconInfo.hbmMask);

    CloseHandle(hFile);  return ret;
}

回复

使用道具 举报

2

主题

6

帖子

7.00

积分

新手上路

Rank: 1

积分
7.00
 楼主| 发表于 2020-8-3 01:30:01 | 显示全部楼层
非常感谢:)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

快速回复 返回顶部 返回列表