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
)
);