Bmp.c
上传用户:s6549606
上传日期:2015-11-11
资源大小:12002k
文件大小:7k
- //////////////////////////////////////////////////////////////
- //Name:bmp.c
- //Purpose: To open a bitmap file and display it on screen.
- //Author: phoenix, CS, TshingHua, Beijing, P.R.C
- //Email: bjlufengjun@www.163.net or lufengjun@hotmail.com
- //Date:April 3, 1998
- //header file
- #include "bmp.h"
- //macro definition
- #define WIDTHBYTES(i) ((i+31)/32*4)
- //function declaration
- int PASCAL WinMain (HANDLE, HANDLE, LPSTR, int);
- LRESULT CALLBACK MainWndProc(HWND , UINT,WPARAM, LPARAM);
- BOOL LoadBmpFile (HWND hWnd,char *BmpFileName);
- //global variable declaration
- BITMAPFILEHEADER bf;
- BITMAPINFOHEADER bi;
- HPALETTE hPalette=NULL;
- HBITMAP hBitmap=NULL;
- HGLOBAL hImgData=NULL;
- HWND hWnd;
- ///////////////////////////////////////////////////////////
- int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
- LPSTR lpszCmdLine, int nCmdShow)
- {
- MSG msg;
- WNDCLASS wndclass;
- if ( ! hPrevInstance ){
- wndclass.style = CS_HREDRAW | CS_VREDRAW;
- wndclass.lpfnWndProc = MainWndProc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hInstance;
- wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
- wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
- wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
- wndclass.lpszMenuName = "BMPMENU";
- wndclass.lpszClassName = "phoenix ip system";
- }
- if ( ! RegisterClass (&wndclass) )
- return FALSE;
-
- hWnd = CreateWindow ("phoenix ip system","Open and display a bitmap",
- WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
- CW_USEDEFAULT, CW_USEDEFAULT, NULL,NULL,
- hInstance, NULL);
- if (!hWnd)
- return FALSE;
- ShowWindow (hWnd, SW_SHOWMAXIMIZED);
- UpdateWindow (hWnd);
-
- while ( GetMessage (&msg, NULL, 0, 0) ){
- TranslateMessage (&msg);
- DispatchMessage (&msg);
- }
- return msg.wParam;
- }
- ////////////////////////////////////////////////////////////////
- LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
- {
- static HDC hDC,hMemDC;
- PAINTSTRUCT ps;
- switch (message){
- case WM_PAINT:
- {
- hDC = BeginPaint(hWnd, &ps);
- if (hBitmap)
- {
- hMemDC = CreateCompatibleDC(hDC);
- if (hPalette)
- {
- SelectPalette (hDC, hPalette, FALSE);
- SelectPalette (hMemDC, hPalette, FALSE);
- RealizePalette (hDC);
- }
- SelectObject(hMemDC, hBitmap);
- BitBlt(hDC, 0, 0, bi.biWidth, bi.biHeight, hMemDC, 0, 0, SRCCOPY);
- DeleteDC(hMemDC);
- }
- EndPaint(hWnd, &ps);
- break;
- }
- case WM_DESTROY: //注意释放内存和位图,调色板句柄
- if(hBitmap!=NULL)
- DeleteObject(hBitmap);
-
- if(hPalette!=NULL)
- DeleteObject(hPalette);
-
- if(hImgData!=NULL){
- GlobalUnlock(hImgData);
- GlobalFree(hImgData);
- }
- PostQuitMessage (0);
- return 0;
- case WM_COMMAND:
- switch (wParam){
- case IDM_LOADBMP:
- //注意重新分配内存和调色板,位图句柄时,先释放原来的
- if(hBitmap!=NULL){
- DeleteObject(hBitmap);
- hBitmap=NULL;
- }
- if(hPalette!=NULL){
- DeleteObject(hPalette);
- hPalette=NULL;
- }
- if(hImgData!=NULL){
- GlobalUnlock(hImgData);
- GlobalFree(hImgData);
- hImgData=NULL;
- }
- if(LoadBmpFile(hWnd,"c:\test.bmp")) //成功,则重画窗口
- InvalidateRect(hWnd,NULL,TRUE);
- break;
- case IDM_EXIT:
- SendMessage(hWnd,WM_DESTROY,0,0L);
- break;
- }
- break;
- }
- return DefWindowProc (hWnd, message, wParam, lParam);
- }
- ////////////////////////////////////////////////////////////////
- BOOL LoadBmpFile (HWND hWnd,char *BmpFileName)
- {
- HFILE hf;
- LPBITMAPINFOHEADER lpImgData;
- LOGPALETTE *pPal;
- LPRGBQUAD lpRGB;
- HPALETTE hPrevPalette;
- HDC hDc;
- HLOCAL hPal;
- DWORD LineBytes;
- DWORD ImgSize;
- DWORD NumColors;
- DWORD i;
- if((hf=_lopen(BmpFileName,OF_READ))==HFILE_ERROR){
- MessageBox(hWnd,"File c:\test.bmp not found!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- _lread(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
- _lread(hf,(LPSTR)&bi,sizeof(BITMAPINFOHEADER));
- LineBytes=(DWORD)WIDTHBYTES(bi.biWidth*bi.biBitCount);
- ImgSize=(DWORD)LineBytes*bi.biHeight;
- if(bi.biClrUsed!=0)
- NumColors=(DWORD)bi.biClrUsed;
- else
- switch(bi.biBitCount){
- case 1:
- NumColors=2;
- break;
- case 4:
- NumColors=16;
- break;
- case 8:
- NumColors=256;
- break;
- case 24:
- NumColors=0;
- break;
- default:
- MessageBox(hWnd,"Invalid color numbers!","Error Message",MB_OK|MB_ICONEXCLAMATION);
- _lclose(hf);
- return FALSE;
- }
- if(bf.bfOffBits!=(DWORD)(NumColors*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
- +sizeof(BITMAPINFOHEADER)))
- {
- MessageBox(hWnd,"Invalid color numbers!","Error Message" ,MB_OK|
- MB_ICONEXCLAMATION);
- _lclose(hf);
- return FALSE;
- }
- bf.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+ImgSize;
- if((hImgData=GlobalAlloc(GHND,(DWORD)(sizeof(BITMAPINFOHEADER)+
- NumColors*sizeof(RGBQUAD)+ImgSize)))==NULL)
- {
- MessageBox(hWnd,"Error alloc memory!","ErrorMessage",MB_OK|
- MB_ICONEXCLAMATION);
- _lclose(hf);
- return FALSE;
- }
-
- lpImgData=(LPBITMAPINFOHEADER)GlobalLock(hImgData);
- _llseek(hf,sizeof(BITMAPFILEHEADER),SEEK_SET);
- _hread(hf,(char *)lpImgData,(long)sizeof(BITMAPINFOHEADER)
- +(long)NumColors*sizeof(RGBQUAD)+ImgSize);
- _lclose(hf);
- if(NumColors!=0)
- {
- hPal=LocalAlloc(LHND,sizeof(LOGPALETTE) + NumColors* sizeof(PALETTEENTRY));
- pPal =(LOGPALETTE *)LocalLock(hPal);
- pPal->palNumEntries =(WORD) NumColors;
- pPal->palVersion = 0x300;
- lpRGB = (LPRGBQUAD)((LPSTR)lpImgData + (DWORD)sizeof(BITMAPINFOHEADER));
- for (i = 0; i < NumColors; i++) {
- pPal->palPalEntry[i].peRed=lpRGB->rgbRed;
- pPal->palPalEntry[i].peGreen=lpRGB->rgbGreen;
- pPal->palPalEntry[i].peBlue=lpRGB->rgbBlue;
- pPal->palPalEntry[i].peFlags=(BYTE)0;
- lpRGB++;
- }
- hPalette=CreatePalette(pPal);
- LocalUnlock(hPal);
- LocalFree(hPal);
- }
- hDc=GetDC(hWnd);
- if(hPalette){
- hPrevPalette=SelectPalette(hDc,hPalette,FALSE);
- RealizePalette(hDc);
- }
- hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpImgData, (LONG)CBM_INIT,
- (LPSTR)lpImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
- (LPBITMAPINFO)lpImgData, DIB_RGB_COLORS);
- if(hPalette && hPrevPalette){
- SelectPalette(hDc,hPrevPalette,FALSE);
- RealizePalette(hDc);
- }
-
- ReleaseDC(hWnd,hDc);
- GlobalUnlock(hImgData);
- return TRUE;
- }