�������� Linux Tags

17. ���ֺ�����

��ʾ�����DZ�����Ҫ�������Ҫ���⣬�����������о�Microsoft Windows�и�����Ч����������С��ʹ�÷����Լ��������ֵķ�ʽ��


Windows 3.1������TrueTypeʹ��ʽд���ߺ�ʹ���������ķ�ʽ�������ֵ����������ǿ��TrueType���������弼������Apple Computer��˾��Microsoft��˾������������������������֧Ԯ�����TrueType�����ܹ��������ţ�����Ӧ�����Ѷ��ʾ����ӡ�����������ܹ���Windows��ʵ�����WYSIWYG��what you see is what you get�����������ã���TrueTypeҲ���������������壬������ת����ĸ���ڲ����ͼ������ĸ��������춼��������ڱ����ҽ�չʾ���ǡ�

�򵥵��������
��

��������������WindowsΪ���������Ӱ�����ֵ�װ�����������Լ����������ṩ�ĸ��ֺ�ʽ��

���������ʽ
��

���Ѿ������෶����ʽ��ʹ�ù���õ����������ʽ��

TextOut (hdc, xStart, yStart, pString, iCount) ;

����xStart��yStart���߼��������ִ�����ʼ�㡣ͨ��������Windows��ʼ���Ƶĵ�һ����ĸ�����Ͻǡ�TextOut��Ҫָ���ִ���ָ����ִ��ij��ȣ������ʽ����ʶ����NULL��ֹ���ִ���

TextOut��ʽ��xStart��yStart�����ĺ������SetTextAlign��ʽ�ı䡣TA_LEFT��TA_RIGHT��TA_CENTER���Ӱ��ʹ��xStart��ˮƽ�����϶�λ�ִ��ķ�ʽ��Ԥ��ֵ��TA_LEFT�������SetTextAlign��ʽ��ָ����TA_RIGHT���������TextOut���лὫ�ִ�������һ����Ԫ��λ�xStart�����ָ����TA_CENTER�����ִ�������λ�xStart��

���Ƶأ�TA_TOP��TA_BOTTOM��TA_BASELINE���Ӱ���ִ��Ĵ�ֱλ�á�TA_TOP��Ԥ��ֵ������ζ���ִ�����ĸ����λ�yStart��ʹ��TA_BOTTOM��ζ���ִ�λ�yStart֮�ϡ�����ʹ��TA_BASELINE��λ�ִ���ʹ��׼��λ�yStart����׼������Сд��ĸp��q��y����ĸ�²����ߡ�

�����ʹ��TA_UPDATECP������SetTextAlign��Windows�ͻ����TextOut��xStart��yStart��������ʹ����MoveToEx��LineTo�����Ŀǰλ�õ���һ����ʽ�趨��λ�á�TA_UPDATECP���ҲʹTextOut��ʽ��Ŀǰλ�ø���Ϊ�ִ��Ľ�β��TA_LEFT�����ִ��Ŀ�ͷ��TA_RIGHT��������ʹ�ö��TextOut������ʾһ������ʱ�dz����á���ˮƽλ����TA_CENTERʱ����TextOut�����ᣬĿǰλ�ò��䡣

��Ӧ�û��ǵã������µ�һϵ��SYSMETS��ʽ��ʾ��������ʱ����ÿһ�ж���Ҫ����һ��TextOut���������ʽ��TabbedTextOut��ʽ��

TabbedTextOut (	hdc, xStart, yStart, pString, iCount,
               					iNumTabs, piTabStops, xTabOrigin) ;

��������ִ��к���Ƕ�����λ��Ԫ����\t����0x09������TabbedTextOut����ݴ��ݸ������������н���λ��Ԫ��չΪ�ո�

TabbedTextOut��ǰ���������TextOut��ͬ����������������λ����������߸�����ͼ��Ϊ��λ����λ������С����磬���ƽ����Ԫ������8��ͼ�أ�����ϣ��ÿ5����Ԫ��һ����λ�������������н�����40��80��120��������˳���������ơ�

����������͵��߸�������0��NULL������λ�����ÿ�˸�ƽ����Ԫ�����趨�����������������1������߸�����ָ��һ����������ʾ��λ����ظ�����ı��������磬���������������1�����ҵ��߸�����ָ��ֵΪ30�ı���������λ����趨��30��60��90��ͼ�ش���������һ�����������˴���λ�����ʼ�������߼�x���꣬�����ִ�����ʼλ�ÿ�����ͬҲ���ܲ�ͬ��

��һ�����׵����������ʽ��ExtTextOut������Ext��ʾ������չ�ģ���

ExtTextOut (hdc, xStart, yStart, iOptions, &rect,
               					pString, iCount, pxDistance) ;

�����������ָ����νṹ��ָ�꣬��iOptions�趨ΪETO_CLIPPEDʱ���ýṹΪ���þ��Σ���iOptions�趨ΪETO_OPAQUEʱ���ýṹΪ��Ŀǰ����ɫ���ı������Ρ�������ѡ�������Զ����ã�Ҳ���Զ������á�

����һ���������������У���ָ�����ִ���������Ԫ�ļ������ʽ����ʹ����ʹ��Ԫ����խ��������Ϊ��ʱ��Ҫ�ڽ�խ�����е����������֡��ò��������趨ΪNULL��ʹ���ڶ�����Ԫ��ࡣ

���д���ֵĸ߼���ʽ��DrawText�����ǵ�һ�����������ڵ���������HELLOWIN��ʽʱ������ָ���������ʼλ�ã�����ͨ��RECT�ṹ��̬����ϣ����ʾ���ֵ�����

DrawText (hdc, pString, iCount, &rect, iFormat) ;

���������������ʽһ����DrawText��Ҫָ���ִ���ָ����ִ��ij��ȡ�Ȼ���������DrawText��ʹ����NULL��β���ִ����Ϳ��Խ�iCount�趨Ϊ-1��Windows���Զ������ִ��ij��ȡ�

��iFormat�趨Ϊ0ʱ��Windows�Ὣ���ֽ���Ϊһϵ����carriage return��Ԫ����\r����0x0D����linefeed��Ԫ����\n����0x0A���ָ����С����ִӾ��ε����Ͻǿ�ʼ��carriage return��Ԫ��linefeed��Ԫ������Ϊ������Ԫ�����Windows�����Ŀǰ�ж���ʼ�µ�һ�С��µ�һ�дӾ��ε���࿪ʼ������һ�е�����տ�һ����Ԫ�ĸ߶ȣ�û���ⲿ�������������ĸ���κ����ֶ�Ӧ����ʾ�������þ��εײ����ұ߻��±ߡ�

������ʹ��iFormat��������DrawText���ڶ�������iFormat��һ�����������ɡ�DT_LEFT��꣨Ԥ��ֵ��ָ�����������У�DT_RIGHTָ�������Ҷ�����У���DT_CENTERָ����λ춾�����ߺ��ұ��м���С���ΪDT_LEFT��ֵ��0���������ֻ��Ҫ����룬�Ͳ���Ҫ����ʶ���֡�

�������ϣ����carriage return��Ԫ��linefeed��Ԫ����Ϊ������Ԫ������԰���ʶ����DT_SINGLELINE��Ȼ�ᣬWindows���carriage return��Ԫ��linefeed��Ԫ����Ϊ����ʾ����Ԫ�������ǿ�����Ԫ����ʹ��DT_SINGLELINEʱ�������Խ���ָ��Ϊλ춾��εĶ��ˣ�DT_TOP�����׶�(DT_BOTTOM)�����м�(DT_VCETER��V��ʾ��ֱ)��

����ʾ��������ʱ��Windowsͨ��ֻ��carriage return��Ԫ��linefeed��Ԫ�����С�Ȼ��������еij��ȳ����˾��εĿ��ȣ������ʹ��DT_WORDBREAK��꣬��ʹWindows�������ֵ�ĩβ���С���춵��л�������ֵ���ʾ��Windows��ѳ������ε����ֲ��ֽ�ȥ������ʹ��DT_NOCLIP������������������껹�ӿ��˺�ʽ���ٶȡ���Windowsȷ���������ֵ��о�ʱ����ͨ��ʹ�ò����ⲿ������Ԫ�߶ȣ�����������о��м����ⲿ��࣬�Ϳ���ʹ�����DT_EXTERNALLEADING��

��������а�����λ��Ԫ����\t����0x09����������Ҫ�������DT_EXPANDTABS�����ڶ�����£���λ����趨�ÿ�˸���Ԫ��λ�á�ͨ��ʹ�����DT_TABSTOP��������ָ����ͬ����λ���������������£�iFormat�ĸ�λԪ�������ÿ������λ�������Ԫλ����ֵ�������ҽ���������ʹ��DT_TABSTOP����ΪiFormat�ĸ�λԪ��Ҳ���������ꡣ

DT_TABSTOP�����ڵ����⣬�������µĺ�ʽDrawTextEx�������������һ������IJ�����

DrawTextEx (hdc, pString, iCount, &rect, iFormat, &drawtextparams) ;

����һ��������ָ��DRAWTEXTPARAMS�ṹ��ָ�꣬���Ķ������£�

typedef struct tagDRAWTEXTPARAMS
{
   	UINT 	cbSize ;         			// size of structure
    	int  	iTabLength ;     		// size of each tab stop
    	int  	iLeftMargin ;    		// left margin
    	int  	iRightMargin ;   		// right margin
    	UINT 	uiLengthDrawn ;  		// receives number of characters processed
} DRAWTEXTPARAMS, * LPDRAWTEXTPARAMS ;

�м��������λ����ƽ����Ԫ������Ϊ��λ�ġ�

���ֵ�װ����������
��

�����������۵�SerTextAlign�⣬��������װ����������Ҳ�����ֲ�����Ӱ�졣���ڶ���װ�������£�������ɫ�Ǻ�ɫ������������������������и��ģ�

SetTextColor (hdc, rgbColor) ;

ʹ�û��ʵ���ɫ�ͻ�ˢ����ɫ��Windows��rgbColor��ֵת��Ϊ��ɫ��������ͨ������GetTextColorȡ��Ŀǰ���ֵ���ɫ��

Windows�ھ��εı�����������ʾ���֣������ܸ��ݱ���ģʽ���趨������ɫ��Ҳ���ܲ���������������ʹ��

SetBkMode (hdc, iMode) ;

���ı���ģʽ������iMode��ֵΪOPAQUE��TRANSPARENT���ڶ��ı���ģʽΪOPAQUE������ʾWindowsʹ�ñ�����ɫ�������εı�����������ʹ��

SetBkColor (hdc, rgbColor) ;

���ı䱳����ɫ��rgbColor��ֵ��ת��Ϊ��ɫ��ֵ���ڶ�����ɫ�ǰ�ɫ��

����������ֿ���̫��������һ���ı������ξͻ��ڸ���һ�������֡��������ԭ����ͨ��ϣ���ڶ��ı���ģʽ��TRANSPARENT���ڱ���ģʽΪTRANSPARENT������£�Windows����Ա���ɫ��Ҳ���Ծ��α���������ɫ��WindowsҲʹ�ñ���ģʽ�ͱ���ɫ�Ե������֮��Ŀ�϶����Ӱˢ����Ӱ���������ɫ����������������۵�������

����Windows��ʽ��WHITE_BRUSHָ��ΪWindows��춲����Ӵ������Ļ�ˢ����ˢ���Ӵ����ṹ��ָ����Ȼ����������ϣ������ʽ���Ӵ�������ʹ�����ڡ�����̨�����趨��ϵͳ��ɫ����һ�£�����������£�������WNDCLASS�ṹ��ָ��������ɫ�����ַ�ʽ��

wndclass.hbrBackground = COLOR_WINDOW + 1 ;

������Ҫ����ʾ������д����ʱ������ʹ��Ŀǰϵͳ��ɫ�趨����ɫ�ͱ���ɫ��

SetTextColor (hdc, GetSysColor (COLOR_WINDOWTEXT)) ;
SetBkColor (hdc, GetSysColor (COLOR_WINDOW)) ;

�����Щ���ᣬ�Ϳ���ʹ���ij�ʽ��ϵͳ��ɫ�ĸ��Ķ��仯��

case 	WM_SYSCOLORCHANGE :
     	InvalidateRect (hwnd, NULL, TRUE) ;
     	break ;

��һ��Ӱ�����ֵ�װ��������������Ԫ��ࡣ����Ԥ��ֵ��0����ʾWindows������Ԫ֮�������κοռ䣬��������ʹ�����º�ʽ����ռ䣺

SetTextCharacterExtra (hdc, iExtra) ;

����iExtra���߼���λ��Windows����ת��Ϊ��ӽ���ͼ�أ���������0���������iExtraȡΪ��ֵ��ϣ������Ԫ����ѹ��һ�𣩣�Windows����������ֵ�ľ���ֵ��Ҳ����˵��������ʹiExtra��ֵС�0��������ͨ������GetTextCharacterExtraȡ��Ŀǰ����Ԫ��࣬Windows�ڴ��ظ�ֵǰ�Ὣͼ�ؼ��ת��Ϊ�߼���λ��

ʹ�ñ�������
��

��������TextOut��TabbedTextOut��ExtTextOut��DrawText��DrawTextEx��д����ʱ��Windowsʹ��װ��������Ŀǰѡ������塣���嶨�����ض��������ʹ�С���Բ�ͬ������ʾ���ֵ���򵥷�����ʹ��Windows�ṩ�ı������壬Ȼ�������ķ�Χ�Ǻ����޵ġ�

�����Ժ�������ĺ�ʽȡ��ij�ֱ�������Ĵ��ţ�

hFont = GetStockObject (iFont) ;

���У�iFont�Ǽ���ʶ����֮һ��Ȼ�ᣬ���Ϳ��Խ�������ѡ��װ�����ݣ�

SelectObject (hdc, hFont) ;

��Щ��Ҳ����ֻ��һ����ɣ�

SelectObject (hdc, GetStockObject (iFont)) ;

���ڶ���װ��������ѡ��������Ϊϵͳ���壬�ܹ���GetStockObject��SYSTEM_FONT����ʶ�����ǵ��͵�ANSI��Ԫ�����塣��GetStockObject��ָ��SYSTEM_FIXED_FONT�����ڱ����ǰ�漸����ʽ��Ӧ�ù��������Ի�õȿ�����Ĵ��ţ���һ������Windows 3.0��ǰ��ϵͳ�������ݡ�����ϣ�����е����嶼������ͬ����ʱ�����Ǻܷ���ġ�

��������OEM_FIXED_FONTҲ��Ϊ�ն˻����壬��Windows��MS-DOS������ʾ�Ӵ���ʹ�õ����壬��������ԭʼIBM-PC��չ��Ԫ�����ݵ���Ԫ����Windows���Ӵ������С����ܱ��ͶԻ������������ʹ��DEFULT_GUI_FONT��

������������ѡ��װ������ʱ������ʹ��GetTextMetrics������Ԫ�ĸ߶Ⱥ�ƽ�����ȡ����ѡ���˵������壬��ôһ��Ҫע�⣬��Ԫ��ƽ������ֻ�Ǹ�ƽ��ֵ��ijЩ��Ԫ������������խ���ڱ��µ����棬�����˽⵽ȷ���ɲ�ͬ������Ԫ����ɵ��ִ��ܿ��ȵķ�����

����GetStockObjectȷʵ�ṩ�˴�ȡ��ͬ�������򵥷�ʽ�������������ܳ�ֿ�����Windows���ṩ�����塣���ã����ῴ��ָ�����������ʹ�С�ķ�����

����ı���
��

����ʣ��IJ�������춴�����ͬ�����塣���������Ӵ���Щ�ض���ʽ��ǰ����Windowsʹ������Ļ���֪ʶ��һ��������˽��Ǻ��кô��ġ�

������̬
��

Windows֧Ԯ���������壬����ν�ġ�GDI���塹�͡��豸���塹��GDI���崢����Ӳ���ĵ����У����豸����������豸�������еġ����磬ͨ��ӡ�����������ڽ����豸���弯��

GDI������������ʽ���������壬�ʻ������TrueType���塣

���������ÿ����Ԫ���Ե���ͼͼ��ͼ������ʽ���棬ÿ�ֵ������嶼���ض����ݺ�Ⱥ���Ԫ��С��Windowsͨ���򵥵ظ���ͼ�ص��л��оͿ�����GDI������������������Ԫ��Ȼ����ֻ�����������Ŵ����壬���Ҳ��ܳ���һ�����޶ȡ��������ԭ��GDI���������ֳ�Ϊ���������ŵġ����塣���Dz�������طŴ����С�������������Ҫ�ŵ�����ʾ���ܣ���ʾ�ٶȺܿ죩�Ϳɶ��ԣ���Ϊ���ֹ���Ƶģ����Ծ�������������

������ͨ����������ʶ��ģ������������������Ϊ��

System �����SYSTEM_FONT��

FixedSys �����SYSTEM_FIXED_FONT��

Terminal �����OEM_FIXED_FONT��

Courier

MS Serif

MS Sans Serif�����DEFAULT_GUI_FONT��

Small Fonts

ÿ����������ֻ�м��ִ�С��������6�֣���Courier�����Ƕ������壬�������ô��ֻ�������������ơ���Serif��ָ������ĸ�ʻ��ڽ���ʱ�ո�С�䡣��sans serif�����岻��serif������塣��Windows�����ڰ汾�У�MS��Microsoft��Serif��MS Sans Serif���屻��ΪTms Rmn��ָ����Times Roman���ƣ���Helv����Helvetica���ƣ���Small Fonts��רΪ��ʾС����Ƶġ�

��Windows3.1��ǰ������GDI�����⣬Windows���ṩ������ֻ�бʻ����塣�ʻ��������ԡ�����㡹�ķ�ʽ�����һϵ���߶Σ��ʻ�����������������ţ�����ζ��ͬ�������������춾����κν����ȵ�ͼ������豸������������ԷŴ����С������ߴ硣�������������ܲ��ã�С����Ŀɶ���Ҳ���㣬����������춱ʻ��ǵ���ֱ�߶��Եúܵ������ʻ�������ʱҲ��Ϊ��ͼ�����壬��Ϊ�����ر��ʺ�춻�ͼ�������Dz��ʺ�춱�ij��ϡ��ʻ�����������У�Modern��Roman��Script��

���GDI���������GDI�ʻ����壬Windows�����ԡ��ϳɡ����塢б�塢�ӵ��ߺͼ�ɾ���ߣ�������ҪΪÿ���������ⴢ�����塣���磬���б�壬Windowsֻ��Ҫ����Ԫ���ϲ������ƶ��Ϳ����ˡ�

��������Truetype���ҽ��ڱ��µ�ʣ������Ҫ��������

TrueType����
��

TrueType����ĵ�����Ԫ��ͨ������ֱ�ߺ����ߵ�����������ġ�Windows����ͨ���ı䶨�������������TrueType����������š�

����ʽ��ʼʹ���ض���С��TrueType����ʱ��Windows�����󻯡����塣�����˵Windowsʹ��TrueType���嵵���а����ġ���ʾ����ÿ����Ԫ������ֱ�ߺ����ߵ�����������š���Щ��ʾ���Բ���������ϳɵ���Ԫ��ú��ѿ������磬��ijЩ�����У���дH������Ӧ��һ��������äĿ������������ܻᵼ������һ����ͼ�ر���һ������������ʾ�Ϳ��Ա�����Щ����������Ȼ�ᣬÿ����Ԫ�ĺϳ�������춽�����Ԫ�ĵ���ͼ����Щ����ͼ�����ڼ������Ա�����ʹ�á�

�����Windowsʹ����13��TrueType���壬���ǵ������������£�

Courier New

Courier New Bold

Courier New Italic

Courier New Bold Italic

Times New Roman

Times New Roman Bold

Times New Roman Italic

Times New Roman Bold Italic

Arial

Arial Bold

Arial Italic

Arial Bold Italic

Symbol

���µ�Windows�汾�У�����б������ˡ��ڴ��ر�ָ�����ҽ�ʹ��Lucida Sans Unicode���壬��������һЩ�����������ط�ʹ�õ���ĸ����

������Ҫ����ϵ��������������ƣ�Courier New�Ƕ������塣�������������Ǵ��ֻ���������塣Times New Roman��Times����ĸ���Ʒ�����������Ϊ��Times of London����ƣ�����������ӡˢ�����ϣ������кܺõĿɶ��ԡ�Arial��Helvetica����ĸ���Ʒ����һ��sans serif���塣Symbol�����������д���ż���

���Ի���ʽ
��

�������TrueType�����б��У�����ע�⵽��Courier��Times New Roman��Arial�Ĵ����б���Ǵ����Լ��������Ƶĵ������壬��һ�����봫ͳ�İ�ʽһ�¡�Ȼ��������ʹ������Ϊ�����б��ֻ��������������⡸���ԡ���Windows�ڶ�����������������оٺ�ѡ��ķ�ʽʱ�����������Եķ����������TrueType���壬�������ʹ�ô�ͳ��������ʽ��

���ֳ�ͻ��Windows�л�û����ȫ����������֮����������ȫͨ���������ض�������ѡ�����塣Ȼ���ڴ��������о�ʱ��Ӧ�ó�ʽ��Ҫϵͳ�е������б�����������Ԥ�ϣ�����˫�ش���ʹ���⸴�ӻ��ˡ�

��ֵ
��

�ڴ�ͳ�İ�ʽ�У����������������ƺʹ�С��ָ�����壬����Ĵ�С�Ե�ĵ�λ����ʾ��һ����1/72Ӣ��ܽӽ��������Ƿdz��ӽ�������ڵ�������ͨ������Ϊ1/72Ӣ�硣��ֵͨ������Ϊ��ĸ���ˣ��������������ţ�����ĸ�׶˵ĸ߶ȣ����磬��ĸ��bq�����ܸ߶ȡ�����һ�����������С�ļ򵥷�ʽ������ͨ�����Ǻܾ�ȷ��

����ĵ�ֵʵ�������Ű���Ƶĸ�������Ƕ�������ض���������Ԫ�Ĵ�С���ܻ��춻�С����ֵ����ʾ�Ĵ�С���ڴ�ͳ���Ű��У���ʹ�õ�ֵ��ָ������Ĵ�С���ڵ����Ű��У���������������ȷ����Ԫ��ʵ�ʴ�С��

����ͼ��
��

�ڵ������������ᵽ������ͨ������GetTextMetricsȡ��װ��������Ŀǰѡ���������Ѷ������Ҳ���ʹ�ù������ʽ��ͼ4-3��ʾ��FONTMETRIC�ṹ������Ĵ�ֱ��С��

TEXTMETRIC�ṹ����һ����λ��tmExternalLeading���ʡ������leading�����������ֹ����ڽ����ֿ������Ǧ�����������������֮������հס�tmInternalLeadingֵ��Ϊ�������ű����Ŀռ��йأ�tmExternalLeading��ʾ��Ԫ��������֮�������ĸ��ӿռ䡣��ʽд���߿���ʹ�û�����ⲿ�ļ��ֵ��

������˵һ��������8���12��ʱ��ָ���Dz����ڲ�����ĸ߶ȡ�ij�ִ�д��ĸ�ϵķ�������ռ���˷ָ��еļ�ࡣ������TEXTMETRIC�ṹ��tmHeightֵʵ��ָ�м�����������ĵ�ֵ������ĵ�ֵ����tmHeight��tmInternalLeading�õ���

�߼�Ӣ������
��

���������ڵ����¡��豸�Ĵ�С��һ���������۵ģ�Windows 98��ϵͳ���嶨��Ϊ����12���о��10�����塣�����ڡ���ʾ���ԡ��Ի�������ѡ����ǡ�С���塹���ǡ������塹���������tmHeightֵΪ16��20ͼ�أ�tmHeight��ȥtmInternalLeading��ֵΪ13��16ͼ�ء������������ѡ��Ͱ�ָ��ÿӢ��ĵ���Ϊ��λ���豸�����ȣ�ѡ��С���塹��Ϊ96dpi��ѡ�񡸴����塹��Ϊ120dpi��

��������LOGPIXELSX��LOGPIXELSY��������GetDeviceCaps��ȡ�ø��豸�����ȡ���ˣ�96��120ͼ����өĻ��ռ�еĶ���������Գ�Ϊ���߼�Ӣ�硹��������ó߲���өĻ������ͼ�أ��Ϳ��ܷ����߼�Ӣ��Ҫ��ʵ�ʵ�Ӣ���һЩ��Ϊʲô�������أ�

��ֽ���ϣ�ÿӢ�����14��8�����Ԫ�ܷ����Ķ���������������鴦����д��Ӧ�ó�ʽʱ������ϣ������ʾ������ʾ������8�����ͣ������ʹ����Ѷ��ʾ����ʵ�ʳߴ磬��û���㹻��ͼ����������ʾ��Ԫ����ʹ��ʾ�������㹻�Ľ����ȣ���өĻ���Ķ�8��������Ȼ�������⡣�������Ķ�ֽ�ϵ�ӡˢ��ʱ���۾������ֵľ���ͨ��ΪһӢ�ߣ���ʹ����Ѷ��ʾ��ʱ���������ͨ��Ϊ��Ӣ�ߡ�

�߼�Ӣ����Ч�ض�өĻ�����˷Ŵ��ܹ���ʾС��8����������塣���ң�ÿӢ��96��ʹ640ͼ�ص���С��ʾ��С��춴�Լ6.5Ӣ�硣��ǡǡ����ҳ�߾�Ϊ1Ӣ���8.5Ӣ�����ֽ����ӡ�����ֵĿ��ȡ�������߼�Ӣ��Ҳ������өĻ���ȣ������ܴ����ʾ���֡�

�����ܻ��ǵ��ڵ����£�Windows NT��������Щ��ͬ����Windows NT�У���GetDeviceCaps�еõ���LOGPIXELSX��ÿӢ���ͼ������ֵ�����HORZRESֵ��ͼ����������HORZSIZEֵ�����������ٳ���25.4��ֵ���Դ����ƣ�LOGPIXELSY��VERTRES��VERTSIZEҲ��һ�¡�Windows��Ϊ��ͬӳ�䷽ʽ�����Ӵ���ƫ�Ʒ�Χʱ��ʹ��HORZRES��HORZSIZE��VERTRES��VERTSIZEֵ��Ȼ������ʾ���ֵij�ʽ��ò�Ҫʹ�ø���LOGPIXELSX��LOGPIXELSYʹ�üٶ�����ʾ�����ȣ���һ����Windows 98��Ϊһ�¡�

