[连载]VC++深入详解(孙鑫视频)第5章-文本编程 笔记

一、插入符(caret)的创建

1.CWnd::CreateSolidCaret 

void CreateSolidCaret( int nWidth, int nHeight );

Parameters

nWidth

Specifies the width of the caret (in logical units). If this parameter is 0, the width is set to the system-defined window-border width.

nHeight

Specifies the height of the caret (in logical units). If this parameter is 0, the height is set to the system-defined window-border height.

Remarks

Creates a solid rectangle for the system caret and claims ownership of the caret. The caret shape can be a line or block.

The parameters nWidth and nHeight specify the caret’s width and height (in logical units); the exact width and height (in pixels) depend on the mapping mode.

The system’s window-border width or height can be retrieved by the GetSystemMetrics Windows function with the SM_CXBORDER and SM_CYBORDER indexes. Using the window-border width or height ensures that the caret will be visible on a high-resolution display.

The CreateSolidCaret member function automatically destroys the previous caret shape, if any, regardless of which window owns the caret. Once created, the caret is initially hidden. To show the caret, the ShowCaret member function must be called.

The system caret is a shared resource. CWnd should create a caret only when it has the input focus or is active. It should destroy the caret before it loses the input focus or becomes inactive.

注意几点:

(1).插入符应该创建在视类中,在视类窗口产生后。在视类中增加WM_CREATE消息的响应函数OnCreate中

(2).如果 nWidth 和 nHeight 是 0, 那么插入符的宽度和高度被设置成系统定义窗口边界的宽度和高度,也可以调用 int GetSystemMetrics( int nIndex );得到系统信息,如GetSystemMetrics(SM_CXBORDER)得到 系统窗口边界宽度。

(3).Once created, the caret is initially hidden(caret被创建后初始是隐藏的,得调用voidCWnd:: ShowCaret( )才能显示。

代码:

int CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct)   //在视类中增加WM_CREATE消息的响应函数  

                                                                                                                 //OnCreate()
{
 if (CView::OnCreate(lpCreateStruct) == -1)
  return -1;
 
 // TODO: Add your specialized creation code here
 CreateSolidCaret(GetSystemMetrics(SM_CXBORDER),GetSystemMetrics(SM_CYBORDER)); //得到系统窗口边

                                                                                                                                                                           //界宽度和高度ShowCaret();                   //显示插入符
 return 0;
}

 2.如何让插入符适合于当前字体大小?

用CDC类中的GetTextMetrics函数实现,可得到设备描述表中当前字体的度量信息,并存放到TEXTMETRIC机构体变量中。

typedef struct tagTEXTMETRIC {  /* tm */
    int  tmHeight;                 //当前字符字体的高度
    int  tmAscent;
    int  tmDescent;
    int  tmInternalLeading;
    int  tmExternalLeading;
    int  tmAveCharWidth;           //当前文本字体的平均宽度
    int  tmMaxCharWidth;
    int  tmWeight;               
    BYTE tmItalic;
    BYTE tmUnderlined;
    BYTE tmStruckOut;
    BYTE tmFirstChar;
    BYTE tmLastChar;
    BYTE tmDefaultChar;
    BYTE tmBreakChar;
    BYTE tmPitchAndFamily;
    BYTE tmCharSet;
    int  tmOverhang;
    int  tmDigitizedAspectX;
    int  tmDigitizedAspectY;
} TEXTMETRIC;
代码如下:
int CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
 if (CView::OnCreate(lpCreateStruct) == -1)
  return -1;
 
 // TODO: Add your specialized creation code here
 TEXTMETRIC tm;
 CClientDC dc(this);
 dc.GetTextMetrics(&tm);
 CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight); //除8是经验值
 ShowCaret();
 return 0;
}
3.创建图形插入符
用函数void CreateCaret(CBitmap* pBitmap);实现。
注意一点:位图变量要定义为CTextViww类的成员变量,不能定义为局部变量,否则不行显示。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值