Tutorials to .com

Tutorials to .com » Software » Asm » WIN32 compilation of: 4. Drawing text

WIN32 compilation of: 4. Drawing text

Print View , by: iSee ,Total views: 21 ,Word Count: 1956 ,Date: Wed, 13 May 2009 Time: 2:10 AM

The fourth lesson drawing text


This lesson, we will learn how to customers in the window area "map" the string. We will also learn about "Environment" concept.

Theory:

Windows in the text is a GUI (graphical user interface) object. In fact each character is composed of many pixels, the point where the number of strokes in the show, this character will appear. That is why I say "draw" characters, rather than write characters. Usually you are in your application's client area "draw" the string (although you can also customers outside the region "Rendering"). Windows under the "mapping" of string methods and Dos is radically different from that in the Dos, you can imagine the screen into a 85 x 25 plane, and Windows under the screen at the same time as a result of the application there are several pictures, so you norms must be strictly adhered to. Windows through the application limit of each customer in his district to do so. Of course, the size of client area is variable, you can adjust at any time.

In your client area "draw" the string, you must get from the Windows client the size you really can not like you did under DOS pleases anywhere on the screen "map", drawing you must be pre-Windows - allow, and then Windows will tell your client the size, font, color and other attributes of GUI objects. You can use these to the client area "map."

What is "Device Context" (DC) do? It is maintained by an internal Windows data structure. An "Environment" and a specific device connected. Such as printers and monitors. For display, the "Environment" and a window connected to specific.

"Environment" in some of the attributes and graphics, such as: colors, fonts and so on. You can change those default values, because the default value is preserved for the convenience. You can the "Environment" is to imagine you're ready to Windows for a graphics environment, and you may need to change at any time based on certain default attributes.

When the need for mapping applications, you must be a "device context" of the handle. There are several ways usually.

  • WM_PAINT message in the use of call BeginPaint
  • In other news in the use of call GetDC
  • call CreateDC set up your own DC

You must be borne in mind in dealing with a single news release you have to "Environment" handle. Do not get a message handling "equipment environment" handle, and in another in the release of information to deal with it.

Windows to send WM_PAINT in the message area to deal with customers drawn, Windows will not save the contents of customer area, which is the method used is "redraw" mechanism (for example, when a customer was just another application area of the client area coverage), Windows Add WM_PAINT message will be the application of the message queue. Redraw the window all the customers is the responsibility of their own window, you need to do in the window part of the process WM_PAINT know what He Ru drawn map.

You must understand the concept of another is "null and void the region." Windows needs a minimum redraw the region of the square is called "invalid region." When Windows found an "invalid region", it will be the application to send a WM_PAINT message, in the WM_PAINT processing, a first window of the structure of the graphics, which include the coordinates of the location of invalid and other areas. BeginPaint You can call the "void areas" effectively, if you do not handle WM_PAINT message, at least to call the default window handler DefWindowProc, or ValidateRect call the "void zone" effective. Otherwise, your application will receive the endless WM_PAINT message.

The following are the steps to respond to this message:

  1. To obtain "equipment environment" Handle
  2. Client Area map
  3. The release of "Environment" Handle

Note that you do not need to explicitly allow the "void areas" effective, this action by the auto-complete BeginPaint. You can BeginPaint and Endpaint in between all of the mapping function call. Almost all of the GDI functions are required "equipment environment" as a parameter of the handle.

Content:

We will write an application, it will show the customer the center of the district and his party "Win32 assembly is great and easy!"

.386
. model flat, stdcall
option casemap: none

WinMain proto: DWORD,: DWORD,: DWORD,: DWORD

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

. DATA
ClassName db "SimpleWinClass", 0
AppName db "Our First Window", 0
OurText db "Win32 assembly is great and easy!", 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 rect: RECT
. IF uMsg == WM_DESTROY
invoke PostQuitMessage, NULL
. ELSEIF uMsg == WM_PAINT
invoke BeginPaint, hWnd, ADDR ps
mov hdc, eax
invoke GetClientRect, hWnd, ADDR rect
invoke DrawText, hdc, ADDR OurText, -1, ADDR rect, \
DT_SINGLELINE or DT_CENTER or DT_VCENTER
invoke EndPaint, hWnd, ADDR ps
. ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
ret
. ENDIF
xor eax, eax
ret
WndProc endp
end start

Analysis:

Most of the code here and the third in the same class. I can only explain some of these differences.

LOCAL hdc: HDC
LOCAL ps: PAINTSTRUCT
LOCAL rect: RECT

These local variables handling the WM_PAINT message GDI function call. hdc is used to store BeginPaint call to return to the "Environment" handle. ps is a type of variable data PAINTSTRUCT. Usually you do not use many of these values, it is passed on to Windows by BeginPaint, after drawing at the end of the transfer to the intact EndPaint. rect is a RECT structure type parameters, which are defined as follows:

RECT Struct left LONG?
top LONG?
right LONG?
bottom LONG?
RECT ends

left and top coordinates of the upper-left corner is square. right and bottom coordinates of the lower right corner is square. The upper left corner of client area coordinates of x = 0, y = 0, so for x = 0, y = 10 the coordinates of the following in it.

invoke BeginPaint, hWnd, ADDR ps
mov hdc, eax
invoke GetClientRect, hWnd, ADDR rect
invoke DrawText, hdc, ADDR OurText, -1, ADDR rect, \
DT_SINGLELINE or DT_CENTER or DT_VCENTER
invoke EndPaint, hWnd, ADDR ps

In dealing with WM_PAINT message, you call BeginPaint function, pass it a window handle and PAINTSTRUCT uninitialized parameters. Eax call in after a successful return to the "Environment" of the handle. The next call GetClientRect to get the size of customers, large and small on the rect, and then put it to DrawText. DrawText's syntax is as follows:

DrawText proto hdc: HDC, lpString: DWORD, nCount: DWORD, lpRect: DWORD, uFormat: DWORD

DrawText is a high-level function call. Automatic processing, as it can wrap the text into the middle areas such as customer chores. So you just focus on "mapping" can be a string. We will explain in the next lesson in low-level function TextOut, the function of a square area in a text format string. It uses the currently selected font, color and background color. It dealt with a square wrapped in order to adapt to the region. It will return to the logic unit measurement equipment, the height of the text, we have here is a pixel unit of measure. Let us take a look at the parameters of the function:

  • hdc: "Environment" of the handle.
  • lpString: text string to display the text string to NULL at the end of either or in nCount that the length of it.
  • nCount: to output the length of the text. NULL if at the end of the parameter must be -1.
  • lpRect: point to the text string to the output of the square of the target region, the cutting must be a square area, that is to say over the region will not be able to show the characters.
  • uFormat: specify how to display. We can use or the following signs or to a:
    • DT_SINGLELINE: whether the one-line display.
    • DT_CENTER: whether the level of the middle.
    • DT_VCENTER: whether the vertical center.

The end of drawing, you must call the release of EndPaint "Environment" of the handle. Well, now we have to "draw" the text string summarize the main points are as follows:

  1. Must be at the beginning and end respectively and BeginPaint call EndPaint;
  2. EndPaint in between BeginPaint and call all of the mapping function;
  3. If the news in other treatment areas to re-draw customers, you can have two options:
    (1) GetDC and ReleaseDC instead of BeginPaint and EndPaint;
    (2) Call UpdateWindow customers InvalidateRect or district does not work, it will force the WM_PAINT Add WINDOWS application message queue, allowing customers to redraw the district.


Assembly Language Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.