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

📄 haigdi.c

📁 HGui4.1.rar
💻 C
📖 第 1 页 / 共 5 页
字号:
						if (mode == BKM_OPAQUE)
							HAI_DRAWPIXEL(pDis, SX, SY, BitsPixel, BmpWidth, bkcolor, bkcolor4bit)
						break;
					}
					break;
				case PS_DASHDASHDOT:
					switch (SX % 9)
					{
					case 0:
					case 1:
					case 2:
					case 3:
					case 6:
						HAI_DRAWPIXEL(pDis, SX, SY, BitsPixel, BmpWidth, fgcolor, fgcolor4bit)
						break;
					default:
						if (mode == BKM_OPAQUE)
							HAI_DRAWPIXEL(pDis, SX, SY, BitsPixel, BmpWidth, bkcolor, bkcolor4bit)
						break;
					}
					break;
				default:
					HAI_DRAWPIXEL(pDis, SX, SY, BitsPixel, BmpWidth, fgcolor, fgcolor4bit)
					break;
			}
			if (SX==EX) break;
			if ((f+=Ha)>=W)
				f-=W,SY+=dY;
			SX++;
		}
	}

	return S_TRUE;
}

S_BOOL hai_Rectangle(SH_DC hDC, S_SHORT left, S_SHORT top, S_SHORT right, S_SHORT bottom)
{
	if (!hDC || HAI_GETHDLTYPE(hDC) != HT_DC)
		return S_FALSE;
	if (!((SP_DC)hDC)->lpImage)
		return S_FALSE;
	if (left   < 0) left   = 0;
	if (top    < 0) top    = 0;
	if (right  < 0) right  = 0;
	if (bottom < 0) bottom = 0;
//	if (left   >= ((SP_DC)hDC)->cx) left   = ((SP_DC)hDC)->cx -1;
//	if (right  >= ((SP_DC)hDC)->cx) right  = ((SP_DC)hDC)->cx -1;
//	if (top    >= ((SP_DC)hDC)->cy) top    = ((SP_DC)hDC)->cy -1;
//	if (bottom >= ((SP_DC)hDC)->cy) bottom = ((SP_DC)hDC)->cy -1;

	hai_MoveTo(hDC, left, top, S_NULL);
	hai_LineTo(hDC, right, top);
	hai_LineTo(hDC, right, bottom);
	hai_LineTo(hDC, left, bottom);
	hai_LineTo(hDC, left, top);

	hai_BitBlt(hDC, (S_SHORT)(left+1), (S_SHORT)(top+1), (S_SHORT)(right-left-1), (S_SHORT)(bottom-top-1), S_NULL, 0, 0, PATCOPY);

	return S_TRUE;
}

 SH_PEN hai_CreatePen(S_WORD width, S_COLOR color, S_BYTE  style)
 {
 	_SP_PEN pPen;

	if (pPen = hai_MemAlloc(sizeof(*pPen)))
	{
		HAI_SETHDLTYPE(pPen, HT_PEN);
		pPen->width = width;
		pPen->color  = color;
		pPen->style  = style;
	}
	return (SH_PEN)pPen;
 }
 
