📄 tut33.html
字号:
adds more text styles such as font weight, spacing,text background color, kerning,
etc. If you don't need these extra features, simply use <font color="#006666"><b>CHARFORMAT</b></font>.
</font></p>
<p><font face="Tahoma" size="-1">To set text formatting, you have to think about
the range of text you want to apply to. Richedit control introduces the notion
of character text range. Richedit control gives each character a number starting
from 0: the first characterin the control has Id of 0, the second character
1 and so on. To specify a text range, you must give the richedit control two
numbers: the IDs of the first and the last character of the range. To apply
the text formatting with <font color="#000066"><b>EM_SETCHARFORMAT</b></font>,
you have at most three choices: </font></p>
<ol>
<li><font face="Tahoma" size="-1">Apply to all text in the control (<font color="#006666"><b>SCF_ALL</b></font>)</font></li>
<li><font face="Tahoma" size="-1">Apply to the text currently in selection (<font color="#006666"><b>SCF_SELECTION</b></font>)</font></li>
<li><font face="Tahoma" size="-1">Apply to the whole word currently in selection
(<font color="#006666"><b>SCF_WORD</b></font> or <font color="#006666"><b>SCF_SELECTION</b></font>)</font></li>
</ol>
<p><font face="Tahoma" size="-1">The first and the second choices are straightforward.
The last choice requires a little explanation. If the current selection only
covers one or more of the characters in the word but not the whole word, specifying
the flag <font color="#006666"><b>SCF_WORD</b></font>+<font color="#006666"><b>SCF_SELECTION</b></font>
applies the text formatting to the whole word. Even if there is no current selection,
ie, only the caret is positioned in the word, the third choice also applies
the text formatting to the whole word.</font></p>
<p><font face="Tahoma" size="-1">To use <font color="#006666"><b>EM_SETCHARFORMAT</b></font>,
you need to fill several members of <font color="#006666"><b>CHARFORMAT</b></font>
(or <font color="#006666"><b>CHARFORMAT2</b></font>) structure. For example,
if we want to set the text color, we will fill the <font color="#006666"><b>CHARFORMAT</b></font>
structure as follows:</font></p>
<pre><font face="Tahoma"><b><font color="#000066">.data?
cf CHARFORMAT <>
....
.code
mov cf.cbSize,sizeof cf
mov cf.dwMask,CFM_COLOR
mov cf.crTextColor,0FF0000h
invoke SendMessage,hwndRichEdit,EM_SETCHARFORMAT,SCF_ALL,addr cf</font></b></font></pre>
<p><font face="Tahoma" size="-1">The above code snippet sets the text color of
the richedit control to pure blue. Note that if there is no text in the richedit
control when <font color="#006666"><b>EM_SETCHARFORMAT</b></font> is issued,
the text entered into the richedit control following the message will use the
text formatting specified by the<font color="#006666"><b> EM_SETCHARFORMAT</b></font>
message.<br>
</font></p>
<h3><font face="Times New Roman, Times, serif" color="#000099">Setting the text/saving
the text</font></h3>
<p><font face="Tahoma" size="-1">For those of you who are used to edit control,
you'll surely be familiar with <font color="#006666"><b>WM_GETTEXT</b></font>/<font color="#006666"><b>WM_SETTEXT</b></font>
as the means to set the text/get the text to/from the control. This method still
works with richedit control but may not be efficient if the file is large. Edit
control limits the text that can be entered into it to 64K but richedit control
can accept text much larger than that. It would be very cumbersome to allocate
a very large block of memory (such as 10 MB or so) to receive the text from
<font color="#006666"><b>WM_GETTEXT</b></font>. Richedit control offers a new
approach to this method, ie. text streaming.</font></p>
<p><font face="Tahoma" size="-1">To put it simply, you provide the address of
a callback function to the richedit control. And richedit control will call
that callback, passing the address of the buffer to it, when it's ready. The
callback will fill the buffer with the data it wants to send to the control
or read the data from the buffer and then waits for the next call until the
operation is finished. This paradigm is used for both streaming in (setting
the text) and streaming out (getting the text out of the control). You'll see
that this method is more efficient: the buffer is provided by the richedit control
itself so the data are divided into chunks. The operations involve two messages:<font color="#006666"><b>
EM_STREAMIN</b></font> and <font color="#006666"><b>EM_STREAMOUT</b></font></font></p>
<p><font face="Tahoma" size="-1">Both <font color="#006666"><b>EM_STREAMIN </b></font>and
<font color="#006666"> <b>EM_STREAMOUT</b></font> use the same syntax:</font></p>
<p><font face="Tahoma" size="-1"> <font color="#0000FF"><b>wParam</b></font> ==
formatting options.</font></p>
<table cellpadding="3" align="center" border="1">
<tr bgcolor="#FFFFCC">
<td><b><font face="MS Sans Serif" size="-1">SF_RTF</font></b></td>
<td><font face="MS Sans Serif" size="-1">The data is in the rich-text format
(RTF) </font></td>
</tr>
<tr bgcolor="#FFFFCC">
<td><b><font face="MS Sans Serif" size="-1">SF_TEXT</font></b></td>
<td><font face="MS Sans Serif" size="-1">The data is in the plain text format</font></td>
</tr>
<tr bgcolor="#FFFFCC">
<td><b><font face="MS Sans Serif" size="-1">SFF_PLAINRTF</font></b></td>
<td><font face="MS Sans Serif" size="-1">Only the keywords common to all languages
are streamed in.</font></td>
</tr>
<tr bgcolor="#FFFFCC">
<td><b><font face="MS Sans Serif" size="-1">SFF_SELECTION</font></b></td>
<td><font face="MS Sans Serif" size="-1">If specified, the target of the operation
is the text currently in selection. If you stream the text in, the text
replaces the current selection. If you stream the text out, only the text
currently in selection is streamed out. If this flag is not specified, the
operation affects the whole text in the control.</font></td>
</tr>
<tr bgcolor="#FFFFCC">
<td><b><font face="MS Sans Serif" size="-1">SF_UNICODE</font></b></td>
<td><font face="MS Sans Serif" size="-1">(Available on RichEdit 2.0 or later)
Specify the unicode text.</font></td>
</tr>
</table>
<p><font face="Tahoma" size="-1"> <font color="#0000FF"><b>lParam</b></font> ==
point to an <font color="#006666"><b>EDITSTREAM</b></font> structure which has
the following definition:</font><font face="Tahoma" size="-1"></font></p>
<pre><font face="Tahoma"> <font color="#006666"><b>EDITSTREAM STRUCT
dwCookie DWORD ?
dwError DWORD ?
pfnCallback DWORD ?
EDITSTREAM ENDS</b></font></font><font face="Tahoma" size="-1"></font></pre>
<table cellpadding="3" align="center" border="1">
<tr bgcolor="#FFFFCC">
<td><b><font face="MS Sans Serif" size="-1">dwCookie</font></b></td>
<td><font face="MS Sans Serif" size="-1">application-defined value that will
be passed to the callback function speficied in <font color="#000066"><b>pfnCallback</b></font>
member below. We normally pass some important value to the callback function
such as the file handle to use in the stream-in/out procedure.</font></td>
</tr>
<tr bgcolor="#FFFFCC">
<td><b><font face="MS Sans Serif" size="-1">dwError</font></b></td>
<td><font face="MS Sans Serif" size="-1">Indicates the results of the stream-in
(read) or stream-out (write) operation. A value of zero indicates no error.
A nonzero value can be the return value of the EditStreamCallback function
or a code indicating that the control encountered an error. </font></td>
</tr>
<tr bgcolor="#FFFFCC">
<td><b><font face="MS Sans Serif" size="-1">pfnCallback</font></b></td>
<td><font face="MS Sans Serif" size="-1">Pointer to an EditStreamCallback
function, which is an application-defined function that the control calls
to transfer data. The control calls the callback function repeatedly, transferring
a portion of the data with each call</font></td>
</tr>
</table>
<p><font face="Tahoma" size="-1">The editstream callback function has the following
definition:</font></p>
<pre><font face="Tahoma"> <b><font color="#990099">EditStreamCallback</font> proto <font color="#CC00CC">dwCookie</font>:DWORD,<br> <font color="#CC00CC">pBuffer</font>:DWORD,
<font color="#CC00CC">NumBytes</font>:DWORD,
<font color="#CC00CC">pBytesTransferred</font>:DWORD</b></font></pre>
<p><font face="Tahoma" size="-1">You have to create a function with the above
prototype in your program. And then pass its address to <font color="#006666"><b>EM_STREAMIN</b></font>
or <font color="#006666"><b>EM_STREAMOUT</b></font> via <font color="#006666"><b>EDITSTREAM</b></font>
structure.</font></p>
<p><font face="Tahoma" size="-1">For stream-in operation (settting the text in
the richedit control)</font><font face="Tahoma">:</font></p>
<pre><font face="Tahoma"> <font color="#660066"><b><font color="#990099">dwCookie</font></b></font>: the application-defined value you pass to <font color="#006666"><b>EM_STREAMIN</b></font> via <font color="#006666"><b>EDITSTREAM</b></font> structure. We almost always
pass the file handle of the file we want to set its content to the control here.</font><font face="Tahoma">
<font color="#990099"><b>pBuffer</b></font>: points to the buffer provided by the richedit control that will receive the text from your callback function.
<font color="#990099"><b>NumBytes</b></font>: the maximum number of bytes you can write the the buffer (pBuffer) in this call. You <font color="#000099"><b>MUST</b></font> always obey this limit, ie, you can send
less data than the value in NumBytes but must not send more data than this value. You can think of this value as the size
of the buffer in pBuffer.
<b><font color="#990099">pBytesTransferred</font></b>: points to a dword that you must set the value indicating the number of bytes you actually transferred to the buffer.
This value is usually identical to the value in <font color="#990099"><b>NumBytes</b></font>. The exception is when the data is to send is less than
the size of the buffer provided such as when the end of file is reached.</font></pre>
<p><font face="Tahoma"><font size="-1"> For stream-out operation (getting the
text out of the richedit control):</font></font><font face="Tahoma"><font size="-1"></font></font><font face="Tahoma"><font size="-1"></font></font></p>
<pre><font face="Tahoma"> <font color="#FFFFFF"><b><font color="#990099">dwCookie</font></b></font>: Same as the stream-in operation. We usually pass the file handle we want to write the data to in this parameter.
<font color="#990099"><b>pBuffer</b></font>: points to the buffer provided by the richedit control that is filled with the data from the richedit control.
To obtain its size, you must examine the value of <font color="#990099"><b>NumBytes</b></font>. <font color="#990099"><b>
NumBytes</b></font>: the size of the data in the buffer pointed to by pBuffer.
<font color="#990099"><b>pBytesTransferred</b></font>: points to a dword that you must set the value indicating the number of bytes you actually read from the buffer.</font><font face="Tahoma"><font size="-1"></font></font></pre>
<p><font face="Tahoma" size="-1">The callback function returns 0 to indicate success
and richedit control will continue calling the callback function if there is
still data left to read/write. If some error occurs during the process and you
want to stop the operation, returns a non-zero value and the richedit control
will discard the data pointed to by pBuffer. The error/success value will be
filled in the <font color="#006666"><b>dwError</b></font> field of <font color="#990000"><b>EDITSTREAM</b></font>
so you can examine the error/success status of the stream operation after <font color="#006666"><b>SendMessage</b></font>
returns. </font><font face="Tahoma"><font size="-1"></font></font></p>
<h3><font face="Times New Roman, Times, serif" color="#0000CC">Example:</font></h3>
<p><font face="Tahoma" size="-1">The example below is a simple editor which you
can open an asm source code file, edit and save it. It uses RichEdit control
version 2.0 or above.</font></p>
<pre align="left"><b><font face="Tahoma">.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\comdlg32.inc
include \masm32\include\gdi32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\comdlg32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
.const
IDR_MAINMENU equ 101
IDM_OPEN equ 40001
IDM_SAVE equ 40002
IDM_CLOSE equ 40003
IDM_SAVEAS equ 40004
IDM_EXIT equ 40005
IDM_COPY equ 40006
IDM_CUT equ 40007
IDM_PASTE equ 40008
IDM_DELETE equ 40009
IDM_SELECTALL equ 40010
IDM_OPTION equ 40011
IDM_UNDO equ 40012
IDM_REDO equ 40013
IDD_OPTIONDLG equ 101
IDC_BACKCOLORBOX equ 1000
IDC_TEXTCOLORBOX equ 1001
RichEditID equ 300
.data
ClassName db "<font color="#0000FF">IczEditClass</font>",0
AppName db "<font color="#0000FF">IczEdit version 1.0</font>",0
RichEditDLL db "<font color="#0000FF">riched20.dll</font>",0
RichEditClass db "<font color="#0000FF">RichEdit20A</font>",0
NoRichEdit db "<font color="#0000FF">Cannot find riched20.dll</font>",0
ASMFilterString db "<font color="#0000FF">ASM Source code (*.asm)</font>",0,"*<font color="#0000FF">.asm</font>",0
db "<font color="#0000FF">All Files (*.*)</font>",0,"<font color="#0000FF">*.*</font>",0,0
OpenFileFail db "<font color="#0000FF">Cannot open the file</font>",0
WannaSave db "<font color="#0000FF">The data in the control is modified. Want to save it?</font>",0
FileOpened dd FALSE
BackgroundColor dd 0FFFFFFh <font color="#006666">; default to white</font>
TextColor dd 0 <font color="#006666">; default to black</font>
.data?
hInstance dd ?
hRichEdit dd ?
hwndRichEdit dd ?
FileName db 256 dup(?)
AlternateFileName db 256 dup(?)
CustomColors dd 16 dup(?)
.code
start:
invoke GetModuleHandle, NULL
mov hInstance,eax
<font color="#FF0033"> invoke LoadLibrary,addr RichEditDLL</font>
.if eax!=0
mov hRichEdit,eax
invoke WinMain, hInstance,0,0, SW_SHOWDEFAULT
invoke FreeLibrary,hRichEdit
.else
invoke MessageBox,0,addr NoRichEdit,addr AppName,MB_OK or MB_ICONERROR
.endif
invoke ExitProcess,eax
WinMain proc hInst:DWORD,hPrevInst:DWORD,CmdLine:DWORD,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:DWORD
mov wc.cbSize,SIZEOF WNDCLASSEX
mov wc.style, CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc, OFFSET WndProc
mov wc.cbClsExtra,NULL
mov wc.cbWndExtra,NULL
push hInst
pop wc.hInstance
mov wc.hbrBackground,COLOR_WINDOW+1
mov wc.lpszMenuName,IDR_MAINMENU
mov wc.lpszClassName,OFFSET ClassName
invoke LoadIcon,NULL,IDI_APPLICATION
mov wc.hIcon,eax
mov wc.hIconSm,eax
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax
invoke RegisterClassEx, addr wc
INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,\
hInst,NULL
mov hwnd,eax
invoke ShowWindow, hwnd,SW_SHOWNORMAL
invoke UpdateWindow, hwnd
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -