WIN32汇编: 5.学习更多的关于文本的知识[1]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 goddragon 的 blog

第五课 学习更多关于“绘制”文本串的知识


我们将做更多的实践去了解有关文本的诸多属性如字体和颜色等。

理论:

windows 的颜色系统是用rgb值来表示的,r 代表红色,g 代表绿色,b 代表兰色。如果您想指定一种颜色就必须给该颜色赋相关的 rgb 值,rgb 的取值范围都是从 0 到 255,譬如您想要得到纯红色,就必须对rgb赋值(255,0,0),纯白色是 (255,255,255)。从我们下面的例子中您可以看出来要想运用好这套基于数字的颜色系统并不容易,这要求您必须对混色和颜色匹配有良好的感觉。

您可以用函数 settextcolor 和 setbkcolor 来“绘制”背景色和字符颜色,但是必须传递一个“设备环境”的句柄和 rgb 值作为参数。rgb 的结构体的定义如下:

rgb_value struct
unused db 0
blue db ?
green db ?
red db ?
rgb_value ends

其中第一字节为 0 而且始终为 0,其它三个字节分别表示兰色、绿色和红色,刚好和 rgb 的次序相反。这个结构体用起来挺别扭,所以我们重新定义一个宏用它来代替。该宏接收红绿蓝三个参数,并在 eax 寄存器中返回 32 位的 rgb 值,宏的定义如下:

rgb macro red,green,blue
xor eax,eax
mov ah,blue
shl eax,8
mov ah,green
mov al,red
endm

您可以把该宏放到头文件中以方便使用。

您可以调用 createfont 和 createfontindirect 来创建自己的字体,这两个函数的差别是前者要求 您传递一系列的参数,而后着只要传递一个指向 logfont 结构的指针。这样就使得后者使用起来更方便,尤其当您需要频繁创建字体时。在我们的例子中由于只要创建一种字体,故用 createfont 就足够了。在调用该函数后会返回所创建的字体的句柄,然后把该句柄选进“设备环境”使其成为当前字体,随后所有的“绘制”文本串的函数在被调用时都要把该句柄作为一个参数传递

例子:

.386
.model flat,stdcall
option casemap:none

winmain proto :dword,:dword,:dword,:dword

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\gdi32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\gdi32.lib

rgb macro red,green,blue
        xor eax,eax
        mov ah,blue
        shl eax,8
        mov ah,green
        mov al,red
endm

.data
classname db "simplewinclass",0
appname  db "our first window",0
teststring  db "win32 assembly is great and easy!",0
fontname db "script",0

.data?
hinstance hinstance ?
commandline lpstr ?

.code
 start:
    invoke getmodulehandle, null
    mov    hinstance,eax
    invoke getcommandline
    mov commandline,eax
    invoke winmain, hinstance,null,commandline, sw_showdefault
    invoke exitprocess,eax

winmain proc hinst:hinstance,hprevinst:hinstance,cmdline:lpstr,cmdshow:dword
    local wc:wndclassex
    local msg:msg
    local hwnd:hwnd
    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,null
    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
    .while true
                invoke getmessage, addr msg,null,0,0
                .break .if (!eax)
                invoke translatemessage, addr msg
                invoke dispatchmessage, addr msg

本文关键:asm
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top