如何使用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
?多行的
?单行的
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_AUTOHSCROLL和ES_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是父窗口的句柄,nClientWidth和nClientHeight都被设定为有用的值。
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 tomemory 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.
举个例子,处理这个通知可以允许应用程序设置一个“需要保存”的标志或者进行一个对数据进行检查的预编辑的操作。
来源 译:如何使用win32 api中的edit控件_无知之无所不知_百度空间