向hellovfp大哥及各位论坛大牛请教关于windows编程文字输出的问题
程序代码:
void skyline_OnPaint(HWND hWnd) { PAINTSTRUCT PaintStruct; HFONT OldFont; HDC PaintDC = BeginPaint(hWnd, &PaintStruct); if (choice == StockFont) { OldFont = SelectFont(PaintDC, TheFont); GetTextFace(PaintDC, sizeof(aFaceName), (LPSTR)aFaceName); GetTextMetrics(PaintDC, &TextMets); SetTextColor(PaintDC, RGB(rand()%255, rand()%255, rand()%255)); TextOut(PaintDC, 10, 10, aFaceName, strlen(aFaceName)); TextOut(PaintDC, 10, 30, "Stock Font", 11); TextOut(PaintDC, 10, 50, "Ten Letters", 11); SelectFont(PaintDC, OldFont); } else { for (int i = 0; i <= 3; i++) { TheFont = GetFont(900 * i, FontChoice[choice]); OldFont = SelectFont(PaintDC, TheFont); SetTextColor(PaintDC, RGB(rand()%255, rand()%255, rand()%255)); TextOut(PaintDC, 200, 200, "skyline!", 8 ); GetTextFace(PaintDC, sizeof(aFaceName), (LPSTR)aFaceName); GetTextMetrics(PaintDC, &TextMets); SelectFont(PaintDC, OldFont); } TextOut(PaintDC, 10, 10, aFaceName, strlen(aFaceName)); } EndPaint(hWnd, &PaintStruct); }
上面是打印skyline!这行文字的旋转字体的WM_PAINT消息函数, 我把相关的变量和定义贴上
程序代码:
static LOGFONT LogFont; static HFONT TheFont; static char aFaceName[80]; TEXTMETRIC TextMets; char * FontChoice[] = {"New Times Roman", "Arial", "Symbol", "StockFont"}; enum TChoice {Roman, Swiss, Symbol, StockFont}; TChoice choice;
关于choice是在WM_COMMAND下处理的
程序代码:
//////////////////////////////////// // Handle WM_COMMAND //////////////////////////////////// #pragma argsused void skyline_OnCommand(HWND hWnd, int id, HWND hWndCtl, UINT codeNotify) { char s[500]; switch (id) { case CM_INFO: { GetFontString(s, TextMets, aFaceName); MessageBox(hWnd, s, "Font Info", MB_OK); break; } case CM_ROMAN: { choice = Roman; InvalidateRect(hWnd, NULL, TRUE); break; } case CM_SWISS: { choice = Swiss; InvalidateRect(hWnd, NULL, TRUE); break; } case CM_SYMBOL: { choice = Symbol; InvalidateRect(hWnd, NULL, TRUE); break; } case CM_ANSI_FIXED_FONT: { if (TheFont != 0) DeleteFont(TheFont); TheFont = GetStockFont(ANSI_FIXED_FONT); choice = StockFont; InvalidateRect(hWnd, NULL, TRUE); break; } case CM_ANSI_VAR_FONT: { if (TheFont != 0) DeleteFont(TheFont); TheFont = GetStockFont(ANSI_VAR_FONT); choice = StockFont; InvalidateRect(hWnd, NULL, TRUE); break; } case CM_DEVICE_DEFAULT_FONT: { if (TheFont != 0) DeleteFont(TheFont); TheFont = GetStockFont(DEVICE_DEFAULT_FONT); choice = StockFont; InvalidateRect(hWnd, NULL, TRUE); break; } case CM_OEM_FIXED_FONT: { if (TheFont != 0) DeleteFont(TheFont); TheFont = GetStockFont(OEM_FIXED_FONT); choice = StockFont; InvalidateRect(hWnd, NULL, TRUE); break; } case CM_SYSTEM_FONT: { if (TheFont != 0) DeleteFont(TheFont); TheFont = GetStockFont(SYSTEM_FONT); choice = StockFont; InvalidateRect(hWnd, NULL, TRUE); break; } case CM_SYSTEM_FIXED_FONT: { if (TheFont != 0) DeleteFont(TheFont); TheFont = GetStockFont(SYSTEM_FIXED_FONT); choice = StockFont; InvalidateRect(hWnd, NULL, TRUE); break; } } }
GetFont()函数是自定义的
程序代码:
HFONT GetFont(int Escapement, char *Name) { memset(&LogFont, 0, sizeof(LOGFONT)); LogFont.lfHeight = 60; LogFont.lfWidth = 50; LogFont.lfWeight = 900; LogFont.lfEscapement = Escapement; LogFont.lfItalic = 1; LogFont.lfUnderline = 1; LogFont.lfStrikeOut = 1; LogFont.lfOutPrecision = OUT_STROKE_PRECIS; LogFont.lfClipPrecision = CLIP_STROKE_PRECIS; LogFont.lfQuality = PROOF_QUALITY; strcpy(LogFont.lfFaceName, Name); if (TheFont != 0) { DeleteFont(TheFont); } TheFont = CreateFontIndirect(&LogFont); return TheFont; }
针对这些代码有问题如下 :
1. 去查了下SelectFont 看不太明白 MSDN上没看到这个 貌似不是个函数 是个宏吧 他在干什么的 有啥子用处?
2. 查了下StockFont 应该是对应的六种字体吧 在 char * FontChoice[] = {"New Times Roman", "Arial", "Symbol", "StockFont"};中他和其他的字符串应该有相似的含义吧,那为什么
enum TChoice {Roman, Swiss, Symbol, StockFont};中的“Arial”和Swiss不对应? 为什么把if (choice == StockFont)作为一个单独条件提出来?
3.aFaceName 究竟是个什么样的字符串 他的内容是什么 从哪里获取的 我曾经单独立出个工程就打印他 并给他先赋值然后用TextOut()函数输出 结果无论我怎么改变赋值的内容他都打印出 System ?GetTextFace()的源字符串和目的字符串都是谁?
4关于GetTextMetrics()百度解释是:该函数把程序当前的字体信息,存放到TEXTMETRIC(即MSDN原文中的specified buffer)中,也就是说这不是个输出函数 是获取函数 ,但是在这段程序中他究竟有个什么作用 会对后面有什么影响?
先问这么多 一大篇啊 麻烦看看。。