⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 showwyy.cpp

📁 一个用C++写的电力系统故障录波数据管理系统
💻 CPP
字号:
// ShowWyy.cpp: implementation of the CShowWyy class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "wgl_32.h"
#include "ShowWyy.h"
#include "showbmp.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CShowWyy::CShowWyy()
{

}

CShowWyy::~CShowWyy()
{

}

int CShowWyy::ShowBMP(HDC hDC,LPSTR lpszSrcFName,int x, int y)
{
	BITMAPFILEHEADER   BFH;
	BITMAPINFOHEADER   BIH;
	HPALETTE           hPalette;
	HPALETTE           hOldPal;
	HPALETTE           hMemOldPal;
	HBITMAP            hBMP;
	HBITMAP            hMemOldBMP;
	HANDLE             hBI_Struct;
	HANDLE             hImage;
	HDC                hMemDC;
	LPBITMAPINFOHEADER lpBIH;
	LPWORD             lpwIndex;
	LPSTR              lpImage;
	RGBQUAD FAR        *lpRGB;
	WORD               wBI_Size;
	WORD               wPalSize;
	WORD               wColors;
	WORD               wBits;
	WORD               wWidthBytes;
	WORD               wWidth;
	WORD               wHeight;
	int                wi;
	char               szStr[256];
	int                iFile;
	iFile = _lopen(lpszSrcFName, OF_READ);
	if (iFile == HFILE_ERROR)
		{
		 strcpy(lpszSrcFName,"default.dat");
		 iFile = _lopen(lpszSrcFName, OF_READ);
		}
	/* BMP File Header */
	_lread(iFile,(LPSTR)&BFH,sizeof(BFH));
	/* Is a Microcosm BMP File? */
  if ( !CHECK_BMP(BFH.bfType) )
	{
	  _lclose(iFile);
	  return (1);
	}
	/* BMP Info Header */
	_lread(iFile,(LPSTR)&BIH,sizeof(BIH));

	if ( BIH.biCompression != BI_RGB )
	{
		_lclose(iFile);
		wsprintf(szStr,"本系统不支持压缩格式的BMP文件!");
		AfxMessageBox( szStr, MB_OK);
		return (1);
	}
	wBits       = BIH.biBitCount;
	wColors     = IMAGE_COLORS(wBits,1);
	wWidthBytes = (WORD)DWORD_WBYTES( BIH.biWidth * (DWORD)wBits );
	if ( BIH.biSizeImage==0 )
		BIH.biSizeImage = (DWORD)wWidthBytes * BIH.biHeight;
	if ( BIH.biClrUsed==0 )
		BIH.biClrUsed = (DWORD)wColors;

	wColors     = (WORD)BIH.biClrUsed;
	/* Allocate for the BITMAPINFO structure and the color table */
	wPalSize    = wColors * sizeof(RGBQUAD);
	wBI_Size    = (WORD)BIH.biSize + wPalSize;
	hBI_Struct  = GlobalAlloc(GHND,(DWORD)wBI_Size);
	lpBIH       = (LPBITMAPINFOHEADER)GlobalLock(hBI_Struct);
	*lpBIH      = BIH;
	lpRGB       = (RGBQUAD FAR *)((LPSTR)lpBIH+BIH.biSize);
	if ( wColors>0 )		/* Read palette */
		_lread(iFile,(LPSTR)lpRGB,wPalSize);
	hPalette    = hCreatePalette(lpRGB,wColors);
	hOldPal     = SelectPalette(hDC,hPalette,FALSE);
	RealizePalette(hDC);
	if ( wColors>0 )
	{  /* Set Palette Index */
		lpwIndex = (LPWORD)((LPSTR)lpRGB);
		for(wi=0;wi<wColors;wi++)
			*lpwIndex++ = wi;
	}
	wWidth      = (WORD)lpBIH->biWidth;
	wHeight     = (WORD)lpBIH->biHeight;
	hBMP        = CreateCompatibleBitmap(hDC,(int)wWidth,1);
	hMemDC      = CreateCompatibleDC(hDC);
	hMemOldPal  = SelectPalette(hMemDC,hPalette,FALSE);
	RealizePalette(hMemDC);

	hImage      = GlobalAlloc(GHND,(DWORD)wWidthBytes);
	lpImage     = (LPSTR)GlobalLock(hImage);
	lpBIH->biHeight    = (DWORD)1;
	lpBIH->biSizeImage = (DWORD)wWidthBytes;
	_llseek(iFile,0L,SEEK_CUR);
	_llseek(iFile,(LONG)BFH.bfOffBits,SEEK_SET);
	for(wi=0;wi<wHeight;wi++)
	{
		_lread(iFile,(LPSTR)lpImage,wWidthBytes);
		SetDIBits(hMemDC,hBMP,0,1,(LPSTR)lpImage,
					 (LPBITMAPINFO)lpBIH,
					 (wBits==24) ? DIB_RGB_COLORS : DIB_PAL_COLORS);
		hMemOldBMP = (HBITMAP)SelectObject(hMemDC,hBMP);
		BitBlt(hDC,x,y+wHeight-wi-1,(int)wWidth,1,hMemDC,0,0,SRCCOPY);
		SelectObject(hMemDC,hMemOldBMP);
	}
	DeleteObject(hBMP);
	SelectPalette(hMemDC,hMemOldPal,FALSE);
   DeleteDC(hMemDC);
   GlobalUnlock(hImage);
   GlobalFree(hImage);
   GlobalUnlock(hBI_Struct);
	GlobalFree(hBI_Struct);
	SelectPalette(hDC,hOldPal,FALSE);
	DeleteObject(hPalette);
	_lclose(iFile);
	return 0;
}


