Draft text for the compilation tutorial

Source: Internet
Author: User
Tags message queue valid win32

In this lesson, we'll learn how to "draw" strings in the client area of a window. We will also learn about the "device environment" concept.

Theory:

The text in Windows is a GUI (graphical user interface) object. Each character is actually made up of a number of pixel points, which are displayed where there are strokes, so that characters appear. That's why I say "draw" characters, not letters. Typically you are "drawing" strings in the client area of your application (although you can also "draw" outside the client area). The "Draw" string method under Windows is quite different from DOS, where you can imagine the screen as a plane of 25, and Windows has a few applications on the screen, so you must follow the rules strictly. Windows does this by restricting each application to his client area. Of course the size of the client area is variable and you can adjust it anytime.

Before you "draw" a string in the client area, you have to get the size of your client area from Windows, and indeed you can't "draw" anywhere on the screen as you would in DOS, you must get Windows permission before you draw, and then Windows will tell you the size of the client area, font , colors, and properties of other GUI objects. You can use these to "draw" in the customer area.

What is a "device environment" (DC)? It is actually a data structure that is maintained internally by Windows. A "device environment" is connected to a specific device. Like printers and monitors. For a monitor, the "device environment" is connected to a particular window.

Some properties in the device environment are related to drawings, such as colors, fonts, and so on. You can change those defaults at any time, and the default values are saved for convenience. You can imagine a "device environment" as a drawing environment that Windows prepares for you, and you can change some of the defaults whenever you need to.

When the application needs to be drawn, you must get a handle to the "Device environment". There are usually several ways.

Use call BeginPaint in WM_PAINT messages

Use call GetDC in other messages

Call CreateDC Build Your own DC

You must keep in mind that you must release the "Device environment" handle after a single message has been processed. Do not get a "device environment" handle in one message processing and release it in another message processing.

Windows does not save the client area's content when Windows sends WM_PAINT messages, and the method is the "redraw" mechanism (for example, when the client area has just been overwritten by another application's client area), window WM_PAINT Messages into the application's message queue. Redrawing the client area of a window is the responsibility of each window, and what you want to do is to process the WM_PAINT part of the window to know what to draw and how to draw.

Another concept that you must understand is "invalid zone". Windows calls a minimal area of the square that needs redrawing called an "invalid zone." When Windows discovers an "invalid zone", it sends a WM_PAINT message to the application, and in the process of WM_PAINT, the window first obtains a structure for the drawing, which includes the coordinate position of the invalid zone, and so on. You can make "invalid zone" valid by calling BeginPaint, if you do not process WM_PAINT messages, at least call the default window handler function DefWindowProc, or call ValidateRect to make "invalid zone" valid. Otherwise, your application will receive an endless WM_PAINT message.

The following steps are in response to the message:

Get the "Device environment" handle

Draw Customer Area

Releasing the device environment handle

Note that you do not have to explicitly invalidate the "invalid Zone", which is automatically completed by BeginPaint. You can call all the drawing functions between BeginPaint and EndPaint. Almost all GDI functions require the handle of the "device environment" as an argument.

Content:

We will write an application that will display a line "Win32 assembly is great and easy!" in the center of the client area

.386
. Model Flat,stdcall
Option Casemap:none
WinMain Proto:D Word,:D Word,:D Word,:D Word
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", 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.