请教一个saveicon的问题

foreasldjj 2010-08-04 09:28:14
我用了季老大的函数,可是保存的都是系统的图标。没有保存我的图标,哪里有问题呢?

代码如下:

#include <windows.h>

#include "resource.h"

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);
}


BOOL 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;
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
HICON hIcon1 = LoadIcon(NULL,MAKEINTRESOURCE(IDI_ICON1));
HICON hIcon2 = LoadIcon(NULL,MAKEINTRESOURCE(IDI_ICON2));
HICON hIcon3 = LoadIcon(NULL,MAKEINTRESOURCE(IDI_ICON3));
HICON hIcon4 = LoadIcon(NULL,MAKEINTRESOURCE(IDI_ICON4));
HICON hIcon5 = LoadIcon(NULL,MAKEINTRESOURCE(IDI_ICON5));

SaveIcon(hIcon1,"c:\\1.ico",32);
SaveIcon(hIcon2,"c:\\2.ico",32);
SaveIcon(hIcon3,"c:\\3.ico",32);
SaveIcon(hIcon4,"c:\\4.ico",32);
SaveIcon(hIcon5,"c:\\5.ico",32);

return 0;
}



以上代码没有保存我放在资源中的ico图标,保存的都是一些系统图标。

有点莫名其妙了。
...全文
55 点赞 收藏 5
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2010-08-04
LoadImage(instance,MAKEINTRESOURCE(IDI_ME),
回复
foreasldjj 2010-08-04
相当的郁闷
HICON himageIcon = (HICON)LoadImage(NULL,"C:\\url_ico\\mm.ico",IMAGE_ICON,0,0,LR_LOADFROMFILE);

这样是有用的,从固定路径获得一个ico图标,但是一旦改成资源中的图标就失败了。
像这样

HICON himageIcon = (HICON)LoadImage(NULLMAKEINTRESOURCE(IDI_ME),IMAGE_ICON,0,0,LR_LOADFROMFILE);
回复
foreasldjj 2010-08-04
我loadicon文件是正确的,可以看到我的得到的ico图像,代码如下:

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
TCHAR szAppName[] = TEXT ("IconDemo") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindow (szAppName, TEXT ("Icon Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HICON hIcon ;
static int cxIcon, cyIcon, cxClient, cyClient ;
HDC hdc ;
HINSTANCE hInstance ;
PAINTSTRUCT ps ;
int x, y ;

switch (message)
{
case WM_CREATE :
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON)) ;
cxIcon = GetSystemMetrics (SM_CXICON) ;
cyIcon = GetSystemMetrics (SM_CYICON) ;

return 0 ;

case WM_SIZE :
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
return 0 ;

case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;

for (y = 0 ; y < cyClient ; y += cyIcon)
for (x = 0 ; x < cxClient ; x += cxIcon)
DrawIcon (hdc, x, y, hIcon) ;

EndPaint (hwnd, &ps) ;
return 0 ;

case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

回复
foreasldjj 2010-08-04
这里是想把我放在资源中的ico文件保存下来。
回复
foreasldjj 2010-08-04
上午就搞定了,楼上你的回答是正确的。

还是把分给你吧,谢谢。
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告
暂无公告