孙鑫VC学习笔记:第五讲 文本编程
1.创建插入符
CWnd::CreateSolidCaret
MSDN
-------------------------------------------------------------------------------------
CWnd::CreateSolidCaret
Creates a solid rectangle for the system caret and claims ownership of the caret.
void CreateSolidCaret(
int nWidth, //插入符的宽度,为0,表示系统定义的窗口边界宽度
int nHeight //插入符的高度,为0,表示系统定义的窗口边界高度
);
The system's window-border width or height can be retrieved by the
GetSystemMetrics Windows function with the SM_CXBORDER and SM_CYBORDER indexes.
To show the caret, the ShowCaret member function must be called.
------------------------------------------------------------------------------------
2.获得文本信息
CDC::GetTextMetrics
------------------------------------------------------------------------------------
Retrieves the metrics for the current font using the attribute device context.
BOOL GetTextMetrics(
LPTEXTMETRIC lpMetrics
) const;
------------------------------------------------------------------------------------
TEXTMETRIC
------------------------------------------------------------------------------------
typedef struct tagTEXTMETRIC {
LONG tmHeight; //字体的高度
LONG tmAscent; //基线以上的部分的长度
LONG tmDescent; //基线以下的地方
LONG tmInternalLeading;
LONG tmExternalLeading;
LONG tmAveCharWidth; //平均字符宽度
LONG tmMaxCharWidth; //最大字符宽度
LONG tmWeight;
LONG tmOverhang;
LONG tmDigitizedAspectX;
LONG tmDigitizedAspectY;
TCHAR tmFirstChar;
TCHAR tmLastChar;
TCHAR tmDefaultChar;
TCHAR tmBreakChar;
BYTE tmItalic;
BYTE tmUnderlined;
BYTE tmStruckOut;
BYTE tmPitchAndFamily;
BYTE tmCharSet;
} TEXTMETRIC, *PTEXTMETRIC;
-------------------------------------------------------------------------------------
TextView.CPP
------------------------------------------------------------------------------------
int CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
CClientDC dc(this);
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight); //创建插入符
ShowCaret(); //显示插入符
return 0;
}
-------------------------------------------------------------------------------------
3.创建图形插入符.
CWnd::CreateCaret
MSDN
-----------------------------------------------------------------------------------
CWnd::CreateCaret
Creates a new shape for the system caret and claims ownership of the caret.
void CreateCaret(
CBitmap* pBitmap
);
------------------------------------------------------------------------------------
4.输出文字
CTextView::OnDraw(CDC *pDC)函数当窗口发生重绘的时候,就会被应用程序的框架所调用.
5.字符串类CString
CString的构造函数
MSDN
----------------------------------------------------------------------------------
CString();
CString(const CString& stringSrc);
throw(CMemoryException);
CString(TCHAR ch, int nRepeat = 1);
throw(CMemoryException);
CString(LPCTSTR lpch, int nLength);
throw(CMemoryException);
CString( const unsigned char* psz);
throw(CMemoryException);
CString(LPCWSTR lpsz);
throw(CMemoryException);
CString(LPCSTR lpsz);
throw(CMemoryException);
------------------------------------------------------------------------------------
LoadString
MSDN
-----------------------------------------------------------------------------------
CString::LoadString
The LoadStringW method reads a Windows string resource (identified by nID) into
an existing CString object.
int LoadStringW(
UINT nID
) throw (CHeap_Exception);
----------------------------------------------------------------------------------
TextView.CPP
------------------------------------------------------------------------------------
void CTextView::OnDraw(CDC* pDC)
{
CTextDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//CString str="Hello Visual C++!";
CString str;
//str="Hello Visual C++!";
str.LoadString(IDS_HELLOVC);
pDC->TextOut(50,50,str);
}
-------------------------------------------------------------------------------------
6.创建路径
CDC::BeginPath函数
MSDN
------------------------------------------------------------------------------------
CDC::BeginPath //打开路径
Opens a path bracket in the device context.
BOOL BeginPath( );
-------------------------------------------------------------------------------------
CDC::GetTextExtent //
MSDN
-------------------------------------------------------------------------------------
CDC::GetTextExtent
Call this member function to compute the width and height of a line of text using
the current font to determine the dimensions.
CSize GetTextExtent(
LPCTSTR lpszString,
int nCount
) const;
CSize GetTextExtent(
const CString& str
) const;
------------------------------------------------------------------------------------
MSDN
------------------------------------------------------------------------------------
CDC::SelectClipPath
Selects the current path as a clipping region for the device context, combining
he new region with any existing clipping region by using the specified mode.
BOOL SelectClipPath(
int nMode
);
RGN_DIFF The new clipping region includes the areas of the current clipping
region, and those of the current path are excluded.
------------------------------------------------------------------------------------
选择当前的路径作为一个剪切区域,联合新的区域和一个现存的剪切区域按照指定的模式进行互
操作
TextView.CPP
------------------------------------------------------------------------------------
void CTextView::OnDraw(CDC* pDC)
{
CTextDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//CString str="Hello Visual C++!";
CString str;
str="Hello Visual C++!";
pDC->TextOut(50,50,str);
CSize sz=pDC->GetTextExtent(str);
pDC->BeginPath();
pDC->Rectangle(50,50,50+sz.cx,50+sz.cy);
pDC->EndPath();
pDC->SelectClipPath(RGN_AND);
for (int i=0; i<300; i+=10)
{
pDC->MoveTo(0,i);
pDC->LineTo(300,i);
pDC->MoveTo(i,0);
pDC->LineTo(i,300);
}
}
-------------------------------------------------------------------------------------
7.实现字符输出的功能
移动插入符:
CWnd::SetCaretPos
------------------------------------------------------------------------------
CWnd::SetCaretPos
Sets the position of the caret.
static void PASCAL SetCaretPos(
POINT point //静态方法,设置插入符的位置
);
-----------------------------------------------------------------------------
获取窗口的背景色:
CDC::GetBKColor
MSDN
--------------------------------------------------------------------------------
CDC::GetBkColor
Returns the current background color.
COLORREF GetBkColor( ) const;
-------------------------------------------------------------------------------
设置文本的颜色
CDC::SetTextColor
MSDN
---------------------------------------------------------------------------------
CDC::SetTextColor
Sets the text color to the specified color.
virtual COLORREF SetTextColor(
COLORREF crColor
);
Return Value
An RGB value for the previous text color.
---------------------------------------------------------------------------
截取字符串函数
CString::Left
MSDN
-------------------------------------------------------------------------------
CString::Left
The Left method extracts the first (that is, leftmost) nCount characters from
a CHString string and returns a copy of the extracted substring. If nCount
exceeds the string length, then the entire string is extracted.
CString Left(
int nCount
) const throw (CHeap_Exception);
---------------------------------------------------------------------------------
CString::GetLength
MSDN
----------------------------------------------------------------------------
CString::GetLength
The GetLength method gets a count of the number of wide characters in this
CString string. The count does not include a NULL terminator.
int GetLength();
--------------------------------------------------------------------------------
8.创建字体
CFont类
CFont::CFont构造函数
MSDN
----------------------------------------------------------------------------------
CFont::CFont
Constructs a CFont object.
CFont( );
Remarks
The resulting object must be initialized with CreateFont, CreateFontIndirect,
CreatePointFont, or CreatePointFontIndirect before it can be used.
----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
CFont::CreatePointFont
This function provides a simple way to create a font of a specified typeface
and point size.
BOOL CreatePointFont(
int nPointSize, //设置字体的大小
LPCTSTR lpszFaceName, //设置字体的名字
CDC* pDC = NULL //转换字体的大小为逻辑单位
);
---------------------------------------------------------------------------------
9.制作卡拉OK字幕.
CDC::DrawText
MSDN
---------------------------------------------------------------------------------
CDC::DrawText
Call this member function to format text in the given rectangle. To specify
additional formatting options, use CDC::DrawTextEx.
virtual int DrawText(
LPCTSTR lpszString,
int nCount,
LPRECT lpRect, //输出的范围
UINT nFormat //输出的格式
);
int DrawText(
const CString& str,
LPRECT lpRect,
UINT nFormat
);
----------------------------------------------------------------------------------
设置定时器CWnd::SetTimer
MSDN
-----------------------------------------------------------------------------------
CWnd::SetTimer
Installs a system timer.
UINT_PTR SetTimer(
UINT_PTR nIDEvent, //非0的定时器标识
UINT nElapse, //发送定时器消息的间隔时间(单位:0.001s)
void (CALLBACK* lpfnTimer //回调函数指针,当我们设定好回调函数,那么
)(HWND, //WM_TIMER消息被放到应用程序消息队列,由
UINT, //CWnd对象进行处理
UINT_PTR,
DWORD
)
);
孙鑫VC学习笔记:第五讲 文本编程相关推荐
- 孙鑫VC++学习笔记(转载至程序员之家--虎非龙)[11--15] .
第11课 1.创建4个菜单,为其添加消息响应,用成员变量保存绘画类型.添加LButtonDown和Up消息. 2.当窗口重绘时,如果想再显示原先画的数据,则需要保存数据.为此创建一个新类来记录绘画类型 ...
- 孙鑫VC++学习笔记(转载至程序员之家--虎非龙)[11--15]
第11课 1.创建4个菜单,为其添加消息响应,用成员变量保存绘画类型.添加LButtonDown和Up消息. 2.当窗口重绘时,如果想再显示原先画的数据,则需要保存数据.为此创建一个新类来记录绘画类型 ...
- 孙鑫VC学习笔记:第七讲
七.对话框 2006年8月5日 14:25 因为笔记是用OneNote做的,上传以后为看不到图片,于是我截图放到相册上面, 相册地址为:http://photo.163.com/photos/good ...
- mfc编程 孙鑫_孙鑫MFC学习笔记7:对话框编程(上)
1.DoModal创建模态对话框 2.Create创建非模态对话框(需要用ShowWindow显示出来) 模态:对话框显示,程序会暂停,直到对话框关闭 非模态:对话框显示,程序继续执行 3.对于模态对 ...
- 孙鑫VC学习系列教程
教程简介 1.循序渐进 从Win32SDK编程开始讲解,帮助大家理解掌握Windows编程的核心 -- 消息循环机制. 2.通俗易懂 编程语言枯燥难懂,然而通过孙鑫老师形象化的讲解,Windows和M ...
- 孙鑫VC++讲座笔记-(6)菜单编程
1,弹出菜单(Pop-up)是不能用来作命令响应的. 2,MFC中菜单项消息如果利用ClassWizard来对菜单项消息分别在上述四个类中进行响应,则菜单消息传递顺序:View类--Doc类--CMa ...
- 孙鑫mfc学习笔记第十四课
第十四课 网络的相关知识,网络程序的编写,Socket是连接应用程序与网络驱动程序的桥梁,Socket在应用程序中创建,通过bind与驱动程序建立关系.此后,应用程序送给Socket的数据,由Sock ...
- 孙鑫MFC学习笔记1.Windows应用程序运行机理
1.MSG结构 hwnd:窗口句柄 message:消息类型 wParam & lParam:消息的附加信息(比如键值) time:消息被投递的时间 tip:typedef的作用是从变量类型区 ...
- mfc编程 孙鑫_孙鑫VC++视频教程笔记-(3)MFC程序框架的剖析 附1-SDI程序流程图
1,寻找WinMain人口: 在安装目录下找到MFC文件夹下的SRC文件夹,SRC下是MFC源代码. 路径:MFC|SRC|APPMODUL.CPP: _tWinMain(HINSTANCE hIns ...
- mfc编程 孙鑫_孙鑫MFC学习笔记6:菜单编程
1.对菜单响应的顺序: 视类,文档类,框架类,应用程序类 2.消息的分类 3.CWnd继承自CCmdTarget类, 所以从CWnd派生出的类也可以接收WM_COMMAND消息 4.命令的消息路由 5 ...
最新文章
- MathML + MathJax在网页中插入公式
- mysql ogg_异构平台mysql-oracle(ogg)安装部署
- 的拼音怎么改正_「我就退出家长群怎么了?」:多少中年父母的崩溃,从家长群开始...
- Delphi下实现全屏快速找图找色 二、矩阵遍历
- python抓取网页图片
- ArrayList add方法深度解析。
- 游戏筑基开发之一级指针、二级指针的使用情形(C语言)
- 在powerDesigner中通过SQL生成pdm
- 内核如何检测soft lockup与hard lockup?
- Mybatis插件机制原理
- Hadoop集群搭建(六):hadoop配置namenode服务
- java经典算法(二)---zws
- GoogleStyle编程代码规范
- android设置传感器的采集方向,Android-传感器开发-方向判断
- Visual Studio下载太慢的解决方法
- 信息与计算机科学就业如何,信息与计算科学专业就业情况怎么样
- CF1139C Edgy TreesDFS求连通块大小、思维
- When you want to give up, remember why you started.
- iOS科普一下根View及其子View中心点含义的坑
- 游戏的汇总,在github和码云上找的,主要是安卓游戏,还有垃圾分类游戏
热门文章
- 一年工作经验的java面试题
- 《探讨大规模无线通信》-高西奇教授 讲座记录
- Windows XP pro with sp2(x64)VOL版(英文原版)光盘镜像 + 简繁中文语言包 + 有效安装密钥 一些网友都有这样的困惑,Windows XP sp3在安装
- Oracle数据库出现问题时,这十个脚本帮你快速定位原因
- 线性代数知识荟萃(5)——矩阵相似
- __setattr__,__getattr__,__delattr__
- 简单的form表单文件上传
- Django学习笔记-MySQL
- 四大顶级开源网络管理工具详解
- python相关参考地址收藏