📄 lion-tutorial05.htm
字号:
<html>
<head>
<link rel="stylesheet" href="../../asm.css">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Iczelion's win32 asm tutorial</title>
</head>
<body bgcolor="#FFFFFF" background="../../images/back01.jpg">
<p align="center">Tutorial 5: More about Text</p>
<hr size="1">
We will experiment more with text attributes, ie. font and color.
<p>Download the example file <a href="files/tut05.zip">here</a>.
<h3> Theory:</h3>
Windows color system is based on RGB values, R=red, G=Green, B=Blue. If you want
to specify a color in Windows, you must state your desired color in terms of these
three major colors. Each color value has a range from 0 to 255 (a byte value).
For example, if you want pure red color, you should use 255,0,0. Or if you want
pure white color, you must use 255,255,255. You can see from the examples that
getting the color you need is very difficult with this system since you have to
have a good grasp of how to mix and match colors. <br>
For text color and background, you use SetTextColor and SetBkColor, both of them
require a handle to device context and a 32-bit RGB value. The 32-bit RGB value's
structure is defined as:
<p><b>RGB_value struct</b> <br>
<b> unused db 0</b> <br>
<b> blue db ?</b> <br>
<b> green db ?</b> <br>
<b> red db ?</b>
<br>
<b>RGB_value ends</b>
<p>Note that the first byte is not used and should be zero. The order of the remaining
three bytes is reversed,ie. blue, green, red. However, we will not use this
structure since it's cumbersome to initialize and use. We will create a macro
instead. The macro will receive three parameters: red, green and blue values.
It'll produce the desired 32-bit RGB value and store it in eax. The macro is
as follows:
<p><b>RGB macro red,green,blue</b> <br>
<b> xor eax,eax</b> <br>
<b> mov ah,blue</b> <br>
<b> shl eax,8</b> <br>
<b> mov ah,green</b> <br>
<b> mov al,red</b> <br>
<b>endm</b>
<p>You can put this macro in the include file for future use. <br>
You can "create" a font by calling CreateFont or CreateFontIndirect. The difference
between the two functions is that CreateFontIndirect receives only one parameter:
a pointer to a logical font structure, LOGFONT. CreateFontIndirect is the more
flexible of the two especially if your programs need to change fonts frequently.
However, in our example, we will "create" only one font for demonstration, we
can get away with CreateFont. After the call to CreateFont, it will return a
handle to a font which you must select into the device context. After that,
every text API function will use the font we have selected into the device context.
<br>
<h3> Content:</h3>
<b>.386</b> <br>
<b>.model flat,stdcall</b> <br>
<b>option casemap:none</b>
<p><b>WinMain proto :DWORD,:DWORD,:DWORD,:DWORD</b>
<p><b>include \masm32\include\windows.inc</b> <br>
<b>include \masm32\include\user32.inc</b> <br>
<b>include \masm32\include\kernel32.inc</b> <br>
<b>include \masm32\include\gdi32.inc</b> <br>
<b>includelib \masm32\lib\user32.lib</b> <br>
<b>includelib \masm32\lib\kernel32.lib</b> <br>
<b>includelib \masm32\lib\gdi32.lib</b>
<p><b>RGB macro red,green,blue</b> <br>
<b> xor eax,eax</b> <br>
<b> mov ah,blue</b> <br>
<b> shl eax,8</b> <br>
<b> mov ah,green</b> <br>
<b> mov al,red</b> <br>
<b>endm</b>
<p><b>.data</b> <br>
<b>ClassName db "SimpleWinClass",0</b> <br>
<b>AppName db "Our First Window",0</b> <br>
<b>TestString db "Win32 assembly is great and easy!",0</b> <br>
<b>FontName db "script",0</b>
<p><b>.data?</b> <br>
<b>hInstance HINSTANCE ?</b> <br>
<b>CommandLine LPSTR ?</b>
<p><b>.code</b> <br>
<b> start:</b> <br>
<b> invoke GetModuleHandle, NULL</b> <br>
<b> mov hInstance,eax</b> <br>
<b> invoke GetCommandLine<br>
mov CommandLine,eax</b><br>
<b> invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT</b>
<br>
<b> invoke ExitProcess,eax</b>
<p><b>WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD</b>
<br>
<b> LOCAL wc:WNDCLASSEX</b> <br>
<b> LOCAL msg:MSG</b> <br>
<b> LOCAL hwnd:HWND</b> <br>
<b> mov wc.cbSize,SIZEOF WNDCLASSEX</b> <br>
<b> mov wc.style, CS_HREDRAW or CS_VREDRAW</b>
<br>
<b> mov wc.lpfnWndProc, OFFSET WndProc</b> <br>
<b> mov wc.cbClsExtra,NULL</b> <br>
<b> mov wc.cbWndExtra,NULL</b> <br>
<b> push hInst</b> <br>
<b> pop wc.hInstance</b> <br>
<b> mov wc.hbrBackground,COLOR_WINDOW+1</b> <br>
<b> mov wc.lpszMenuName,NULL</b> <br>
<b> mov wc.lpszClassName,OFFSET ClassName</b>
<br>
<b> invoke LoadIcon,NULL,IDI_APPLICATION</b> <br>
<b> mov wc.hIcon,eax</b> <br>
<b> mov wc.hIconSm,eax</b> <br>
<b> invoke LoadCursor,NULL,IDC_ARROW</b> <br>
<b> mov wc.hCursor,eax</b> <br>
<b> invoke RegisterClassEx, addr wc</b> <br>
<b> invoke CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\</b>
<br>
<b> WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,\</b>
<br>
<b> CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,\</b>
<br>
<b> hInst,NULL</b>
<br>
<b> mov hwnd,eax</b> <br>
<b> invoke ShowWindow, hwnd,SW_SHOWNORMAL</b> <br>
<b> invoke UpdateWindow, hwnd</b> <br>
<b> .WHILE TRUE</b> <br>
<b>
invoke GetMessage, ADDR msg,NULL,0,0</b> <br>
<b>
.BREAK .IF (!eax)</b> <br>
<b>
invoke TranslateMessage, ADDR msg</b> <br>
<b>
invoke DispatchMessage, ADDR msg</b> <br>
<b> .ENDW</b> <br>
<b> mov eax,msg.wParam</b> <br>
<b> ret</b> <br>
<b>WinMain endp</b>
<p><b>WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM</b> <br>
<b> LOCAL hdc:HDC</b> <br>
<b> LOCAL ps:PAINTSTRUCT</b> <br>
<b> LOCAL hfont:HFONT</b>
<p><b> .IF uMsg==WM_DESTROY</b> <br>
<b> invoke PostQuitMessage,NULL</b>
<br>
<b> .ELSEIF uMsg==WM_PAINT</b> <br>
<b> invoke BeginPaint,hWnd, ADDR ps</b>
<br>
<b> mov hdc,eax</b>
<br>
<b> invoke CreateFont,24,16,0,0,400,0,0,0,OEM_CHARSET,\</b>
<br>
<b>
OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,\</b> <br>
<b>
DEFAULT_QUALITY,DEFAULT_PITCH or FF_SCRIPT,\</b> <br>
<b>
ADDR FontName</b> <br>
<b> invoke SelectObject, hdc, eax</b>
<br>
<b> mov hfont,eax</b>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -