;运行效果
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; win32汇编环境,窗口程序中设置RichEdit内文本的字体
; 代码抄进radasm里面,可以直接编译运行。主要的部分加了备注,研究一下就明白了。
;以下为asm文件
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.386
.model flat,stdcall
option casemap:none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Include 文件定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include windows.inc
include gdi32.inc
includelib gdi32.lib
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 数据段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data?
hInstance dd ?
hWinMain dd ?
hMainRichEdit dd ? ;加载richedit的Dll文件的变量
hRichEdit01 dd ? ;richedit控件句柄
.const
szClassName db 'MyClass',0
szCaptionMain db '窗口程序中设置RichEdit控件本文的字体',0
szHello01 db 13,10 ;13是回车的值,10是换行的值
db ' 泊船瓜洲',13,10
db ' 京口瓜洲一水间,',13,10
db ' 钟山只隔数重山。',13,10
db ' 春风又绿江南岸,',13,10
db ' 明月何时照我还。',13,10,0
.data
szDllEdit db 'RichEd20.dll',0 ;先自定义字符串,也是版本号
;以下为控件使用的类名
RichEditClassName db "RichEdit20A",0 ;先自定义字符串
szFontName db "华文隶书",0 ;也可换成常用字体,查WORD里面有
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
RGB macro red,green,blue ;设个颜色宏,后面方便调用
xor eax,eax
mov ah,blue
shl eax,8
mov ah,green
mov al,red
endm
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 代码段
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; 窗口过程
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcWinMain proc uses ebx edi esi hWnd,uMsg,wParam,lParam
local @stPs:PAINTSTRUCT
local @stRect:RECT
local @hDc
LOCAL @cf01:CHARFORMAT ;CHARFORMAT是文本格式结构
LOCAL @szBuffer[32]:byte
mov eax,uMsg
;********************************************************************
.if eax == WM_CREATE
invoke LoadLibrary,offset szDllEdit ;需要先加载dll文件
invoke CreateWindowEx, NULL, addr RichEditClassName, NULL, ES_MULTILINE + WS_CHILD + ES_READONLY+ \
WS_VISIBLE + WS_BORDER + WS_VSCROLL + WS_TABSTOP, 10,10,280,430,hWnd, NULL, hInstance, NULL
mov hRichEdit01, eax ;创建richedit控件
.elseif eax == WM_PAINT
invoke BeginPaint,hWnd,addr @stPs
mov @hDc,eax
;刷新客户区的代码
RGB 85,107,47 ;可调3个值,以此得到不同的色。就是红绿蓝三色合成的色
invoke SendMessage,hRichEdit01,EM_SETBKGNDCOLOR,0,eax ;设置背景色
;以下在richedit控件里添加字符串
mov @cf01.cbSize,sizeof @cf01 ;确定CHARFORMAT结构所占字节大小,好分配内存空间给@cf01
mov @cf01.dwMask,CFM_COLOR + CFM_SIZE + CFM_OFFSET + CFM_FACE ;使下面各个要输入的参数有效,具体针对哪项可上网搜资料
RGB 255,255,255 ;调用RGB颜色宏
mov @cf01.crTextColor,eax ;eax是通过上面颜色宏得到的颜色值
mov @cf01.yHeight,400 ;字符的宽度,就是大小
mov @cf01.yOffset,200 ;离基线的距离,正值为往上移,负值为往下移,调整字符的上下位置用
invoke lstrcpy,addr @cf01.szFaceName,addr szFontName ;设置字体
invoke SendMessage,hRichEdit01,EM_SETCHARFORMAT,SCF_ALL,addr @cf01 ;要先发送格式,再发送文本
invoke SendMessage,hRichEdit01,WM_SETTEXT,FALSE,addr szHello01 ;把文本发送给控件
;在richedit控件里添加字符串结束
invoke EndPaint,hWnd,addr @stPs
;********************************************************************
.elseif eax == WM_CLOSE
invoke DestroyWindow,hWinMain
invoke FreeLibrary,hMainRichEdit ;结束前释放dll文件
invoke PostQuitMessage,NULL
;********************************************************************
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
;********************************************************************
xor eax,eax
ret
_ProcWinMain endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_WinMain proc
local @stWndClass:WNDCLASSEX
local @stMsg:MSG
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke RtlZeroMemory,addr @stWndClass,sizeof @stWndClass
;********************************************************************
; 注册窗口类
;********************************************************************
invoke LoadCursor,0,IDC_ARROW
mov @stWndClass.hCursor,eax
push hInstance
pop @stWndClass.hInstance
mov @stWndClass.cbSize,sizeof WNDCLASSEX
mov @stWndClass.style,CS_HREDRAW or CS_VREDRAW
mov @stWndClass.lpfnWndProc,offset _ProcWinMain
mov @stWndClass.hbrBackground,COLOR_WINDOW + 1
mov @stWndClass.lpszClassName,offset szClassName
invoke RegisterClassEx,addr @stWndClass
;********************************************************************
; 建立并显示窗口
;********************************************************************
invoke CreateWindowEx,WS_EX_CLIENTEDGE,offset szClassName,offset szCaptionMain,\
WS_MINIMIZEBOX or WS_CAPTION or WS_SYSMENU,\
100,100,310,490,\
NULL,NULL,hInstance,NULL
mov hWinMain,eax
invoke ShowWindow,hWinMain,SW_SHOWNORMAL
invoke UpdateWindow,hWinMain
;********************************************************************
; 消息循环
;********************************************************************
.while TRUE
invoke GetMessage,addr @stMsg,NULL,0,0
.break .if eax == 0
invoke TranslateMessage,addr @stMsg
invoke DispatchMessage,addr @stMsg
.endw
ret
_WinMain endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
call _WinMain
invoke ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end start