���ԣ���Windows NT�£�����ʽ���ض��ĵ�ֵ��ʾ����ʱ�������ܲ�ʹ��Windows�ṩ��ӳ�䷽ʽ����ʽ������Windows 98һ����ÿӢ����߼�ͼ�����������Լ���ӳ�䷽ʽ���ҽ�����������ֵ�ӳ�䷽ʽ��Ϊ��Logical Twips��ӳ�䷽ʽ���������趨���£�

SetMapMode 	(hdc, MM_ANISOTROPIC) ;
SetWindowExtEx 	 (hdc, 1440, 1440, NULL) ;
SetViewportExt 	 (hdc,	GetDeviceCaps (hdc, LOGPIXELSX),
                     	GetDeviceCaps (hdc, LOGPIXELSY), NULL) ;

ʹ������ӳ�䷽ʽ�趨�����ܹ��Ե�ֵ��20����ָ�������С�����磬Ϊ12����ȡ240��ע�⣬��MM_TWIPSӳ�䷽ʽ��ͬ��yֵ��өĻ������������������ʾ���ֵ�������ʱ�ܷ��㡣

���ס���߼�Ӣ����ʵ��Ӣ���IJ��������ʾ�����ڡ�����ӡ�豸�ϣ�GDI�ͳ�����ȫһ�µġ�

�߼�����
��

��Ȼ�����Ѿ���ȷ���߼�Ӣ����߼���λ�ĸ����ô�������Ǿ��������߼����塣

�߼�������һ��GDI��������Ĵ��Ŵ�����HFONT��̬�ı����У��߼���������������������߼����ʼ��߼���ˢһ�������dz���������ֻ�е�Ӧ�ó�ʽ����SelectObject����ѡ��װ������ʱ�����ų�Ϊ��ʵ����������磬����߼����ʣ�������Ϊ����ָ���������ɫ����������������ѡ��װ������ʱ��Windows�Ž���ת��Ϊ�豸����Ч����ɫ��ֻ�д�ʱ��Windows��֪���豸��ɫ��������

�߼�����Ľ�����ѡ��
��

������͸������CreateFont��CreateFontIndirect�������߼����塣CreateFontIndirect��ʽ����һ��ָ��LOGFONT�ṹ��ָ�꣬�ýṹ��14����λ��CreateFont��ʽ����14��������������LOGFONT�ṹ��14����λ��ʽ��ͬ�������ǽ��е����������߼�����ĺ�ʽ�����ᵽ��һ�㣬����ΪWindows�����������������������ĺ�ʽ������Ϊ���Ѽ�ס14����λ�����Ժ���ʹ��CreateFont����ˣ�����Ҫ����CreateFontIndirect��

�����ֻ����ķ�ʽ��춶���LOGFONT�ṹ�е���λ���Ա����CreateFontIndirect��

  • �����Լòµ¥µØ½ï¿½LOGFONT�ṹ����λ�趨Ϊ������������硣����������£��ں���SelectObjectʱ��Windowsʹ�á�����ӳ�䡹���㷨���豸����Ч��������ѡ������Щ������ƥ������塣������������Ѷ��ʾ����ӡ�����ϵ���Ч���壬������������������Ҫ�����൱��IJ��
    ��
  • �������о��豸�ϵ��������岢����ѡ�������öԻ������������ʾ��ʹ���ߡ��ҽ��ڱ����������������оٺ�ʽ�����������������Ѿ��������ˣ���Ϊ�����ַ���Ҳ���Խ����о١�
    ��
  • �����Բ��üòµ¥µÄ·ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ChooseFont��ʽ�����ڵ�ʮһ�������۹������ʽ���ܹ�ʹ��LOGFONT�ṹֱ�ӽ������塣
    ��

�ڱ��£���ʹ�õ�һ�ֺ͵����ַ�����

�����ǽ�����ѡ���ɾ���߼�����ij���

  1. ͨ������CreateFont��CreateFontIndirect�����߼����壬��Щ��ʽ����HFONT��̬���߼�������š�
  2. ʹ��SelectObject���߼�����ѡ��װ�����ݣ�Windows��ѡ�����߼�������ƥ�����ʵ���塣
  3. ʹ��GetTextMetrics���������õ���������ʽ��ȷ����ʵ����Ĵ�С�����硣�ڸ�����ѡ��װ�������ᣬ����ʹ����Щ��Ѷ���ʵ����趨���ֵļ�ࡣ
  4. ��ʹ�����߼������ᣬ����DeleteObjectɾ���߼����壬������ѡ����Ч��װ������ʱ����Ҫɾ�����壬Ҳ��Ҫɾ���������塣

GetTextFace��ʽʹ��ʽ�ܹ�ȷ��Ŀǰѡ��װ�����ݵ��������ƣ�

GetTextFace (hdc, sizeof (szFaceName) / sizeof (TCHAR), szFaceName) ;

��ϸ��������Ѷ���Դ�GetTextMetrics�еõ���

GetTextMetrics (hdc, &textmetric) ;

���У�textmetric��TEXTMETRIC��̬�ı�����������20����λ��

�����ҽ���ϸ����LOGFONT��TEXTMETRIC�ṹ����λ���������ṹ��һЩ���Ƶ���λ�������������׻�����������ֻ���ס��LOGFONT��춶����߼����壬��TEXTMETRIC���ȡ��Ŀǰѡ��װ�������е�������Ѷ��

PICKFONT��ʽ
��

ʹ�ó�ʽ17-1��ʾ��PICKFONT�����Զ���LOGFONT�ṹ��������λ�������ʽ�����߼����壬�����߼�����ѡ��װ����������ʾ��ʵ��������硣���Ǹ�����ij�ʽ��ͨ�������ǿ����˽��߼�����ӳ��Ϊ��ʵ����ķ�ʽ��

 ï¿½ï¿½Ê½17-1  PICKFONT
PICKFONT.C
/*----------------------------------------------------------------------------
   	PICKFONT.C -- 		Create Logical Font
                 							(c) Charles Petzold, 1998
---------------------------------------------------------------------------*/

#include <windows.h>
#include "resource.h"

	// Structure shared between main window and dialog box
typedef struct
{
     	int        				iDevice, iMapMode ;
     	BOOL       				fMatchAspect ;
     	BOOL       				fAdvGraphics ;
     	LOGFONT    			lf ;
     	TEXTMETRIC 		tm ;
     	TCHAR      				szFaceName [LF_FULLFACESIZE] ;
}
DLGPARAMS ;
     	// Formatting for BCHAR fields of TEXTMETRIC structure
#ifdef UNICODE
#define BCHARFORM TEXT ("0x%04X")
#else
#define BCHARFORM TEXT ("0x%02X")
#endif

     	// Global variables
HWND  hdlg ;
TCHAR szAppName[] = TEXT ("PickFont") ;

     	// Forward declarations of functions
LRESULT	CALLBACK WndProc 		(HWND, UINT, WPARAM, LPARAM) ;
BOOL  		CALLBACK DlgProc 	(HWND, UINT, WPARAM, LPARAM) ;
void SetLogFontFromFields    	(HWND hdlg, DLGPARAMS * pdp) ;
void SetFieldsFromTextMetric 	(HWND hdlg, DLGPARAMS * pdp) ;
void MySetMapMode	(HDC hdc, int iMapMode) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    							PSTR szCmdLine, int iCmdShow)
{
     	HWND     				hwnd ;
     	MSG      				msg ;
     	WNDCLASS 			wndclass ;
     
     	wndclass.style         		    = CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   			= WndProc ;
     	wndclass.cbClsExtra    			= 0 ;
     	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     			= hInstance ;
     	wndclass.hIcon         			= LoadIcon (NULL, IDI_APPLICATION) ;
     	wndclass.hCursor       			= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  		= szAppName ; 
     	wndclass.lpszClassName 		= szAppName ;
     
     	if (!RegisterClass (&wndclass))
     	{
          			MessageBox (	NULL, TEXT ("This program requires Windows NT!"),
               								szAppName, MB_ICONERROR) ;
      			return 0 ;
     }
     
     	hwnd = CreateWindow (	szAppName, TEXT ("PickFont: Create Logical Font"),
                          		WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
                          		CW_USEDEFAULT, CW_USEDEFAULT,
                          		CW_USEDEFAULT, CW_USEDEFAULT,
                          		NULL, NULL, hInstance, NULL) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     
     	while (GetMessage (&msg, NULL, 0, 0))
	{
          		if (hdlg == 0 || !IsDialogMessage (hdlg, &msg))
          		{
               				TranslateMessage (&msg) ;
               				DispatchMessage (&msg) ;
          		}
     	}
     	return msg.wParam ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
     	static DLGPARAMS dp ;
     	static TCHAR     		szText[] = 	TEXT ("\x41\x42\x43\x44\x45 ")
                        TEXT ("\x61\x62\x63\x64\x65 ")

                        TEXT ("\xC0\xC1\xC2\xC3\xC4\xC5 ")
                        TEXT ("\xE0\xE1\xE2\xE3\xE4\xE5 ") 
#ifdef UNICODE
                        TEXT ("\x0390\x0391\x0392\x0393\x0394\x0395 ")
                        TEXT ("\x03B0\x03B1\x03B2\x03B3\x03B4\x03B5 ")
                        TEXT ("\x0410\x0411\x0412\x0413\x0414\x0415 ")
                        TEXT ("\x0430\x0431\x0432\x0433\x0434\x0435 ")
                        TEXT ("\x5000\x5001\x5002\x5003\x5004") 
#endif
                                 ;
     	HDC              						hdc ;
     	PAINTSTRUCT      				ps ;
     	RECT             						rect ;
     
     	switch (message)
     	{
     	case WM_CREATE:
          			dp.iDevice = IDM_DEVICE_SCREEN ;
          			hdlg = CreateDialogParam (((LPCREATESTRUCT) lParam)->hInstance, 
                szAppName, hwnd, DlgProc, (LPARAM) &dp) ;
          			return 0 ;
     	case 	WM_SETFOCUS:
          			SetFocus (hdlg) ;
          			return 0 ;

     	case 	WM_COMMAND:
			switch (LOWORD (wParam))
          			{
          			case 	IDM_DEVICE_SCREEN:
          			case 	IDM_DEVICE_PRINTER:
               			CheckMenuItem (GetMenu (hwnd), dp.iDevice, MF_UNCHECKED) ;
               			dp.iDevice = LOWORD (wParam) ;
               			CheckMenuItem (GetMenu (hwnd), dp.iDevice, MF_CHECKED) ;
               			SendMessage (hwnd, WM_COMMAND, IDOK, 0) ;
               			return 0 ;
          			}
          			break ;

     	case 	WM_PAINT:
          			hdc = BeginPaint (hwnd, &ps) ;

               				// Set graphics mode so escapement works in Windows NT

          SetGraphicsMode (hdc, dp.fAdvGraphics ? GM_ADVANCED : GM_COMPATIBLE) ;

               				// Set the mapping mode and the mapper flag

          			MySetMapMode (hdc, dp.iMapMode) ;
          			SetMapperFlags (hdc, dp.fMatchAspect) ;

               				// Find the point to begin drawing text

          			GetClientRect (hdlg, &rect) ;
          			rect.bottom += 1 ;
          			DPtoLP (hdc, (PPOINT) &rect, 2) ;

               				// Create and select the font; display the text

          		SelectObject (hdc, CreateFontIndirect (&dp.lf)) ;
          		TextOut (hdc, rect.left, rect.bottom, szText, lstrlen (szText)) ;

          		DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
          		EndPaint (hwnd, &ps) ;
          			return 0 ;
          
     	case 	WM_DESTROY:
          			PostQuitMessage (0) ;
          			return 0 ;
     	}
     	return DefWindowProc (hwnd, message, wParam, lParam) ;
}

BOOL CALLBACK DlgProc (	HWND hdlg, UINT message, WPARAM wParam,LPARAM lParam)
{
     	static DLGPARAMS 		* 	pdp ;
     	static PRINTDLG    			pd = { sizeof (PRINTDLG) } ;
     	HDC                							hdcDevice ;
     	HFONT        							hFont ;
     
	switch (message)
     	{
     	case 	WM_INITDIALOG:
               		// Save pointer to dialog-parameters structure in WndProc

          			pdp = (DLGPARAMS *) lParam ;

          			SendDlgItemMessage (hdlg, 	IDC_LF_FACENAME, EM_LIMITTEXT, 
LF_FACESIZE - 1, 0) ;
         CheckRadioButton (hdlg,IDC_OUT_DEFAULT, IDC_OUT_OUTLINE,
                                  		  IDC_OUT_DEFAULT) ;
         CheckRadioButton (hdlg,IDC_DEFAULT_QUALITY, IDC_PROOF_QUALITY,
                                  	      IDC_DEFAULT_QUALITY) ;
         CheckRadioButton (hdlg,IDC_DEFAULT_PITCH, IDC_VARIABLE_PITCH,
                                  		  IDC_DEFAULT_PITCH) ;
         CheckRadioButton (hdlg,IDC_FF_DONTCARE, IDC_FF_DECORATIVE,
                                  		  IDC_FF_DONTCARE) ;
         CheckRadioButton (hdlg,IDC_MM_TEXT, IDC_MM_LOGTWIPS,
                                  		  IDC_MM_TEXT) ;
           SendMessage (hdlg, WM_COMMAND, IDOK, 0) ;
										// fall through
        	case 	WM_SETFOCUS:
          			SetFocus (GetDlgItem (hdlg, IDC_LF_HEIGHT)) ;
          			return FALSE ;

     	   case 	WM_COMMAND:
          			switch (LOWORD (wParam))
          			{
          			case 	IDC_CHARSET_HELP:
           					MessageBox (	hdlg, 
                    TEXT	("0 		= 	Ansi\n")
                    TEXT	("1 		= 	Default\n")
                    TEXT	("2 		= 	Symbol\n")
                    TEXT 	("128 	= 	Shift JIS (Japanese)\n")
                    TEXT 	("129 	= 	Hangul (Korean)\n")
                    TEXT 	("130 	= 	Johab (Korean)\n")
                    TEXT 	("134 	= 	GB 2312 (Simplified Chinese)\n")
                    TEXT 	("136 	= 	Chinese Big 5 (Traditional Chinese)\n")
                    TEXT 	("177 	= 	Hebrew\n")
                    TEXT 	("178 	= 	Arabic\n")
                    TEXT 	("161 	= 	Greek\n")
                    TEXT 	("162 	= 	Turkish\n")
                    TEXT 	("163 	= 	Vietnamese\n")
                    TEXT 	("204 	= 	Russian\n")
                    TEXT 	("222 	= 	Thai\n")
                    TEXT 	("238 	= 	East European\n")
                    TEXT 	("255 	= 	OEM"),
                      szAppName, MB_OK | MB_ICONINFORMATION) ;
               					return TRUE ;

               					// These radio buttons set the lfOutPrecision field

          			case 	IDC_OUT_DEFAULT:   
               					pdp->lf.lfOutPrecision = OUT_DEFAULT_PRECIS ;  
               					return TRUE ;

          			case 	IDC_OUT_STRING:
               					pdp->lf.lfOutPrecision = OUT_STRING_PRECIS ;  
               					return TRUE ;

          			case 	IDC_OUT_CHARACTER:
               					pdp->lf.lfOutPrecision = OUT_CHARACTER_PRECIS ;  
               					return TRUE ;

          			case 	IDC_OUT_STROKE:
               					pdp->lf.lfOutPrecision = OUT_STROKE_PRECIS ;  
               					return TRUE ;

          			case 	IDC_OUT_TT:
               					pdp->lf.lfOutPrecision = OUT_TT_PRECIS ;  
               					return TRUE ;

          			case	IDC_OUT_DEVICE:
               					pdp->lf.lfOutPrecision = OUT_DEVICE_PRECIS ;  
               					return TRUE ;

          			case 	IDC_OUT_RASTER:
               					pdp->lf.lfOutPrecision = OUT_RASTER_PRECIS ;  
               					return TRUE ;

          			case 	IDC_OUT_TT_ONLY:
               					pdp->lf.lfOutPrecision = OUT_TT_ONLY_PRECIS ;  
               					return TRUE ;

          			case 	IDC_OUT_OUTLINE:
               					pdp->lf.lfOutPrecision = OUT_OUTLINE_PRECIS ;  
               					return TRUE ;

               				// These three radio buttons set the lfQuality field

          			case 	IDC_DEFAULT_QUALITY:
               					pdp->lf.lfQuality = DEFAULT_QUALITY ;   
               					return TRUE ;

          			case 	IDC_DRAFT_QUALITY:
              	 				pdp->lf.lfQuality = DRAFT_QUALITY ;  
               					return TRUE ;

          			case 	IDC_PROOF_QUALITY:
               					pdp->lf.lfQuality = PROOF_QUALITY ;  
               					return TRUE ;

               				// These three radio buttons set the lower nibble
               				//   of the lfPitchAndFamily field

          			case 	IDC_DEFAULT_PITCH:
               					pdp->lf.lfPitchAndFamily = 
                    (0xF0 & pdp->lf.lfPitchAndFamily) | DEFAULT_PITCH ; 
               					return TRUE ;

          			case 	IDC_FIXED_PITCH:
               					pdp->lf.lfPitchAndFamily = 
                    (0xF0 & pdp->lf.lfPitchAndFamily) | FIXED_PITCH ; 
               					return TRUE ;

          			case 	IDC_VARIABLE_PITCH:
               					pdp->lf.lfPitchAndFamily = 
                    (0xF0 & pdp->lf.lfPitchAndFamily) | VARIABLE_PITCH ;  
               					return TRUE ;

               					// These six radio buttons set the upper nibble
               					//   of the lfPitchAndFamily field

          			case 	IDC_FF_DONTCARE:
               					pdp->lf.lfPitchAndFamily =
                     (0x0F & pdp->lf.lfPitchAndFamily) | FF_DONTCARE ;  
               					return TRUE ;

          			case 	IDC_FF_ROMAN:
               					pdp->lf.lfPitchAndFamily =
                     (0x0F & pdp->lf.lfPitchAndFamily) | FF_ROMAN ;  
               					return TRUE ;

          			case 	IDC_FF_SWISS:
               					pdp->lf.lfPitchAndFamily =
                    (0x0F & pdp->lf.lfPitchAndFamily) | FF_SWISS ;  
               					return TRUE ;

          			case 	IDC_FF_MODERN:
               					pdp->lf.lfPitchAndFamily =
                    (0x0F & pdp->lf.lfPitchAndFamily) | FF_MODERN ;  
               					return TRUE ;

          			case 	IDC_FF_SCRIPT:
               					pdp->lf.lfPitchAndFamily =
                    (0x0F & pdp->lf.lfPitchAndFamily) | FF_SCRIPT ;  
               					return TRUE ;

          			case 	IDC_FF_DECORATIVE:
               					pdp->lf.lfPitchAndFamily =
                    (0x0F & pdp->lf.lfPitchAndFamily) | FF_DECORATIVE ;  
               					return TRUE ;

               					// Mapping mode:

          			case 	IDC_MM_TEXT:
          			case 	IDC_MM_LOMETRIC:
          			case 	IDC_MM_HIMETRIC:
          			case 	IDC_MM_LOENGLISH:
          			case 	IDC_MM_HIENGLISH:
          			case 	IDC_MM_TWIPS:
          			case 	IDC_MM_LOGTWIPS:
               					pdp->iMapMode = LOWORD (wParam) ;
               					return TRUE ;

               					// OK button pressed
               					// -----------------

          			case 	IDOK:
                    						// Get LOGFONT structure

               					SetLogFontFromFields (hdlg, pdp) ;

                    	// Set Match-Aspect and Advanced Graphics flags

			pdp->fMatchAspect = IsDlgButtonChecked 	(hdlg,	IDC_MATCH_ASPECT) ;
			pdp->fAdvGraphics = IsDlgButtonChecked 	(hdlg,	IDC_ADV_GRAPHICS) ;

				// Get Information Context

			if (pdp->iDevice == IDM_DEVICE_SCREEN)
			{
				hdcDevice = CreateIC (TEXT ("DISPLAY"), NULL, NULL, NULL) ;
			}
               			else
              	 		{
                    				pd.hwndOwner = hdlg ;
                    				pd.Flags = PD_RETURNDEFAULT | PD_RETURNIC ;
                    				pd.hDevNames = NULL ;
                    				pd.hDevMode = NULL ;

                    				PrintDlg (&pd) ;

                    				hdcDevice = pd.hDC ;
         			}
                   				// Set the mapping mode and the mapper flag

               			MySetMapMode (hdcDevice, pdp->iMapMode) ;
			SetMapperFlags (hdcDevice, pdp->fMatchAspect) ;

                    				// Create font and select it into IC

               			hFont = CreateFontIndirect (&pdp->lf) ;
               			SelectObject (hdcDevice, hFont) ;

                    				// Get the text metrics and face name

               			GetTextMetrics (hdcDevice, &pdp->tm) ;
               			GetTextFace (hdcDevice, LF_FULLFACESIZE, pdp->szFaceName) ;
			DeleteDC (hdcDevice) ;
               			DeleteObject (hFont) ;

                    			// Update dialog fields and invalidate main window

               			SetFieldsFromTextMetric (hdlg, pdp) ;
               			InvalidateRect (GetParent (hdlg), NULL, TRUE) ;
               			return TRUE ;
          		}
          		break ;
     	}
  	return FALSE ;
}
void SetLogFontFromFields (HWND hdlg, DLGPARAMS * pdp)
{
     	pdp->lf.lfHeight  = 	GetDlgItemInt (hdlg, IDC_LF_HEIGHT,  NULL, TRUE) ;
     	pdp->lf.lfWidth   = 	GetDlgItemInt (hdlg, IDC_LF_WIDTH,  NULL, TRUE) ;
     	pdp->lf.lfEscapement=GetDlgItemInt (hdlg, IDC_LF_ESCAPE, NULL, TRUE) ;
     	pdp->lf.lfOrientation=GetDlgItemInt (hdlg,IDC_LF_ORIENT,	NULL, TRUE) ;
     	pdp->lf.lfWeight  =GetDlgItemInt (hdlg, IDC_LF_WEIGHT, NULL, TRUE) ;
     	pdp->lf.lfCharSet =GetDlgItemInt (hdlg, IDC_LF_CHARSET, NULL, FALSE) ;
     	pdp->lf.lfItalic    =IsDlgButtonChecked(hdlg,IDC_LF_ITALIC) == BST_CHECKED ;
     	pdp->lf.lfUnderline =IsDlgButtonChecked (hdlg, IDC_LF_UNDER)  == BST_CHECKED ;
     	pdp->lf.lfStrikeOut =IsDlgButtonChecked (hdlg, IDC_LF_STRIKE) == BST_CHECKED ;
     	GetDlgItemText (hdlg, IDC_LF_FACENAME, pdp->lf.lfFaceName, LF_FACESIZE) ;
}

void SetFieldsFromTextMetric (HWND hdlg, DLGPARAMS * pdp) 
{
     	TCHAR   		szBuffer [10] ;
     	TCHAR * 		szYes 	= TEXT ("Yes") ; 
     	TCHAR * 		szNo	= TEXT ("No") ;
     	TCHAR * 		szFamily [] = {	TEXT ("Don't Know"),	
TEXT ("Roman"),
                        TEXT ("Swiss"), TEXT ("Modern"),
                        TEXT ("Script"), TEXT ("Decorative"), 
                        TEXT ("Undefined") } ;

     	SetDlgItemInt (hdlg, IDC_TM_HEIGHT, pdp->tm.tmHeight,  	TRUE) ;
     	SetDlgItemInt (hdlg, IDC_TM_ASCENT, pdp->tm.tmAscent,    TRUE) ;
     	SetDlgItemInt (hdlg, IDC_TM_DESCENT,pdp->tm.tmDescent, 	TRUE) ;
     	SetDlgItemInt (hdlg, IDC_TM_INTLEAD,pdp->tm.tmInternalLeading,  		 TRUE) ;
     	SetDlgItemInt (hdlg, IDC_TM_EXTLEAD,pdp->tm.tmExternalLeading,  	 TRUE) ;
     	SetDlgItemInt (hdlg, IDC_TM_AVECHAR,pdp->tm.tmAveCharWidth,     			    TRUE) ;
     	SetDlgItemInt (hdlg, IDC_TM_MAXCHAR,  	pdp->tm.tmMaxCharWidth,     			TRUE) ;
     	SetDlgItemInt (hdlg, IDC_TM_WEIGHT,   	pdp->tm.tmWeight,           			TRUE) ;
     	SetDlgItemInt (hdlg, IDC_TM_OVERHANG,	pdp->tm.tmOverhang,         			TRUE) ;
     	SetDlgItemInt (hdlg, IDC_TM_DIGASPX,  	pdp->tm.tmDigitizedAspectX, 		    TRUE) ;
     	SetDlgItemInt (hdlg, IDC_TM_DIGASPY,  	pdp->tm.tmDigitizedAspectY, 		    TRUE) ;

     	wsprintf (szBuffer, BCHARFORM, pdp->tm.tmFirstChar) ;
     	SetDlgItemText (hdlg, IDC_TM_FIRSTCHAR, szBuffer) ;

     	wsprintf (szBuffer, BCHARFORM, pdp->tm.tmLastChar) ;
     	SetDlgItemText (hdlg, IDC_TM_LASTCHAR, szBuffer) ;

     	wsprintf (szBuffer, BCHARFORM, pdp->tm.tmDefaultChar) ;
     	SetDlgItemText (hdlg, IDC_TM_DEFCHAR, szBuffer) ;

     	wsprintf (szBuffer, BCHARFORM, pdp->tm.tmBreakChar) ;
     	SetDlgItemText (hdlg, IDC_TM_BREAKCHAR, szBuffer) ;

     	SetDlgItemText (hdlg, IDC_TM_ITALIC, 	pdp->tm.tmItalic 			? szYes : szNo) ;
     	SetDlgItemText (hdlg, IDC_TM_UNDER, 	 	pdp->tm.tmUnderlined	? szYes : szNo) ;
     	SetDlgItemText (hdlg, IDC_TM_STRUCK, 	pdp->tm.tmStruckOut		? szYes : szNo) ;

     	SetDlgItem	Text (hdlg, IDC_TM_VARIABLE, 
               					TMPF_FIXED_PITCH & pdp->tm.tmPitchAndFamily ? szYes : szNo) ;

     	SetDlgItem	Text (hdlg, IDC_TM_VECTOR, 
               					TMPF_VECTOR & pdp->tm.tmPitchAndFamily ? szYes : szNo) ;

     	SetDlgItem	Text (hdlg, IDC_TM_TRUETYPE, 
               					TMPF_TRUETYPE & pdp->tm.tmPitchAndFamily ? szYes : szNo) ;

     	SetDlgItem	Text (hdlg, IDC_TM_DEVICE, 
               					TMPF_DEVICE & pdp->tm.tmPitchAndFamily ? szYes : szNo) ;

     	SetDlgItem	Text (hdlg, IDC_TM_FAMILY, 
               					szFamily [min (6, pdp->tm.tmPitchAndFamily >> 4)]) ;

     	SetDlgItemInt	(hdlg, IDC_TM_CHARSET,   	pdp->tm.tmCharSet, FALSE) ;
     	SetDlgItemText	(hdlg, IDC_TM_FACENAME, pdp->szFaceName) ;
}

