2010年5月18日星期二

在WinAPI中输入文字-字体定义

//add in .h file
COLORREF myRGB;
HFONT myFont, oldFont;
int nFont_Size;
RECT myRect;
DRAWTEXTPARAMS dtp;

//add in .ccp WM_CREATE

case WM_CREATE:
dtp.cbSize = sizeof(DRAWTEXTPARAMS);
dtp.iLeftMargin = 20;
dtp.iRightMargin = 20;
dtp.iTabLength = 4;

// SetClientWindowSize(hWnd, 400, 300);
GetClientRect(hWnd,&myRect);
myRect.top = myRect.bottom / 2 - 30;

nStringLength = 0;
nFont_Size = 5;
hdc = GetDC(hWnd);
myFont = MyCreateFont(nFont_Size*5, ANSI_CHARSET, _T("Time New Roma"));
oldFont = (HFONT)SelectObject(hdc, myFont);
myRGB = RGB(0, 0, 0);
ReleaseDC(hWnd, hdc);
break;

HFONT MyCreateFont(int nHeight, DWORD dwCharSet, LPCTSTR lpName)
{
return(CreateFont(nHeight, 0, 0, 0,
FW_DONTCARE,
FALSE, FALSE, FALSE,
dwCharSet,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE,
lpName));
}

WM_PAINT

SelectObject(hdc, myFont);
SetTextColor(hdc, myRGB);
//To display text in specified type
DrawTextW(hdc, (LPCWSTR)szChar, -1, &myRect, DT_WORDBREAK|DT_CENTER|DT_VCENTER);

没有评论: