;运行效果
;win32汇编环境,对多行编辑框添加或删除文本
;主要要先设置文本的开始点与结束点,然后把一段文本顶替上去。没有添加文本或删除文本的概念,只有顶替。如果开始点与结束点都是前面文本的长度值,则成了从后面添加文本的效果。如果结束点大于开始点,则变成删除
;直接抄进RadAsm可编译运行。重点部分加备注。
;下面为asm文件
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.386
.model flat,stdcall
option casemap:none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include windows.inc
include user32.inc
include kernel32.inc
includelib user32.lib
includelib kernel32.lib
; 自定义函数声明;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DlgProc proto :DWORD,:DWORD,:DWORD,:DWORD ;对话框窗口函数
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data
DlgName db "MyDialog",0
szTitle db "提示",0
szHello01 db "我是后面添加的字符串",0
.data?
hInstance HINSTANCE ?
hEdit01 dd ?
.const
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Equ 等值定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ICO_MAIN equ 1000 ;图标
ID_EDIT01 equ 11 ;编辑框控件标识符
IDB01 equ 21 ;按钮控件标识符
IDB02 equ 22
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
start:
invoke GetModuleHandle, NULL
mov hInstance,eax
invoke DialogBoxParam, hInstance, ADDR DlgName,NULL, addr DlgProc, NULL
invoke ExitProcess,eax
DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL @szBuffer[1024]:byte
LOCAL @szBuffer01[1024]:byte
LOCAL @AllCount
.if uMsg == WM_INITDIALOG
invoke LoadIcon,hInstance,ICO_MAIN ;加载图标
invoke SendMessage,hWnd,WM_SETICON,ICON_BIG,eax
invoke GetDlgItem,hWnd,ID_EDIT01
mov hEdit01,eax
.elseif uMsg == WM_COMMAND
mov eax,wParam
.if ax == IDB01 ;添加文本操作
invoke GetWindowTextLength,hEdit01
mov @AllCount,eax
invoke SendMessage,hEdit01,EM_SETSEL ,@AllCount,@AllCount ;后面两个参数为所选文本的开始点与结束点,如果把它们都置为前面所有文本的长度值,则从前面文本的最后一个字符开始,即成为添加文本的操作
;invoke SendMessage,hEdit01,EM_SETSEL ,-1,-1 ;把上句换成此句,效果一样。-1的值指全部文本。
invoke SendMessage,hEdit01,EM_REPLACESEL,FALSE,addr szHello01 ;把第一段文本发送给控件,EM_REPLACESEL是顶替所选文本内容的操作,严格上来说不是添加文本,而是代替。只不过代替的地方是最后字符的后面。但同样实现了目地。
.elseif ax == IDB02 ;删除文本操作
invoke GetWindowTextLength,hEdit01
mov @AllCount,eax
invoke lstrlen,addr szHello01
mov ebx,eax
mov eax,@AllCount
sub eax,ebx
invoke SendMessage,hEdit01,EM_SETSEL ,eax,@AllCount
invoke SendMessage,hEdit01,EM_REPLACESEL,FALSE,0
.endif
.elseif uMsg == WM_CLOSE
invoke EndDialog, hWnd,NULL
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
DlgProc endp
end start
;下面为rc文件内容
#include "resource.h" //提示缺少该文件,可以在资源里下载
#define ICO_MAIN 1000 //图标
#define ID_EDIT01 11 //编辑框标识符
#define IDB01 21 //按钮标识符
#define IDB02 22
ICO_MAIN ICON "Main.ico"
//定义对话框
MyDialog DIALOG 10, 10, 140, 140
STYLE DS_CENTER | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE | WS_OVERLAPPED | DS_MODALFRAME | DS_3DLOOK
CAPTION "对话框程序模版"
BEGIN
CONTROL "我是多行编辑框,按回车可以换行",ID_EDIT01,"Edit",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP|ES_MULTILINE|ES_WANTRETURN|ES_AUTOVSCROLL|WS_VSCROLL,10, 10, 120, 70,WS_EX_CLIENTEDGE //设置成多行编辑框,按回车时加回车符
CONTROL "添加一段文字至编辑框",IDB01,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,10,90,120,14
CONTROL "把刚才添加的内容删了",IDB02,"Button",WS_CHILDWINDOW|WS_VISIBLE|WS_TABSTOP,10,110,120,14
END