void MySetMapMode (HDC hdc, int iMapMode)
{
     	switch (iMapMode)
     	{
     	case 	IDC_MM_TEXT:       	SetMapMode (hdc, MM_TEXT) ;       	break ;
     	case 	IDC_MM_LOMETRIC:	SetMapMode (hdc, MM_LOMETRIC) ;   	break ;
     	case 	IDC_MM_HIMETRIC:	SetMapMode (hdc, MM_HIMETRIC) ;   	break ;
     	case 	IDC_MM_LOENGLISH: 	SetMapMode (hdc, MM_LOENGLISH) ;  	break ;
     	case 	IDC_MM_HIENGLISH:  	SetMapMode (hdc, MM_HIENGLISH) ;  	break ;
     	case 	IDC_MM_TWIPS:      	SetMapMode (hdc, MM_TWIPS) ;      			break ;
     	case 	IDC_MM_LOGTWIPS:
          			SetMapMode (hdc, MM_ANISOTROPIC) ;
          			SetWindowExtEx (hdc, 1440, 1440, NULL) ;
          			SetViewportExtEx (hdc,	GetDeviceCaps (hdc, LOGPIXELSX),
                    GetDeviceCaps (hdc, LOGPIXELSY), NULL) ;
          			break ;
     	}
}
 PICKFONT.RC
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
// Dialog
PICKFONT DIALOG DISCARDABLE  0, 0, 348, 308
STYLE WS_CHILD | WS_VISIBLE | WS_BORDER
FONT 8, "MS Sans Serif"
BEGIN
    	LTEXT           			"&Height:",IDC_STATIC,8,10,44,8
    	EDITTEXT        		IDC_LF_HEIGHT,64,8,24,12,ES_AUTOHSCROLL
    	LTEXT           				"&Width",IDC_STATIC,8,26,44,8
    	EDITTEXT        			IDC_LF_WIDTH,64,24,24,12,ES_AUTOHSCROLL
    	LTEXT           				"Escapement:",IDC_STATIC,8,42,44,8
    	EDITTEXT        			IDC_LF_ESCAPE,64,40,24,12,ES_AUTOHSCROLL
    	LTEXT           				"Orientation:",IDC_STATIC,8,58,44,8
    	EDITTEXT        			IDC_LF_ORIENT,64,56,24,12,ES_AUTOHSCROLL
    	LTEXT           				"Weight:",IDC_STATIC,8,74,44,8
    	EDITTEXT        			IDC_LF_WEIGHT,64,74,24,12,ES_AUTOHSCROLL
    	GROUPBOX        			"Mapping Mode",IDC_STATIC,97,3,96,90,WS_GROUP
    	CONTROL 			"Text",IDC_MM_TEXT,"Button",BS_AUTORADIOBUTTON,104,13,56,
             						8
    	CONTROL         			"Low Metric",IDC_MM_LOMETRIC,"Button",BS_AUTORADIOBUTTON,
                    						104,24,56,8
    	CONTROL 			High Metric",IDC_MM_HIMETRIC,"Button",
                    						BS_AUTORADIOBUTTON,104,35,56,8
    	CONTROL         			"Low English",IDC_MM_LOENGLISH,"Button",
                    						BS_AUTORADIOBUTTON,104,46,56,8
    	CONTROL        	"		High English",IDC_MM_HIENGLISH,"Button",
                    						BS_AUTORADIOBUTTON,104,57,56,8
    	CONTROL  			"Twips",IDC_MM_TWIPS,"Button",BS_AUTORADIOBUTTON,104,68,
                    						56,8
    	CONTROL         			"Logical Twips",IDC_MM_LOGTWIPS,"Button",
                    						BS_AUTORADIOBUTTON,104,79,64,8
    	CONTROL 			"Italic",IDC_LF_ITALIC,"Button",BS_AUTOCHECKBOX | 
                    						WS_TABSTOP,8,90,48,12
    	CONTROL			"Underline",IDC_LF_UNDER,"Button",BS_AUTOCHECKBOX | 
                    						WS_TABSTOP,8,104,48,12
    	CONTROL         			"Strike Out",IDC_LF_STRIKE,"Button",BS_AUTOCHECKBOX | 
                    						WS_TABSTOP,8,118,48,12
    	CONTROL         			"Match Aspect",IDC_MATCH_ASPECT,"Button",BS_AUTOCHECKBOX | 
                    						WS_TABSTOP,60,104,62,8
    	CONTROL         			"Adv Grfx Mode",IDC_ADV_GRAPHICS,"Button",
                    						BS_AUTOCHECKBOX | WS_TABSTOP,60,118,62,8
    	LTEXT           				"Character Set:",IDC_STATIC,8,137,46,8
    	EDITTEXT        			IDC_LF_CHARSET,58,135,24,12,ES_AUTOHSCROLL
    	PUSHBUTTON  		"?",IDC_CHARSET_HELP,90,135,14,14
    	GROUPBOX        			"Quality",IDC_STATIC,132,98,62,48,WS_GROUP
    	CONTROL         			"Default",IDC_DEFAULT_QUALITY,"Button",
                    						BS_AUTORADIOBUTTON,136,110,40,8
    	CONTROL 			"Draft",IDC_DRAFT_QUALITY,"Button",BS_AUTORADIOBUTTON,
                    						136,122,40,8
    	CONTROL 			"Proof",IDC_PROOF_QUALITY,"Button",BS_AUTORADIOBUTTON,
                    						136,134,40,8
    	LTEXT           				"Face Name:",IDC_STATIC,8,154,44,8
    	EDITTEXT        			IDC_LF_FACENAME,58,152,136,12,ES_AUTOHSCROLL
    	GROUPBOX        			"Output Precision",IDC_STATIC,8,166,118,133,WS_GROUP
    	CONTROL         			"OUT_DEFAULT_PRECIS",IDC_OUT_DEFAULT,"Button",
                    						BS_AUTORADIOBUTTON,12,178,112,8
    	CONTROL         			"OUT_STRING_PRECIS",IDC_OUT_STRING,"Button",
                    						BS_AUTORADIOBUTTON,12,191,112,8
    	CONTROL  			"OUT_CHARACTER_PRECIS",IDC_OUT_CHARACTER,"Button",
                    						BS_AUTORADIOBUTTON,12,204,112,8
    	CONTROL         			"OUT_STROKE_PRECIS",IDC_OUT_STROKE,"Button",
                    						BS_AUTORADIOBUTTON,12,217,112,8
    	CONTROL 			"OUT_TT_PRECIS",IDC_OUT_TT,"Button",BS_AUTORADIOBUTTON,
                    						12,230,112,8
    	CONTROL         			"OUT_DEVICE_PRECIS",IDC_OUT_DEVICE,"Button",
                    						BS_AUTORADIOBUTTON,12,243,112,8
    	CONTROL        			"OUT_RASTER_PRECIS",IDC_OUT_RASTER,"Button",
                    						BS_AUTORADIOBUTTON,12,256,112,8
    	CONTROL         			"OUT_TT_ONLY_PRECIS",IDC_OUT_TT_ONLY,"Button",
                    						BS_AUTORADIOBUTTON,12,269,112,8
    	CONTROL         			"OUT_OUTLINE_PRECIS",IDC_OUT_OUTLINE,"Button",
                    						BS_AUTORADIOBUTTON,12,282,112,8
    	GROUPBOX        			"Pitch",IDC_STATIC,132,166,62,50,WS_GROUP
    	CONTROL  			"Default",IDC_DEFAULT_PITCH,"Button",BS_AUTORADIOBUTTON,
                    						137,176,52,8
    	CONTROL 			"Fixed",IDC_FIXED_PITCH,"Button",BS_AUTORADIOBUTTON,137,
                    						189,52,8
    	CONTROL         			"Variable",IDC_VARIABLE_PITCH,"Button",
                    						BS_AUTORADIOBUTTON,137,203,52,8
    	GROUPBOX        			"Family",IDC_STATIC,132,218,62,82,WS_GROUP
    	CONTROL       	  		"Don't Care",IDC_FF_DONTCARE,"Button",BS_AUTORADIOBUTTON,
                    						137,229,52,8
    	CONTROL 			"Roman",IDC_FF_ROMAN,"Button",BS_AUTORADIOBUTTON,137,241,
                    						52,8
    	CONTROL  			"Swiss",IDC_FF_SWISS,"Button",BS_AUTORADIOBUTTON,137,253,
                    						52,8
    	CONTROL  				"Modern",IDC_FF_MODERN,"Button",BS_AUTORADIOBUTTON,137,
                    							265,52,8
    	CONTROL				"Script",IDC_FF_SCRIPT,"Button",BS_AUTORADIOBUTTON,137,
                    							277,52,8
    	CONTROL         				"Decorative",IDC_FF_DECORATIVE,"Button",
                    							BS_AUTORADIOBUTTON,137,289,52,8
    	DEFPUSHBUTTON  		"OK",IDOK,247,286,50,14
    	GROUPBOX        				"Text Metrics",IDC_STATIC,201,2,140,272,WS_GROUP
    	LTEXT           					"Height:",IDC_STATIC,207,12,64,8
    	LTEXT           					"0",IDC_TM_HEIGHT,281,12,44,8
    	LTEXT           					"Ascent:",IDC_STATIC,207,22,64,8
    	LTEXT           					"0",IDC_TM_ASCENT,281,22,44,8
    	LTEXT           					"Descent:",IDC_STATIC,207,32,64,8
    	LTEXT           					"0",IDC_TM_DESCENT,281,32,44,8
    	LTEXT           					"Internal Leading:",IDC_STATIC,207,42,64,8
    	LTEXT           					"0",IDC_TM_INTLEAD,281,42,44,8
    	LTEXT           					"External Leading:",IDC_STATIC,207,52,64,8
    	LTEXT           					"0",IDC_TM_EXTLEAD,281,52,44,8
    	LTEXT           					"Ave Char Width:",IDC_STATIC,207,62,64,8
    	LTEXT          	 				"0",IDC_TM_AVECHAR,281,62,44,8
    	LTEXT           					"Max Char Width:",IDC_STATIC,207,72,64,8
    	LTEXT           					"0",IDC_TM_MAXCHAR,281,72,44,8
    	LTEXT           					"Weight:",IDC_STATIC,207,82,64,8
    	LTEXT           					"0",IDC_TM_WEIGHT,281,82,44,8
    	LTEXT           					"Overhang:",IDC_STATIC,207,92,64,8
    	LTEXT           					"0",IDC_TM_OVERHANG,281,92,44,8
    	LTEXT           					"Digitized Aspect X:",IDC_STATIC,207,102,64,8
    	LTEXT           					"0",IDC_TM_DIGASPX,281,102,44,8
    	LTEXT           					"Digitized Aspect Y:",IDC_STATIC,207,112,64,8
    	LTEXT           					"0",IDC_TM_DIGASPY,281,112,44,8
    	LTEXT          		 			"First Char:",IDC_STATIC,207,122,64,8
    	LTEXT           					"0",IDC_TM_FIRSTCHAR,281,122,44,8
    	LTEXT           					"Last Char:",IDC_STATIC,207,132,64,8
    	LTEXT           					"0",IDC_TM_LASTCHAR,281,132,44,8
    	LTEXT           					"Default Char:",IDC_STATIC,207,142,64,8
    	LTEXT           					"0",IDC_TM_DEFCHAR,281,142,44,8
    	LTEXT           					"Break Char:",IDC_STATIC,207,152,64,8
    	LTEXT           					"0",IDC_TM_BREAKCHAR,281,152,44,8
    	LTEXT           					"Italic?",IDC_STATIC,207,162,64,8
    	LTEXT           				"0",IDC_TM_ITALIC,281,162,44,8
    	LTEXT           				"Underlined?",IDC_STATIC,207,172,64,8
    	LTEXT           				"0",IDC_TM_UNDER,281,172,44,8
    	LTEXT           				"Struck Out?",IDC_STATIC,207,182,64,8
    	LTEXT           				"0",IDC_TM_STRUCK,281,182,44,8
    	LTEXT           				"Variable Pitch?",IDC_STATIC,207,192,64,8
    	LTEXT           				"0",IDC_TM_VARIABLE,281,192,44,8
    	LTEXT           				"Vector Font?",IDC_STATIC,207,202,64,8
    	LTEXT           				"0",IDC_TM_VECTOR,281,202,44,8
    	LTEXT           				"TrueType Font?",IDC_STATIC,207,212,64,8
    	LTEXT           				"0",IDC_TM_TRUETYPE,281,212,44,8
    	LTEXT           				"Device Font?",IDC_STATIC,207,222,64,8
    	LTEXT           				"0",IDC_TM_DEVICE,281,222,44,8
    	LTEXT           				"Family:",IDC_STATIC,207,232,64,8
    	LTEXT           				"0",IDC_TM_FAMILY,281,232,44,8
    	LTEXT           				"Character Set:",IDC_STATIC,207,242,64,8
    	LTEXT           				"0",IDC_TM_CHARSET,281,242,44,8
    	LTEXT           				"0",IDC_TM_FACENAME,207,262,128,8
END

/////////////////////////////////////////////////////////////////////////////
// Menu
PICKFONT MENU DISCARDABLE 
BEGIN
  	POPUP "&Device"
    	BEGIN
        MENUITEM "&Screen",			IDM_DEVICE_SCREEN, CHECKED
        MENUITEM "&Printer",  			IDM_DEVICE_PRINTER
    	END
END
 RESOURCE.H
// Microsoft Developer Studio generated include file.
// Used by PickFont.rc

#define 		IDC_LF_HEIGHT      1000
#define 		IDC_LF_WIDTH       1001
#define 		IDC_LF_ESCAPE      1002
#define 		IDC_LF_ORIENT      1003
#define 		IDC_LF_WEIGHT      1004
#define 		IDC_MM_TEXT        1005
#define 		IDC_MM_LOMETRIC    1006
#define 		IDC_MM_HIMETRIC    1007
#define 		IDC_MM_LOENGLISH   1008
#define 		IDC_MM_HIENGLISH   1009
#define 		IDC_MM_TWIPS       1010
#define 		IDC_MM_LOGTWIPS    1011
#define 		IDC_LF_ITALIC      1012
#define 		IDC_LF_UNDER       1013
#define 		IDC_LF_STRIKE      1014
#define 		IDC_MATCH_ASPECT   1015
#define 		IDC_ADV_GRAPHICS   1016
#define 		IDC_LF_CHARSET     1017
#define 		IDC_CHARSET_HELP   1018
#define 		IDC_DEFAULT_QUALITY     1019
#define 		IDC_DRAFT_QUALITY       1020
#define 		IDC_PROOF_QUALITY       1021
#define 		IDC_LF_FACENAME         1022
#define 		IDC_OUT_DEFAULT         1023
#define 		IDC_OUT_STRING          1024
#define 		IDC_OUT_CHARACTER       1025
#define 		IDC_OUT_STROKE          1026
#define 		IDC_OUT_TT              1027
#define 		IDC_OUT_DEVICE          1028
#define 		IDC_OUT_RASTER          1029
#define 		IDC_OUT_TT_ONLY         1030
#define 		IDC_OUT_OUTLINE         1031
#define 		IDC_DEFAULT_PITCH       1032
#define 		IDC_FIXED_PITCH         1033
#define 		IDC_VARIABLE_PITCH      1034
#define 		IDC_FF_DONTCARE         1035
#define 		IDC_FF_ROMAN            1036
#define 		IDC_FF_SWISS            1037
#define 		IDC_FF_MODERN           1038
#define 		IDC_FF_SCRIPT           1039
#define 		IDC_FF_DECORATIVE       1040
#define 		IDC_TM_HEIGHT           1041
#define 		IDC_TM_ASCENT           1042
#define 		IDC_TM_DESCENT          1043
#define 		IDC_TM_INTLEAD          1044
#define 		IDC_TM_EXTLEAD          1045
#define 		IDC_TM_AVECHAR          1046
#define 		IDC_TM_MAXCHAR          1047
#define 		IDC_TM_WEIGHT           1048
#define 		IDC_TM_OVERHANG         1049
#define 		IDC_TM_DIGASPX          1050
#define 		IDC_TM_DIGASPY          1051
#define 		IDC_TM_FIRSTCHAR        1052
#define 		IDC_TM_LASTCHAR         1053
#define 		IDC_TM_DEFCHAR          1054
#define 		IDC_TM_BREAKCHAR        1055
#define 		IDC_TM_ITALIC           1056
#define 		IDC_TM_UNDER            1057
#define 		IDC_TM_STRUCK           1058
#define 		IDC_TM_VARIABLE         1059
#define 		IDC_TM_VECTOR           1060
#define 		IDC_TM_TRUETYPE         1061
#define 		IDC_TM_DEVICE           1062
#define 		IDC_TM_FAMILY           1063
#define 		IDC_TM_CHARSET          1064
#define 		IDC_TM_FACENAME         1065
#define 		IDM_DEVICE_SCREEN       40001
#define 		IDM_DEVICE_PRINTER      40002

ͼ17-1��ʾ�˵��͵�PICKFONTөĻ��ʾ��PICKFONT��벿����ʾ��һ����ģ̬�Ի����飬͸������������ѡ���߼�����ṹ�Ĵ󲿷���λ���Ի�������Ұ벿����ʾ������ѡ��װ��������GetTextMetrics�Ľ�����Ի�������²�����ʽʹ������������ʾһ���ִ�����Ϊ��ģ̬�Ի�����dz������������1024��768��������ʾ��С��ִ�������ʽ��


��

ͼ17-1 ���͵�PICKFONTөĻ��ʾ��Windows NT�µ�Unicode�汾��

��ģ̬�Ի����黹����һЩ���߼�����ṹ��ѡ������ǰ�����Logical Twips����ʽ��ӳ�䷽ʽ����Match Aspect��ѡ�����Windows���߼���������ʵ����ƥ��ķ�ʽ���͡�Adv Grtx Mode�����趨Windows NT�еĸ߼�ͼ��ģʽ���������ҽ�����Щ����ϸ���ۡ�

�ӡ�Device�����ܱ��У�����ѡ���ڶ�ӡ������������Ѷ��ʾ��������������£�PICKFONT���߼�����ѡ��ӡ����װ�������У�����ӡ������ʾTEXTMETRIC�ṹ��Ȼ�ᣬ��ʽ���߼�����ѡ���Ӵ�װ�������У�����ʾ�����ִ�����ˣ���ʽ��ʾ�����ֿ��ܻ�ʹ����TEXTMETRIC��λ�����������壨ӡ�������壩��ͬ�����壨өĻ���壩��

PICKFONT��ʽ�Ĵ󲿷��߼����ڴ����Ի�����ı�Ҫ����������Ҳ�����ϸ���۸ó�ʽ�Ĺ�����ʽ��ֻ���ͽ�����ѡ���߼������ԭ����

�߼�����ṹ
��

�����Ժ���CreateFont�������߼����壬���Ǿ���14�������ĺ�ʽ��һ�㣬����һ��LOGFONT��̬�Ľṹ

LOGFONT lf ;

Ȼ���ٶ���ýṹ����λ�������һЩ������ᣬ����ʹ��ָ��ýṹ��ָ�����CreateFontIndirect��

hFont = CreatFontIndirect (&lf) ;

�������趨LOGFONT�ṹ��ÿ����λ������߼�����ṹ����Ϊ��̬��������ô���е���λ�����ʼ��Ϊ0��0һ����Ԥ��ֵ��Ȼ�ᣬ���Բ��ø��Ķ�ֱ��ʹ������ṹ��CreateFontIndirect�ᴫ������Ĵ��š�������������ѡ��װ������ʱ����õ�һ���������ڶ����塣�����Ը����Լ�����Ҫ����ȷ��ģ�������LOGFONT�ṹ��Windows����һ����ʵ����������Ҫ����ƥ�䡣

��������LOGFONT�ṹ��ÿ����λʱ������������PICKFONT��ʽ���������ǡ�����ϣ����ʽʹ����������κ���λʱ�������˰���Enter��OK����ť��

