Tutorials to .com

Tutorials to .com » Software » Asm » WIN32 compilation: 5. To learn more knowledge on the text

WIN32 compilation: 5. To learn more knowledge on the text

Print View , by: iSee ,Total views: 14 ,Word Count: 1617 ,Date: Mon, 11 May 2009 Time: 2:28 AM

Fifth lesson to learn more about the "mapping" knowledge of the text strings

We will do more practice to get acquainted with the text of a number of attributes such as fonts and colors.


Windows color system is used to indicate the RGB values, R on behalf of the red, G on behalf of Green, B on behalf of blue. If you want to specify a color to be given to the related RGB color value, RGB value ranges from 0-255, for example, you want to be pure red, it is necessary to assign to the RGB (255,0,0 ), pure white is (255,255,255). From our examples below you can see in order to make good use of this system based on the number of colors is not easy, it requires you to be blending and color matching of a good feeling.

You can use SetTextColor and SetBkColor functions to "draw" the background color and character color, but must be communicated to a "device context" of the handle and the RGB values of the parameters. RGB definition of the structure is as follows:

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

The first byte is always 0 and 0, the other three bytes, respectively, blue, green and red, and RGB is just the opposite order. This structure very awkward to use, so we re-define a macro to replace it. The RGB macro to receive three parameters, and return in eax register 32-bit RGB value, the definition of macros as follows:

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

You can put the macro in the header files to facilitate use.

CreateFont and you can call CreateFontIndirect to create their own fonts, the difference between these two functions is that the former requires you to pass a series of parameters, and then just send a point to the LOGFONT structure pointer. This makes the latter more convenient to use, especially when you need to create a font frequent. In our example as long as the creation of a font as a result, it is sufficient to use CreateFont. After calling this function will return the handle to create a font, and then elected to the handle, "Environment" into the current font, and then all of the "draw" the text string in the function to be called when the Handle as a parameter


. 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

. 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
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
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
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, \
hInst, NULL
mov hwnd, eax
invoke ShowWindow, hwnd, SW_SHOWNORMAL
invoke UpdateWindow, hwnd
invoke GetMessage, ADDR msg, NULL, 0,0
. BREAK. IF (! Eax)
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
mov eax, msg.wParam
WinMain endp

WndProc proc hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM

invoke PostQuitMessage, NULL
invoke BeginPaint, hWnd, ADDR ps
mov hdc, eax
invoke CreateFont, 24,16,0,0,400,0,0,0, OEM_CHARSET, \
ADDR FontName
invoke SelectObject, hdc, eax
mov hfont, eax
RGB 200,200,50
invoke SetTextColor, hdc, eax
RGB 0,0,255
invoke SetBkColor, hdc, eax
invoke TextOut, hdc, 0,0, ADDR TestString, SIZEOF TestString
invoke SelectObject, hdc, hfont
invoke EndPaint, hWnd, ADDR ps
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
xor eax, eax
WndProc endp

end start


CreateFont logic functions to generate a font, it as close as possible to the parameters specified in the relevant value. This function is probably all the Windows API function parameters brought the most. It returns a handle to logical font to use for the call SelectObject function. We elaborate on the following parameters for this function:

CreateFont proto \
nHeight: DWORD, \
nWidth: DWORD, \
nEscapement: DWORD, \
nOrientation: DWORD, \
nWeight: DWORD, \
cItalic: DWORD, \
cUnderline: DWORD, \
cStrikeOut: DWORD, \
cCharSet: DWORD, \
cOutputPrecision: DWORD, \
cClipPrecision: DWORD, \
cQuality: DWORD, \
cPitchAndFamily: DWORD, \
lpFacename: DWORD

nHeight: want to use the font height, 0 for default.
nWidth: want to use the width of the font, under normal circumstances is best to use 0, so that Windows will automatically select one for you and a high degree of matching value. Because in our example, then do make the characters can not be displayed because of too small, so we set it to 16.
nEscapement: the relative character of each character of the previous rotation, the general set to 0.900 to 90 degrees on behalf of, 1800 to 190 degrees to 270 degrees 2700.
nOrientation: the direction of the font.
nWeight: the font size of the stroke.

Windows for our pre-defined value as follows:

FW_DONTCARE equal to 0
FW_EXTRALIGHT equivalent to 200
FW_ULTRALIGHT equivalent to 200
FW_LIGHT equivalent to 300
FW_NORMAL equivalent to 400
FW_REGULAR equivalent to 400
FW_MEDIUM equivalent to 500
FW_SEMIBOLD equivalent to 600
FW_DEMIBOLD equivalent to 600
FW_BOLD equivalent to 700
FW_EXTRABOLD equivalent to 800
FW_ULTRABOLD equivalent to 800
FW_HEAVY equivalent to 900
FW_BLACK equivalent to 900

cItalic: 0 for normal, other values are in italics.
cUnderline: 0 for normal, other values are underlined.
cStrikeOut: 0 for normal, delete the lines for other values.
cCharSet: character set fonts. General choice OEM_CHARSET, it allows the Windows operating system will be selected and related character sets.
cOutputPrecision: we have chosen to specify the font closer to the true accuracy of the font. General selection OUT_DEFAULT_PRECIS, it determines the way the default mapping.
cClipPrecision: We chose the font specified in the region beyond the cutting accuracy when cutting. General selection CLIP_DEFAULT_PRECIS, it determines the precision cutting.
cQuality: specify the quality of the output font. It should be noted that the GDI, as far as possible close to the real font, a total of three ways: DEFAULT_QUALITY, PROOF_QUALITY and DRAFT_QUALITY.
cPitchAndFamily: font and font family.
lpFacename: specify the name of the font.

Described above is not necessarily a good understanding, if you want more information, should refer to WIN32 API Guide.

invoke SelectObject, hdc, eax
mov hfont, eax

We get a point in the logic of the handle font SelectObject function call after it chose to enter the "Environment", we can also call to the function such as color, pens, paint brushes and other GDI objects elected "equipment environment." The function will return to an old "equipment environment" handle. You must save the handle in order to complete the "mapping" the work of the election and then put it back. After SelectObject call function in the mapping function are all against the "equipment environment".

RGB 200,200,50
invoke SetTextColor, hdc, eax
RGB 0,0,255
invoke SetBkColor, hdc, eax

We have a macro RGB color, then call the SetTextColor and SetBkColor.

invoke TextOut, hdc, 0,0, ADDR TestString, SIZEOF TestString

TextOut call our customer area in front of our fonts and colors selected "draw" the text string.

invoke SelectObject, hdc, hfont

In our "map" is completed, must be restored "equipment environment." We must do this each time.

Assembly Language Tutorial Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.