16,551
社区成员
发帖
与我相关
我的任务
分享
unsigned long TMainForm::GetCHNFontEx(
char *lpszOutString,
char *lpszBuffer,
char *lpszStringName,
char *lpszFontName,
int nHeight,
int nWidth,
bool ftbBold,
bool ftbItalic,
bool ftbUnderline,
bool ftbStrikeOut,
int nAngle,
DWORD ftwCharSet,
DWORD fdwOutputPrecision,
DWORD fdwClipPrecision,
DWORD fdwQuality,
DWORD fdwPitchAndFamily)
{
if (lpszOutString==NULL || Trim((AnsiString)lpszOutString)=="") return 0; //空字串,返回
HDC hDC;
HBITMAP hBitmap,hOldBMP;
HFONT hFont,hOldFont;
SIZE size1;
COLORREF BackColor=RGB(0,0,0);
COLORREF TextColor=RGB(255,255,255);
if ((hDC=GetDC(0))==NULL) return 2; //不能取得DC句柄
if ((hDC=CreateCompatibleDC(hDC))==NULL) return 3; //不能建立内存图形设备CreateCompatibleDC
if ((hFont=CreateFont(
nHeight, nWidth, nAngle, nAngle,
(ftbBold?700:0), ftbItalic, ftbUnderline, ftbStrikeOut,
ftwCharSet, fdwOutputPrecision, fdwClipPrecision,
fdwQuality, fdwPitchAndFamily, lpszFontName
))==NULL)
return 1; //不能创建逻辑字体
if ((hOldFont=SelectObject(hDC,hFont))==NULL) return 1;
GetTextExtentPoint32(hDC,lpszOutString,StrLen(lpszOutString),&size1);
unsigned int nOneLineSize = (size1.cx/8+(size1.cx%8?1:0));
if(lpszBuffer==NULL)//若缓冲区为空,返回所需缓冲区的大小
{
return (nOneLineSize * 2 * size1.cy + StrLen(lpszStringName) +
Trim(IntToStr(nOneLineSize*size1.cy)).Length() +
Trim(IntToStr(nOneLineSize)).Length() + 10 //"~DGR:,,,"
);
}
if ((hBitmap=CreateCompatibleBitmap(hDC,size1.cx,size1.cy))==NULL)
{ //不能创建位图设备CreateCompatibleBitmap
SelectObject(hDC,hOldFont);
DeleteObject(hFont);
DeleteDC(hDC);
return 4;
}
if ((hOldBMP=SelectObject(hDC,hBitmap))==NULL)
{
SelectObject(hDC,hOldFont);
DeleteObject(hBitmap);
DeleteObject(hFont);
DeleteDC(hDC);
return 4;
}
SetBkColor(hDC,BackColor);
SetTextColor(hDC,TextColor);
FillRect(hDC,&Rect(0,0,size1.cx,size1.cy),0); //????
//BitBlt(GetDC(0),0,0,size1.cx,size1.cy,hDC,0,0,SRCCOPY);
TextOut(hDC,0,0,lpszOutString,StrLen(lpszOutString));
//BitBlt(GetDC(0),0,0,size1.cx,size1.cy,hDC,0,0,SRCCOPY);
//取点串点阵
unsigned char bits,k;
char cHex[] = "0123456789ABCDEF";
char *lpBuffer = lpszBuffer;
StrCopy(lpszBuffer, "~DGR:");
StrCat(lpszBuffer, lpszStringName);
StrCat(lpszBuffer, (","+Trim(IntToStr(nOneLineSize*size1.cy))+","+Trim(IntToStr(nOneLineSize))+",").c_str());
lpBuffer += StrLen(lpszBuffer);
for (int i=0;i<size1.cy;i++)
{
bits = 0;
k = 0x80;
for(int j=0;j<size1.cx;j++)
{
if(GetPixel(hDC,j,i)==TextColor) bits|=k;
k>>=1;
if(!k)
{
*lpBuffer++ = cHex[bits>>4];
*lpBuffer++ = cHex[bits&0xF];
bits = 0;
k = 0x80;
}
}
if(k && k<0x80)
{
*lpBuffer++= cHex[bits>>4];
*lpBuffer++ = cHex[bits&0xF];
}
}
*lpBuffer = 0;
SelectObject(hDC,hOldBMP);
SelectObject(hDC,hOldFont);
DeleteObject(hBitmap);
DeleteObject(hFont);
DeleteDC(hDC);
return (lpBuffer-lpszBuffer);
}
char str1[]="字";
int count=GetCHNFontEx(str1,NULL,str3,"宋体",32,32,false,false,false,false,0,GB2312_CHARSET,0,0,0,0);
char *str2=new char[count];
memset(str2,0x00,count*sizeof(char));
GetCHNFontEx(str1,str2,str3,"宋体",32,32,false,false,false,false,0,GB2312_CHARSET,0,0,0,0);

if ((hFont=CreateFont(
nHeight, nWidth, nAngle, nAngle,
(ftbBold?700:0), ftbItalic, ftbUnderline, ftbStrikeOut,
ftwCharSet, fdwOutputPrecision, fdwClipPrecision,
fdwQuality, fdwPitchAndFamily, lpszFontName
))==NULL)