LOGFONT�ṹ��ǰ������λ���߼���λ��������������ӳ�䷽ʽ��Ŀǰ�趨��

  •  lfHeight ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ß¼ï¿½ï¿½ï¿½Î»ï¿½ï¿½Ê¾ï¿½ï¿½Ï£ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ôªï¿½ß¶È¡ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ô½ï¿½lfHeight�趨0����ʹ���ڶ���С�����߸�����λ�����ĺ��彫���趨Ϊ���������������lfHeight�趨Ϊ�������ͱ�ʾ��ϣ����ֵ��ʾ�����ڲ�����������ⲿ������ĸ߶ȡ�ʵ���ϣ���Ҫ��������о�ΪlfHeight�������lfHeight�趨Ϊ��ֵ����Windows�Ὣ�����ֵ��Ϊ���ֵһ�µ�����߶ȡ�����һ������Ҫ�����������Ҫ�ض���ֵ�����壬�ɽ���ֵת��Ϊ�߼���λ������lfHeight��λ�趨Ϊ��ֵ�ĸ��������lfHeight����ֵ����TEXTMETRIC�ṹ��tmHeight��λ����Ϊ��ֵ����ʱ��΢С��ƫ��������������������𣩡����lfHeight�Ǹ�ֵ���������Ե��벻����tmInternalLeading���TEXTMETRIC�ṹ��tmHeight��λ��ƥ�䡣
    ��
  •  lfWidth ï¿½ï¿½ï¿½ß¼ï¿½ï¿½ï¿½Î»ï¿½ï¿½ï¿½ï¿½Ôªï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½È¡ï¿½ï¿½Ú¶ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Â£ï¿½ï¿½ï¿½ï¿½Ô½ï¿½ï¿½ï¿½Öµï¿½è¶¨Îª0����Windows�����ݸ߶�ѡ�����塣ʹ�÷���ֵ�Ե������岢������̫�����ã������TrueType���壬�������ɵ���������ñ�������Ԫ�������խ�����塣�����λ��Ӧ�TEXTMETRIC�ṹ��tmAveCharWidth��λ��Ҫ��ȷʹ��lfWidth��λ�����ȰѴ���lfWidth���LOGFONT�ṹ�趨Ϊ0�������߼����壬����ѡ��װ�����ݣ�Ȼ�����GetTextMetrics���õ�tmAveCharWidth��λ���ɰ�����������ֵ�Ĵ�С��Ȼ��ʹ�������ڵ�lfWidth��tmAveCharWidthֵ�����ڶ������塣
    ��

    ��������λָ�����ֵġ���λ�Ƕȡ��͡����򡹡������ϣ�lfEscapementʹ�ִ��ܹ���һ���ĽǶ���д����ÿ����Ԫ�Ļ�׼������ˮƽ��ƽ�У�����lfOrientationʹ������Ԫ��б��������������λ��������ô��Ч����ʹ��������ֻ�������������²��ܺܺõ������ã�ʹ��TureType���塢ִ��Windows NT�Լ�������CM_ADVANCED����趨����SetGraphicsMode��ͨ��ѡ�С�Adv Grfx Mode����ȡ���飬���ܹ����PICKFONT�е�������Ҫ��

    ����֤PICKFONT�е���Щ��λʱ��Ҫע�ⵥλ��ʮ��֮һ�ȣ���ʱ�뷽����ת��������������һ��ֵʹ�����ִ���ʧ����ˣ���ʹ��0��-600��3000��3600֮���ֵ��

  •  lfEscapement ï¿½ï¿½ï¿½Ç´ï¿½Ë®Æ½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê±ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê®ï¿½ï¿½Ö®ï¿½ï¿½ï¿½Ä½Ç¶È¡ï¿½ï¿½ï¿½Ö¸ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ð´ï¿½ï¿½ï¿½ï¿½Ê±ï¿½Ö´ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ôªï¿½ï¿½ï¿½ÃµÄ·ï¿½Ê½ï¿½ï¿½ï¿½ï¿½17-1�ṩ�˼������ӣ�
    ��
    ��17-1
    ֵ ��Ԫ�ķ���
    0 �������ң��ڶ���
    900 ����
    1800 ��������
    2700 ����

    ��Windows 98�У����ֵ�趨��TrueType���ֵ���λ�ǶȺͷ�����Windows NT�У����ֵͨ��Ҳ�������趨��������GM_ADVANCED��������SetGraphicsModeʱ�������ļ���˵��������������

  •  lfOrientation ï¿½ï¿½ï¿½Ç´ï¿½Ë®Æ½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê±ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê®ï¿½ï¿½Ö®ï¿½ï¿½ï¿½Ä½Ç¶È£ï¿½ï¿½ï¿½Ó°ï¿½ìµ¥ï¿½ï¿½ï¿½ï¿½Ôªï¿½ï¿½ï¿½ï¿½Û¡ï¿½ï¿½ï¿½17-2�ṩ�˼������ӣ�
    ��
    ��17-2
    ֵ ��Ԫ���
    0 �������ڶ���
    900 ������б90��
    1800 �ߵ�
    2700 ������б90��

    �����λһ�㲻�����ã�������Windows NT��ʹ��TrueType���壬����ͼ��ģʽ�趨ΪGM_ADVANCED, ����������������ļ���˵��������������

    ����10����λ���£�

  •  lfWeight ï¿½ï¿½ï¿½ï¿½ï¿½Î»Ê¹ï¿½ï¿½ï¿½Ü¹ï¿½Ö¸ï¿½ï¿½ï¿½ï¿½ï¿½å¡£WINGDI.H��ͷ���������˿���������λ��һ��ֵ���μ���17-3����
    ��
    ��17-3
    ֵ ʶ����
    0 FW_DONTCARE
    100 FW_THIN
    200 FW_EXTRALIGHT��FW_ULTRALIGHT
    300 FW_LIGHT
    400 FW_NORMAL��FW_REGULAR
    500 FW_MEDIUM
    600 FW_SEMIBOLD��FW_DEMIBOLD
    700 FW_BOLD
    800 FW_EXTRABOLD��FW_ULTRABOLD
    900 FW_HEAVY��FW_BLACK

    ��ʵ�ϣ�������ǰ�ù����κ�һ��ֵ�����ơ������ԶԱ�׼��ʹ��0��400���Դ���ʹ��700��

  •  lfItalic ï¿½Ú·ï¿½ï¿½ï¿½ÖµÊ±ï¿½ï¿½ï¿½ï¿½Ö¸ï¿½ï¿½Ð±ï¿½å¡£Windows����GDI���������Ϻϳ�б�塣�༴��Windows�����ƶ���������Ԫ����ͼ��ģ��б�塣���TrueType���壬Windowsʹ��������б����������б�汾��
    ��
  •  lfUnderline ï¿½Ú·ï¿½ï¿½ï¿½ÖµÊ±ï¿½ï¿½ï¿½ï¿½Ö¸ï¿½ï¿½ï¿½ï¿½ï¿½ß£ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½GDI�����϶����úϳɵġ�Ҳ����˵��Windows GDIֻ���ڰ����ո��ÿ����Ԫ���ߡ�
    ��
  •  lfStrikeOut ï¿½Ú·ï¿½ï¿½ï¿½ÖµÊ±ï¿½ï¿½ï¿½ï¿½Ö¸ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ó¦ï¿½ï¿½ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ß´ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ò²ï¿½ï¿½ï¿½ï¿½GDI����ϳɵġ�
    ��
  •  lfCharSet ï¿½ï¿½ï¿½ï¿½Ö¸ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ôªï¿½ï¿½ï¿½ï¿½Ò»ï¿½ï¿½Î»Ôªï¿½ï¿½ï¿½Öµï¿½ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½ï¿½Ò»ï¿½Ú¡ï¿½ï¿½ï¿½Ôªï¿½ï¿½ï¿½ï¿½Unicode���и���ϸ�����������λ����PICKFONT�У������԰��´����ʺŵİ�ť��ȡ���ܹ�ʹ�õ���Ԫ���б���
    ��

    ע��lfCharSet��λ��Ψһ�������ʾԤ��ֵ����λ����ֵ�൱�ANSI_CHARSET��ANSI��Ԫ����������ŷʹ�á�DEFAULT_CHARSET������1����ʾ��ʽִ�еĻ������ڶ�����Ԫ����

  •  lfOutPrecision ï¿½ï¿½Ö¸ï¿½ï¿½ï¿½ï¿½Windows��ʵ�ʵ�����ƥ�������������С������ķ�ʽ������һ�����ӵ���λ��һ�����ʹ�á���鿴���LOGFONT�ṹ���ļ��Եõ�����ϸ����Ѷ��ע�⣬����ʹ��OUT_TT_ONLY_PRECIS�����ȷ���õ�����TrueType���塣
    ��
  •  lfClipPrecision ï¿½ï¿½ï¿½ï¿½ï¿½Î»Ö¸ï¿½ï¿½ï¿½Ëµï¿½ï¿½ï¿½Ôªï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½Î»ì¶¼ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê±ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ôªï¿½Ä·ï¿½Ê½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Î»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê¹ï¿½Ã£ï¿½PICKFONT��ʽҲû��ʹ������
    ��
  •  lfQuality ï¿½ï¿½ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½Windows��ָ��й������������ʵ��������ƥ���ָ���ʵ��ֻ�Ե������������壬����Ӱ��TrueType���塣DRAFT_QUALITY���ָ����ҪGDI���ŵ��������Եõ���Ҫ�Ĵ�С��PROOF_QUALITY���ָ���������š�PROOF_QUALITY������Ư���������ǿ��ܱ���ϣ����ҪСһЩ�������λ��Ҳ����ʹ��DEFAULT_QUALITY����0����
    ��
  •  lfPitchAndFamily ï¿½ï¿½ï¿½Î»Ôªï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½É¡ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê¹ï¿½ï¿½Î»Ôªï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Å½ï¿½ï¿½ï¿½ï¿½ì¶´ï¿½ï¿½ï¿½Î»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê¶ï¿½ï¿½ï¿½Ö¡ï¿½ï¿½ï¿½Íµï¿½ï¿½ï¿½Î»ÔªÖ¸ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ç¶ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ôªï¿½Ä¿ï¿½ï¿½ï¿½ï¿½ï¿½È£ï¿½ï¿½ï¿½ï¿½Ç±ï¿½ï¿½ï¿½ï¿½ï¿½Î¼ï¿½ï¿½ï¿½17-4����
    ��
    ��17-4
    ֵ ʶ����
    0 DEFAULT_PITCH
    1 FIXED_PITCH
    2 VARIABLE_PITCH

    λԪ����ϰ벿��ָ������ϵ�У��μ���17-5����

    ��17-5
    ֵ ʶ����
    0x00 FW_DONTCARE
    0x10 FF_ROMAN�������serifs��
    0x20 FF_SWISS���������serifs��
    0x30 FF_MODERN��������
    0x40 FF_SCRIPT��ģ����д��
    0x50 FF_DECORATIVE
  •  lfFaceName ï¿½ï¿½ï¿½Ç¹ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Courier��Arial��Times New Roman����ʵ���������ơ������λ�ǿ���ΪLF_FACESIZE����32����Ԫ����λԪ�����С����Ҫ�õ�TrueType��б���������壬�����ַ�������lfFaceName��λ��ʹ���������������ƣ���Times New Roman Italic�������߿���ʹ�û������ƣ���Times New Roman�������趨lfItalic���
    ��

����ӳ�����㷨
��

���趨���߼�����ṹ�ᣬ����CreateFontIndirect���õ��߼�������š�������SelectObject���߼�����ѡ��װ������ʱ��WindowsѰ��������������ӽ�ƥ���ʵ�����塣��ʹ�á�����ӳ�����㷨�����ṹ��ijЩ��λҪ��������λ����ҪһЩ��

�˽�����ӳ�����÷�ʽ�ǻ�һЩʱ������PICKFONT�������Ǽ���ָ�ϣ�

  • lfCharSet����Ԫ������λ�Ƿdz���Ҫ�ġ������ָ����OEM_CHARSET(255)����õ�ij�ֱʻ�������ն˻����壬��Ϊ������Ψһʹ��OEM��Ԫ�������塣Ȼ��������TrueType��Big Fonts���ij��֣��ڵ����¡�TrueType�ʹ����塵һ�����۹�������һ��TrueType������ӳ�䵽����OEM��Ԫ���Ȳ�ͬ����Ԫ��������Ҫʹ��SYMBOL_CHARSET(2) ���õ�Symbol�����Wingdings���塣
    ��
  • lfPitchAndFamily���FIXED_PITCH���ֵ����Ҫ����Ϊ��ʵ���ϸ���Windows���봦��������塣
    ��
  • lfFaceName��λ����Ҫ����Ϊ��ָ������������������������lfFaceName�趨ΪNULL������lfPitchAndFamily��λ�н���ֵ�趨ΪFF_DONTCARE�����ֵ����Ϊָ��������ϵ�У����Ը���λҲ����Ҫ��
    ��
  • ��춵������壬Windows����ͼ���lfHeightֵ����ʹ��Ҫ���ӽ�С����Ĵ�С��ʵ������ĸ߶�����С춻�����������壬����û�и�С��������������Ҫ�󡣶�춱ʻ���TrueType���壬Windows���òµ¥µØ½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Åµï¿½ï¿½ï¿½Òªï¿½Ä¸ß¶È¡ï¿½
    ��
  • ����ͨ����lfQuality�趨ΪPROOF_QUALITY����ֹWindows���ŵ������塣��ô�����Ը���Windows���������߶�û�����������Ҫ��
    ��
  • ���ָ���˶����ʾ�����ض��ݺ�Ȳ�Э����lfHeight��lfWeightֵ��Windows��ӳ�䵽Ϊ��ʾ����������ͬ�ݺ�ȵ��豸��Ƶĵ������塣���ǵõ�ϸ�������ļ��ɣ���Ȼ�����TrueType�����Dz���Ҫ�ģ���һ����ԣ������������Ϊ��һ���豸�������塣������ͨ���������С�Match Aspect���ĺ�ȡ���飬��PICKFONT����ɡ����ѡ���˺�ȡ���飬PICKFONT��ʹ��TRUE��������SetMapperFlags��
    ��

ȡ��������Ѷ
��

��PICKFONT�з�ģ̬�Ի�������Ҳ�������ѡ��װ���������GetTextMetrics��ʽ�л�õ���Ѷ��ע�⣬����ʹ��PICKFONT�ġ�Device�����ܱ�ָ��װ��������өĻ�����ڶ�ӡ��������Ϊ��ӡ��������Ч��������ܲ�ͬ�����Խ��Ҳ���ܲ�ͬ������PICKFONT���б��ĵײ��Ǵ�GetTextFace�õ�����Ч�������ơ�

������ֵ�����ݺ�����⣬Windows���Ƶ�TEXTMETRIC�ṹ�����д�Сֵ�����߼���λ��ʾ��TEXTMETRIC�ṹ����λ���£�

  •  tmHeight ï¿½ß¼ï¿½ï¿½ï¿½Î»ï¿½ï¿½ï¿½ï¿½Ôªï¿½ß¶È¡ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Æµï¿½ï¿½LOGFONT�ṹ��ָ����lfHeight��λ��ֵ�������ֵΪ�������ʹ����о࣬���ǵ�ֵ�����LOGFONT�ṹ��lfHeight��λΪ������tmHeight���tmInternalLeading��λӦ���Ƶ��lfHeight��λ�ľ���ֵ��
    ��
  •  tmAscent ï¿½ß¼ï¿½ï¿½ï¿½Î»ï¿½Ä»ï¿½×¼ï¿½ï¿½ï¿½ï¿½ï¿½Ïµï¿½ï¿½ï¿½Ôªï¿½ï¿½Ö±ï¿½ï¿½Ð¡ï¿½ï¿½
    ��
  •  tmDescent ï¿½ß¼ï¿½ï¿½ï¿½Î»ï¿½Ä»ï¿½×¼ï¿½ï¿½ï¿½ï¿½ï¿½Âµï¿½ï¿½ï¿½Ôªï¿½ï¿½Ö±ï¿½ï¿½Ð¡ï¿½ï¿½
    ��
  •  tmInternalLeading ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½tmHeightֵ�ڵĴ�ֱ��С��ͨ����һЩ��д��ĸ��ע������ռ�ݡ�ͬ����������tmHeightֵ��tmInternalLeadingֵ����������ĵ�ֵ��
    ��
  •  tmExternalLeading tmHeight ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ð¾à¸½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Æ¼ï¿½ï¿½ï¿½ì¶¸ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Öµï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ð¡ï¿½
    ��
  •  tmAveCharWidth ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ð¡Ð´ï¿½ï¿½Ä¸ï¿½ï¿½Æ½ï¿½ï¿½ï¿½ï¿½ï¿½È¡ï¿½
    ��
  •  tmMaxCharWidth ï¿½ß¼ï¿½ï¿½ï¿½Î»ï¿½ï¿½ï¿½ï¿½Ôªï¿½ï¿½ï¿½ï¿½ï¿½È¡ï¿½ï¿½ï¿½ì¶¶ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½å£¬ï¿½ï¿½ï¿½Öµï¿½ï¿½tmAveCharWidth��ͬ��
    ��
  •  tmWeight ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Î§ï¿½ï¿½0��999��ʵ���ϣ������λΪ400ʱ�DZ�׼���壬700ʱ�Ǵ��塣
    ��
  •  tmOverhang Windows�ںϳ�б������ʱ���ӵ�����������Ԫ�Ķ�����������߼���λ��������������б�廯ʱ��tmAveCharWidthֵ���ֲ��䣬��Ϊб�廯���ִ�����ͬ�������ִ����ܿ�����ȡ�ҪΪ����Ӵ֣�Windows������΢����ÿ����Ԫ�Ŀ��ȡ���춴��壬tmAveCharWidthֵС�tmOverhangֵ�����û�мӴֵ���ͬ�����tmAveCharWidthֵ��
    ��
  •  tmDigitizedAspectX��tmDigitizedAspectY ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Êµï¿½ï¿½Ýºï¿½È¡ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê¹ï¿½ï¿½LOGPIXELSX��LOGPIXELSYʶ���ִ�GetDeviceCaps�õ���ֵ��ͬ��
    ��
  •  tmFirstChar ï¿½ï¿½ï¿½ï¿½ï¿½Ðµï¿½Ò»ï¿½ï¿½ï¿½ï¿½Ôªï¿½ï¿½ï¿½ï¿½Ôªï¿½ï¿½ï¿½ë¡£
    ��
  •  tmLastChar ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ò»ï¿½ï¿½ï¿½ï¿½Ôªï¿½ï¿½ï¿½ï¿½Ôªï¿½ï¿½ï¿½ë¡£ï¿½ï¿½ï¿½TEXTMETRIC�ṹͨ������GetTextMetricsW����ʽ�Ŀ���Ԫ�汾����ã���ô���ֵ���ܴ��255��
    ��
  •  tmDefaultChar Windows�����ʾ���������е���Ԫ����Ԫ���룬ͨ���Ǿ��Ρ�
    ��
  •  tmBreakChar ï¿½Úµï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ê±ï¿½ï¿½Windows�����ij�ʽ���ȷ�����ֶϿ�����Ԫ�����������һЩ��ֵĶ���������EBCDIC���壩��������32���հ���Ԫ��
    ��
  •  tmItalic ï¿½ï¿½ï¿½Ð±ï¿½ï¿½ï¿½ï¿½Îªï¿½ï¿½ï¿½ï¿½Öµï¿½ï¿½
    ��
  •  tmUnderlined ï¿½ï¿½ì¶µï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Îªï¿½ï¿½ï¿½ï¿½Öµï¿½ï¿½
    ��
  •  tmStruckOut ï¿½ï¿½ï¿½É¾ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Îªï¿½ï¿½ï¿½ï¿½Öµï¿½ï¿½
    ��
  •  tmPitchAndFamily ï¿½ï¿½ï¿½ï¿½Î»Ôªï¿½Ç±ï¿½Ê¾ï¿½ï¿½ï¿½ï¿½Ä³Ð©ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ê£¬ï¿½ï¿½ï¿½ï¿½WINGDI.H�ж����ʶ����ָ�����μ���17-6����
    ��
    ��17-6
    ֵ ʶ����
    0x01 TMPF_FIXED_PITCH
    0x02 TMPF_VECTOR
    0x04 TMPF_TRUETYPE
    0x08 TMPF_DEVICE

    ����TMPF_FIXED_PITCH����������ʲô�����������Ԫ�DZ���ģ������λԪΪ1���ڶ����λԪ��TMPF_VECTOR�����TrueType�����ʹ�����������ŵ��������������壨��PostScript�����壩Ϊ1��TMPF_DEVICE����ʾ�豸���壨��ӡ�������õ�����)������������GDI�����塣

    �����λ�ĵ��ĸߵ�λԪ��ʾ����ϵ�У�������LOGFONT��lfPitchAndFamily��λ�����õ�ֵ��ͬ��

  •  tmCharSet ï¿½ï¿½Ôªï¿½ï¿½Ê¶ï¿½ï¿½ï¿½Ö¡ï¿½
    ��

��Ԫ����Unicode
��

���ڵ�����������Windows��Ԫ���ĸ�����������DZ��봦���漰���̵Ĺ��ʻ����⡣��LOGFONT��TEXTMETRIC�ṹ�У��������壨��ʵ�����壩����Ԫ����0��255֮��ĵ���λԪ�����ֵ��ʾ��������WINGDI.H�е���Ԫ��ʶ����������ʾ��

#define 		ANSI_CHARSET            			0
#define 		DEFAULT_CHARSET         			1
#define 		SYMBOL_CHARSET          			2
#define 		MAC_CHARSET             			77
#define 		SHIFTJIS_CHARSET        			128
#define 		HANGEUL_CHARSET         			129
#define 		HANGUL_CHARSET          			129	
#define 		JOHAB_CHARSET           			130
#define 		GB2312_CHARSET          			134
#define 		CHINESEBIG5_CHARSET     			136
#define 		GREEK_CHARSET           			161
#define 		TURKISH_CHARSET         			162
#define 		VIETNAMESE_CHARSET      			163
#define 		HEBREW_CHARSET          			177
#define 		ARABIC_CHARSET          			178
#define 		BALTIC_CHARSET          			186
#define 		RUSSIAN_CHARSET         			204
#define 		THAI_CHARSET            			222
#define 		EASTEUROPE_CHARSET      			238
#define 		OEM_CHARSET             			255

��Ԫ����ҳ����ĸ������ƣ�������Ԫ���ض��Windows����ͨ��С춻���255��

�뱾������г�ʽһ���������Դ��ж����UNICODEʶ���ֱ���PICKFONT��Ҳ���Բ���UNICODEʶ���ֱ�������������һ���������ڸ�����ϵij�ʽ�������汾�ֱ�λ�DEBUG��RELEASEĿ¼�С�

ע�⣬�ڳ�ʽ��Unicode�汾��PICKFONT�����Ӵ��ײ���ʾ���ִ�Ҫ����һЩ���������汾�У��ִ�����Ԫ������0x40��0x45��0x60��0x65��������ѡ����������Ԫ��������SYMBOL_CHARSET������Щ��Ԫ���붼��ʾ������ĸ����ǰ�����д��Сд��ĸ����A��E��a��e����

��ִ��PICKFONT��ʽ�ķ�Unicode�汾ʱ����������12����Ԫ����Ԫ����0xC0��0xC5�Լ�0xE0��0xE5�����������ѡ�����Ԫ�������ANSI_CHARSET�������Ԫ�����Ӧ춴�д��Сд��ĸA�ļ������汾�����GREEK_CHARSET����Щ�����Ӧ�ϣ����ĸ������ĸ�����RUSSIAN_CHARSET����Ӧ�˹������ĸ������ĸ��ע�⣬����ѡ��һ����Ԫ��ʱ��������ܻ�ı䣬������Ϊ�����������û����Щ��Ԫ����TrueType��������С������ܻ���������TrueType�����ǡ�Big fonts�����Ұ������ֲ�ͬ��Ԫ������ĸ�������ִ��Windows��Զ���汾����Щ��Ԫ�ᱻ����Ϊ˫λԪ����Ԫ�����һᰴ��������ʾ�������ǰ���ĸ��ʾ��

��Windows NT��ִ��PICKFONT��Unicode�汾ʱ������0xC0��0xC5�Լ�0xE0��0xE5ͨ���Ǵ�д��Сд��ĸA�ļ������汾������SYMBOL_CHARSET������ΪUnicode�ж�������Щ���롣��ʽҲ��ʾ0x0390��0x0395�Լ�0x03B0��0x03B5����Ԫ���롣���������Unicode���ж��壬��Щ�������Ƕ�Ӧ�ϣ����ĸ������ĸ��ͬ���أ���ʽ��ʾ0x0410��0x0415�Լ�0x0430��0x0435����Ԫ���룬���Ƕ�Ӧ�˹������ĸ������ĸ��Ȼ������Щ��Ԫ�����ܴ�����ڶ������У�������ѡ��GREEK_CHARSET��RUSSIAN_CHARSET���õ����ǡ�����������£�LOGFONT�ṹ�е���Ԫ��ID������ʵ�ʵ���Ԫ������Ԫ������Unicode������Ԫ��IDָ������������Ԫ������Ԫ��

����ѡ��HEBREW_CHARSET������177����ϣ������ĸ����������Windowsͨ����Big Fonts�У������ҵϵͳѡ��Lucida Sans Unicode����һ���������ڷ�ģ̬�Ի���������½�����֤��

PICKFONTҲ��ʾ0x5000��0x5004����Ԫ���룬���Ƕ�Ӧ춺������ͳ������������ֵ�һ���֡������ִ��Windows��Զ���汾�����������˱�Lucida Sans Unicode����������Unicode���壬�Ϳ��Կ�����Щ��Bitstream CyberBit�������������һ�����壬�����Դ� http://www.bitstream.com/products/world/cyberbits ï¿½ï¿½ï¿½Òµï¿½ï¿½ï¿½ï¿½ï¿½Lucida Sans Unicode��Լ��300K����Bitstream CyberBit��Լ��13M�����������װ���������壬����Ҫһ��Lucida Sans Unicode��֧Ԯ������ʱ��Windows��ѡ��������Щ�����磺SHIFTJIS_CHARSET (����)��HANGUL_CHARSET���������JOHAB_CHARSET���������GB2312_CHARSET���������ģ���CHINESEBIG5_CHARSET ���������ģ���

���µ�������һ����ʽ�������鿴Unicode�����������ĸ��

EZFONTϵͳ
��

TrueType����ϵͳ���Դ�ͳ���Ű�Ϊ������ΪWindows�Բ�ͬ�ķ�ʽ��ʾ�����ṩ���ι̵Ļ���������һЩWindows������ѡ��ʽ���ݽϾɼ�����ʹ�û����ϵĵ��������������ӡ�����豸��������ӡ���һ�ڽ������о���������������ܹ�ʹ��ʽ�����ʾ����ӡ������ȫ����Ч������б�����������ChooseFont���Ի����飨�������ۣ�ȷʵ����������˳�ʽ�о�����ı�Ҫ�ԡ�

��Ϊ��׼TrueType����������κ�ϵͳ��ʹ�ã�����Щ������������ʾ���Լ�ӡ���������һ������ʽ��ѡ��TrueType�������ȱ����Ѷ�������ȡ��ij�����Ƶ�����ʱ����û�б�Ҫ�о������ˡ���ʽֻ��򵥲���ȷ��ѡ��ϵͳ�д��ڵ�TrueType���壨��Ȼ������ʹ���߹���ɾ�����ǣ������ַ�����ָ���������ƣ������ǵ�ʮ�����С�TrueType���塵һ�����г���13�������е�һ�֣��������Сһ���򵥡��Ұ����ַ�������EZFONT����������塹������ʽ17-2�г�����������������

 ï¿½ï¿½Ê½17-2  EZFONT
EZFONT.H
/*---------------------------------------------------------------------------
   EZFONT.H header file
----------------------------------------------------------------------------*/

HFONT EzCreateFont (	HDC hdc, TCHAR * szFaceName, int iDeciPtHeight,
                    	int iDeciPtWidth, int iAttributes, BOOL fLogRes) ;

#define 		EZ_ATTR_BOLD          						1
#define 		EZ_ATTR_ITALIC        						2
#define 		EZ_ATTR_UNDERLINE     						4
#define 		EZ_ATTR_STRIKEOUT     						8
 EZFONT.C
/*----------------------------------------------------------------------------
   EZFONT.C --	Easy Font Creation
          						(c) Charles Petzold, 1998
----------------------------------------------------------------------------*/

#include <windows.h>
#include <math.h>
#include "ezfont.h"

HFONT EzCreateFont (	HDC hdc, TCHAR * szFaceName, int iDeciPtHeight,
                    	int iDeciPtWidth, int iAttributes, BOOL fLogRes)
{
     	FLOAT      				cxDpi, cyDpi ;
     	HFONT      				hFont ;
     	LOGFONT    			lf ;
     	POINT      				pt ;
     	TEXTMETRIC 		tm ;
     
  	SaveDC (hdc) ;
	SetGraphicsMode (hdc, GM_ADVANCED) ;
     	ModifyWorldTransform 		(hdc, NULL, MWT_IDENTITY) ;
     	SetViewportOrgEx 			(hdc, 0, 0, NULL) ;
     	SetWindowOrgEx   				(hdc, 0, 0, NULL) ;
     
     	if (fLogRes)
     	{
          		cxDpi = (FLOAT) GetDeviceCaps (hdc, LOGPIXELSX) ;
          		cyDpi = (FLOAT) GetDeviceCaps (hdc, LOGPIXELSY) ;
     	}
     	else
     	{
          		cxDpi = (FLOAT) (25.4 * GetDeviceCaps (hdc, HORZRES) /
                                GetDeviceCaps (hdc, HORZSIZE)) ;
          		cyDpi = (FLOAT) (25.4 * GetDeviceCaps (hdc, VERTRES) /
                                GetDeviceCaps (hdc, VERTSIZE)) ;
	}
     
     		pt.x = (int) (iDeciPtWidth	* cxDpi / 72) ;
     		pt.y = (int) (iDeciPtHeight	* cyDpi / 72) ;
     
     		DPtoLP (hdc, &pt, 1) ;
     		lf.lfHeight         			= - (int) (fabs (pt.y) / 10.0 + 0.5) ;
     		lf.lfWidth          			= 0 ;
     		lf.lfEscapement     			= 0 ;
     		lf.lfOrientation    			= 0 ;
     		lf.lfWeight         			= iAttributes & EZ_ATTR_BOLD     	 ?  700	:0 ;
    		lf.lfItalic         			= iAttributes & EZ_ATTR_ITALIC 	?		1	:0 ;
   		lf.lfUnderline      				= iAttributes & EZ_ATTR_UNDERLINE ?	1	:0 ;
    		lf.lfStrikeOut      			= iAttributes & EZ_ATTR_STRIKEOUT ?	1	:0 ;
     		lf.lfCharSet        			= DEFAULT_CHARSET ;
     		lf.lfOutPrecision   			= 0 ;
     		lf.lfClipPrecision 		        = 0 ;
     		lf.lfQuality        			= 0 ;
     lf.lfPitchAndFamily 	                = 0 ;
     
     		lstrcpy (lf.lfFaceName, szFaceName) ;
     
     		hFont = CreateFontIndirect (&lf) ;
     
     if (iDeciPtWidth != 0)
     		{
          				hFont = (HFONT) SelectObject (hdc, hFont) ;
          				GetTextMetrics (hdc, &tm) ;
          				DeleteObject (SelectObject (hdc, hFont)) ;
          				lf.lfWidth = (int) (tm.tmAveCharWidth *
                        fabs (pt.x) / fabs (pt.y) + 0.5) ;
          				hFont = CreateFontIndirect (&lf) ;
     		}
     
     		RestoreDC (hdc, -1) ;
     		return hFont ;
}

