//如何设置编辑框文字大小
1.在头文件中定义一个画笔类型的变量
CFont m_font;
2.在成员函数OnInitDialog()中添加如下代码:
CEditFontDlg *p = (CEditFontDlg *)GetDlgItem(IDC_EDIT_FONT);//获得编辑框指针。 GetDlgItem()是返回一个CWnd* 要强制转换一下 m_font.CreatePointFont(400,"楷体");//设定画笔大小 和 字体 p->SetFont(&m_font);//将画笔选入
例如:
创建一个基于对话框的MFC应用程序命名为:EditFont 添加一个编辑框空间ID改为:IDC_EDIT_FONT
在类头文件中定义一个画笔类型的成员变量用于定义你想要的画笔.
看最后一行添加的代码.
class CEditFontDlg : public CDialog { // Construction public: CEditFontDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CEditFontDlg) enum { IDD = IDD_EDITFONT_DIALOG }; CString m_strFont; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CEditFontDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CEditFontDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); //}}AFX_MSG DECLARE_MESSAGE_MAP() CFont m_font;//定义一个画笔成员变量 };
在类成员函数OnInitDialog()里面设定画笔的大小,添加画笔进去.
看代码下面三行添加的代码
BOOL CEditFontDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here CEditFontDlg *p = (CEditFontDlg *)GetDlgItem(IDC_EDIT_FONT);//获得编辑框指针。 GetDlgItem()是返回一个CWnd* 要强制转换一下 m_font.CreatePointFont(400,"楷体");//设定画笔大小 和 字体 p->SetFont(&m_font);//将画笔选入 return TRUE; // return TRUE unless you set the focus to a control }