VS2010-MFC (dialog box: Font dialog box)

Source: Internet
Author: User

Transferred from: http://www.jizhuomi.com/software/175.html

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:

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:

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;     *lpchoosefont;

The choosefon struct has a member lpLogFont, which is a pointer to a logfont struct variable , as stated above, LogFont contains font features, for example, we can The font name is learned through LogFont's 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

Chicken Peck Rice 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
voidCexample18dlg::onbnclickedfontbutton () {//Todo:add your control notification handler code hereCString Strfontname;//Font nameLOGFONT LF;//logfont Variable//clear lf All bytes to 0memset (&LF,0,sizeof(LOGFONT)); //set the element font name in LF to "Arial"_tcscpy_s (Lf.lffacename, Lf_facesize, _t ("Song Body")); //constructs a Font dialog box with the initial selection font named "Arial"CFontDialog Fontdlg (&LF); if(IDOK = = Fontdlg.domodal ())//Display Font dialog box    {           //if M_font already has a font resource object associated with it, release it        if(m_font.m_hobject) {M_font.           DeleteObject (); }           //Create a new font using the LogFont of the selected fontM_font.           CreateFontIndirect (FontDlg.m_cf.lpLogFont); //gets the CWnd pointer for the edit box idc_font_edit and sets its fontGetDlgItem (Idc_font_edit)->setfont (&M_font); //If the user selects the OK button for the font dialog, gets the name of the selected font and displays it in the edit boxStrfontname = fontdlg.m_cf.lplogfont-> Lffacename;       Setdlgitemtext (Idc_font_edit, strfontname); }   }  

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.