EZFONT.Cֻ��һ����ʽ����ΪEzCreateFont��������ʾ��

hFont = EzCreateFont (	hdc, szFaceName, iDeciPtHeight, iDeciPtWidth,
                      								iAttributes, fLogRes) ;

��ʽ����������š���ͨ������SelectObject��������ѡ��װ�����ݣ�Ȼ�����GetTextMetrics��GetOutlineTextMetrics��ȷ������ߴ����߼������е�ʵ�ʴ�С���ڳ�ʽ��ֹǰ��Ӧ�ú���DeleteObjectɾ���κν��������塣

szFaceName�����������κ�TrueType�������ơ���ѡ�������Խ�ӽ���׼���壬���������ϵͳ�д��ڵĻ��ʾ�Խ��

����������ָ������ĵ�ֵ���������ĵ�λ��ʮ��֮һ�㡣������������Ҫ�ĵ�ֵΪʮ���ֶ���֮һ����ֵӦΪ125��

���ĸ�����ͨ��Ӧ�趨Ϊ����������������ͬ��Ȼ����ͨ��������λ�趨Ϊ��ֵͬ���Խ����������խ��TrueType���塣���Ե�Ϊ��λ����������Ŀ��ȣ���ʱ��֮Ϊ����ġ�ȫ����em-width��������Ҫ������������Ԫ��ƽ�����Ȼ��������ƵĶ�����������ڹ�ȥ���Ű漼���У���д��ĸM�Ŀ�����߶�����ȵġ���ǣ�����ȫ�����Σ�em-square�����ĸ�������ˣ�����ȫ����������Դ���������ȫ����������ȫ�ߣ�����ĵ�ֵ��ʱ����Ԫ����������������趨�Ŀ��ȡ�����խ��ȫ��ֵ���Բ�����ϸ���������Ԫ��

�����Խ�iAttributes�����趨Ϊ���¶�����EZFONT.H�е�ֵ��

EZ_ATTR_BOLD
EZ_ATTR_ITALIC 
EZ_ATTR_UNDERLINE
EZ_ATTR_STRIKEOUT

����ʹ��EZ_ATTR_BOLD��EZ_ATTR_ITALIC���߽���ʽ��Ϊ����TrueType�������Ƶ�һ���֡�

���ᣬ���ǽ�����fLogRes�趨Ϊ�߼�ֵTRUE���Ա�ʾ�����ֵ���豸�ġ��߼������ȡ����Ǻϣ����С��߼������ȡ���GetDeviceCaps��ʽʹ��LOGPIXELSX��LOGPIXELSY�����Ĵ���ֵ�����⣬���ݽ����ȵ������С�Ǵ�HORZRES��HORZSIZE��VERTRES��VERTSIZE��������ġ�������Windows NT�µ���Ѷ��ʾ����������ͬ��

EzCreateFont��ʽ��ʼֻ����һЩ���Windows NT�ĵ�����������SetGraphicsMode��ModifyWorldTransform��ʽ��������Windows 98�²������á���ΪWindows NT��ȫ��ת��Ӧ�����޸�������Ӵ�С�����ã�����ڼ��������С֮ǰ��ȫ��ת���趨ΪԤ��ֵ����ת����

EzCreateFont�������趨LOGFONT�ṹ����λ������CreateFontIndirect��CreateFontIndirect��������Ĵ��š�EzCreateFont��ʽ����Ҫ�����ǽ�����ĵ�ֵת��ΪLOGFONT�ṹ��lfHeight��λ��Ҫ����߼���λ����ʵ�����Ƚ���ֵת��Ϊװ�õ�λ��ͼ�أ���Ȼ����ת��Ϊ�߼���λ��Ϊ��ɵ�һ������ʽʹ��GetDeviceCaps����ͼ�ص��߼���λ��ת���ƺ�ֻ��򵥵غ���DPtoLP������װ�õ㵽�߼��㡹����ʽ������Ϊ��ʹDPtoLPת������������������ʹ�ý�����������ʾ����ʱ����ͬ��ӳ�䷽ʽ������Ч�������ζ��Ӧ���ں���EzCreateFont��ʽǰ�趨ӳ�䷽ʽ���ڴ��������£�ֻʹ��һ��ӳ�䷽ʽ���Ӵ����ض�������ƣ��������Ҫ����ʲô���⡣

��ʽ17-3��ʾ��EZTEST��ʽ�����ϸ�ؿ�����EZFONT�������˳�ʽʹ�������EZTEST�������������˱��������ʽҪʹ�õ�FONTDEMO������

 ï¿½ï¿½Ê½17-3  EZTEST
EZTEST.C
/*--------------------------------------------------------------------------
   	EZTEST.C --	Test of EZFONT
               						(c) Charles Petzold, 1998
---------------------------------------------------------------------------*/

#include <windows.h>
#include "ezfont.h"

TCHAR szAppName 	[] = TEXT ("EZTest") ;
TCHAR szTitle   	[] = TEXT ("EZTest: Test of EZFONT") ;

void PaintRoutine (HWND hwnd, HDC hdc, int cxArea, int cyArea)
{
     	HFONT      				hFont ;
     	int        				y, iPointSize ;
     	LOGFONT    			lf ;
     	TCHAR      				szBuffer [100] ;
     	TEXTMETRIC 		tm ;

          		// Set Logical Twips mapping mode

     	SetMapMode (hdc, MM_ANISOTROPIC) ;
     	SetWindowExtEx (hdc, 1440, 1440, NULL) ;
     	SetViewportExtEx (hdc, 	GetDeviceCaps 	(hdc, LOGPIXELSX),
        GetDeviceCaps 	(hdc, LOGPIXELSY), NULL) ;

        		// Try some fonts

     	y = 0 ;
     	for (iPointSize = 80 ; iPointSize <= 120 ; iPointSize++)
     	{
          		hFont = EzCreateFont (	hdc, TEXT ("Times New Roman"), 
                                		iPointSize, 0, 0, TRUE) ;

          		GetObject (hFont, sizeof (LOGFONT), &lf) ;

          		SelectObject (hdc, hFont) ;
          		GetTextMetrics (hdc, &tm) ;
          		TextOut (hdc, 0, y, szBuffer, 
               			wsprintf (	szBuffer, 
                TEXT ("Times New Roman font of %i.%i points, ")
                TEXT ("lf.lfHeight = %i, tm.tmHeight = %i"),
                 iPointSize / 10, iPointSize % 10,
                 lf.lfHeight, tm.tmHeight)) ;

          		DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
          		y += tm.tmHeight ;
     	}
}
 FONTDEMO.C
/*---------------------------------------------------------------------------
   	FONTDEMO.C --	 	Font Demonstration Shell Program
                 							(c) Charles Petzold, 1998
-----------------------------------------------------------------------------*/

#include <windows.h>
#include "..\\EZTest\\EzFont.h"
#include "..\\EZTest\\resource.h"

extern  	void     			PaintRoutine (HWND, HDC, int, int) ;
LRESULT 	CALLBACK 		WndProc (HWND, UINT, WPARAM, LPARAM) ;

HINSTANCE hInst ;

extern TCHAR szAppName [] ;
extern TCHAR szTitle [] ;

int WINAPI WinMain (	HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    								PSTR szCmdLine, int iCmdShow)
{
     	TCHAR    				szResource [] = TEXT ("FontDemo") ;
     	HWND     				hwnd ;
     	MSG      				msg ;
     	WNDCLASS 			wndclass ;
     
     	hInst = hInstance ;
     	wndclass.style         			= CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   			= WndProc ;
     	wndclass.cbClsExtra    			= 0 ;
     	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     			= hInstance ;
     	wndclass.hIcon         			= LoadIcon (NULL, IDI_APPLICATION) ;
     	wndclass.hCursor       			= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  		= szResource ;
     	wndclass.lpszClassName 		= szAppName ;
     
     	if (!RegisterClass (&wndclass))
     	{
          			MessageBox (	NULL, TEXT ("This program requires Windows NT!"),
                      								szAppName, MB_ICONERROR) ;
          			return 0 ;
     	}
     
     	hwnd = CreateWindow (	szAppName, szTitle,
                       WS_OVERLAPPEDWINDOW,
                       CW_USEDEFAULT, CW_USEDEFAULT,
                       CW_USEDEFAULT, CW_USEDEFAULT,
                       NULL, NULL, hInstance, NULL) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     
     	while (GetMessage (&msg, NULL, 0, 0))
     	{
          		TranslateMessage (&msg) ;
          		DispatchMessage (&msg) ;
     	}
	return msg.wParam ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
     	static DOCINFO  		di = { sizeof (DOCINFO), TEXT ("Font Demo: Printing") } ;
     	static int      			cxClient, cyClient ;
     	static PRINTDLG 	pd 	= { sizeof (PRINTDLG) } ;
     	BOOL            					fSuccess ;
     	HDC             					hdc, hdcPrn ;
     	int             					cxPage, cyPage ;
     	PAINTSTRUCT    			ps ;
     
     	switch (message)
     	{
     	case 	WM_COMMAND:
          			switch (wParam)
          		{
          			case 	IDM_PRINT:

                    							// Get printer DC

               					pd.hwndOwner 	= hwnd ;
               					pd.Flags     	= PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION ;

               					if (!	PrintDlg (&pd))
                    							return 0 ;

               					if (NULL == (hdcPrn = pd.hDC))
               					{
                    MessageBox(	hwnd, TEXT ("Cannot obtain Printer DC"),
                                szAppName, MB_ICONEXCLAMATION | MB_OK) ;
                    						return 0 ;
               					}
                    						// Get size of printable area of page

               					cxPage = GetDeviceCaps (hdcPrn, HORZRES) ;
               					cyPage = GetDeviceCaps (hdcPrn, VERTRES) ;

               					fSuccess = FALSE ;

                    						// Do the printer page

               					SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
               					ShowCursor (TRUE) ;

               					if ((StartDoc (hdcPrn, &di) > 0) && (StartPage (hdcPrn) > 0))
               				{
                    		PaintRoutine (hwnd, hdcPrn, cxPage, cyPage) ;
                    
                    					if (EndPage (hdcPrn) > 0)
                    					{
                         							fSuccess = TRUE ;
                         							EndDoc (hdcPrn) ;
                    					}
               				}
               				DeleteDC (hdcPrn) ;

               				ShowCursor (FALSE) ;
               				SetCursor (LoadCursor (NULL, IDC_ARROW)) ;

               				if (!fSuccess)
                    						MessageBox (hwnd, 
             TEXT ("Error encountered during printing"),
             szAppName, MB_ICONEXCLAMATION | MB_OK) ;
               				return 0 ;

          		case 	IDM_ABOUT:
               				MessageBox (	hwnd, 	TEXT ("Font Demonstration Program\n")
              TEXT ("(c) Charles Petzold, 1998"),
              szAppName, MB_ICONINFORMATION | MB_OK) ;
               				return 0 ;
          		}
          		break ;
          
     	case 	WM_SIZE:
          			cxClient = LOWORD (lParam) ;
          			cyClient = HIWORD (lParam) ;
          			return 0 ;
          
     	case 	WM_PAINT:
          			hdc = BeginPaint (hwnd, &ps) ;
          
          			PaintRoutine (hwnd, hdc, cxClient, cyClient) ;
          
          			EndPaint (hwnd, &ps) ;
          			return 0 ;
          
     	case 	WM_DESTROY :
          			PostQuitMessage (0) ;
          			return 0 ;
     }
     	return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 FONTDEMO.RC
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
// Menu
FONTDEMO MENU DISCARDABLE 
BEGIN
    	POPUP "&File"
    	BEGIN
        		MENUITEM "&Print...",  						IDM_PRINT
    	END
    	POPUP "&Help"
    	BEGIN
        		MENUITEM "&About...",  	 					IDM_ABOUT
    	END
END
 RESOURCE.H
// Microsoft Developer Studio generated include file.
// Used by FontDemo.rc

#define IDM_PRINT       40001
#define IDM_ABOUT       40002

EZTEST.C�е�PaintRoutine��ʽ��ӳ�䷽ʽ�趨ΪLogical Twips��Ȼ�Ὠ�����巶Χ��8�㵽12�㣨���Ϊ0.1�㣩��Times New Roman���塣��һ��ִ�д˳�ʽʱ������������ܻ�ʹ����������������ʹ�ô�С������ͬ�����壬����TEXTMETRIC��ʽҲ������Щ���������ͬ�ĸ߶ȡ���һ�ж��ǵ������Ľ������ʾ���ϵ�ͼ���Dz������ģ���������ʾÿһ�����ܵ������С�����ǣ�FONTDEMO��dz�ʽʹ��ӡ����������Dz�ͬ�ġ��������ᷢ�������С���ֵø��Ӿ�ȷ��

�������ת
��

����PICKFONT�п����Ѿ�ʵ����ˣ�LOGFONT�ṹ��lfOrientation��lfEscapement��λ������תTrueType���֡������ϸ����һ�£����GDI������ɶ�����ѣ���ΪΧ��ԭ����ת�����Ĺ�ʽ�ǹ����ġ�

��ȻEzCreateFont����ָ���������ת�Ƕȣ�������FONTROT����������ת������ʽչʾ���������ں��к�ʽ�ᣬ���е����Ƿdz����׵ġ���ʽ17-4��ʾ��FONTROT.C�������ó�ʽҲ��Ҫ������ʾ��EZFONT������FONTDEMO������

 ï¿½ï¿½Ê½17-4  FONTROT
FONTROT.C
/*----------------------------------------------------------------------------
   	FONTROT.C --	Rotated Fonts
                						(c) Charles Petzold, 1998
-----------------------------------------------------------------------------*/
#include <windows.h>
#include "..\\eztest\\ezfont.h"

TCHAR szAppName 	[] = TEXT ("FontRot") ;
TCHAR szTitle   	[] = TEXT ("FontRot: Rotated Fonts") ;

void PaintRoutine (HWND hwnd, HDC hdc, int cxArea, int cyArea)
{
     	static TCHAR 	szString [] = TEXT ("   Rotation") ;
     	HFONT        				hFont ;
     	int          				i ;
     	LOGFONT      			lf ;

     	hFont = EzCreateFont (hdc, TEXT ("Times New Roman"), 540, 0, 0, TRUE) ;
     	GetObject (hFont, sizeof (LOGFONT), &lf) ;
  	DeleteObject (hFont) ;

     	SetBkMode (hdc, TRANSPARENT) ;
     	SetTextAlign (hdc, TA_BASELINE) ;
     	SetViewportOrgEx (hdc, cxArea / 2, cyArea / 2, NULL) ;

     	for (i = 0 ; i < 12 ; i ++)
     	{
          			lf.lfEscapement = lf.lfOrientation = i * 300 ;
          			SelectObject (hdc, CreateFontIndirect (&lf)) ;

          			TextOut (hdc, 0, 0, szString, lstrlen (szString)) ;
          			DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
     	}
}

FONTROT����EzCreateFontֻ��Ϊ�˻����54��Times New Roman������ص�LOGFONT�ṹ��Ȼ�ᣬ��ʽɾ�������塣��for��Ȧ�У����ÿ��30�ȵĽǶȣ����������岢��ʾ���֡������ͼ17-2��ʾ��


��

ͼ17-2 FONTROT��өĻ��ʾ

�������ͼ����ת����������ת���ĸ�רҵ��������Ȥ������֪�����ij�ʽ��Windows NT��ִ�н��ܵ����ƣ�������ʹ��XFORM���������ת����ʽ����

�����о�
��

�����о��Ǵ�GDI��ȡ���豸��ȫ����Ч�����б��ij��򡣳�ʽ����ѡ������һ�����壬��������ʾ�ڶԻ������й�ʹ����ѡ�����ȼ򵥵ؽ���һ���оٺ�ʽ��Ȼ����ʾʹ��ChooseFont��ʽ�ķ�����ChooseFont������Ӧ�ó�ʽ�н��������оٵı�Ҫ�ԡ�

�оٺ�ʽ
��

��Windows�����ڣ������о���Ҫʹ��EnumFonts��ʽ��

EnumFonts (hdc, szTypeFace, EnumProc, pData) ;

��ʽ�����о����е����壨���ڶ��������趨ΪNULL����ֻ�г��ض����������������������о�callback��ʽ�����ĸ������Ǵ��ݸ��ú�ʽ�Ŀ�ѡ���ϡ�GDIΪϵͳ�е�ÿ���������callback��ʽ�������������LOGFONT��TEXTMETRIC�ṹ�Լ�һЩ��ʾ������̬����괫�ݸ�����

EnumFontFamilies��ʽ��Windows 3.1���о�TrueType����ĺ�ʽ��

EnumFontFamilies (hdc, szFaceName, EnumProc, pData) ;

ͨ����һ�κ���EnumFontFamiliesʱ���ڶ��������趨ΪNULL��Ϊÿ������ϵ�У�����Times New Roman������һ��EnumProccallback��ʽ��Ȼ�ᣬӦ�ó�ʽʹ�ø��������ƺͲ�ͬ��callback��ʽ�ٴκ���EnumFontFamilies��GDIΪ����ϵ���е�ÿ�����壨����Times New Roman Italic�����еڶ���callback��ʽ����춷�TrueType���壬��callback��ʽ����ENUMLOGFONT�ṹ��������LOGFONT�ṹ���ϡ�ȫ������λ�͡���̬����λ���ɣ�����̬����λ���������ơ�Italic����Bold������TEXTMETRIC�ṹ�����TrueType���崫��NEWTEXTMETRIC�ṹ��NEWTEXTMETRIC�ṹ����TEXTMETRIC�ṹ�е���Ѷ�������ĸ���λ��

EnumFontFamiliesEx��ʽ���Ƽ���Windows��32λԪ�İ汾��ʹ�ã�

EnumFontFamiliesEx (hdc, &logfont, EnumProc, pData, dwFlags) ;

�ڶ���������ָ��LOGFONT�ṹ��ָ�꣬����lfCharSet��lfFaceName��λָ������Ҫ�оٵ�������Ѷ��Callback��ʽ��ENUMLOGFONTEX��NEWTEXTMETRICEX�ṹ�еõ�ÿ���������Ѷ��

��ChooseFont���Ի�����
��

�ڵ�ʮһ����΢������ChooseFont��ͨ�öԻ����顣���ڣ��������������о٣���Ҫ��ϸ�˽�һ��ChooseFont��ʽ���ڲ�����ԭ����ChooseFont��ʽ�õ�ָ��CHOOSEFONT�ṹ��ָ���Դ���Ϊ����Ψһ����������ʾ�г���������ĶԻ����顣���ô�ChooseFont�еĴ���ֵ��LOGFONT�ṹ��CHOOSEFONT�ṹ��һ���֣��ܹ������߼����塣

��ʽ17-5��ʾ��CHOSFONT��ʽչʾ��ʹ��ChooseFont��ʽ�ķ���������ʾ�˺�ʽ�����LOGFONT�ṹ����λ����ʽҲ��ʾ����PICKFONT����ʾ����ͬ�ִ���

 ï¿½ï¿½Ê½17-5  CHOSFONT
CHOSFONT.C
/*-----------------------------------------------------------------------------
   	CHOSFONT.C -- 	ChooseFont Demo
                 						(c) Charles Petzold, 1998
-----------------------------------------------------------------------------*/

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    							PSTR szCmdLine, int iCmdShow)
{
	static TCHAR 	szAppName[] = TEXT ("ChosFont") ;
     	HWND         				hwnd ;
     	MSG          				msg ;
     	WNDCLASS     			wndclass ;
     	
     	wndclass.style         			= CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   			= WndProc ;
     	wndclass.cbClsExtra    			= 0 ;
     	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     			= hInstance ;
     	wndclass.hIcon         			= LoadIcon (NULL, IDI_APPLICATION) ;
     	wndclass.hCursor       			= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  		= szAppName ;
     	wndclass.lpszClassName 		= szAppName ;
     
     	if (!RegisterClass (&wndclass))
     	{
          		MessageBox (	NULL, TEXT ("This program requires Windows NT!"),
               							szAppName, MB_ICONERROR) ;
          		return 0 ;
     	}
     
     	hwnd = CreateWindow (	szAppName, TEXT ("ChooseFont"),
                          	WS_OVERLAPPEDWINDOW,
                          	CW_USEDEFAULT, CW_USEDEFAULT,
                          	CW_USEDEFAULT, CW_USEDEFAULT,
                          	NULL, NULL, hInstance, NULL) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     	while (GetMessage (&msg, NULL, 0, 0))
     	{
          		TranslateMessage (&msg) ;
          		DispatchMessage (&msg) ;
     }
  	return msg.wParam ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
     	static CHOOSEFONT 		cf ;
     	static int        		cyChar ;
     	static LOGFONT    		if ;
     	static TCHAR  szText[] = TEXT ("\x41\x42\x43\x44\x45 ")
                                 TEXT ("\x61\x62\x63\x64\x65 ")
                                 TEXT ("\xC0\xC1\xC2\xC3\xC4\xC5 ")
                                 TEXT ("\xE0\xE1\xE2\xE3\xE4\xE5 ") 
#ifdef UNICODE
       TEXT ("\x0390\x0391\x0392\x0393\x0394\x0395 ")
       TEXT ("\x03B0\x03B1\x03B2\x03B3\x03B4\x03B5 ")
     TEXT ("\x0410\x0411\x0412\x0413\x0414\x0415 ")
       TEXT ("\x0430\x0431\x0432\x0433\x0434\x0435 ")
       TEXT ("\x5000\x5001\x5002\x5003\x5004") 
#endif
                                 		;
     	HDC               				hdc ;
     	int               				y ;
     	PAINTSTRUCT       				ps ;
     	TCHAR             				szBuffer [64] ;
     	TEXTMETRIC        				tm ;
     
     	switch (message)
     	{
     	case WM_CREATE:
               				// Get text height
        			cyChar = HIWORD (GetDialogBaseUnits ()) ;
               				// Initialize the LOGFONT structure
          			GetObject (GetStockObject (SYSTEM_FONT), sizeof (lf), &lf) ;
               				// Initialize the CHOOSEFONT structure
			cf.lStructSize    			= sizeof (CHOOSEFONT) ;
          			cf.hwndOwner     	= hwnd ;
          			cf.hDC            	= NULL ;
          			cf.lpLogFont      	= &lf ;
          			cf.iPointSize    	= 0 ;
          			cf.Flags         	= CF_INITTOLOGFONTSTRUCT |
                              			  CF_SCREENFONTS | CF_EFFECTS ;
          			cf.rgbColors      	= 0 ;
          			cf.lCustData      	= 0 ;
          			cf.lpfnHook       	= NULL ;
          			cf.lpTemplateName 	= NULL ;
          			cf.hInstance      	= NULL ;
          			cf.lpszStyle      	= NULL ;
          			cf.nFontType      	= 0 ;      
          			cf.nSizeMin       	= 0 ;
          			cf.nSizeMax       	= 0 ;
          			return 0 ;

     	case	WM_COMMAND:
          			switch (LOWORD (wParam))
          			{
          			case 	IDM_FONT:
               					if (ChooseFont (&cf))
                    			InvalidateRect (hwnd, NULL, TRUE) ;
               					return 0 ;
          			}
          			return 0 ;

     	case 	WM_PAINT:
          			hdc = BeginPaint (hwnd, &ps) ;

               					// Display sample text using selected font

          			SelectObject (hdc, CreateFontIndirect (&lf)) ;
          			GetTextMetrics (hdc, &tm) ;
          			SetTextColor (hdc, cf.rgbColors) ;
          			TextOut (hdc, 0, y = tm.tmExternalLeading, szText, lstrlen (szText)) ;

               					// Display LOGFONT structure fields using system font

          			DeleteObject 		(SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
          			SetTextColor 	(hdc, 0) ;
          
          			TextOut (hdc, 0, y += tm.tmHeight, szBuffer,
        wsprintf (szBuffer, TEXT ("lfHeight = %i"), lf.lfHeight)) ;

          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (szBuffer, TEXT ("lfWidth = %i"), lf.lfWidth)) ;
          
          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (	szBuffer, TEXT ("lfEscapement = %i"), 
                         	        lf.lfEscapement)) ;
          
          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (	szBuffer, TEXT ("lfOrientation = %i"), 
                         			lf.lfOrientation)) ;

          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (szBuffer, TEXT ("lfWeight = %i"),lf.lfWeight)) ;

          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (szBuffer, TEXT ("lfItalic = %i"),lf.lfItalic)) ;

          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (szBuffer, TEXT ("lfUnderline = %i"),lf.lfUnderline)) ;

          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (szBuffer, TEXT ("lfStrikeOut = %i"),lf.lfStrikeOut)) ;

          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (szBuffer, TEXT ("lfCharSet = %i"),lf.lfCharSet)) ;

          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (	szBuffer, TEXT ("lfOutPrecision = %i"), 
                   lf.lfOutPrecision)) ;

          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (szBuffer, TEXT ("lfClipPrecision = %i"), 
                  lf.lfClipPrecision)) ;

          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (	szBuffer, TEXT ("lfQuality = %i"),lf.lfQuality)) ;

          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (	szBuffer, TEXT ("lfPitchAndFamily = 0x%02X"), 
                     lf.lfPitchAndFamily)) ;

          			TextOut (hdc, 0, y += cyChar, szBuffer,
        wsprintf (	szBuffer, TEXT ("lfFaceName = %s"),lf.lfFaceName)) ;

          			EndPaint (hwnd, &ps) ;
          			return 0 ;
     	case 	WM_DESTROY:
          			PostQuitMessage (0) ;
          			return 0 ;
     	}
     	return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 CHOSFONT.RC
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
// Menu
CHOSFONT MENU DISCARDABLE 
BEGIN
 	MENUITEM "&Font!",                      						IDM_FONT