HPALETTE CShowWyy::hCreatePalette(RGBQUAD FAR *lpRGB,WORD wColors)
{
	HPALETTE     hPalette;
	HANDLE       hLogPal;
   NPLOGPALETTE npLogPal;
	WORD         wPalSize;
   WORD         wi;
   BYTE         byR;
   BYTE         byG;
	BYTE         byB;

   if ( wColors )
   {
      /* Allocate for the logical palette structure */
      wPalSize = sizeof(LOGPALETTE) + wColors * sizeof(PALETTEENTRY);
		hLogPal  = LocalAlloc(GHND,wPalSize);
		if ( !hLogPal )
			return NULL;
		npLogPal = (NPLOGPALETTE)LocalLock(hLogPal);
		npLogPal->palNumEntries = wColors;
		npLogPal->palVersion    = BMP_PAL_VER;

      /* Fill in the palette entries from the DIB color table */
      /*         and create a logical color palette.          */
      for(wi=0;wi<wColors;wi++)
      {
         npLogPal->palPalEntry[wi].peRed   = (lpRGB+wi)->rgbRed;
			npLogPal->palPalEntry[wi].peGreen = (lpRGB+wi)->rgbGreen;
			npLogPal->palPalEntry[wi].peBlue  = (lpRGB+wi)->rgbBlue;
			npLogPal->palPalEntry[wi].peFlags = (BYTE)0;
		}
		hPalette = CreatePalette((LPLOGPALETTE)npLogPal);
		LocalUnlock(hLogPal);
      LocalFree(hLogPal);
	}
	else
	{
		/* A 24 bitcount DIB has no color table entries so, */
		/*   set the number of to the maximum value (256).  */
		wColors  = 256;

      /* Allocate for the logical palette structure */
		wPalSize = sizeof(LOGPALETTE) + wColors * sizeof(PALETTEENTRY);
      hLogPal  = LocalAlloc(GHND,wPalSize);
      if ( !hLogPal )
			return NULL;
		npLogPal = (NPLOGPALETTE)LocalLock(hLogPal);
		npLogPal->palNumEntries = wColors;
		npLogPal->palVersion    = BMP_PAL_VER;

		/* Generate 256 (= 8*8*4) RGB combinations */
		/*    to fill the palette entries.         */
		byR = byG = byB = 0;
		for(wi=0;wi<wColors;wi++)
		{
			npLogPal->palPalEntry[wi].peRed   = byR;
			npLogPal->palPalEntry[wi].peGreen = byG;
			npLogPal->palPalEntry[wi].peBlue  = byB;
			npLogPal->palPalEntry[wi].peFlags = (BYTE)0;
			if ( !(byR += 32) )
			{
				if ( !(byG += 32) )
					byB += 64;
			}
		}
		hPalette = CreatePalette((LPLOGPALETTE)npLogPal);
		LocalUnlock(hLogPal);
      LocalFree(hLogPal);
	}
	return hPalette;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -