您的位置:首页 > 其它

vc 根据字体对话框和颜色对话框设置控件里的字体和颜色

2008-11-16 17:33 676 查看
视频教程及其源码下载地址http://download.csdn.net/source/780472

题目:编写一个基于对话框的应用程序,要求其能实现如下功能:
(1)可以用来打开及显示文本文件。
(2)可以设置所显示的文本的字体颜色。
★打开Microsoft Visual C++ 6.0
文件->新建
选择MFC AppWizard(exe)
输入工程文件名。
这里填写09
点击确定按钮,程序将自动出现MFC应用程序向导
选择基本对话框
点击完成按钮
出现新建工程信息对话框。
点击确定按钮

★★★★首先是定义颜色类★★★★★

★点工具栏->插入->类
name 任意填写 这里填的是CStatic09
Base class选择CStatic
点击确定按钮。

★鼠标切换左边的类视图区出现 09 classes
鼠标左键双击CStatic09 左边将出现N行代码
找到
class My09Static : public CStatic
{
// Construction
public:
My09Static();
。。。

在下面输入:
private:
COLORREF m_ForeColor; //文本颜色
COLORREF m_BackColor; //背景色
CBrush m_BkBrush; //背景刷
public:
void SetForeColor(COLORREF color);//设置文本颜色
void SetBkColor(COLORREF color);//设置背景颜色
找到
protected:
//{{AFX_MSG(My09Static)
// NOTE - the ClassWizard will add and remove member functions here.
/}}AFX_MSG
DECLARE_MESSAGE_MAP()

在中间加入
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);//消息响应函数
效果如下:
protected:
//{{AFX_MSG(My09Static)
// NOTE - the ClassWizard will add and remove member functions here.
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);//消息响应函数
/}}AFX_MSG
DECLARE_MESSAGE_MAP()

★点击CStatic09左边的+号按钮
出现CStatic09()
右键双击CStatic09() 左边将出现N行代码
找到
CStatic09::CStatic09()
{
}
在大括号与小括号之间输入:

//文字颜色
m_ForeColor = GetSysColor( COLOR_BTNTEXT );
//背景色
m_BackColor = GetSysColor( COLOR_BTNFACE );
//背景刷
m_BkBrush.CreateSolidBrush(m_BackColor);

找到
BEGIN_MESSAGE_MAP(My09Static, CStatic)
//{{AFX_MSG_MAP(My09Static)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

在中间输入ON_WM_CTLCOLOR_REFLECT()//颜色宏定义

效果如下
BEGIN_MESSAGE_MAP(My09Static, CStatic)
//{{AFX_MSG_MAP(My09Static)
// NOTE - the ClassWizard will add and remove mapping macros here.
ON_WM_CTLCOLOR_REFLECT()//颜色宏定义
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

最后找到
/////////////////////////////////////////////////////////////////////////////
// CStatic09 message handlers

在下面输入
HBRUSH CStatic09::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC->SetTextColor( m_ForeColor );
pDC->SetBkColor( m_BackColor );
return (HBRUSH)m_BkBrush.GetSafeHandle();

}

void CStatic09::SetForeColor(COLORREF color) //设置文本颜色
{
m_ForeColor = color;
}

void CStatic09::SetBkColor(COLORREF color) //设置背景颜色
{
m_BackColor = color;
m_BkBrush.Detach();
m_BkBrush.CreateSolidBrush( m_BackColor );
}

◆◆◆设置对话框代码◆◆◆
◆找到类视图中的CMu09Dlg,双击后显示代码。
在文件前面加入头文件
#include "Static09.h"
找到
class CMy09Dlg : public CDialog
{
// Construction
public:
CMy09Dlg(CWnd* pParent = NULL); // standard constructor
在下面声明变量
CStatic09 m_Edit1;
CFont m_Font; //控件字体
COLORREF m_clrTxtColor;//字体颜色控制

◆点击类视图中CMy09Dlg后单击CMy09Dlg右边的+号
找到DoDataExchange(CDataExchange* pDX)双击后找到
void CMy09Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy09Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
在之间输入
DDX_Control(pDX, IDC_EDIT1, m_Edit1);//关联一个控件
效果如下
void CMy09Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy09Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
DDX_Control(pDX, IDC_EDIT1, m_Edit1);
//}}AFX_DATA_MAP
}
◆在类视图中找到OnlnitDialog()双击后找到
// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}
在之间输入
m_Edit1.SetBkColor( RGB(255,255,255) );//设置字体背景为白色
效果如下:
// TODO: Add extra initialization here
m_Edit1.SetBkColor( RGB(255,255,255) );//设置编辑框字体背景为白色
return TRUE; // return TRUE unless you set the focus to a control
☆☆☆设置主对话框☆☆☆

☆点击资源视图找到 09 resources
下面的Dialog
点击Dialog左边的+号,找到IDD_MY09_DIALOG
双击该对话框左边出现对话框视图
删除“TODO: 在这里设置对话控制。”这个标签
然后分别添加三个按钮控件和Edit文本框控件
分别将三个按钮改名为 打开文件 改变字体大小 和改变字体颜色

☆双击打开文件按钮将弹出一个对话框点击ok
出现
void CMy09Dlg::OnButton1()
{
// TODO: Add your control notification handler code here

}
在括号中添加打开文件的代码
char szFilters[]=
"TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||";
//设置打开后缀名为txt的文件
CFileDialog fileDlg (TRUE, "txt", "*.txt",
OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);

// Display the file dialog. When user clicks OK, fileDlg.DoModal()
// returns IDOK.
if( fileDlg.DoModal()==IDOK )
{
CString pathName = fileDlg.GetPathName();
CStdioFile out;
out.Open(pathName, CFile::modeRead);
CString sSql="",s;
//读取文件
do{
out.ReadString(s);
sSql=sSql+s+(char)10;
}
while (out.GetPosition()!=out.GetLength());
out.Close();
GetDlgItem(IDC_EDIT1)->SetWindowText(sSql); //显示文本
UpdateData(FALSE);
}

然后返回对话框设计界面
☆双击打开改变字体大小按钮将弹出一个对话框点击ok
出现
void CMy09Dlg::OnButton2()
{
// TODO: Add your control notification handler code here
}
在括号中添加改变字体大小的代码

LOGFONT lf;
GetDlgItem(IDC_EDIT1)->GetFont()->GetLogFont(&lf);
//使用按钮的当前字体初始化字体对话框
CFontDialog dlgFontDlg(&lf);
//显示字体选择对话框
if (dlgFontDlg.DoModal() == IDOK)
{
//如果用户在字体选择对话框中单击了“确定”按钮
//则将按钮ID_BUTTON_DEMODE的标题文本字体设置为所选定的字体
static CFont font;
dlgFontDlg.GetCurrentFont(&lf);
font.DeleteObject();
font.CreateFontIndirect(&lf);
GetDlgItem(IDC_EDIT1)->SetFont(&font);
}

☆然后返回对话框设计界面
双击打开改变字体颜色按钮将弹出一个对话框点击ok
出现
void CMy09Dlg::OnButton3()
{
// TODO: Add your control notification handler code here

}
在括号中添加改变字体颜色的代码
CColorDialog colorDlg;
//显示
if(colorDlg.DoModal())
{
CString m_str;
char TempStr1[10]; //定义
char TempStr2[10];
char TempStr3[10];
int x,y,z;
memset(TempStr1,'/0',10);
memset(TempStr2,'/0',10);
memset(TempStr3,'/0',10);
//获得颜色
m_clrTxtColor = colorDlg.GetColor();
static CString sMessage;
sMessage.Format("%03d, %03d, %03d",
GetRValue( m_clrTxtColor ),
GetGValue( m_clrTxtColor ),
GetBValue( m_clrTxtColor ));
sscanf(sMessage,"%s %s %s",TempStr1,TempStr2,TempStr3); //赋值变量
x=atoi(TempStr1);//类型转换
y=atoi(TempStr2);
z=atoi(TempStr3);
m_Edit1.SetForeColor(RGB(x,y,z) );
//刷新窗口
Invalidate();
}

2008-11-16 河边杨柳
http://hebianyangliu.cn/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