MFC文本编程:创建插入符

一、创建文本插入符

Step01、创建插入符

相关函数/结构体
            CWnd::CreateSolidCaret(); 
             定义:void CreateSolidCaret(int nWidth,int nHeight);
        在View类中加入WM_CREATE消息的响应函数OnCreate,加入CreateSolidCaret()
        效果:没有显示插入符呀!   

QUOTE FROM MSDN
The CreateSolidCaret method 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 method must be called.

Step02、显示插入符

        加上ShowCaret();
    Step03、让插入符跟字体大小相关
        相关函数/结构体:
            获取字符信息CDC::GetTextMetrics
            字符属性结构体TEXTMETRIC Structure

ExpandedBlockStart.gif code
    /  
   
// CTextView message handlers 

   
int CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    { 
       
if (CView::OnCreate(lpCreateStruct) ==   - 1
           
return   - 1
       
// 创建设备描述表 
        CClientDC dc(this); 
       
// 定义文本信息结构体变量 
        TEXTMETRIC tm; 
       
// 获得设备描述表中的文本信息 
        dc.GetTextMetrics(
& tm); 
       
// 根据字体大小,创建合适的插入符 
        CreateSolidCaret(tm.tmAveCharWidth
/ 8 , tm.tmHeight); 
       
// 显示插入符 

        ShowCaret(); 
        SetTimer(
1 , 100 , NULL ); 
       
return   0
    } 

 

二、创建图形插入符

Step01、创建Bitmap


        定义:CBitmap bitmap;要放在View类的头文件中,并将其访问权限设置为private。为什么private?因为只在View类中调用。 

QUOTE
若将CBitmap bitmap放在CTextView中,bitmap为局部变量,当OnCreate()执行完毕会发生析构,相关的资源也会被销毁。那么说那幅位图也被销毁?应该不会吧?

Step02、创建图形插入符

  
        相关函数
            CWnd::CreateCaret();
                定义:void CreateCaret(CBitmap* pBitmap);                

Quote from MSDN
The bitmap must have previously been created by the CBitmap::CreateBitmap member function, the CreateDIBitmap Windows function, or the CBitmap::LoadBitmap member function.

所以需要Step01中把bitmap定义为全局变量。

ExpandedBlockStart.gifOnCreate消息响应函数源代码
1     
2      /// //
3      // CTextView message handlers
4      int CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
5      { 
6          if (CView::OnCreate(lpCreateStruct) ==   - 1
7              return   - 1
8          bitmap.LoadBitmap(IDB_BITMAP1); 
9          CreateCaret( & bitmap); 
10          ShowCaret(); 
11          SetTimer( 1 , 100 ,NULL); 
12          return   0
13      }

转载于:https://www.cnblogs.com/TonyEwsn/archive/2010/01/25/1656308.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值