分享 啰啰嗦嗦学习VC++_<<第五课:字处理\光标>>

作者在 2010-07-19 19:26:41 发布以下内容

字处理程序:

一、插入符用到的函数: OnCreate中实现

CreateSolidCaret(int nWidth, int nHeight);

ShowCaret( );

GetTextMetrics ( LPTEXTMETRIC lpMetrics );                 /**/   GetSystemMetrics

TEXTMETRIC Structure

 

1CreateSolidCaret     caret-插入记号;solid-固体、实心、可靠;mapping mode-映射模式

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 theGetSystemMetrics 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.

插入符的创建时在窗口创建之后才能进行。窗口的创建用到WM_CREATE()这个函数,在MFC中为OnCreate()用来响应 WM_CREATE()这个函数。因为视类始终覆盖在框架类之上,在视类(VIEW)中创建窗口的插入符。虽然只有框架类(frame )中有OnCreate(LPCREATESTRUCT lpCreateStruct)这个响应函数而视类中没有,我们可以添加一个OnCreate()响应函数。

2GetSystemMetrics     metrics-度量

The GetSystemMetrics function retrieves various system metrics (widths and heights of display elements) and system configuration settings. All dimensions retrieved by GetSystemMetrics are in pixels.

int GetSystemMetrics(  int nIndex   // system metric or configuration setting);Parameters

nIndex

[in] Specifies the system metric or configuration setting to retrieve. All SM_CX* values are widths. All SM_CY* values are heights. The following values are defined.

CDC::GetTextMetrics 

BOOL GetTextMetrics( LPTEXTMETRIC lpMetrics ) const;      const-常数;  Metric-度量尺度;  metrics-[]规格;

Return Value

Nonzero if the function is successful; otherwise 0.

Parameters

lpMetrics

Points to the TEXTMETRIC structure that receives the metrics.

Remarks

Retrieves the metrics for the current font using the attribute device context.

3TEXTMETRIC Structure 

The TEXTMETRIC structure has the following form:

tmDescent

gh

tmAscent

base line

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;

The TEXTMETRIC structure contains basic information about a physical font. All sizes are given in logical units; that is, they depend on the current mapping mode of the display context.

For more complete information about this structure, see TEXTMETRIC in the Win32 SDK documentation.


1

2

3

4

5

6

7

8

9

10

11

12

int C_LES5_View::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

if (CView::OnCreate(lpCreateStruct) == -1)

        return -1;     

// TODO: Add your specialized creation code here

CClientDC dc(this);

 TEXTMETRIC textm;

dc.GetTextMetrics(&textm);

CreateSolidCaret(textm.tmAveCharWidth/8,(textm.tmAscent+textm.tmDescent));

ShowCaret();

return 0;

}


第九行代码中:textm.tmAveCharWidth/8 这里除以8是根据经验的来的。该行代码与下边的这行代码效果一样:

CreateSolidCaret(textm.tmAveCharWidth/8,textm.tmHeight);

二、用图标作为插入符:     OnCreate中实现

用到的函数:

CreateCaret

 

1CWnd::CreateCaret 

void CreateCaret( CBitmap* pBitmap );

Parameters

pBitmap

Identifies the bitmap that defines the caret shape.

Remarks

Creates a new shape for the system caret and claims ownership of the caret.

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

CreateCaret 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

2

3

4

5

6

7

8

{

……

CBitmap bitmap;

    bitmap.LoadBitmap(IDB_BITMAP1);

       CreateCaret(&bitmap);

       ShowCaret();

……

}


上边这段代码运行后并没有出现想要的bitmap光标。原因是这里定义了第3行的bitmap为局部变量在第8行的花括号执行后就被释放了即被析构了,因此要把bitmap定义为View的成员变量。把bitmap放在View类的头文件中位置如下,将第3行代码删除:


1

2

3

4

5

6

7

class C_LES5_View : public CView

{

       ……

private:

              CBitmap bitmap;

       ……

}


三、窗口中写文字:          OnDraw中实现

用到的函数:

CString

LoadString

 

1CString

CString does not have a base class.

A CString object consists of a variable-length sequence of characters. CString provides functions and operators using a syntax similar to that of Basic. Concatenation and comparison operators, together with simplified memory management, make CString objects easier to use than ordinary character arrays.

CString is based on the TCHAR data type. If the symbol _UNICODE is defined for your program, TCHAR is defined as type wchar_t, a 16-bit character type; otherwise, it is defined as char, the normal 8-bit character type. Under Unicode, then, CString objects are composed of 16-bit characters. Without Unicode, they are composed of 8-bit char type.

When not using _UNICODE, CString is enabled for multibyte character sets (MBCS, also known as double-byte character sets, DBCS). Note that for MBCS strings, CString still counts, returns, and manipulates strings based on 8-bit characters, and your application must interpret MBCS lead and trail bytes itself.

2CString::LoadString

BOOL LoadString( UINT nID );
throw( CMemoryException );

Return Value

Nonzero if resource load was successful; otherwise 0.

Parameters

nID

A Windows string resource ID.

说明:窗口重画都是通过OnDraw(CDC* pDC){}这个函数完成,因此在OnDraw(CDC* pDC)内编写画图或者编写文本在窗口重画的时候内容才不会丢失。同时因为调用OnDraw(CDC* pDC)时会给OnDraw传递一个CDC的指针*pDC,因此可以直接使用这个pDC而不用再定义一个指针了。


1

2

3

4

5

6

7

8

9

void C_LES5_View::OnDraw(CDC* pDC)

{

       C_LES5_Doc* pDoc = GetDocument();

       ASSERT_VALID(pDoc);

       // TODO: add draw code for native data here

    CString str;

       str="我是李强,我爱我的老巫婆就象老鼠爱大米!";

       pDC->TextOut(100,100,str);

}


67行,可以改写为:CString str("我是李强,我爱我的老巫婆就象老鼠爱大米!");

8

默认分类 | 阅读 1242 次
文章评论,共0条
游客请输入验证码