#include <windows.h> enum {W=800, H=600}; HBITMAP hBmp; HWND hwnd; HDC hdc, hdcMem; UCHAR* pic; //將 c1 與 c2 以 nc1:nc-nc1 比例混色, 傳回 BGR 格式 inline int mixColor (int c1, int c2, int nc1, int nc) { int a = nc - nc1; typedef UCHAR U; int r = (U(c1>>16)*nc1 + U(c2>>16)*a)/ nc; int g = (U(c1>>8) *nc1 + U(c2>>8) *a)/ nc; int b = (U(c1) *nc1 + U(c2) *a)/ nc; return (b<<16)|(g<<8)|r; } //在 (tx, ty) 處秀出字型=face, 大小=size 的文字 s, 顏色由 c1 過渡到 c2, void show_text (int tx, int ty, wchar_t* s, int c1, int c2, int size=24, wchar_t* face = L"標楷體") { static int bits = GetDeviceCaps (hdc, BITSPIXEL) * GetDeviceCaps (hdc, PLANES); HFONT font = CreateFont (size,0,0,0,FW_BOLD,0,0,0,0,0,0,0,0,face); HGDIOBJ oldFont = SelectObject (hdcMem, font); SetBkColor (hdcMem,0); SetTextColor (hdcMem, 93); TextOut (hdcMem, 0,0, s, wcslen(s)); SelectObject (hdcMem, oldFont); DeleteObject (font); int p,x,y, h = size, w = size * wcslen(s); if (w > W) w = W; if (h > H) h = H; BITMAPINFO info0 = {{40, w, h, 1, bits, 0,0,0,0,0,0}, {{0}}}; GetDIBits (hdcMem, hBmp, 0, h, pic, &info0, DIB_RGB_COLORS); if (bits == 32) { int *o = (int*) pic; for (p=y=0; y<h; ++y, p+=w) for (x=0; x<w; ++x) if (RGB(0,0,93) == o[p+x]) o[p+x] = mixColor (c1,c2, y,h); else o[p+x] = 0; } BITMAPINFO info = {{40, w, h, 1, bits, 0,0,0,0,0,0}, {{0}}}; SetDIBitsToDevice (hdc, tx,ty,w,h, 0,0,0,h, pic, &info, DIB_RGB_COLORS); } void init() { SetConsoleTitle (L"Conso"); SMALL_RECT size = {0, 0, W-1, H-1}; SetConsoleWindowInfo (GetStdHandle (STD_OUTPUT_HANDLE), TRUE, &size); hdc = GetDC (hwnd = FindWindowW (0, L"Conso")); hdcMem = CreateCompatibleDC (hdc); hBmp = CreateCompatibleBitmap (hdc, W, H); pic = new UCHAR[W*H]; SelectObject (hdcMem, hBmp); } void free() { delete[] pic; ReleaseDC (hwnd, hdc); DeleteObject (hBmp); DeleteDC (hdcMem); } int main (/* daviddr 081104 */) { init(); show_text (30,80, L"漸層字!", RGB(255,10,10), RGB(255,250,50)); show_text (30,120, L"這是48號字體", RGB(55,10,220), RGB(255,90,90), 48); system ("pause"); free(); return 0; }
2009年10月31日 星期六
漸層字
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言