END
 RESOURCE.H
// Microsoft Developer Studio generated include file.
// Used by ChosFont.rc

#define IDM_FONT     40001

��һ��ĶԻ�����һ����CHOOSEFONT�ṹ��Flags��λ�г�������ѡ�CHOSFONTָ����CF_INITLOGFONTSTRUCT���ʹWindows���ݴ��ݸ�ChooseFont�ṹ��LOGFONT�ṹ�ԶԻ������ѡ����г�ʼ����������ʹ�������ָ��ֻҪ�г�TrueType���壨CF_TTONLY����ֻҪ�г��������壨CF_FIXEDPITCHONLY�����޷������壨CF_SCRIPTSONLY����Ҳ������ʾөĻ���壨CF_SCREENFONTS������ӡ���壨CF_PRINTERFONTS���������߶���ʾ��CF_BOTH����������������£�CHOOSEFONT�ṹ��hDC��λ������ӡ����װ�����ݡ�CHOSFONT��ʽʹ��CF_SCREENFONTS��ꡣ

CF_EFFECTS��꣨CHOSFONT��ʽʹ�õĵ�������꣩ǿ�ȶԻ����������춵��ߺ�ɾ���ߵĺ�ȡ���鲢������ѡ�����ֵ���ɫ���ڳ�ʽ���б任������ɫ���ѣ���������һ�ԡ�

ע�⡸Font���Ի���������ChooseFont��ʾ�ġ�Script����λ������ʹ����ѡ����������������Ԫ�����ʵ�����Ԫ��ID��LOGFONT�ṹ�д��ء�

ChooseFont��ʽʹ���߼�Ӣ��ӵ�ֵ�м���lfHeight��λ�����磬�ٶ����ӡ���ʾ���ԡ��Ի������а�װ�ˡ�С���塹������ζ��������Ѷ��ʾװ�����ݵ�GetDeviceCaps�Ͳ���LOGPIXELSY����96�����ʹ��ChooseFontѡ��72���Times Roman���壬ʵ��������Ҫ1Ӣ��ߵ����塣��ChooseFont�����ᣬLOGFONT�ṹ��lfHeight��λ���-96��ע�⸺�ţ�������ָ����ĵ�ֵ���96ͼ�أ�����1�߼�Ӣ�硣

���ϴ����������Ҫ֪���ġ������ס���¼���:

  • �����Windows NT���趨�˶���ӳ�䷽ʽ�����߼������������ʵ�ʴ�С��һ�¡����磬��������ݶ���ӳ�䷽ʽ�������Ի�һ�ѳߣ��ᷢ���������岻�����Ӧ��ʹ������������Logical Twipsӳ�䷽ʽ������ͼ�Σ������������Сһ�¡�
    ��
  • ���Ҫʹ���κη�MM_TEXTӳ�䷽ʽ����ȷ���ڰ�����ѡ��װ�����ݺ���ʾ����ʱ��û���趨ӳ�䷽ʽ������ GDI����ΪLOGFONT�ṹ��lfHeight��λ���߼����ꡣ
    ��
  • ��ChooseFont�趨��LOGFONT�ṹ��lfHeight��λ����ͼ��ֵ��������ֻ�������Ѷ��ʾ��������Ϊӡ����װ�����ݽ�������ʱ���������lfHeightֵ��ChooseFont��ʽʹ��CHOOSEFONT�ṹ��hDC��λֻΪ������ڶԻ������е�ӡ�������塣��װ�����ݴ��Ų�Ӱ��lfHeightֵ��
    ��

���˵��ǣ�CHOOSEFONT�ṹ����һ��iPointSize��λ�����ṩ��ʮ��֮һ��Ϊ��λ����ѡ����Ĵ�С��������ʲôװ�����ݺ�ӳ�䷽ʽ�����ܰ������λת��Ϊ�߼���С�����lfHeight��λ����EZFONT.C�����ҵ����ʵij�ʽ�룬�����Ը�����Ҫ������

��һ��ʹ��ChooseFont�ij�ʽ��UNICHARS�������ʽ�����鿴һ�������������Ԫ������о�Lucida Sans Unicode���壨�ڶ�����ʾ���壩��Bitstream CyberBit�����������á�UNICHARS����ʹ��TextOutW��ʽ����ʾ�������Ԫ����˿�����Windows NT��Windows 98��ִ������

 ï¿½ï¿½Ê½17-6  UNICHARS