S_VOID _hai_DrawHLine(SH_DC hDC, S_SHORT SX, S_SHORT EX, S_SHORT SY, S_COLOR c)
{
	S_BYTE  C4, color4bit[2];
	S_BYTE  mask[2] = {0xF, 0xF0};
	S_BYTE  *pDis;
	S_WORD  bits, BmpW;

	EX += ((SP_DC)hDC)->x;//client to screen
	SX += ((SP_DC)hDC)->x;
	SY += ((SP_DC)hDC)->y;
	if (SX > EX)
	{
		bits = EX; EX = SX; SX = bits;
	}

	color4bit[1] = (S_BYTE)c;
	color4bit[0] = (S_BYTE)c << 4;

	pDis = (S_BYTE*)(((SP_DC)hDC)->lpImage);
	bits = ((_SP_BITMAP)(((SP_DC)hDC)->hBitmap))->BitsPixel;
	BmpW = ((_SP_BITMAP)(((SP_DC)hDC)->hBitmap))->WidthBytes;

	switch (bits)
	{
	case 4:
		C4 = color4bit[1]|color4bit[0];
		pDis += SY*BmpW + (SX>>1);
		if (SX < EX && SX & 0x1)
		{
			*pDis = *pDis&mask[SX&0x1] | color4bit[SX&0x1];
			SX++, pDis++;
		}
		for (;SX < EX-1; SX += 2)
			*pDis++ = C4;
		if (SX == EX-1)
			*pDis = *pDis&mask[SX&0x1] | color4bit[SX&0x1];
		break;
	case 8:
		C4 = (S_BYTE)c;
		pDis += SY*BmpW + SX;
		for (;SX < EX; SX++)
			*pDis++ = C4;
		break;
	case 16:
		pDis += SY*BmpW + (SX<<1);
		for (;SX < EX; SX++)
		{
			*((S_WORD*)pDis) = (S_WORD)c;
			pDis += 2;
		}
		break;
	case 24:
		pDis += SY*BmpW + SX*3;
		for (;SX < EX; SX++)
		{
			*pDis++ = (S_BYTE)c;
			*pDis++ = (S_BYTE)(c>>8);
			*pDis++ = (S_BYTE)(c>>16);
		}
		break;
	default:
		break;
	}
}

S_VOID _hai_DrawVLine(SH_DC hDC, S_SHORT SX, S_SHORT SY, S_SHORT EY, S_COLOR c)
{
	S_BYTE  C4, color4bit[2];
	S_BYTE  mask[2] = {0xF, 0xF0};
	S_BYTE  *pDis;
	S_WORD  bits, BmpW;

	EY += ((SP_DC)hDC)->y;
	SX += ((SP_DC)hDC)->x;
	SY += ((SP_DC)hDC)->y;
	if (SY > EY)
	{
		bits = EY;EY = SY;SY = bits;
	}

	color4bit[1] = (S_BYTE)c;
	color4bit[0] = (S_BYTE)c << 4;

	pDis = (S_BYTE*)(((SP_DC)hDC)->lpImage);
	bits = ((_SP_BITMAP)(((SP_DC)hDC)->hBitmap))->BitsPixel;
	BmpW = ((_SP_BITMAP)(((SP_DC)hDC)->hBitmap))->WidthBytes;

	switch (bits)
	{
	case 4:
		pDis += SY*BmpW + (SX>>1);
		for (;SY < EY; SY++)
		{
			*pDis = *pDis&mask[SX&0x1] | color4bit[SX&0x1];
			pDis += BmpW;
		}
		break;
	case 8:
		C4 = (S_BYTE)c;
		pDis += SY*BmpW + SX;
		for (;SY < EY; SY++)
		{
			*pDis = C4;
			pDis += BmpW;
		}
		break;
	case 16:
		pDis += SY*BmpW + (SX<<1);
		for (;SY < EY; SY++)
		{
			*((S_WORD*)pDis) = (S_WORD)c;
			pDis += BmpW;
		}
		break;
	case 24:
		pDis += SY*BmpW + SX*3;
		for (;SY < EY; SY++)
		{
			*(pDis+0) = (S_BYTE)c;
			*(pDis+1) = (S_BYTE)(c>>8);
			*(pDis+2) = (S_BYTE)(c>>16);
			pDis += BmpW;
		}
		break;
	default:
		break;
	}
}

