Vs2010/mfc dialog box: Font dialog box

Source: Internet
Author: User

Font dialog box)

In the previous section, we explained the use of the file dialog box, and this section focuses on how the Font dialog box is applied.

The Font dialog box is used to select fonts. We can often see it too. MFC uses the CFontDialog class to encapsulate all the actions of the Font dialog box. The Font dialog box is also a modal dialog box.

CFontDialog constructors for class

Let's get to know the CFontDialog class first. Its common constructor prototypes are as follows:

CFontDialog (
lpLogFont lplfinitial = NULL,
DWORD dwFlags = cf_effects | Cf_screenfonts,
cdc* Pdcprinter = NULL,
cwnd* pParentWnd = NULL
);

Parameter description:

Lplfinitial: A pointer to the LOGFONT structure data that you can use to set some characteristics of the font.

DwFlags: Specifies one or more properties of the selected font, as detailed in MSDN.

Pdcprinter: A pointer to a print device context.

pParentWnd: Pointer to the parent window of the Font dialog box.

The first parameter in the above constructor is the LogFont pointer, and the LOGFONT structure contains most of the font's characteristics, including font height, width, orientation, name, and so on. The following is the definition of this struct:

typedef struct TAGLOGFONT {
LONG lfheight;
LONG Lfwidth;
LONG lfescapement;
LONG lforientation;
LONG Lfweight;
BYTE Lfitalic;
BYTE Lfunderline;
BYTE lfstrikeout;
BYTE lfCharSet;
BYTE lfoutprecision;
BYTE lfclipprecision;
BYTE lfquality;
BYTE lfpitchandfamily;
TCHAR Lffacename[lf_facesize];
} LOGFONT;

Gets the font selected in the Fonts dialog box

After we select a font in the Font dialog box, how do I get the selected font? We can indirectly obtain CFont objects of the selected font through the member variable M_CF of the CFontDialog class. M_CF is a variable of type Choosefont, the CHOOSEFONT structure is defined as follows:

typedef struct {
DWORD lStructSize;
HWND hWndOwner;
HDC hdc;
lpLogFont lpLogFont;
INT ipointsize;
DWORD Flags;
COLORREF rgbcolors;
LPARAM Lcustdata;
Lpcfhookproc Lpfnhook;
LPCTSTR Lptemplatename;
HINSTANCE hinstance;
LPTSTR Lpszstyle;
WORD nFontType;
INT nsizemin;
INT Nsizemax;
} Choosefont, *lpchoosefont;

There is a member lpLogFont in the Choosefon struct, which is a pointer to the LOGFONT struct variable, as stated above, LogFont contains the font characteristics, for example, we can learn the font name through LogFont lffacename.

What we end up getting is the CFont object of the selected font, and how does the logfont with the font get the corresponding CFont object? This can be achieved by using the member function of the CFont class CreateFontIndirect. The function prototypes are as follows:

BOOL createfontindirect (const logfont* lplogfont);

The parameter is the LogFont pointer type, and we can pass in the lpLogFont member of the CFontDialog class member variable M_CF to get the CFont object of the selected font.

Font dialog box Application instance

Give you an example of a font dialog box. First introduce the function that this instance wants to implement, generate a dialog box, place a "font selection" button and an edit box in the dialog box. Clicking on the "Font selection" button will bring up the Font dialog box. The edit box displays the selected font name and displays the font name string in the selected font, for example, if you select Arial, the string "Arial" is displayed in the edit box in Arial.

Here are the steps to create this instance:

1. Create a dialog-based MFC project with the name "Example18".

2. In the Idd_example18_dialog template of the auto-generated main dialog box, delete the Todo:place dialog controls here. Static text box, add a button, the ID of idc_font_button,caption set to "Font selection", used to display the Font dialog box to select the font, and then add an edit box, the ID is set to Idc_font_edit, used to display the font name string in the selected font.

3. Add the private member variable for the Cexample18dlg class in Example18Dlg.h: CFont M_font, which is used to save the selected font in the edit box.

4. Add the message handler function Cexample18dlg::onbnclickedfontbutton () for the button Idc_font_button click Message.

5. Modify the message handler function Cexample18dlg::onbnclickedfontbutton () as follows:

C + + code
  1. void Cexample18dlg::onbnclickedfontbutton ()
  2. {
  3. //Todo:add your control notification handler code here
  4. CString Strfontname; //font name
  5. LOGFONT LF; //LogFont variable
  6. //To clear lf all bytes 0
  7. memset (&LF, 0, sizeof (LOGFONT));
  8. //The element font name in LF is set to "Arial"
  9. _tcscpy_s (Lf.lffacename, Lf_facesize, _t ("song Body"));
  10. //Construct Font dialog box, initial selection font named "Arial"
  11. CFontDialog Fontdlg (&LF);
  12. if (IDOK = = Fontdlg.domodal ()) //Display Font dialog box
  13. {
  14. //If M_font has a font resource object associated with it, release it
  15. if (m_font.m_hobject)
  16. {
  17. M_font. DeleteObject ();
  18. }
  19. //Use the LogFont of the selected font to create a new font
  20. M_font. CreateFontIndirect (FontDlg.m_cf.lpLogFont);
  21. //Get the CWnd pointer for the edit box Idc_font_edit and set its font
  22. GetDlgItem (Idc_font_edit)->setfont (&m_font);
  23. //If the user selects the OK button for the font dialog, gets the name of the selected font and displays it in the edit box
  24. Strfontname = fontdlg.m_cf.lplogfont->lffacename;
  25. Setdlgitemtext (Idc_font_edit, strfontname);
  26. }
  27. }

6. Finally, compile and run the program. Display the Results dialog box, click the "Font Selection" button, will pop up the Font dialog box, the default selection is "Arial", we changed to select "Chinese Choi Wan" font point "OK", the edit box will appear as follows:

To this, we have learned the use of Font dialog box, for later in the interface development control display font is very helpful.

Vs2010/mfc dialog box: Font dialog box

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.