Win32 assembly: 5. Learn more knowledge about text

zhaozj2021-02-17  62

Fifth lesson learning more about "drawing" knowledge of text strings

We will do more practices to understand many properties such as fonts and colors.

theory:

Windows coloring system is represented by RGB value, R represents red, G represents green, b represents a blue. If you want to specify a color, you must give the color of the RGB value, RGB's value range from 0 to 255, such as you want to get pure red, you must assign RGB (255, 0, 0) Pure white is (255, 255, 255). From the example below you can see if you want to use this set of digital-based color systems, it is not easy, which requires you to have a good feeling of mixing colors and colors.

You can "draw" background color and character colors with functions setTextColor and SetBKColor, but must pass a "device environment" handle and RGB value as parameters. The definition of the RGB structure is as follows:

RGB_VALUE STRUCTUNUSED DB 0BLUE DB? Green DB? Red DB? RGB_VALUE ENDS

The first byte is 0 and always 0, and the other three bytes indicate linear, green, and red, just in the order of the RGB. This structure is very tapped, so we redefine a macro replace it. The macro receives three parameters of red and green, and returns 32-bit RGB values ​​in the EAX register. The macro is defined as follows:

RGB Macro Red, Green, Bluexor Eax, Eaxmov Ah, Blueshl Eax, 8mov Ah, Greenmov Al, Redendm

You can put this macro into the header file for easy use.

You can call CREATEFONT and CREATEFONTINDIRECT to create your own font, the difference between the two functions requires you to deliver a range of parameters, and then just pass a pointer to the LogFont structure. This makes the latter more convenient, especially when you need frequent fonts frequently. In our example, since it creates a font, it is enough to use CreateFont. After calling the function, you will return the handle of the created font, then select the "device environment" to make it a current font, then all "Draw" text strings are called to use the handle as the call. A parameter transmission

example:

.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 incrudelib /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 .Endw Mov EAX, MSG.WParam Ret Winmain Endp

WndProc proc hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM LOCAL hdc: HDC LOCAL ps: PAINTSTRUCT LOCAL hfont: HFONT.IF uMsg == WM_DESTROY invoke PostQuitMessage, NULL .ELSEIF uMsg == WM_PAINT invoke BeginPaint, hWnd, ADDR ps mov hdc, eax invoke CreateFont, 24,16,0,0,400,0,0,0, OEM_CHARSET, / OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, / DEFAULT_QUALITY, DEFAULT_PITCH or FF_SCRIPT, / 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 .ELSE invoke DefWindowProc, HWND, UMSG, WPARAM, LPARAM RET .Endif Xor Eax, EAX RET WNDPROC END p

End Start

analysis:

The CREATEFONT function produces a logical font, which is as close as possible to the relevant values ​​specified in the parameters. This function is probably one of the most parameters in all Windows API functions. It returns a handle pointing to the logical font for use in the SELECTOBJECT function. Let's explain the parameters of this function in detail:

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: The height of the font that wants to use, 0 is default. NWIDTH: I hope to use the width of the font, it is best to use 0, so that Windows will automatically select one and highly matched values. Because what is done in our example, the characters cannot be displayed too small, so we set it 16. Nescapement: The rotation angle of each character is relative to the previous character, is generally set to 0.900 representation to 90 degrees, 1800 revolutions 190 degrees, 2700 rpm 270 degrees. Norientation: The direction of the font. Nweight: The thickness of the font strokes. Windows is predefined for us as follows:

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

CITALIC: 0 is normal, and other values ​​are oblique. CunderLine: 0 is normal, and other values ​​are underlined. CSTRIKEOUT: 0 is normal, and other values ​​are deleted lines. CCharset: The character set of the font. Generally select OEM_CHARSET, which makes Windows use the character sets related to the operating system. COUTPUTPRECISION: Specifies the accuracy of the font we have selected close to the real font. Generally, OUT_DEFAULT_PRECIS is generally selected, which determines the default mapping mode. CClipPrecision: Specifies the cropping accuracy of the font we have selected in the crop area. Generally, Clip_Default_Precis is generally selected, which determines the cropping precision. CQUALITY: Specifies the quality of the output font. It points out how GDI should be close to the real fonts as much as possible, there are three ways: default_quality, proof_quality, and draft_quality. Cpitchandfamily: Dictionaries and font families. LPFACENAME: Specifies the name of the font.

The above description is not necessarily understood, if you want to get more information, you should refer to the Win32 API guide.

Invoke SelectObject, HDC, Eaxmov HFONT, EAX

After we get the handle of the logical font, you must call the SelectObject function to select it into the "device environment", we can also call the function to select the "Equipment Environment" like this like color, pen, brush and other GDI objects. This function returns an old "device environment" handle. You must save this handle to select it back after completing the "draw". Draw functions for all of the SelectObject functions are for this "device environment".

RGB 200, 200, 50 Invoke SetTextColor, HDC, EaxRGB 0,0,255 Invoke SetBkcolor, HDC, EAX

We create colors with macro RGB and then call SetTextColor and SetBkcolor.

Invoke TextOut, HDC, 0, 0, Addr Teststring, Sizeof Teststring

We call TextOUT to "draw" text strings in the customer area with our previous fonts and colors. Invoke SelectObject, HDC, HFONT

After we "draw" is complete, the "Equipment Environment" must be restored. We must do this every time.

转载请注明原文地址:https://www.9cbs.com/read-30133.html

New Post(0)