SH_BITMAP  hai_LoadBitmap(S_BYTE *filename)
{
	S_BYTE  *pRes;
	_SP_BITMAP pBitmap = S_NULL;

	if ((S_DWORD)filename & BUILTIN_RESOURCE_FLAG)
	{
		S_BYTE bits;
		S_SHORT w, h;
		pRes = resource + ((S_DWORD)filename&~BUILTIN_RESOURCE_FLAG);
		w = HAI_MAKEWORD(pRes);
		h = HAI_MAKEWORD(pRes+2);
		bits = *(pRes+4);
		pBitmap = (_SP_BITMAP)hai_MemAlloc(sizeof(_S_BITMAP) + BITMAP_WIDTHBYTES(w, bits) * h + 4);
		if (!pBitmap)
			return S_NULL;
		HAI_SETHDLTYPE(pBitmap, HT_BITMAP);
		pBitmap->BitsPixel	= bits;
		pBitmap->width		= w;
		pBitmap->height		= h;
		pBitmap->WidthBytes = BITMAP_WIDTHBYTES(w, bits);
		pBitmap->lpImage	= ((S_BYTE *)pBitmap)+sizeof(_S_BITMAP);
		hai_RleDecode(pBitmap->lpImage, pRes+5, (S_WORD)((w+1)>>1));
	}
	else
	{
		S_DWORD size;
		SH_FILE hFile = hai_fopen(filename, "rb");
		if (hFile)
		{
			hai_fseek(hFile, 0, SEEK_END);
			size = hai_ftell(hFile);
			pRes = hai_MemAlloc(size);
			if (pRes)
			{
				hai_fseek(hFile, 0, SEEK_SET);
				size = hai_fread(pRes, size, 1, hFile);
				if (*pRes=='B'&&*(pRes+1)=='M')
					pBitmap = (_SP_BITMAP)hai_BmpDecode(pRes);
				else if (*pRes==0x0A)
					pBitmap = (_SP_BITMAP)hai_PcxDecode(pRes);
				else if (HAI_MAKEWORD(pRes)==0&&(HAI_MAKEWORD(pRes+2)==1||HAI_MAKEWORD(pRes+2)==2))
				{
					pBitmap = (_SP_BITMAP)hai_IcoDecode(pRes, 1);
					HAI_SETHDLTYPE(pBitmap, HT_BITMAP);
				}
				hai_MemFree(pRes);
			}
			hai_fclose(hFile);
		}
	}

	return (SH_BITMAP)pBitmap; 
}

SH_BITMAP hai_CreateCompatibleBitmap(SH_DC hDC)
{
	_SP_BITMAP pSrc;
	_SP_BITMAP pDst;
	
	if (!hDC || HAI_GETHDLTYPE(hDC) != HT_DC)
		return (SH_BITMAP)S_NULL;
	pSrc = (_SP_BITMAP)(((SP_DC)hDC)->hBitmap);
	if (pSrc == S_NULL)
		return (SH_BITMAP)S_NULL;
		
	pDst = (_SP_BITMAP)hai_MemAlloc(sizeof(_S_BITMAP) + pSrc->height*pSrc->WidthBytes);
	if (!pDst)
		return (SH_BITMAP)S_NULL;

	memset(pDst, 0x00, sizeof(_S_BITMAP) + pSrc->height*pSrc->WidthBytes);
	HAI_SETHDLTYPE(pDst, HT_BITMAP);
	pDst->BitsPixel	= pSrc->BitsPixel;
	pDst->width		= pSrc->width;
	pDst->height		= pSrc->height;
	pDst->WidthBytes = pSrc->WidthBytes;
	pDst->lpImage	= ((S_BYTE *)pDst)+sizeof(_S_BITMAP);

	if (pSrc->lpImage)
		memcpy(pDst->lpImage, pSrc->lpImage, pSrc->WidthBytes*pSrc->height);

	return (SH_BITMAP)pDst;
}

SH_BITMAP hai_CreateBitmap(S_WORD  w, S_WORD  h, S_WORD  BitsPixel, S_VOID  *lpImage)
{
	_SP_BITMAP pBmp;

	if (BitsPixel != 4 && BitsPixel != 8 && BitsPixel != 16 && BitsPixel != 24)
		return (SH_BITMAP)S_NULL;

	pBmp = (_SP_BITMAP)hai_MemAlloc(sizeof(_S_BITMAP));
	if (!pBmp)
		return (SH_BITMAP)S_NULL;

	HAI_SETHDLTYPE(pBmp, HT_BITMAP);
	pBmp->width = w;
	pBmp->height = h;
	pBmp->BitsPixel = BitsPixel;
	pBmp->WidthBytes = BITMAP_WIDTHBYTES(w, BitsPixel);
	pBmp->lpImage = lpImage;
	
	return (SH_BITMAP)pBmp;
}

