译:如何使用win32 api中的edit控件

36 篇文章 0 订阅

译:如何使用 win32 api 中的 edit 控件

――――利用 Edit 类进行 windows gui 编程的方法和技巧

© Guy Lecky-Thompson

Jun 14, 2007

Article describing the Win32 edit class and how to use it as a child window control in a Windows application as a self-maintaining simple text editor.

文章描述了 win32 中的 edit 类和如何把 edit 类作为一个窗口应用程序中的子窗口控件来实现功能完备的简单的文本编辑。

Introduction

Edit controls are very useful child controls because they provide a self-contained text editing area responding correctly to all key presses. As long as they have the input focus they can handle:

Text entry

Ctrl-C, Ctrl-X, Ctrl-V & Ctrl-Z shortcuts

Scrollbars

etc.

介绍

Edit 控件是非常有用的子控件,因为它提供了一个正确的反映所有的按键的文本编辑区域。一旦它获取到输入焦点,它将拥有如下:

•文本输入

Ctrl-C, Ctrl-X, Ctrl-V & Ctrl-Z 快捷方式

•滚动条

•等等

This is part of the automatic GUI that Windows developers are grateful for, and part of the reason that Windows programming, once understood, enables the programmer to build great applications relatively easily. There are a few things to watch out for, but the reader should grasp the fundamentals as a result of reading this article.

这只是 windows 开发者非常感激自动的 GUI 的部分原因,另一部分原因是一旦理解 windows 编程,开发者就会更简单的开发出应用程序。读者需要在文章读完后懂得原理,因为有些事情需要注意。

Creating the Edit Box

There are 2 kinds of edit box:

Multiline

Single Line

创建 editbox

有两种 edit box

l 多行的

l 单行的

The difference between the 2 is that a multiline edit box can have scroll bars (managed by Windows), and the text wraps around automatically. A single line edit box is the default.

这两种 editbox 的不同之处在于多行的 editbox 拥有滚动条(被 windows 管理),并且文本会自动的换行。而单行的 editbox 则缺少这一部分。

Creating an edit box requires a simple call to CreateWindow with the class set to "edit" and the style flags set according to whether the control should have scroll bars, scroll automatically, be single or multiline, etc. One style flag to be aware of is the ES_WANTRETURN flag which tells Windows that we do not want the enter key to perform the default action, but to insert a carriage return in the edit box.

创建一个 editbox 只需要简单的调用 createwindow ,并且在 class 参数中设置为“ edit ”,风格标志位根据需要例如控件是否需要滚动条、是否自动滚动、是单行的还是多行的等等。一个众所周知的标志位 ES_WANTRETURN 告诉 windows 我们不要回车进行默认的动作,但要插入一个回车符在 editbox 中。

This is only valid for multiline edit controls, and without it, carriage returns can not be inserted by the user. A simple example of a multiline edit box (like one would use for a Notepad style application) is as follows:

这个只在多行的 edit 控件中有效,没有这个标志位,回车符将不会被用户插入进去。一个简单的多行的 edit box 例子如下所示(类似一个记事本风格的应用程序)

CreateWindow( "edit", "",

WS_VISIBLE|WS_CHILD|WS_BORDER|WS_VSCROLL|WS_HSCROLL|

ES_MULTILINE|ES_WANTRETURN|ES_AUTOHSCROLL|ES_AUTOVSCROLL,

0, 0, nClientWidth, nClientHeight, hwnd, (HMENU)nEditID, hInstance, NULL);

In the style flags, we could have omitted the WS_HSCROLL entry, but this would have created an automatically wrapping multiline edit control with no horizontal scroll bar. Without the ES_AUTOHSCROLL or ES_AUTOVSCROLL flags, we lose the automatic scroll bar interface, and also limit the text entry capabilities.

在风格标志位中,我们可以省略 WS_HSCROLL ,但是那样会创建一个自动封装了不可见的滚动条的 edit 控件。没有 ES_AUTOHSCROLLES_AUTOVSCROLL 标志位,我们会失去自动滚动条的接口,并且也少了文本输入的功能。

A simple single line edit control can be created with:

一个简单的单行的 edit 控件可以如下创建:

CreateWindow( "edit", "",

WS_VISIBLE|WS_CHILD|WS_BORDER|ES_AUTOHSCROLL|ES_AUTOVSCROLL,

0, 0, nClientWidth, nClientHeight, hwnd, (HMENU)nEditID, hInstance, NULL);

The chief differences are the lack of scroll bars, and the processing of the enter key. In both cases, the nEditID is assumed to be an identifier known to the application, the hwnd is the parent window handle, and the nClientWidth are nClientHeight are presumed to be set to useful values.

主要的不同是少了滚动条和访问回车。在两个例子中 nEditID 都被假设为应用程序所知道的标识符, hwnd 是父窗口的句柄, nClientWidthnClientHeight 都被设定为有用的值。

Getting Text from the Edit Box

The GetDlgItemText message can be used to retrieve text from the edit control:

edit box 中获取文本

GetDlgItemText 消息可以被用作从 edit 控件中获取文本。

GetDlgItemText(hwnd, nEditID, szString, nMax);

The szString parameter is an LPSTR, and nMax indicates the maximum number of characters that we can cope with. If the programmer needs to memory dynamically for this, then a WM_GETTEXTLENGTH message can be sent to the control:

szString 参数是一个 LPSTR 类型的, nMax 表示我们最大可以支持的字符个数。如果程序员需要动态的为文本分配内存, WM_GETTEXTLENGTH 消息可以被发送给控件。

SendMessage( GetDlgItem( hwnd, nEditID), WM_GETTEXTLENGTH, 0, 0L)

The return value from this is the number of characters in the text control.

返回值是 text 控件中的字符个数。

Processing Notification Messages

Finally, the edit control can update the parent window as to when the contents are about to change. This happens through the WM_COMMAND message, where the EN_CHANGE identifier is set as the low word of the WPARAM parameter. The test for this, inside the WM_COMMAND case statement, is likely to look like:

处理通知消息

最后, edit 控件当文本内容发生改变后更新父窗口。这些都是由 WM_COMMAND 消息产生,当 EN_CHANGE 被设置在 wparam 的低字节。测试一下这个消息,插入 WM_COMMAND 情况如下所示:

if (HIWORD(wParam) == EN_CHANGE && LOWORD(wParam) == nEditID)

Processing this notification allows the application to, for example, set a 'need to save' flag, or perform pre-edit control checks on the data.

举个例子,处理这个通知可以允许应用程序设置一个“需要保存”的标志或者进行一个对数据进行检查的预编辑的操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值