UNICHARS.C
/*---------------------------------------------------------------------------
   	UNICHARS.C -- 	Displays 16-bit character codes
                 						(c) Charles Petzold, 1998
-----------------------------------------------------------------------------*/

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    							PSTR szCmdLine, int iCmdShow)
{
     	static TCHAR 	szAppName[] = TEXT ("UniChars") ;
     	HWND         				hwnd ;
     	MSG          				msg ;
     	WNDCLASS     			wndclass ;
     
     	wndclass.style         			= CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   			= WndProc ;
     	wndclass.cbClsExtra    			= 0 ;
     	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     			= hInstance ;
     	wndclass.hIcon         			= LoadIcon (NULL, IDI_APPLICATION) ;
     	wndclass.hCursor       			= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  		= szAppName ;
     	wndclass.lpszClassName 		= szAppName ;

     	if (!RegisterClass (&wndclass))
     	{
          		MessageBox (	NULL, TEXT ("This program requies Windows NT!"), 
                      							szAppName, MB_ICONERROR) ;
          		return 0 ;
     }
     
     	hwnd = CreateWindow (	szAppName, TEXT ("Unicode Characters"),
                         WS_OVERLAPPEDWINDOW | WS_VSCROLL,
                         CW_USEDEFAULT, CW_USEDEFAULT,
                         CW_USEDEFAULT, CW_USEDEFAULT,
                         NULL, NULL, hInstance, NULL) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     	while (GetMessage (&msg, NULL, 0, 0))
     {
          		TranslateMessage (&msg) ;
          		DispatchMessage (&msg) ;
     }
     	return msg.wParam ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
     	static CHOOSEFONT 		cf ;
     	static int        				iPage ;
     	static LOGFONT    			    lf ;
     	HDC               				hdc ;
     	int               				cxChar, cyChar, x, y, i, cxLabels ;
     	PAINTSTRUCT       				ps ;
     	SIZE              				size ;
     	TCHAR             				szBuffer [8] ;
     	TEXTMETRIC        				tm ;
     	WCHAR             				ch ;

     	switch (message)
     	{
     	case 	WM_CREATE:
          			hdc = GetDC (hwnd) ;
          			lf.lfHeight = - GetDeviceCaps (hdc, LOGPIXELSY) / 6 ;  // 12 points
          			lstrcpy (lf.lfFaceName, TEXT ("Lucida Sans Unicode")) ;
          			ReleaseDC (hwnd, hdc) ;

          			cf.lStructSize 	= sizeof (CHOOSEFONT) ;
          			cf.hwndOwner   		= hwnd ;
          			cf.lpLogFont   		= &lf ;
          			cf.Flags       		= CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS ;

          			SetScrollRange 		(hwnd, SB_VERT, 0, 255, FALSE) ;
          			SetScrollPos 		(hwnd, SB_VERT, iPage,  TRUE ) ;
          			return 0 ;

     	case 	WM_COMMAND:
          			switch (LOWORD (wParam))
          			{
          			case 	IDM_FONT:
               					if (	ChooseFont (&cf))
                    					InvalidateRect (hwnd, NULL, TRUE) ;
               					return 0 ;
			}
         	 		return 0 ;
     	case 	WM_VSCROLL:
          			switch (LOWORD (wParam))
             				{
               				case SB_LINEUP:    iPage -=  1 	;  break 	;
               				case SB_LINEDOWN:  iPage +=  1 	;  break 	;
               				case SB_PAGEUP:    iPage -= 16 	;  break 	;
               				case SB_PAGEDOWN:  iPage += 16 	;  break 	;
               				case SB_THUMBPOSITION:iPage= HIWORD (wParam);  break ;

               				default:
                    					return 0 ;
               				}

          			iPage = max (0, min (iPage, 255)) ;

          			SetScrollPos (hwnd, SB_VERT, iPage, TRUE) ;
          			InvalidateRect (hwnd, NULL, TRUE) ;
          			return 0 ;

     	case 	WM_PAINT:
          			hdc = BeginPaint (hwnd, &ps) ;

          			SelectObject (hdc, CreateFontIndirect (&lf)) ;

          			GetTextMetrics (hdc, &tm) ;
          			cxChar = tm.tmMaxCharWidth ;
          			cyChar = tm.tmHeight + tm.tmExternalLeading ;

          			cxLabels = 0 ;

          			for (i = 0 ; i < 16 ; i++)
          			{
               			wsprintf (szBuffer, TEXT (" 000%1X: "), i) ;
               			GetTextExtentPoint (hdc, szBuffer, 7, &size) ;

               				cxLabels = max (cxLabels, size.cx) ;
          			}

          			for (y = 0 ; y < 16 ; y++)
          			{
               wsprintf (szBuffer, TEXT (" %03X_: "), 16 * iPage + y) ;
               					TextOut (hdc, 0, y * cyChar, szBuffer, 7) ;

               					for (x = 0 ; x < 16 ; x++)
               					{
                    				ch = (WCHAR) (256 * iPage + 16 * y + x) ;
                    				TextOutW (hdc, 	x * cxChar + cxLabels,
                                   					y * cyChar, &ch, 1) ;
               }
          			}

          DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
          			EndPaint (hwnd, &ps) ;
          			return 0 ;

     	case 	WM_DESTROY:
          			PostQuitMessage (0) ;
          			return 0 ;
 	}
     	return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 UNICHARS.RC
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
// Menu
UNICHARS MENU DISCARDABLE 
BEGIN
  	MENUITEM "&Font!",    		  							IDM_FONT
END
 RESOURCE.H
// Microsoft Developer Studio generated include file.
// Used by Unichars.rc

#define IDM_FONT            40001

�����ʽ
��

����ѡ�񲢽����߼�����������ᣬ�Ϳ��Դ������ָ�ʽ�ˡ����������������ַ�ʽ֮һ�������ֵ�ÿһ�з���ҳ�߾��ڣ�����롢���Ҷ��롢���л��ɢ���룭����ҳ�߾��һ�˵���һ�ˣ����ּ����ȡ����ǰ���ַ�ʽ������ʹ�ô���DT_WORDBREAK������DrawText��ʽ�������ַ����о����ԡ����磬���޷�ȷ��DrawText������ֵ��ĸ�����ǡ�÷��ھ����ڡ�DrawText���һЩ�������Ǻܷ���ģ����Ը����ӵĸ�ʽ�����������Ҫ�õ�TextOut��

�����ָ�ʽ
��

�����ֵ������õ�һ����ʽ��GetTextExtentPoint32�������ʽ��������ʾ��Windows���ڰ汾��һЩ�仯�����ú�ʽ����װ��������ѡ���Ŀǰ����ó��ִ��Ŀ��Ⱥ͸߶ȣ�

GetTextExtentPoint32 (hdc, pString, iCount, &size) ;

�߼���λ�����ֿ��Ⱥ͸߶���SIZE�ṹ��cx��cy��λ�д��ء���ʹ��һ�����ֵ����ӣ��ٶ�����һ������ѡ��װ�����ݣ�����Ҫд�����֣�

TCHAR * szText [] = TEXT ("Hello, how are you?") ;

��ϣ�����ִӴ�ֱ����yStart��ʼ��ҳ�߾�������xLeft��xRight�趨������������Ǽ������ֿ�ʼ����ˮƽ�����xStartֵ��

��������Զ���������ʾ����ô����������൱���ף���ͨ�����������ġ��������õ��ִ������ֿ��ȣ�

GetTextExtentPoint32 (hdc, szText, lstrlen (szText), &size) ;

���size.cx�ȣ�xRight�� xLeft������һ�о�̫���ˣ����ܷ���ҳ�߾��ڡ����Ǽٶ����ܷŽ�ȥ��

Ҫ����������֣�ֻҪ��xStart�趨Ϊ��xLeft��ȣ�Ȼ��д�����֣�

TextOut (hdc, xStart, yStart, szText, lstrlen (szText)) ;

������ס����ڿ��԰�size.cy�ӵ�yStart��д��һ�������ˡ�

Ҫ���Ҷ������֣������¹�ʽ����xStart��

xStart = xRight - size.cx ;

�������������¹�ʽ��

xStart = (xLeft + xRight - size.cx) / 2 ;

���ڿ�ʼ���ҵ�����������ҳ�߾��ڷ�ɢ�������֡�ҳ�߾�֮��ľ����ǣ�xRight��xLeft�����粻���������ֿ��Ⱦ���size.cx������֮��

xRight - xLeft - size.cx

�������ִ��������ո���Ԫ��ƽ�����á��������������ᣬ��������̫�㡣���Ժ���

SetTextJustification (hdc, xRight - xLeft - size.cx, 3)

����ɡ��ڶ����������ִ��ڿո���Ԫ����Ҫ����Ŀռ����������������ǿո���Ԫ������������Ϊ3�����ڰ�xStart�趨��xLeft��ȣ���TextOutд�����֣�

TextOut (hdc, xStart, yStart, szText, lstrlen (szText)) ;

���ֻ���xLeft��xRightҳ�߾�֮���ɢ���롣

���ۺ�ʱ����SetTextJustification������ռ��������ڿո���Ԫ��ƽ�����䣬���ͻ��ۻ�һ������ֵ���⽫Ӱ�������GetTextExtentPoint32���С�ÿ�ο�ʼ�µ�һ�У�������ͨ������

SetTextJustification (hdc, 0, 0) ;

���������ֵ��

ʹ�ö���
��

����������������䣬�ͱ����ͷ��ʼ��ɨ���ִ���Ѱ�ҿո���Ԫ��ÿ������һ���ո񣨻���������춶Ͽ�һ�е���Ԫ���������GetTextExtentPoint32��ȷ�������Ƿ��ܷ�������ҳ�߾�֮�䡣�����ֳ��������Ŀռ�ʱ����Ҫ�˻���һ���հס����ڣ����Ѿ��ܹ�ȷ��һ�е��ִ��ˡ������Ҫ��ɢ������У�����SetTextJustification��TextOut���������ֵ����������һ�С�

��ʾ�ڳ�ʽ17-7�е�JUSTIFY1��Mark Twain�ġ�The Adventures of Huckleberry Finn���еĵ�һ�����������Ĵ����������ԴӶԻ�������ѡ����Ҫ�����壬Ҳ����ʹ�ù��ܱ�ѡ�������Ķ��뷽ʽ������롢���Ҷ��롢���л��ɢ���룩��ͼ17-3�ǵ��͵�JUSTIFY1өĻ��ʾ��

 ï¿½ï¿½Ê½17-7  JUSTIFY1
JUSTIFY1.C
/*--------------------------------------------------------------------------
   	JUSTIFY1.C -- Justified Type Program #1
                 							(c) Charles Petzold, 1998
---------------------------------------------------------------------------*/

#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
TCHAR szAppName[] = TEXT ("Justify1") ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    							PSTR szCmdLine, int iCmdShow)
{
     	HWND     			hwnd ;
     	MSG      			msg ;
     	WNDCLASS	 	wndclass ;
     
     	wndclass.style         			= CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   			= WndProc ;
     	wndclass.cbClsExtra    			= 0 ;
     	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     			= hInstance ;
     	wndclass.hIcon         			= LoadIcon (NULL, IDI_APPLICATION) ;
     	wndclass.hCursor       			= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  		= szAppName ;
     	wndclass.lpszClassName 		= szAppName ;
     
     	if (!RegisterClass (&wndclass))
     	{
          			MessageBox (	NULL, TEXT ("This program requires Windows NT!"),
               								szAppName, MB_ICONERROR) ;
      			return 0 ;
     }
     
     	hwnd = CreateWindow (	szAppName, TEXT ("Justified Type #1"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     
     	while (GetMessage (&msg, NULL, 0, 0))
     	{
          			TranslateMessage (&msg) ;
          			DispatchMessage (&msg) ;
     }
     	return msg.wParam ;
}

void DrawRuler (HDC hdc, RECT * prc)
{
     	static int iRuleSize [16] = {360, 72,144, 72,216, 72,144,72,
 288, 72,144, 72,216, 72,144,72 } ;
     	int        				i, j ;
     	POINT      				ptClient ;
     
     	SaveDC (hdc) ;
          				// Set Logical Twips mapping mode
     	SetMapMode (hdc, MM_ANISOTROPIC) ;
     	SetWindowExtEx (hdc, 1440, 1440, NULL) ;
     	SetViewportExtEx (hdc,	GetDeviceCaps (hdc, LOGPIXELSX),
                            	GetDeviceCaps (hdc, LOGPIXELSY), NULL) ;
     
          				// Move the origin to a half inch from upper left
     
     	SetWindowOrgEx (hdc, -720, -720, NULL) ;
          				// Find the right margin (quarter inch from right)
     	ptClient.x = prc->right ;
     	ptClient.y = prc->bottom ;
     	DPtoLP (hdc, &ptClient, 1) ;
     	ptClient.x -= 360 ;
     
          				// Draw the rulers
	MoveToEx 		(hdc, 0,          -360, NULL) ;
     	LineTo   		(hdc, ptClient.x, -360) ;
     	MoveToEx 		(hdc, -360,          0, NULL) ;
     	LineTo   		(hdc, -360, ptClient.y) ;
     
     	for (i = 0, j = 0 ; i <= ptClient.x ; i += 1440 / 16, j++)
     {
          		MoveToEx 		(hdc, i, -360, NULL) ;
          		LineTo   		(hdc, i, -360 - iRuleSize [j % 16]) ;
     	}
     
     	for (i = 0, j = 0 ; i <= ptClient.y ; i += 1440 / 16, j++)
     {
          		MoveToEx 		(hdc, -360, i, NULL) ;
          		LineTo   		(hdc, -360 - iRuleSize [j % 16], i) ;
     }
     
     	RestoreDC (hdc, -1) ;
}
void Justify (HDC hdc, PTSTR pText, RECT * prc, int iAlign)
{
     	int   		xStart, yStart, cSpaceChars ;
     	PTSTR 		pBegin, pEnd ;
     	SIZE  		size ;
     
     	yStart = prc->top ;
     	do                            // for each text line
     {
   		cSpaceChars = 0 ;  		// initialize number of spaces in line

          		while (*	pText == '')    	// skip over leading spaces
               					pText++ ;

		pBegin = pText ;         				// set pointer to char at beginning of line
          
         		do                       		// until the line is known
          		{
               					pEnd =pText ;	// set pointer to char at end of line

                    							// skip to next space 
               
               					while (*pText != '\0' && *pText++ != ' ') ;

               					if (*pText == '\0')
                    							break ;

                    							// after each space encountered, calculate extents

               					cSpaceChars++ ;
               					GetTextExtentPoint32(hdc, pBegin, pText - pBegin - 1, &size) ;
          }
          		while (size.cx < (prc->right - prc->left)) ;
          		cSpaceChars-- ;               			// discount last space at end of line
          
          		while (*(pEnd - 1) == ' ') 	// eliminate trailing spaces
          		{
               				pEnd-- ;
               				cSpaceChars-- ;
          }

               	// if end of text and no space characters, set pEnd to end
          		if (*	pText == '\0' || cSpaceChars <= 0)
               					pEnd = pText ;

          		GetTextExtentPoint32 (hdc, pBegin, pEnd - pBegin, &size) ;
          
          		switch (iAlign)       // use alignment for xStart
       		{
          		case 	IDM_ALIGN_LEFT:
               				xStart = prc->left ;
               				break ;
               
          		case 	IDM_ALIGN_RIGHT:
               				xStart = prc->right - size.cx ;
               				break ;
               
     		case 	IDM_ALIGN_CENTER:
               				xStart = (prc->right + prc->left - size.cx) / 2 ;
               				break ;
               
       		case 	IDM_ALIGN_JUSTIFIED:
               				if (*	pText != '\0' && cSpaceChars > 0)
SetTextJustification (hdc, prc->right-prc->left - size.cx, cSpaceChars) ;
               				xStart = prc->left ;
               				break ;
          		}
               				// display the text
          
          		TextOut (hdc, xStart, yStart, pBegin, pEnd - pBegin) ;

               			// prepare for next line

          		SetTextJustification (hdc, 0, 0) ;
          		yStart += size.cy ;
          		pText = pEnd ;
     }
     	while (*pText && yStart < prc->bottom - size.cy) ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
     	static CHOOSEFONT	cf ;
     	static DOCINFO    			di = { sizeof (DOCINFO), TEXT ("Justify1: Printing") } ;
     	static int        				iAlign = IDM_ALIGN_LEFT ;
     	static LOGFONT    		lf ;
     	static PRINTDLG   		pd ;
     	static TCHAR      			szText[] = { 
      TEXT ("You don't know about me, without you ")
      TEXT ("have read a book by the name of \"The ")
      TEXT ("Adventures of Tom Sawyer,\" but that ")
      TEXT ("ain't no matter. That book was made by ")
      TEXT ("Mr. Mark Twain, and he told the truth, ")
      TEXT ("mainly. There was things which he ")
      TEXT ("stretched, but mainly he told the truth. ")
      TEXT ("That is nothing. I never seen anybody ")
      TEXT ("but lied, one time or another, without ")
      TEXT ("it was Aunt Polly, or the widow, or ")
      TEXT ("maybe Mary. Aunt Polly -- Tom's Aunt ")
      TEXT ("Polly, she is -- and Mary, and the Widow ")
      TEXT ("Douglas, is all told about in that book ")
      TEXT ("-- which is mostly a true book; with ")
      TEXT ("some stretchers, as I said before.") } ;
     	BOOL              				fSuccess ;
     	HDC               				hdc, hdcPrn ;
     	HMENU             				hMenu ;
     	int               				iSavePointSize ;
     	PAINTSTRUCT       		ps ;
     	RECT    				rect ;
     
     	switch (message)
     	{
     	case 	WM_CREATE:
               					// Initialize the CHOOSEFONT structure

          			GetObject (GetStockObject (SYSTEM_FONT), sizeof (lf), &lf) ;

          			cf.lStructSize    			= sizeof (CHOOSEFONT) ;
          			cf.hwndOwner      			= hwnd ;
          			cf.hDC            			= NULL ;
          			cf.lpLogFont      			= &lf ;
          			cf.iPointSize     			= 0 ;
          			cf.Flags         	 		= CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS | 
                    CF_EFFECTS ;
          			cf.rgbColors      			= 0 ;
          			cf.lCustData     		 	= 0 ;
          			cf.lpfnHook       			= NULL ;
          			cf.lpTemplateName 		    = NULL ;
          			cf.hInstance      			= NULL ;
          			cf.lpszStyle      			= NULL ;
          			cf.nFontType      			= 0 ;      
          			cf.nSizeMin       			= 0 ;
          			cf.nSizeMax       			= 0 ;
  
          			return 0 ;

     	case 	WM_COMMAND:
          			hMenu = GetMenu (hwnd) ;
          
          			switch (LOWORD (wParam))
          			{
          			case IDM_FILE_PRINT:
                                   		// Get printer DC

               					pd.lStructSize 		= sizeof (PRINTDLG) ;
               					pd.hwndOwner   		= hwnd ;
               					pd.Flags       		= PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION ;

               					if (!PrintDlg (&pd))
                    							return 0 ;

               					if (NULL == (hdcPrn = pd.hDC))
               					{
                   MessageBox (	hwnd, TEXT ("Cannot obtain Printer DC"),
                                szAppName, MB_ICONEXCLAMATION | MB_OK) ;
                    			return 0 ;
               					}
                    						// Set margins of 1 inch

               	rect.left   	= 	GetDeviceCaps (hdcPrn, LOGPIXELSX) -
                             		GetDeviceCaps (hdcPrn, PHYSICALOFFSETX) ;

               rect.top  = 	GetDeviceCaps (hdcPrn, LOGPIXELSY) 
                            GetDeviceCaps (hdcPrn, PHYSICALOFFSETY) ;
rect.right = 	GetDeviceCaps (hdcPrn, PHYSICALWIDTH) -
                            GetDeviceCaps (hdcPrn, LOGPIXELSX) -
                            GetDeviceCaps (hdcPrn, PHYSICALOFFSETX) ;
rect.bottom= 	GetDeviceCaps (hdcPrn, PHYSICALHEIGHT) - 
                            GetDeviceCaps (hdcPrn, LOGPIXELSY) -
                            GetDeviceCaps (hdcPrn, PHYSICALOFFSETY) ;

                    						// Display text on printer

               					SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
               					ShowCursor (TRUE) ;

               					fSuccess = FALSE ;
     if ((StartDoc (hdcPrn, &di) > 0) && (StartPage (hdcPrn) > 0))
               					{
                         			// Select font using adjusted lfHeight

                    	iSavePointSize = lf.lfHeight ;
                    	lf.lfHeight = -(GetDeviceCaps (hdcPrn, LOGPIXELSY) *
                                        cf.iPointSize) / 720 ;

                    	SelectObject (hdcPrn, CreateFontIndirect (&lf)) ;
                    	lf.lfHeight = iSavePointSize ;

                        // Set text color 
		
                    	SetTextColor (hdcPrn, cf.rgbColors) ;
               
                        // Display text

                    	Justify (hdcPrn, szText, &rect, iAlign) ;

                    	if (EndPage (hdcPrn) > 0)
                    							{	
                         						fSuccess = TRUE ;
                         						EndDoc (hdcPrn) ;
                    							}
               				}
               				ShowCursor (FALSE) ;
               				SetCursor (LoadCursor (NULL, IDC_ARROW)) ;

               				DeleteDC (hdcPrn) ;

               				if (!fSuccess)
                    MessageBox (hwnd, TEXT ("Could not print text"),
                                szAppName, MB_ICONEXCLAMATION | MB_OK) ;
               		return 0 ;

		case	IDM_FONT:
               				if (ChooseFont (&cf))
                    						InvalidateRect (hwnd, NULL, TRUE) ;
               				return 0 ;
               
          		case 	IDM_ALIGN_LEFT:
          		case 	IDM_ALIGN_RIGHT:
          		case 	IDM_ALIGN_CENTER:
          		case 	IDM_ALIGN_JUSTIFIED:
               				CheckMenuItem (hMenu, iAlign, MF_UNCHECKED) ;
               				iAlign = LOWORD (wParam) ;
               				CheckMenuItem (hMenu, iAlign, MF_CHECKED) ;
               				InvalidateRect (hwnd, NULL, TRUE) ;
               				return 0 ;
          			}
          			return 0 ;

     	case 	WM_PAINT:
          			hdc = BeginPaint (hwnd, &ps) ;
          
          			GetClientRect (hwnd, &rect) ;
          			DrawRuler (hdc, &rect) ;
          
          			rect.left 	+=	GetDeviceCaps (hdc, LOGPIXELSX) / 2 ;
          			rect.top 	+=	GetDeviceCaps (hdc, LOGPIXELSY) / 2 ;
     			rect.right		-= 	GetDeviceCaps (hdc, LOGPIXELSX) / 4 ;

          			SelectObject (hdc, CreateFontIndirect (&lf)) ;
          			SetTextColor (hdc, cf.rgbColors) ;
          
          			Justify (hdc, szText, &rect, iAlign) ;
          
          			DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
          			EndPaint (hwnd, &ps) ;
          			return 0 ;
          
     	case 	WM_DESTROY:
          			PostQuitMessage (0) ;
          			return 0 ;
     }
     	return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 JUSTIFY1.RC
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
// Menu
JUSTIFY1 MENU DISCARDABLE BEGIN POPUP "&File"
    	BEGIN
        		MENUITEM "&Print", 						IDM_FILE_PRINT
    	END
    	POPUP "&Font"
    	BEGIN
        	MENUITEM "&Font...",      IDM_FONT
    	END
    	POPUP "&Align"
    	BEGIN
       MENUITEM "&Left", 				IDM_ALIGN_LEFT, CHECKED
       MENUITEM "&Right",    			IDM_ALIGN_RIGHT
       MENUITEM "&Centered",  		IDM_ALIGN_CENTER
       MENUITEM "&Justified", 		IDM_ALIGN_JUSTIFIED
	END
END
 RESOURCE.H
// Microsoft Developer Studio generated include file.
// Used by Justify1.rc

#define 		IDM_FILE_PRINT   40001
#define 		IDM_FONT         40002
#define 		IDM_ALIGN_LEFT   40003
#define 		IDM_ALIGN_RIGHT  40004
#define 		IDM_ALIGN_CENTER 40005
#define 		IDM_ALIGN_JUSTIFIED  40006

JUSTIFY1����ʾ������ϲ��������ʾ�˳߹棨��Ȼ��λ���߼�Ӣ�磩���߹���DrawRuler��ʽ������һ�����νṹ�����˷�ɢ�������ֵ�����

�漰�����ֽ��и�ʽ�����Ĵ���������Justify��ʽʵ������ʽ��Ѱ���ֿ�ʼ�Ŀհף���ʹ��GetTextExtentPoint32����ÿһ�С����еij��ȳ�����ʾ����Ŀ��ȣ�JUSTIFY1������ǰ�Ŀո�ʹ���е���linefeed��������iAlign������ֵ���еĶ��뷽ʽ�У�ͬ����롢���Ҷ��롢���л��ɢ���롣

JUSTIFY1�������������磬��û�д�������Ԫ�����⡣���⣬��ÿ�����������ʱ����ɢ�����������ʧЧ����ʹ���ǽ������������ر��ѵ����⣬��һ������̫�������ұ߾��Ų���ʱ����ʽ�Բ���������������Ȼ���������ڳ�ʽ�ж�ͬһ��ʹ�ö������壨��ͬWindows���鴦����ʽ����������������ʱ�����������ӡ���û�����������ִ������ף���ֻ�DZ��������������еĹ�������һЩ��


��

ͼ17-3 ���͵�JUSTIFY1өĻ��ʾ

��ӡ���Ԥ��
��

��Щ���岻��Ϊ����өĻ�ϲ鿴�õģ���Щ�����������ӡ�ġ�ͨ������������£����ֵ�өĻԤ����������ӡ����ĸ�ʽ��ȷ��ϡ���ʾͬ�������塢��С����Ԫ��ʽ�Dz����ġ�ʹ��TrueType�Ǹ��ݾ������⻹��Ҫ�������е�ÿһ����ͬ��λ�öϿ�������WYSIWYG�е��ѵ㡣

JUSTIFY1����һ����Print��ѡ�����ѡ�����ҳ����ϡ�����ұ��趨1Ӣ��ı߾ࡣ��������ʽ����ȫ��өĻ��ʾ���޹ء�������һ����Ȥ����ϰ����JUSTIFY1�и��ļ��г�ʽ�룬ʹөĻ��ӡ�����߼�����һ��6Ӣ��ĸ�ʽ�����Ρ�����������WM_PAINT�͡�Print���������ʽ�и���rect.right�Ķ��塣��WM_PAINT������ʽ�У����Ӧ����Ϊ��

rect.right = rect.left + 6 * GetDeviceCaps (hdc, LOGPIXELSX) ;

�ڡ�Print���������ʽ�У����Ӧ����Ϊ��

rect.right = rect.left + 6 * GetDeviceCaps (hdcPrn, LOGPIXELSX) ;

���ѡ����һ��TrueType���壬өĻ�ϵ�linefeed���Ӧ��ӡ�����������ͬ��

��ʵ�������������������ʹ�����豸ʹ��ͬ����ֵ����ͬ���壬����������ʾ��ͬ���ĸ�ʽ�������У���ͬ����ʾ�����ȼ��������Ҳ��ʹlinefeed�����ڲ�ͬ�ط�����Ȼ����Ҫһ�ָ������ķ�������өĻ�ϵ���ӡ���Ԥ����

��ʽ17-8��ʾ��JUSTIFY2ʾ�������ַ�����һ�����ԡ�JUSTIFY2�еij�ʽ��������Microsoft��David Weise��д��TTJUST����TrueType Justify������ʽ�����ó�ʽ�������ݱ���ǰ���һ���汾�е�JUSTIFY1��ʽ��Ϊ���ֳ���һ��ʽ�������ӵĸ����ԣ���Herman Melville�ġ�Moby-Dick���еĵ�һ�´�����Mark TwainС˵��ժ¼��

 ï¿½ï¿½Ê½17-8  JUSTIFY2
JUSTIFY2.C
/*-----------------------------------------------------------------------------
   	JUSTIFY2.C -- 		Justified Type Program #2
                 							(c) Charles Petzold, 1998
-----------------------------------------------------------------------------*/

#include <windows.h>
#include "resource.h"

#define OUTWIDTH 6       			// Width of formatted output in inches
#define LASTCHAR 127     			// Last character code used in text

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
TCHAR szAppName[] = TEXT ("Justify2") ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    							PSTR szCmdLine, int iCmdShow)
{
	HWND     			hwnd ;
     	MSG      			msg ;
     	WNDCLASS wndclass ;
     	wndclass.style         			= CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   			= WndProc ;
     	wndclass.cbClsExtra    			= 0 ;
     	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     			= hInstance ;
     	wndclass.hIcon         			= LoadIcon (NULL, IDI_APPLICATION) ;
     	wndclass.hCursor       			= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  		= szAppName ;
     	wndclass.lpszClassName 		= szAppName ;
          	if (!RegisterClass (&wndclass))
     {
          		MessageBox (NULL, TEXT ("This program requires Windows NT!"),
               							szAppName, MB_ICONERROR) ;
          		return 0 ;
     }
     
     	hwnd = CreateWindow (	szAppName, TEXT ("Justified Type #2"),
                          	WS_OVERLAPPEDWINDOW,
                          	CW_USEDEFAULT, CW_USEDEFAULT,
                          	CW_USEDEFAULT, CW_USEDEFAULT,
                          	NULL, NULL, hInstance, NULL) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     
     	while (GetMessage (&msg, NULL, 0, 0))
     	{
          		TranslateMessage (&msg) ;
          		DispatchMessage (&msg) ;
     	}
     	return msg.wParam ;
}

void DrawRuler (HDC hdc, RECT * prc)
{
     	static int iRuleSize [16] = {360,72,144, 72,216,72,144,72,288,72,144,
72,216,72,144, 72 } ;
     	int       			i, j ;
     	POINT  			ptClient ;
     
     	SaveDC (hdc) ;
          			// Set Logical Twips mapping mode
     	SetMapMode (hdc, MM_ANISOTROPIC) ;
     	SetWindowExtEx (hdc, 1440, 1440, NULL) ;
     	SetViewportExtEx (hdc, 	GetDeviceCaps (hdc, LOGPIXELSX),
                          GetDeviceCaps (hdc, LOGPIXELSY), NULL) ;
     
			// Move the origin to a half inch from upper left
     
     	SetWindowOrgEx (hdc, -720, -720, NULL) ;
          			// Find the right margin (quarter inch from right)
     	ptClient.x = prc->right ;
     	ptClient.y = prc->bottom ;
     	DPtoLP (hdc, &ptClient, 1) ;
     	ptClient.x -= 360 ;
     
          			// Draw the rulers
     	MoveToEx 		(hdc, 0,    -36	0, NULL) ;
     	LineTo   		(hdc, OUTWIDTH * 1440,	-36	0) ;
     	MoveToEx 		(hdc, -360,   0, NULL) ;
     	LineTo   		(hdc, -360,      ptClient.y) ;
     
     	for (i = 0, j = 0 ; 	i <= ptClient.x && i <= OUTWIDTH * 1440 ;
              i += 1440 / 16, j++)
     	{
          			MoveToEx 		(hdc, i, -360, NULL) ;
          			LineTo   		(hdc, i, -360 - iRuleSize [j % 16]) ;
     	}
     
	for (i = 0, j = 0 ; i <= ptClient.y ; i += 1440 / 16, j++)
     	{
          			MoveToEx 		(hdc, -360, i, NULL) ;
          			LineTo   		(hdc, -360 - iRuleSize [j % 16], i) ;
	}
     
     	RestoreDC (hdc, -1) ;
}

/*---------------------------------------------------------------------------
   	GetCharDesignWidths:	Gets character widths for font as large as the
                         								original design size
----------------------------------------------------------------------------*/

UINT GetCharDesignWidths (HDC hdc, UINT uFirst, UINT uLast, int * piWidths)
{
     	HFONT             						hFont, hFontDesign ;
     	LOGFONT           					lf ;
     	OUTLINETEXTMETRIC 		otm ;

     	hFont = GetCurrentObject (hdc, OBJ_FONT) ;
     	GetObject (hFont, sizeof (LOGFONT), &lf) ;

          		// Get outline text metrics (we'll only be using a field that is
          		//		independent of the DC the font is selected into)

     	otm.otmSize = sizeof (OUTLINETEXTMETRIC) ;
     	GetOutlineTextMetrics (hdc, sizeof (OUTLINETEXTMETRIC), &otm) ;

          		// Create a new font based on the design size
     	lf.lfHeight 	= - (int) otm.otmEMSquare ;
     	lf.lfWidth 	 	= 0 ;
     	hFontDesign 	= CreateFontIndirect (&lf) ;

          		// Select the font into the DC and get the character widths

     	SaveDC (hdc) ;
     	SetMapMode (hdc, MM_TEXT) ;
     	SelectObject (hdc, hFontDesign) ;

     	GetCharWidth (hdc, uFirst, uLast, piWidths) ;
     	SelectObject (hdc, hFont) ;
     	RestoreDC (hdc, -1) ;

          			// Clean up
     	DeleteObject (hFontDesign) ;
     	return otm.otmEMSquare ;
}

/*--------------------------------------------------------------------------
   	GetScaledWidths:	Gets floating point character widths for selected
                     							font size
----------------------------------------------------------------------------*/

void GetScaledWidths (HDC hdc, double * pdWidths)
{
     	double  		dScale ;
     	HFONT   			hFont ;
     	int     			aiDesignWidths [LASTCHAR + 1] ;
     	int     			i ;
     	LOGFONT 		lf ;
     	UINT    			uEMSquare ;

          			// Call function above

     	uEMSquare = GetCharDesignWidths (hdc, 0, LASTCHAR, aiDesignWidths) ;
          			// Get LOGFONT for current font in device context
     	hFont = GetCurrentObject (hdc, OBJ_FONT) ;
     	GetObject (hFont, sizeof (LOGFONT), &lf) ;
          			// Scale the widths and store as floating point values
     	dScale = (double) -lf.lfHeight / (double) uEMSquare ;
     	for (	i = 0 ; i <= LASTCHAR ; i++)
          				pdWidths[i] = dScale * aiDesignWidths[i] ;
}

/*----------------------------------------------------------------------------
   	GetTextExtentFloat:  Calculates text width in floating point
----------------------------------------------------------------------------*/

double GetTextExtentFloat (double * pdWidths, PTSTR psText, int iCount)
{
     	double 	dWidth = 0 ;
     	int 		i ;

     	for (	i = 0 ; i < iCount ; i++)
          				dWidth += pdWidths [psText[i]] ;

     return dWidth ;
}

/*----------------------------------------------------------------------------
   Justify: Based on design units for screen/printer compatibility
-----------------------------------------------------------------------------*/

void Justify (HDC hdc, PTSTR pText, RECT * prc, int iAlign)
{
     	double 	dWidth, adWidths[LASTCHAR + 1] ;
     	int    		xStart, yStart, cSpaceChars ;
     	PTSTR  		pBegin, pEnd ;
     	SIZE   		size ;

          		// Fill the adWidths array with floating point character widths

     	GetScaledWidths (hdc, adWidths) ;
     	yStart = prc->top ;
     	do                            	 // for each text line
     {
          		cSpaceChars = 0 ;        // initialize number of spaces in line

          		while (*pText == ' ')	// skip over leading spaces
               				pText++ ;

    		pBegin = pText ;         	// set pointer to char at beginning of line
          
 		do                       		// until the line is known
        		{
               			pEnd = pText ; 		// set pointer to char at end of line

                    						// skip to next space 
               
               			while (*pText != '\0' && *pText++ != ' ') ;

           			if (*pText == '\0')
                    					break ;

                    		// after each space encountered, calculate extents

               			cSpaceChars++ ;
               			dWidth = GetTextExtentFloat (adWidths, 	pBegin, 
                                                     pText - pBegin - 1) ;
          		}
          		while (dWidth < (double) (prc->right - prc->left)) ;
          
          		cSpaceChars-- ;        // discount last space at end of line
          
          		while (*(pEnd - 1) == ' ')	// eliminate trailing spaces
          		{
               			pEnd-- ;
               			cSpaceChars-- ;
          		}

               			// if end of text and no space characters, set pEnd to end
          
          		if (*pText == '\0' || cSpaceChars <= 0)
               				pEnd = pText ;

               			// Now get integer extents

          		GetTextExtentPoint32(hdc, pBegin, pEnd - pBegin, &size) ;
          
          		switch (iAlign)        	// use alignment for xStart
          		{
          		case 	IDM_ALIGN_LEFT:
               				xStart = prc->left ;
               				break ;

          		case 	IDM_ALIGN_RIGHT:
               				xStart = prc->right - size.cx ;
               				break ;
          		case 	IDM_ALIGN_CENTER:
               				xStart = (prc->right + prc->left - size.cx) / 2 ;
               				break ;
               
          		case 	IDM_ALIGN_JUSTIFIED:
               				if (*pText != '\0' && cSpaceChars > 0)
                    						SetTextJustification (hdc,
                                prc->right - prc->left - size.cx,
                                cSpaceChars) ;
               				xStart = prc->left ;
               				break ;
          		}
               				// display the text
          
          		TextOut (hdc, xStart, yStart, pBegin, pEnd - pBegin) ;

               				// prepare for next line

          		SetTextJustification (hdc, 0, 0) ;
          		yStart += size.cy ;
         		pText = pEnd ;
     }
     	while (*pText && yStart < prc->bottom - size.cy) ;
}

LRESULT CALLBACK WndProc (	HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
     	static 	CHOOSEFONT	cf ;
     	static 		DOCINFO  			di = { sizeof (DOCINFO), TEXT ("Justify2: Printing") } ;
     	static 	int        			iAlign = IDM_ALIGN_LEFT ;
     	static 	LOGFONT    		lf ;
     	static 	PRINTDLG   		pd ;
     	static 	TCHAR      			szText[] = { 
   TEXT ("Call me Ishmael. Some years ago -- never ")
   TEXT ("mind how long precisely -- having little ")
   TEXT ("or no money in my purse, and nothing ")
   TEXT ("particular to interest me on shore, I ")
   TEXT ("thought I would sail about a little and ")
   TEXT ("see the watery part of the world. It is ")
   TEXT ("a way I have of driving off the spleen, ")
   TEXT ("and regulating the circulation. Whenever ")
   TEXT ("I find myself growing grim about the ")
   TEXT ("mouth; whenever it is a damp, drizzly ")
   TEXT ("November in my soul; whenever I find ")
   TEXT ("myself involuntarily pausing before ")
   TEXT ("coffin warehouses, and bringing up the ")
   TEXT ("rear of every funeral I meet; and ")
   TEXT ("especially whenever my hypos get such an ")
   TEXT ("upper hand of me, that it requires a ")
   TEXT ("strong moral principle to prevent me ")
   TEXT ("from deliberately stepping into the ")
   TEXT ("street, and methodically knocking ")
   TEXT ("people's hats off -- then, I account it ")
   TEXT ("high time to get to sea as soon as I ")
   TEXT ("can. This is my substitute for pistol ")
   TEXT ("and ball. With a philosophical flourish ")
   TEXT ("Cato throws himself upon his sword; I ")
   TEXT ("quietly take to the ship. There is ")
   TEXT ("nothing surprising in this. If they but ")
   TEXT ("knew it, almost all men in their degree, ")
   TEXT ("some time or other, cherish very nearly ")
   TEXT ("the same feelings towards the ocean with ")
   TEXT ("me.") } ;
     	BOOL              			fSuccess ;
     	HDC               			hdc, hdcPrn ;
     	HMENU             			hMenu ;
     	int               			iSavePointSize ;
     	PAINTSTRUCT       			ps ;
     	RECT              			rect ;
     
     	switch (message)
     	{
     	case 	WM_CREATE:
               				// Initialize the CHOOSEFONT structure

          			hdc = GetDC (hwnd) ;
          			lf.lfHeight = - GetDeviceCaps (hdc, LOGPIXELSY) / 6 ;
          			lf.lfOutPrecision = OUT_TT_ONLY_PRECIS ;
          			lstrcpy (lf.lfFaceName, TEXT ("Times New Roman")) ;
          			ReleaseDC (hwnd, hdc) ;

          			cf.lStructSize    			= sizeof (CHOOSEFONT) ;
          			cf.hwndOwner      			= hwnd ;
          			cf.hDC            			= NULL ;
          			cf.lpLogFont      			= &lf ;
          			cf.iPointSize     			= 120 ;

               				// Set flags for TrueType only!

          			cf.Flags     = 	CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS | 
                              		CF_TTONLY | CF_EFFECTS ;
          			cf.rgbColors      		= 0 ;
          			cf.lCustData      		= 0 ;
          			cf.lpfnHook       		= NULL ;
          			cf.lpTemplateName 		= NULL ;
          			cf.hInstance      		= NULL ;
          			cf.lpszStyle      		= NULL ;
          			cf.nFontType      		= 0 ;      
          			cf.nSizeMin       		= 0 ;
          			cf.nSizeMax       		= 0 ;
 
          			return 0 ;

     	case 	WM_COMMAND:
          			hMenu = GetMenu (hwnd) ;
          
          			switch (LOWORD (wParam))
          			{
          			case 	IDM_FILE_PRINT:
                    					// Get printer DC

               					pd.lStructSize = sizeof (PRINTDLG) ;
               					pd.hwndOwner   = hwnd ;
			pd.Flags       		= PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION ;

	          				if (!PrintDlg (&pd))
                    							return 0 ;

               					if (NULL == (hdcPrn = pd.hDC))
               					{
                MessageBox	(hwnd, TEXT ("Cannot obtain Printer DC"),
                             szAppName, MB_ICONEXCLAMATION | MB_OK) ;
                    		 return 0 ;
               					}
                    						// Set margins for OUTWIDTH inches wide

               				rect.left = (GetDeviceCaps (hdcPrn, PHYSICALWIDTH) -
                            GetDeviceCaps (hdcPrn, LOGPIXELSX)*OUTWIDTH)/2 
                           	-	GetDeviceCaps (hdcPrn, PHYSICALOFFSETX) ;
               
               				rect.right = 	rect.left + 
                            GetDeviceCaps (hdcPrn, LOGPIXELSX) * OUTWIDTH ;

                    		// Set margins of 1 inch at top and bottom

               				rect.top  = GetDeviceCaps (hdcPrn, LOGPIXELSY) -
                            GetDeviceCaps (hdcPrn, PHYSICALOFFSETY) ;

               		       rect.bottom =GetDeviceCaps (hdcPrn, PHYSICALHEIGHT) - 
                           GetDeviceCaps (hdcPrn, LOGPIXELSY) -
                           GetDeviceCaps (hdcPrn, PHYSICALOFFSETY) ;

                    						// Display text on printer

               				SetCursor (LoadCursor (NULL, IDC_WAIT)) ;
               				ShowCursor (TRUE) ;

               				fSuccess = FALSE ;

               				if ((StartDoc (hdcPrn, &di) > 0) && (StartPage (hdcPrn) > 0))
               				{
                         	// Select font using adjusted lfHeight

                    	iSavePointSize = lf.lfHeight ;
                    	lf.lfHeight = -(GetDeviceCaps (hdcPrn, LOGPIXELSY) *
                                        cf.iPointSize) / 720 ;

                    SelectObject (hdcPrn, CreateFontIndirect (&lf)) ;
                    					lf.lfHeight = iSavePointSize ;

                         						// Set text color 

                    					SetTextColor (hdcPrn, cf.rgbColors) ;
               
                         						// Display text

                 Justify (hdcPrn, szText, &rect, iAlign) ;

                    					if (EndPage (hdcPrn) > 0)
                    					{
                         							fSuccess = TRUE ;
                         							EndDoc (hdcPrn) ;
                    					}
               				}
               			ShowCursor (FALSE) ;
               			SetCursor (LoadCursor (NULL, IDC_ARROW)) ;

               			DeleteDC (hdcPrn) ;

               			if (!fSuccess)
                    MessageBox (hwnd, TEXT ("Could not print text"),
                                szAppName, MB_ICONEXCLAMATION | MB_OK) ;
               			return 0 ;
          			case 	IDM_FONT:
               					if (ChooseFont (&cf))
                    					InvalidateRect (hwnd, NULL, TRUE) ;
               					return 0 ;
               
          			case 	IDM_ALIGN_LEFT:
          			case 	IDM_ALIGN_RIGHT:
          			case 	IDM_ALIGN_CENTER:
          			case 	IDM_ALIGN_JUSTIFIED:
               					CheckMenuItem (hMenu, iAlign, MF_UNCHECKED) ;
               					iAlign = LOWORD (wParam) ;
               					CheckMenuItem (hMenu, iAlign, MF_CHECKED) ;
               					InvalidateRect (hwnd, NULL, TRUE) ;
               					return 0 ;
          			}
          			return 0 ;

     	case 	WM_PAINT:
          			hdc = BeginPaint (hwnd, &ps) ;
          
          			GetClientRect (hwnd, &rect) ;
          			DrawRuler (hdc, &rect) ;
          
          			rect.left	+= GetDeviceCaps (hdc, LOGPIXELSX) / 2 ;
          			rect.top	+= GetDeviceCaps (hdc, LOGPIXELSY) / 2 ;
          			rect.right 	= rect.left + OUTWIDTH * GetDeviceCaps (hdc, LOGPIXELSX) ;

          			SelectObject (hdc, CreateFontIndirect (&lf)) ;
          			SetTextColor (hdc, cf.rgbColors) ;
          
          			Justify (hdc, szText, &rect, iAlign) ;
          
          			DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
          			EndPaint (hwnd, &ps) ;
          			return 0 ;
          
     	case 	WM_DESTROY:
          			PostQuitMessage (0) ;
          			return 0 ;
     }
     	return DefWindowProc (hwnd, message, wParam, lParam) ;
}
 JUSTIFY2.RC
//Microsoft Developer Studio generated resource script.
#include "resource.h"
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
// Menu
JUSTIFY2 MENU DISCARDABLE BEGIN POPUP "&File"
    	BEGIN
        		MENUITEM "&Print",  		IDM_FILE_PRINT
    	END
    	POPUP "&Font"
    	BEGIN
        		MENUITEM "&Font...",  	IDM_FONT
    	END
    	POPUP "&Align"
    	BEGIN
        		MENUITEM "&Left",			IDM_ALIGN_LEFT, CHECKED
        		MENUITEM "&Right",    	IDM_ALIGN_RIGHT
        		MENUITEM "&Centered",		IDM_ALIGN_CENTER
        		MENUITEM "&Justified",  	IDM_ALIGN_JUSTIFIED
    	END
END
 RESOURCE.H
// Microsoft Developer Studio generated include file.
// Used by Justify2.rc

#define 		IDM_FILE_PRINT      40001
#define 		IDM_FONT            40002
#define 		IDM_ALIGN_LEFT      40003
#define 		IDM_ALIGN_RIGHT     40004
#define 		IDM_ALIGN_CENTER    40005
#define 		IDM_ALIGN_JUSTIFIED 40006

JUSTIFY2��ʹ��TrueType���塣������GetCharDesignWidths��ʽ�У���ʽʹ��GetOutlineTextMetrics��ʽȡ��һ���������ƺ�����Ҫ����Ѷ����OUTLINETEXTMETRIC��otmEMSquare��λ��

TrueType������ȫ����em-square������������ƣ�����˵����em����ָһ�ַ�����̬�Ŀ��ȣ�M�ڿ����ϵ�������ֵ�Ĵ�С�����κ��ض�TrueType�����������Ԫ������ͬ������������Ƶģ���Ȼ��Щ��Ԫͨ���в�ͬ�Ŀ��ȡ�OUTLINETEXTMETRIC�ṹ��otmEMSquare��λ�����������ض����������ȫ����ʽ�Ĵ�С�����ᷢ�֣���춴����TrueType���壬otmEMSquare��λ���2048������ζ����������2048��2048����������Ƶġ�

�ؼ���춣�����Ϊ��Ҫʹ�õ��ض�TrueType���������趨һ��LOGFONT�ṹ����lfHeight��λ���otmEMSquareֵ�ĸ������ڽ������岢����ѡ��װ�������ᣬ�ɺ���GetCharWidth���ú�ʽ���߼���λ�ṩ�����е�����Ԫ�Ŀ��ȡ�ͨ������Ϊ��Щ��Ԫ������Ϊ��ͬ�������С��������Ԫ���Ȳ���׼ȷ����ʹ������otmEMSquare��С�����壬��Щ�����������κ�װ�������޹صľ�ȷ������

GetCharDesignWidths��ʽ�����ַ�ʽ���ԭʼ����Ԫ��ƿ��ȣ��������Ǵ��������������С�JUSTIFY2��ʽ���Լ��������н�ʹ��ASCII��Ԫ����ˣ�������в���Ҫ�ܴ�GetScaledWidths��ʽ����Щ������̬����ת��Ϊ�����豸�߼������������ʵ�ʵ�ֵ�ĸ�����ȡ�GetTextExtentFloat��ʽʹ����Щ������ȼ��������ִ��Ŀ��ȡ������µ�Justify��ʽ���Լ��������п��ȵIJ�����

��Ȥ�Ķ���
��

��������������ʾ������Ԫ�ṩ�˽�����������ͼ�μ������ϵĿ����ԡ�ǰ��������������ת����ķ�ʽ�����イ��һЩ�������ɡ�����֮ǰ�����˽�������Ҫ��Ԥ��֪ʶ����ͼ·������չ���ʡ�

GDI��ͼ·��
��

��ͼ·���Ǵ�����GDI�ڵ�ֱ�ߺ����ߵļ��ϡ���ͼ·������Windows��32λԪ�汾�з����ġ���ͼ·������ȥ�������������ȷʵ���Խ���ͼ·��ת��Ϊ���򣬲�ʹ�û�ͼ·�����м��á����������ǻᷢ�����ߵIJ�ͬ��

Ҫ�����ͼ·�������ȼ򵥺���

BeginPath (hdc) ;

���иú���֮�ᣬ�������κ��ߣ����磬ֱ�ߡ��������������ߣ�����Ϊ��ͼ·��������GDI�ڲ���������ʾ��װ�������ϡ���ͼ·����������������������ɡ�Ҫ���������ߣ�Ӧʹ��LineTo��PolylineTo��BezierTo��ʽ����Щ��ʽ����Ŀǰλ��Ϊ��㻮�ߡ����ʹ��MoveToEx�ı���Ŀǰλ�ã�����������Ļ��ߺ�ʽ�����ߺ����˻ᵼ��Ŀǰλ�øı���Ӵ�/�Ӳ���ʽ��������������ͼ·���н�����һ���µ��ӻ�ͼ·������ˣ���ͼ·������һ�����ӻ�ͼ·����ÿһ���ӻ�ͼ·����һϵ��������߶Ρ�

��ͼ·���е�ÿ���ӻ�ͼ·�������dz����Ļ��յġ�����ӻ�ͼ·��֮��һ�������ߵĵ�һ����������һ�������ߵ�����һ����ͬ�������ӻ�ͼ·��ͨ������CloseFigure�����������Ҫ�Ļ���CloseFigure����һ��ֱ�߷���ӻ�ͼ·��������Ļ��ߺ�ʽ����ʼһ���µ��ӻ�ͼ·�������ᣬͨ������ĺ��н�����ͼ·�����壺

EndPath (hdc) ;

��ʱ�������������������ʽ֮һ��

StrokePath (hdc) ;
FillPath (hdc) ;
StrokeAndFillPath (hdc) ;
hRgn = PathToRegion (hdc) ;
SelectClipPath (hdc, iCombine) ;

��Щ��ʽ�е�ÿһ�������ڻ�ͼ·����������ᣬ���������

StrokePathʹ��Ŀǰ���ʻ��ƻ�ͼ·���������ܻ���棺��ͼ·���ϵĵ�����Щ��Ϊʲô����������Щ��ͼ·��Ƭ�������ػ��ߣ������һ������ԭ��

�����ĸ���ʽ��ֱ�߹ر��κγ����Ļ�ͼ·����FillPath����Ŀǰ�Ķ�����ģʽʹ��Ŀǰ��ˢ����ͼ·����StrokeAndFillPathһ��������������Ҳ�ɽ���ͼ·��ת��Ϊ���򣬻��߽���ͼ·�����ij����������iCombine������CombineRgn��ʽʹ�õ�RGN_ ϵ�г���֮һ����ָ���˻�ͼ·����Ŀǰ��������Ľ�Ϸ�ʽ��

��������ȡʱ����ͼ·���Ȼ�ͼ�������������Ϊ��ͼ��������ɾ��Ρ���Բ������ε���϶��壻��ͼ·�����ɱ��������߶��壬������Windows NT�л����ɻ�����ɡ���GDI�У���ͼ·��������Ĵ���Ҳ��ȫ��ͬ����ͼ·����ֱ�߼����߶���ļ��ϣ�����ͼ����ͨ�������ϣ���ɨ���ߵļ��ϡ�

��չ����
��

�ں���StrokePathʱ��ʹ��Ŀǰ���ʻ��ƻ�ͼ·�����ڵ��������������Խ������������CreatePen��ʽ�������ͼ·���ķ�����WindowsҲ֧Ԯһ����ΪExtCreatePen����չ���ʺ�ʽ���С��ú�ʽ��ʾ���佨����ͼ·���Լ�ʹ�û�ͼ·��Ҫ�Ȳ�ʹ�û�ͼ·���������á�ExtCreatePen��ʽ������ʾ��

hPen = ExtCreatePen (iStyle, iWidth, &lBrush, 0, NULL) ;

������ʹ�øú�ʽ�����ػ����߶Σ��������������Windows 98��֧ԮһЩ���ܡ�����������ʾ��ͼ·��ʱ��Windows 98�Բ�֧ԮһЩ���ܣ���������溯ʽ�����������������趨Ϊ0��NULL��ԭ��

���ExtCreatePen�ĵ�һ����������ʹ�õ������������۵�����CreatePen�ϵ�������ʽ������ʹ��PS_GEOMETRIC���������Щ��ʽ������iWidth�������߼���λ��ʾ�߿����ܹ�ת����������ʹ��PS_COSMETIC������iWidth����������1����Windows 98�У����߻�㻭����ʽ�Ļ��ʱ�����PS_COSMETIC����Windows NT��ȡ����������ơ�

CreatePen��һ��������ʾ��ɫ��ExtCreatePen����Ӧ������ֻ��ʾ��ɫ������ʹ�û�ˢ��PS_GEOMETRIC�����ڲ���ɫ���û�ˢ������͸������ͼ���塣

�ڻ��ƿ��߶�ʱ�����ǿ���Ҫ��ע�߶ζ˵����ۡ�������ֱ�߻�����ʱ�����ܻ�Ҫ��ע�߶μ���������ۡ�������CreatePen����ʱ����Щ�˵㼰�����ͨ����Բ�εģ�ʹ��ExtCreatePen��������ʱ���ǿ���ѡ�񡣣�ʵ���� ����Windows 98�У�ֻ����ʹ�û���ʵ����ͼ·��ʱ���ǿ���ѡ����Windows NT��Ҫ�����������߶εĶ˵����ʹ��ExtCreatePen�е����л�����ʽ���壺

PS_ENDCAP_ROUND
PS_ENDCAP_SQUARE
PS_ENDCAP_FLAT

��square����ʽ�롸flat����ʽ�IJ�ͬ���ǣ�ǰ�߽�����չ��һ�������˵����ƣ���ͼ·�����߶μ��������ͨ��������ʽ�趨��

PS_JOIN_ROUND
PS_JOIN_BEVEL
PS_JOIN_MITER

��bevel����ʽ��������жϣ���miter����ʽ��������Ϊ��ͷ����ʽ17-9��ʾ��ENDJOIN�ǶԴ˵�һ���Ϻõ�˵����

 ï¿½ï¿½Ê½17-9  ENDJOIN
ENDJOIN.C
/*---------------------------------------------------------------------------
   	ENDJOIN.C --	Ends and Joins Demo
                						(c) Charles Petzold, 1998
----------------------------------------------------------------------------*/

#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    							PSTR szCmdLine, int iCmdShow)
{
     	static TCHAR 	szAppName[] = TEXT ("EndJoin") ;
     	HWND         				hwnd ;
     	MSG          				msg ;
     	WNDCLASS     			wndclass ;
     
     	wndclass.style         			= CS_HREDRAW | CS_VREDRAW ;
     	wndclass.lpfnWndProc   			= WndProc ;
     	wndclass.cbClsExtra    			= 0 ;
     	wndclass.cbWndExtra    			= 0 ;
     	wndclass.hInstance     			= hInstance ;
     	wndclass.hIcon         			= LoadIcon (NULL, IDI_APPLICATION) ;
     	wndclass.hCursor       			= LoadCursor (NULL, IDC_ARROW) ;
     	wndclass.hbrBackground 		= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     	wndclass.lpszMenuName  		= NULL ;
     	wndclass.lpszClassName 		= szAppName ;
     
     	if (!RegisterClass (&wndclass))
     {
          		MessageBox (	NULL, TEXT ("This program requires Windows NT!"),
                      							szAppName, MB_ICONERROR) ;
          		return 0 ;
     }
     
     	hwnd = CreateWindow (	szAppName, TEXT ("Ends and Joins Demo"),
                          		WS_OVERLAPPEDWINDOW,
                          		CW_USEDEFAULT, CW_USEDEFAULT,
                          		CW_USEDEFAULT, CW_USEDEFAULT,
                          		NULL, NULL, hInstance, NULL) ;
     
     	ShowWindow (hwnd, iCmdShow) ;
     	UpdateWindow (hwnd) ;
     
     	while (GetMessage (&msg, NULL, 0, 0))
     	{
          		TranslateMessage (&msg) ;
          		DispatchMessage (&msg) ;
     	}
     	return msg.wParam ;
}
LRESULT CALLBACK WndProc (	HWND hwnd, UINT iMsg, WPARAM wParam,LPARAM lParam)
{
     	static int iEnd[] =	{PS_ENDCAP_ROUND,PS_ENDCAP_SQUARE,PS_ENDCAP_FLAT } ;
     	static int iJoin[]=	{PS_JOIN_ROUND,	PS_JOIN_BEVEL,PS_JOIN_MITER } ;
     	static int	cxClient, cyClient ;
     	HDC         		hdc ;
     	int         		i ;
     	LOGBRUSH    		ib ;
     	PAINTSTRUCT 		ps ;
     
     	switch (iMsg)
     	{
     	case 	WM_SIZE:
          			cxClient = LOWORD (lParam) ;
          			cyClient = HIWORD (lParam) ;
          			return 0 ;
          
     	case 	WM_PAINT:
          			hdc = BeginPaint (hwnd, &ps) ;
          
          			SetMapMode (hdc, MM_ANISOTROPIC) ;
          			SetWindowExtEx (hdc, 100, 100, NULL) ;
          			SetViewportExtEx (hdc, cxClient, cyClient, NULL) ;
          
          			lb.lbStyle = BS_SOLID ;
          			lb.lbColor = RGB (128, 128, 128) ;
          			lb.lbHatch = 0 ;
          
          			for (i = 0 ; i < 3 ; i++)
          			{
               		SelectObject (hdc, ExtCreatePen (PS_SOLID | PS_GEOMETRIC |
                                  iEnd [i] | iJoin [i], 10, &lb, 0, NULL)) ;
               					BeginPath (hdc) ;
               					MoveToEx 		(hdc, 10 + 30 * i, 25, NULL) ;
               					LineTo   		(hdc, 20 + 30 * i, 75) ;
               					LineTo   		(hdc, 30 + 30 * i, 25) ;
               
               					EndPath (hdc) ;
               					StrokePath (hdc) ;
               
               					DeleteObject (
                    SelectObject (hdc,GetStockObject (BLACK_PEN))) ;

               					MoveToEx 	(hdc, 10 + 30 * i, 25, NULL) ;
               					LineTo   	(hdc, 20 + 30 * i, 75) ;
               					LineTo   	(hdc, 30 + 30 * i, 25) ;
          			}
			EndPaint (hwnd, &ps) ;
			return 0 ;
          
     	case 	WM_DESTROY:
          			PostQuitMessage (0) ;
         	 		return 0 ;
     	}
  	return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}

��ʽʹ�������˵���������ʽ��������V�εĿ��߶Ρ���ʽҲʹ�ñ��ú�ɫ���ʻ�������ͬ�����ߡ������ͽ�������ͨ����ϸ�����˱Ƚϡ������ͼ17-4��ʾ��


��

ͼ17-4 ENDJOIN��өĻ��ʾ

���ڴ�Ҹ�����ΪʲôWindows֧ԮStrokePath��ʽ�ˣ�����ֱ�����ֱ�ߣ�GDI���ò���ÿһ������ʹ�ö˵㡣ֻ���ڻ�ͼ·�������У�GDI֪���߶�������IJ�ʹ���߶ε�����㡣

�ĸ�������ʽ
��

�⾿����ʲô�ô��أ���ϸ����һ�£������������Ԫ��һϵ������ֵ���壬��Щ���궨����ֱ�ߺ�ת���ߡ������ֱ�߼������ܳ�Ϊ��ͼ·�������һ���֡�

ȷʵ���ԣ���ʽ17-10��ʾ��FONTOUT1��ʽ�Դ�����չʾ��

 ï¿½ï¿½Ê½17-10  FONTOUT1
FONTOUT1.C
/*-----------------------------------------------------------------------------
   	FONTOUT1.C -- 		Using Path to Outline Font
                 							(c) Charles Petzold, 1998
----------------------------------------------------------------------------*/

#include <windows.h>
#include "..\\eztest\\ezfont.h"

TCHAR szAppName [] = TEXT ("FontOut1") ;
TCHAR szTitle [] = TEXT ("FontOut1: Using Path to Outline Font") ;

void PaintRoutine (HWND hwnd, HDC hdc, int cxArea, int cyArea)
{
     	static TCHAR 	szString [] = TEXT ("Outline") ;
     	HFONT        				hFont ;
     	SIZE         				size ;

     	hFont = EzCreateFont (hdc, TEXT ("Times New Roman"), 1440, 0, 0, TRUE) ;
     	SelectObject (hdc, hFont) ;
     	GetTextExtentPoint32 (hdc, szString, lstrlen (szString), &size) ;
     	BeginPath (hdc) ;
     	TextOut (hdc, (	cxArea - size.cx) / 2, (cyArea - size.cy) / 2,
                    							szString, lstrlen (szString)) ;
     	EndPath (hdc) ;
     	StrokePath (hdc) ;
     	SelectObject (hdc, GetStockObject (SYSTEM_FONT)) ;
     	DeleteObject (hFont) ;
}

�˳�ʽ�ͱ�������ij�ʽ��ʹ����ǰ����ʾ��EZFONT��FONTDEMO������

��ʽ������144���TrueType���岢����GetTextExtentPoint32��ʽȡ�����ַ���Ĵ�С��Ȼ�ᣬ���л�ͼ·�������е�TextOut��ʽʹ��������ʾ�����Ӵ��д�����ĵ�λ�á���Ϊ��TextOut��ʽ�ĺ����DZ���ͼ·���趨��������Χ�ģ���BeginPath��EndPath����֮�䣩��ʽ�н��еģ�GDI��������ʾ���֡��෴����ʽ����Ԫ���������ڻ�ͼ·�������С�

�ڻ�ͼ·����������ᣬFONTOUT1����StrokePath����Ϊװ��������δѡ��ָ���Ļ��ʣ�����GDI����ʹ���ڶ����ʻ�����Ԫ��������ͼ17-5��ʾ��


��

ͼ17-5 FONTOUT1��өĻ��ʾ

�������Ƕ��õ�ʲô�أ������Ѿ��������������������Ԫ�������ִ�����Ϊʲô��Χ���������أ�

����һ�£����ֱ���ģʽʹ���ڶ���OPAQUE��������TRANSPARENT���þ��ξ������ַ������������������չʾ�����ڶ���OPAQUEģʽ��GDI��������ʱ��ʹ�õ��������裺���Ȼ���һ�����ľ��Σ�����������Ԫ�����ַ�����ε�����Ҳ��˳�Ϊ��ͼ·����һ���֡�

ʹ��ExtCreatePen��ʽ���ܹ�ʹ���ڶ���������Ķ�������������Ԫ����������ʽ17-11��ʾ��FONTOUT2�Դ�����չʾ��

 ï¿½ï¿½Ê½17-11  FONTOUT2
FONTOUT2.C
/*-----------------------------------------------------------------------------
   	FONTOUT2.C -- 		Using Path to Outline Font
                 							(c) Charles Petzold, 1998
----------------------------------------------------------------------------*/

#include <windows.h>
#include "..\\eztest\\ezfont.h"

TCHAR szAppName [] = TEXT ("FontOut2") ;
TCHAR szTitle [] = TEXT ("FontOut2: Using Path to Outline Font") ;

void PaintRoutine (HWND hwnd, HDC hdc, int cxArea, int cyArea)
{
     	static TCHAR 	szString [] = TEXT ("Outline") ;
     	HFONT        				hFont ;
     	LOGBRUSH     			lb ;
     	SIZE         				size ;

     	hFont = EzCreateFont (hdc, TEXT ("Times New Roman"), 1440, 0, 0, TRUE) ;
     	SelectObject (hdc, hFont) ;
     	SetBkMode (hdc, TRANSPARENT) ;

     	GetTextExtentPoint32 (hdc, szString, lstrlen (szString), &size) ;
     	BeginPath (hdc) ;
     	TextOut (hdc, (	cxArea - size.cx) / 2, (cyArea - size.cy) / 2,
                    	szString, lstrlen (szString)) ;
     	EndPath (hdc) ;
     	lb.lbStyle 	= 	BS_SOLID ;
     	lb.lbColor 	= 	RGB (255, 0, 0) ;
     	lb.lbHatch 	= 	0 ;

     	SelectObject (hdc, ExtCreatePen (PS_GEOMETRIC | PS_DOT,
                      GetDeviceCaps (hdc, LOGPIXELSX) / 24, &lb, 0, NULL)) ;
     	StrokePath (hdc) ;
     	DeleteObject (SelectObject (hdc, GetStockObject (BLACK_PEN))) ;
     	SelectObject (hdc, GetStockObject (SYSTEM_FONT)) ;
     	DeleteObject (hFont) ;
}

�˳�ʽ����StrokePath֮ǰ��������ѡ��װ�����ݣ�һ��3�㣨1/24Ӣ�磩���ĺ�ɫ���߱ʡ���ʽ��Windows NT��ִ��ʱ�������ͼ17-6��ʾ��Windows 98��֧Ԯ����1ͼ�ؿ��ķ�ʵ�ıʣ����Windows 98����ʵ�ĵĺ�ɫ�ʻ��ơ�


��

ͼ17-6 FONTOUT2��өĻ��ʾ

��Ҳ����ʹ�û�ͼ·�����������������ǰ��������ʽ��ʾ�ķ���������ͼ·����ѡ��һ�����ͼ����Ȼ�����FillPath���ܺ��е���һ����ʽ��StrokeAndFillPath�������ƻ�ͼ·������������һ����ʽ���н�����䡣

StrokeAndFillPath��ʽ���ʽ17-12 FONTFILL��չʾ��

 ï¿½ï¿½Ê½17-12  FONTFILL
FONTFILL.C
/*----------------------------------------------------------------------------
   	FONTFILL.C -- 		Using Path to Fill Font
                 							(c) Charles Petzold, 1998
-----------------------------------------------------------------------------*/

#include <windows.h>
#include "..\\eztest\\ezfont.h"

TCHAR szAppName [] = TEXT ("FontFill") ;
TCHAR szTitle [] = TEXT ("FontFill: Using Path to Fill Font") ;
void PaintRoutine (HWND hwnd, HDC hdc, int cxArea, int cyArea)
{
     	static TCHAR szString [] = TEXT ("Filling") ;
     	HFONT        				hFont ;
     	SIZE         				size ;

     	hFont = EzCreateFont (hdc, TEXT ("Times New Roman"), 1440, 0, 0, TRUE) ;
     	SelectObject (hdc, hFont) ;
     	SetBkMode (hdc, TRANSPARENT) ;

     	GetTextExtentPoint32 (hdc, szString, lstrlen (szString), &size) ;
     	BeginPath (hdc) ;
     	TextOut (hdc, (	cxArea - size.cx) / 2, (cyArea - size.cy) / 2,
                    							szString, lstrlen (szString)) ;
     	EndPath (hdc) ;
     	SelectObject (hdc, CreateHatchBrush (HS_DIAGCROSS, RGB (255, 0, 0))) ;
     	SetBkColor (hdc, RGB (0, 0, 255)) ;
     	SetBkMode (hdc, OPAQUE) ;

     	StrokeAndFillPath (hdc) ;
     	DeleteObject (SelectObject (hdc, GetStockObject (WHITE_BRUSH))) ;
     	SelectObject (hdc, GetStockObject (SYSTEM_FONT)) ;
     	DeleteObject (hFont) ;
}

FONTFILLʹ���ڶ����ʻ��ƻ�ͼ·������������ʹ��HS_DIAGCROSS��ʽ������ɫ����Ӱ��ˢ��ע���ʽ�ڽ�����ͼ·��ʱ������ģʽ�趨ΪTRANSPARENT��������ͼ·��ʱ�ֽ�������ΪOPAQUE���������ܹ�Ϊ����ͼ��ʹ����ɫ�ı�����ɫ�������ͼ17-7��ʾ��

���������ڱ���ʽ�г��Լ���������۲�����Ӱ�졣���ȣ����������һ��SetBkMode���б�Ϊע�⣬���õ���ͼ����������Ԫ���������ǵ����ַ��鱳������ͨ����������ʵ������Ҫ�ģ���ȷʵ����������

���⣬�����Ԫ����������������ʱ������������Ч�ط����ڶ���ALTERNATE������ģʽ���ҵľ����ʾ�����ʹ��WINDING���ģʽ���򹹽�TrueType�����Ա��������ֵ��������� ��O�����ڲ�����䣩����ʹ��ALTERNATEģʽ����ȫ��


��

ͼ17-7 FONTFILL��өĻ��ʾ

���ᣬ��ʹ��һ����ͼ·�������Ҳ��һ��TrueType���壬����������������ʽ17-13 FONTCLIP��ʾ��

 ï¿½ï¿½Ê½17-13  FONTCLIP
FONTCLIP.C
/*---------------------------------------------------------------------------
   	FONTCLIP.C -- 		Using Path for Clipping on Font
                 							(c) Charles Petzold, 1998
----------------------------------------------------------------------------*/

#include <windows.h>
#include "..\\eztest\\ezfont.h"

TCHAR szAppName [] = TEXT ("FontClip") ;
TCHAR szTitle	[] = TEXT ("FontClip: Using Path for Clipping on Font") ;

void PaintRoutine (HWND hwnd, HDC hdc, int cxArea, int cyArea)
{
     	static TCHAR 	szString [] = TEXT ("Clipping") ;
     	HFONT        				hFont ;
     	int          				y, iOffset ;
	POINT        			pt [4] ;
     	SIZE         			size ;

     	hFont = EzCreateFont (hdc, TEXT ("Times New Roman"), 1200, 0, 0, TRUE) ;
     	SelectObject (hdc, hFont) ;

     	GetTextExtentPoint32 (hdc, szString, lstrlen (szString), &size) ;
     	BeginPath (hdc) ;
     	TextOut (hdc, (	cxArea - size.cx) / 2, (cyArea - size.cy) / 2,
            							szString, lstrlen (szString)) ;
     	EndPath (hdc) ;
               				// Set clipping area
     	SelectClipPath (hdc, RGN_COPY) ;
               				// Draw Bezier splines
    	iOffset = (cxArea + cyArea) / 4 ;
     	for (y = -iOffset ; y < cyArea + iOffset ; y++)
     	{
          		pt[0].x = 0 ;
          		pt[0].y = y ;

          		pt[1].x = cxArea / 3 ;
          		pt[1].y = y + iOffset ;

          		pt[2].x = 2 * cxArea / 3 ;
          		pt[2].y = y - iOffset ;

          		pt[3].x = cxArea ;
          		pt[3].y = y ;

          		SelectObject (hdc, CreatePen (PS_SOLID, 1,
               				RGB (rand () % 256, rand () % 256, rand () % 256))) ;
          		PolyBezier (hdc, pt, 4) ;
          		DeleteObject (SelectObject (hdc, GetStockObject (BLACK_PEN))) ;
     }

	DeleteObject (SelectObject (hdc, GetStockObject (WHITE_BRUSH))) ;
 	SelectObject (hdc, GetStockObject (SYSTEM_FONT)) ;
	DeleteObject (hFont) ;
}

��ʽ�й��ⲻʹ��SetBkMode������ʵ����ͬ��Ч������ʽ�ڻ�ͼ·��֧���л���һЩ���֣�Ȼ�����SelectClipPath������ʹ�������ɫ����һϵ�б��������ߡ�

���FONTCLIP��ʽʹ��TRANSPARENTѡ�����SetBkMode�����������߽�����������Ԫ�������ڲ������ڶ�OPAQUEѡ��ı���ģʽ�£������������������ַ����ڲ������������ڲ�����ͼ17-8��ʾ��


��

ͼ17-8 FONTCLIP��өĻ��ʾ

������������FONTCLIP�в���SetBkMode�������۲�TRANSPARENTѡ��ı仯��

FONTDEMO��dz�ʽ��������ӡ����ʾ��ЩЧ�������������������Լ���һЩ����Ч����