搞了2个小时了,其实很简单,有两个函数Windows API官方说明上写着仅限Windows 16bit平台使用,但是为神马我虚拟机的32位XP可以用呢?
这个问题暂时想不通,不过总算是解决了.
只需要把OpenFile函数换成了CreateFile函数把_lread函数换成了ReadFile函数,把_lclose函数换成了CloseHandle函数,然后注释掉_lseek函数即可.
附上相关函数的官方帮助文档:
_lseek:http://msdn.microsoft.com/zh-cn/library/1yee101t(v=VS.80).aspx
_lread:找不到
_lclose:找不到.
CreateFile:http://msdn.microsoft.com/zh-cn/library/aa363858%28en-us,VS.85%29.aspx
ReadFile:http://msdn.microsoft.com/zh-cn/library/aa365467.aspx
CloseHandle:http://msdn.microsoft.com/en-us/library/ms724211(v=VS.85).aspx
书上P279,Load_Bitmap_File函数
int Load_Bitmap_File(BITMAP_FILE_PTR bitmap, char *filename)
{
// this function opens a bitmap file and loads the data into bitmap
HANDLE file_handle; // the file handle
int index; // looping index
UCHAR *temp_buffer = NULL; // used to convert 24 bit images to 16 bit
OFSTRUCT file_data; // the file data information
// open the file if it exists
//if ((file_handle = OpenFile(filename,&file_data,OF_READ))==-1)
// return(0);
if ((file_handle = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == NULL) //注意这里,把OpenFile函数换成了CreateFile函数
return(0);
// now load the bitmap file header
//_lread(file_handle, &bitmap->bitmapfileheader,sizeof(BITMAPFILEHEADER));
DWORD numberOfBytesRead;
ReadFile(file_handle, &bitmap->bitmapfileheader, sizeof(BITMAPFILEHEADER),&numberOfBytesRead,NULL); //注意这里,把_lread函数换成了ReadFile函数
// test if this is a bitmap file
if (bitmap->bitmapfileheader.bfType!=BITMAP_ID)
{
// close the file
//_lclose(file_handle);
CloseHandle(file_handle); //注意这里把_lclose函数换成了CloseHandle函数
// return error
return(0);
} // end if
// now we know this is a bitmap, so read in all the sections
// first the bitmap infoheader
// now load the bitmap file header
//_lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));
ReadFile(file_handle,&bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER),&numberOfBytesRead,NULL);//注意这里,把_lread函数换成了ReadFile函数
// now load the color palette if there is one
if (bitmap->bitmapinfoheader.biBitCount == 8)
{
// _lread(file_handle, &bitmap->palette,MAX_COLORS_PALETTE*sizeof(PALETTEENTRY));
ReadFile(file_handle,&bitmap->palette,MAX_COLORS_PALETTE*sizeof(PALETTEENTRY),&numberOfBytesRead,NULL);//注意这里,把_lread函数换成了ReadFile函数
// now set all the flags in the palette correctly and fix the reversed
// BGR RGBQUAD data format
//for (index=0; index < MAX_COLORS_PALETTE; index++)
// {
// // reverse the red and green fields
// int temp_color = bitmap->palette[index].peRed;
// bitmap->palette[index].peRed = bitmap->palette[index].peBlue;
// bitmap->palette[index].peBlue = temp_color;
//
// // always set the flags word to this
// bitmap->palette[index].peFlags = PC_NOCOLLAPSE;
// } // end for index //这段反转RGB顺序的代码貌似没什么意义,因为不管有没有这段代码,程序在Windows 7 x64下运行时显示的图像都是花的,不管是我编译出来的还是书上光盘自带的程序,都是花的...
} // end if
// finally the image data itself
//_lseek(file_handle,-(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END); //这代码没搞懂,为什么到这里要移动一下文件指针呢?也没有找到对应Handle的seek函数,注释掉也能用.
// now read in the image, if the image is 8 or 16 bit then simply read it
// but if its 24 bit then read it into a temporary area and then convert
// it to a 16 bit image
if (bitmap->bitmapinfoheader.biBitCount==8 || bitmap->bitmapinfoheader.biBitCount==16 ||
bitmap->bitmapinfoheader.biBitCount==24)
{
// delete the last image if there was one
if (bitmap->buffer)
free(bitmap->buffer);
// allocate the memory for the image
if (!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))
{
// close the file
//_lclose(file_handle);
CloseHandle(file_handle);//注意这里把_lclose函数换成了CloseHandle函数
// return error
return(0);
} // end if
// now read it in
//_lread(file_handle,bitmap->buffer,bitmap->bitmapinfoheader.biSizeImage);
ReadFile(file_handle,bitmap->buffer,bitmap->bitmapinfoheader.biSizeImage,&numberOfBytesRead,NULL);//注意这里,把_lread函数换成了ReadFile函数
} // end if
else
{
// serious problem
return(0);
} // end else
#if 0
// write the file info out
printf("\nfilename:%s \nsize=%d \nwidth=%d \nheight=%d \nbitsperpixel=%d \ncolors=%d \nimpcolors=%d",
filename,
bitmap->bitmapinfoheader.biSizeImage,
bitmap->bitmapinfoheader.biWidth,
bitmap->bitmapinfoheader.biHeight,
bitmap->bitmapinfoheader.biBitCount,
bitmap->bitmapinfoheader.biClrUsed,
bitmap->bitmapinfoheader.biClrImportant);
#endif
// close the file
//_lclose(file_handle);
CloseHandle(file_handle);//注意这里把_lclose函数换成了CloseHandle函数
// flip the bitmap
Flip_Bitmap(bitmap->buffer,
bitmap->bitmapinfoheader.biWidth*(bitmap->bitmapinfoheader.biBitCount/8),
bitmap->bitmapinfoheader.biHeight);
// return success
return(1);
} // end Load_Bitmap_File
运行结果:
程序刚启动的瞬间颜色是正常的,但是闪烁过后就变成花的了.暂时不知道什么原因,不过至少可以显示出图片了
XP下运行的效果,同样的,按ESC退出程序时也会变花.
虚拟XP下程序启动瞬间不会闪烁.