static S_VOID _memcpy8to4(S_BYTE *pDst, S_BYTE *pSrc, S_WORD nPixel)
{
	S_WORD i;
	S_WORD odd = nPixel&0x1;

	nPixel &= 0xFFFE;
	for (i = 0; i < nPixel; i += 2)
	{
		*pDst++ = (*pSrc&0xF0)|(*(pSrc+1)>>4);
		pSrc += 2;
	}
	if (odd)
		*pDst++ = *pSrc&0xF0;
}
static S_VOID _memcpy16to4(S_BYTE *pDst, S_BYTE *pSrc, S_WORD nPixel)
{
	S_BYTE by;
	S_WORD i;
	S_DWORD c, r, g, b, y;
	S_WORD odd = nPixel&0x1;

	nPixel &= 0xFFFE;
	for (i = 0; i < nPixel; i += 2)
	{
		c = *(S_WORD *)pSrc;
		pSrc += 2;
		r = (c&0x1F)<<3;
		g = (c&0x7E0)>>3;
		b = (c&0xF800)>>8;
		y = ((66*r + 129*g + 25*b + 128) >> 8) + 16;
		if (y > 255) y = 255;
		by = (S_BYTE)y&0xF0;
		
		c = *(S_WORD *)pSrc;
		pSrc += 2;
		r = (c&0x1F)<<3;
		g = (c&0x7E0)>>3;
		b = (c&0xF800)>>8;
		y = ((66*r + 129*g + 25*b + 128) >> 8) + 16;
		if (y > 255) y = 255;

		*pDst++ = (S_BYTE)(by|((y&0xF0)>>4));
	}
	if (odd)
	{
		c = *(S_WORD *)pSrc;
		pSrc += 2;
		r = (c&0x1F)<<3;
		g = (c&0x7E0)>>3;
		b = (c&0xF800)>>8;
		y = ((66*r + 129*g + 25*b + 128) >> 8) + 16;
		if (y > 255) y = 255;
		
		*pDst++ = (S_BYTE)(y&0xF0);
	}
}
static S_VOID _memcpy24to4(S_BYTE *pDst, S_BYTE *pSrc, S_WORD nPixel)
{
	S_BYTE by;
	S_WORD i;
	S_DWORD r, g, b, y;
	S_WORD odd = nPixel&0x1;

	nPixel &= 0xFFFE;
	for (i = 0; i < nPixel; i += 2)
	{
		r = (S_DWORD)*pSrc++;
		g = (S_DWORD)*pSrc++;
		b = (S_DWORD)*pSrc++;
		y = ((66*r + 129*g + 25*b + 128) >> 8) + 16;
		if (y > 255) y = 255;
		by = (S_BYTE)y&0xF0;
		
		r = (S_DWORD)*pSrc++;
		g = (S_DWORD)*pSrc++;
		b = (S_DWORD)*pSrc++;
		y = ((66*r + 129*g + 25*b + 128) >> 8) + 16;
		if (y > 255) y = 255;

		*pDst++ = (S_BYTE)(by|((y&0xF0)>>4));
	}
	if (odd)
	{
		r = (S_DWORD)*pSrc++;
		g = (S_DWORD)*pSrc++;
		b = (S_DWORD)*pSrc++;
		y = ((66*r + 129*g + 25*b + 128) >> 8) + 16;
		if (y > 255) y = 255;
		
		*pDst++ = (S_BYTE)(y&0xF0);
	}
}
static S_BOOL _hai_ConverBitmapTo4Bits(_SP_BITMAP pBmpDst, _SP_BITMAP pBmpSrc)
{
	S_WORD i;
	S_BYTE * pSrc = (S_BYTE *)(pBmpSrc->lpImage);
	S_BYTE * pDst = (S_BYTE *)(pBmpDst->lpImage);
	S_WORD  WidthBytes = pBmpSrc->WidthBytes;

	if (pBmpDst->BitsPixel != 4)
		return S_FALSE;

	switch (pBmpSrc->BitsPixel)
	{
	case 4:
		for (i = 0; i < pBmpDst->height; i++)
		{
			memcpy(pDst, pSrc, WidthBytes);
			pSrc += WidthBytes;
			pDst += pBmpDst->WidthBytes;
		}
		break;
	case 8:
		for (i = 0; i < pBmpDst->height; i++)
		{
			_memcpy8to4(pDst, pSrc, pBmpSrc->width);
			pSrc += WidthBytes;
			pDst += pBmpDst->WidthBytes;
		}
		break;
	case 16:
		for (i = 0; i < pBmpDst->height; i++)
		{
			_memcpy16to4(pDst, pSrc, pBmpSrc->width);
			pSrc += WidthBytes;
			pDst += pBmpDst->WidthBytes;
		}
		break;
	case 24:
		for (i = 0; i < pBmpDst->height; i++)
		{
			_memcpy24to4(pDst, pSrc, pBmpSrc->width);
			pSrc += WidthBytes;
			pDst += pBmpDst->WidthBytes;
		}
		break;
	default:
		return S_FALSE;
		break;
	}
	return S_TRUE;
}

