Purpose of study Master the two methods of getting the font handle; Call the CreateFont function to create a custom font; Call the SetTextColor and SetBkColor functions to set the font color and background colors; Master the process of text output (get font information, format text, output text). ? 1. Learn how to get a font handle: Hfont HF; Defines the font handle variable HF Hf=getstockobject (); Get system font Handle SelectObject (HDC,HF); Selecting fonts into the device environment ? 2. CreateFont function to create a custom font Hfont = CreateFont ( int nheight ,//Height of the font int nwidth , //Average character width int nescapement , //Angle of escapement int norientation , //base-line Orientation angle int Fnweight , //font weight DWORD Fdwitalic , //Italic attribute option DWORD Fdwunderline , //Underline attribute option DWORD Fdwstrikeout , //Strikeout attribute option DWORD Fdwcharset , //Character Set Identifier DWORD fdwoutputprecision , //Output precision DWORD fdwclipprecision , //clipping precision DWORD fdwquality , //output quality DWORD fdwpitchandfamily , //Pitch and family LPCTSTR lpszface //typeface name ); 3. Set the font color and background colors SetTextColor (Hdc,crcolor); Set Font Color SetBkColor (Hdc,crcolor); Set Background color 4. Mastering the process of text output (1) Get font information GetTextMetrics (HDC,&TM); (2) Formatting text Use GetTextExtentPoint32 () to determine the coordinates of subsequent text, and then determine the coordinates after the line break based on the current font information. (3) Output text Output text using TextOut or DrawText: BOOL TextOut(
HDChdc, // handle to DC
intnXStart, // x-coordinate of starting position
intnYStart, // y-coordinate of starting position
LPCTSTRlpString, // character string
intcbString // number of characters
);
int DrawText(
HDC hDC, // handle to DC
LPCTSTR lpString, // text to draw
int nCount, // text length
LPRECT lpRect, // formatting dimensions
UINT uFormat // text-drawing options
);
5. Write the program: Design a line of text on the window, the text can scroll to the left in the window display, and each display round, change the color and font. The main code of the program is as follows: static int flag=0; int x=0; LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) { ???? int Wmid, wmevent; ???? Paintstruct PS; ???? HDC hdc; ???? RECT Clientrect; ???? TCHAR szhello[max_loadstring]; ???? LoadString (HInst, Ids_hello, Szhello, max_loadstring); ???? Hfont HF;???????????? ???? Textmetric TM;???? ???? Char text[]= "Windows programming Experiment three";???????? The output string. ???? Char chfont[7];???????????????????????????????? Defines a string that contains the font. ???? int Xorg,yorg,y=0,ncharlen=strlen (text); ???? Switch (message) ???? { ???????? Case WM_CREATE: ???????? SetTimer (hwnd,1,50,null);???????? Set the timer. Wm_timer messages are sent every 0.2 seconds. ???????? Break ???????? Case WM_COMMAND: ???????????? Wmid = LoWord (WParam); ???????????? Wmevent = HiWord (WParam); ???????????? Parse the menu selections: ???????????? Switch (WMID) ???????????? { ???????????????? Case Idm_about: ???????????????? DialogBox (HInst, (LPCTSTR) Idd_aboutbox, HWnd, (Dlgproc) about); ???????????????? Break ???????????????? Case Idm_exit: ???????????????? DestroyWindow (HWND); ???????????????? Break ???????????????? Default ???????????????? Return DefWindowProc (hWnd, message, WParam, LParam); ????????????} ???????????? Break ???????? Case WM_PAINT: ???????????? HDC = BeginPaint (hWnd, &ps); ???????????? GetClientRect (Hwnd,&clientrect); ???????????? xorg= (Clientrect.left+clientrect.right)/2; ???????????? yorg= (Clientrect.top+clientrect.bottom)/2; ? ???????????? Setviewportorgex (Hdc,0,yorg,null); ???????????? Todo:add any drawing code here ... ? ???????????? if (flag%4==0)???? Sets the font color. ???????????? { ???????????????? SetTextColor (Hdc,rgb (255,0,0)); ???????????????? strcpy (ChFont, "italics"); ????????????} ???????????? else if (flag%4==1) ???????????? { ???????????????? SetTextColor (Hdc,rgb (0,255,0)); ???????????????? strcpy (ChFont, "Song Body"); ????????????} ???????????? else if (flag%4==2) ???????????? { ???????????????? SetTextColor (Hdc,rgb (0,0,255)); ???????????????? strcpy (ChFont, "Fangsong"); ????????????} ???????????? else if (flag%4==3) ???????????? { ???????????????? SetTextColor (Hdc,rgb (255,255,0)); ???????????????? strcpy (ChFont, "blackbody"); ????????????} ???????????? Hf=createfont (???????????? Gets the font handle. ???????????????? ,???????????????? Font height ???????????????? 0,???????????????????? The system automatically adjusts the width. ???????????????? 0,???????????????????? Text level ???????????????? 0,???????????????????? Font Tilt is 0 ???????????????? ,???????????????? Font size. 400 is normal. ???????????????? 0,???????????????????? The font does not tilt. ???????????????? 0,???????????????????? No underline. ???????????????? 0,???????????????????? No dashes. ???????????????? Gb2312_charset,???????? Character ???????????????? Out_default_precis,???? The default output precision. ???????????????? clip_default_precis,//Default cropping precision ???????????????? Default_quality,???? The default output quality. ???????????????? default_pitch| ff_dontcare,//default Spacing ???????????????? ChFont);???????? The font name. ? ???????????? SelectObject (HDC,HF);???? Select the font. ???????????? GetTextMetrics (HDC,&TM);???? Gets the font information. ???????????? TextOut (Hdc,x,y-tm.tmheight/2,&text[0],ncharlen); Output. ? ???????????? x-=10; ???????????? if (x+tm.tmavecharwidth*ncharlen<0) { ???????????????? flag++; ???????????????? if (flag==4) ???????????????????? flag=0; ???????????????? X=clientrect.right; ????????????} ? ???????????? DeleteObject (HF);???? Delete the font. ???????????? EndPaint (HWnd, &ps); ???????????? Break ???????? Case Wm_timer: ???????? InvalidateRect (hwnd,null,1);???? Refreshes the user area. ???????? Break ???????? Case Wm_destroy: ???????????? PostQuitMessage (0); ???????????? Break ???????? Default ???????????? Return DefWindowProc (hWnd, message, WParam, LParam); } return 0; } Compile and debug the program, |