static S_VOID _memcpy4to8(S_BYTE *pDst, S_BYTE *pSrc, S_WORD nPixel)
{
	S_WORD i;
	S_WORD odd = nPixel&0x1;

	nPixel &= 0xFFFE;
	for (i = 0; i < nPixel; i += 2)
	{
		*pDst++ = *pSrc&0xF0;
		*pDst++  = *pSrc<<4;
		pSrc++;
	}
	if (odd)
		*pDst++ = *pSrc&0xF0;
}
static S_VOID _memcpy16to8(S_BYTE *pDst, S_BYTE *pSrc, S_WORD nPixel)
{
	S_WORD i;
	S_DWORD c, r, g, b, y;

	for (i = 0; i < nPixel; i++)
	{
		c = *(S_WORD *)pSrc;
		pSrc += 2;
		r = (c&0x1F)<<3;
		g = (c&0x7E0)>>3;
		b = (c&0xF800)>>8;
		y = ((66*r + 129*g + 25*b + 128) >> 8) + 16;
		if (y > 255) y = 255;

		*pDst++ = (S_BYTE)y;
	}
}
static S_VOID _memcpy24to8(S_BYTE *pDst, S_BYTE *pSrc, S_WORD nPixel)
{
	S_WORD i;
	S_DWORD r, g, b, y;

	for (i = 0; i < nPixel; i++)
	{
		r = (S_DWORD)*pSrc++;
		g = (S_DWORD)*pSrc++;
		b = (S_DWORD)*pSrc++;
//		y = 0.30*r+0.59*g+0.11*b;
		y = ((66*r + 129*g + 25*b + 128) >> 8) + 16;
		if (y > 255) y = 255;

		*pDst++ = (S_BYTE)y;
	}
}

static S_BOOL _hai_ConverBitmapTo8Bits(_SP_BITMAP pBmpDst, _SP_BITMAP pBmpSrc)
{
	S_WORD i;
	S_BYTE * pSrc = (S_BYTE *)(pBmpSrc->lpImage);
	S_BYTE * pDst = (S_BYTE *)(pBmpDst->lpImage);
	S_WORD  WidthBytes = pBmpSrc->WidthBytes;

	if (pBmpDst->BitsPixel != 8)
		return S_FALSE;

	switch (pBmpSrc->BitsPixel)
	{
	case 4:
		for (i = 0; i < pBmpDst->height; i++)
		{
			_memcpy4to8(pDst, pSrc, pBmpSrc->width);
			pSrc += WidthBytes;
			pDst += pBmpDst->WidthBytes;
		}
		break;
	case 8:
		for (i = 0; i < pBmpDst->height; i++)
		{
			memcpy(pDst, pSrc, WidthBytes);
			pSrc += WidthBytes;
			pDst += pBmpDst->WidthBytes;
		}
		break;

⌨️ 快捷键说明

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