Tutorials to .com

Tutorials to .com » Software » Asm » WIN32 compilation: 6. To deal with the keyboard information

WIN32 compilation: 6. To deal with the keyboard information

Print View , by: iSee ,Total views: 40 ,Word Count: 1480 ,Date: Mon, 11 May 2009 Time: 12:21 AM

Sixth Division to deal with keyboard input message


In this lesson, we will have to learn is how to deal with WINDOWS procedures keyboard messages.

Theory:

Most of the PC because there is only one keyboard, all running WINDOWS procedures must share it. WINDOWS will be responsible for the information to the keyboard input focus to that application. Although the screen may have several applications at the same time window, but only one window at a time when the input focus. Input focus of that article the title of the application is always high-brightness display. In fact your point of view can be seen from two keyboard messages: First, you can host it as a collection of key information, in this case, when you press a key, WINDOWS will send a WM_KEYDOWN input focus to the application of that to remind it has a key is pressed. When you release the key, WINDOWS will WM_KYEUP send a message that a key was released. Your key as each is a button; another is: You can put the keyboard as a character input device. When you press the "a" key, WINDOWS send a message WM_CHAR input focus to the application, tell it "a" key is pressed. WINDOWS actually send internal messages WM_KEYDOWN and WWM_KEYUP input focus to the application, and these messages will be translated into the TranslateMessage call WM_CHAR news. WINDOWS window function will be to decide whether the course of processing the information received, in general you do not the General Assembly to deal with WM_KEYDOWN, WM_KEYUP news cycle in the message TranslateMessage function will convert the news WM_CHAR news. In our courses will deal only with the WM_CHAR.

Examples:

.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
includelib \ masm32 \ lib \ gdi32.lib

. data
ClassName db "SimpleWinClass", 0
AppName db "Our First Window", 0
char WPARAM 20h; the character the program receives from keyboard

. 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

. IF uMsg == WM_DESTROY
invoke PostQuitMessage, NULL
. ELSEIF uMsg == WM_CHAR
push wParam
pop char
invoke InvalidateRect, hWnd, NULL, TRUE
. ELSEIF uMsg == WM_PAINT
invoke BeginPaint, hWnd, ADDR ps
mov hdc, eax
invoke TextOut, hdc, 0,0, ADDR char, 1
invoke EndPaint, hWnd, ADDR ps
. ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
ret
. ENDIF
xor eax, eax
ret
WndProc endp
end start

Analysis:


char WPARAM 20h; the character the program receives from keyboard

This variable will be saved from the keyboard characters received. Because it is the window through the process of variable transmission WPARAM, we simply defined as type WPARAM. As we refresh the window when the first (that is, one that has just been created) there is no keyboard input so we set him at a space (20h), that was the case when you can not see anything.

. ELSEIF uMsg == WM_CHAR
push wParam
pop char
invoke InvalidateRect, hWnd, NULL, TRUE

This paragraph is used to deal with WM_CHAR messages. It received char in characters Add variable, and then call InvalidateRect, which makes window InvalidateRect invalid client area so that it will issue a WM_PAINT message, and the force WM_PAINT messages WINDOWS its customers to re-draw the district. The function syntax is as follows:

InvalidateRect proto hWnd: HWND, \
lpRect: DWORD, \
bErase: DWORD

lpRect is the point we want the customer area of a square structure invalid pointer body. If the value is equal to NULL, then the whole client area is invalid; bErase told WINDOWS boolean erase the context of whether, if it is TRUE, then WINDOWS in BeginPaint function call to erase the background. Therefore, we practice here is: we will redraw the preservation of all relevant customer data area, and then send WM_PAINT message, the procedures to deal with the above message and then re-drawn in accordance with the relevant customer data area. Although things do go a bit like Camponotus, but to deal with such an enormous WINDOWS news groups, there is no certain rules can not. In fact, we can be obtained by calling the GetDC device context handle, and then draw the characters, and then call ReleaseDC release device context handle, there is no doubt this area can be drawing in the customers the correct characters. However, if after receiving the WM_PAINT message to be dealt with, the customer area will re-set, and a little before our characters will disappear drawn. Therefore, in order to allow the correct character has been shown, it is necessary to put them in the process WM_PAINT processing. And in the message handling can send WM_PAINT message.

invoke TextOut, hdc, 0,0, ADDR char, 1

InvalidateRect when calling, WM_PAINT message is sent to the WINDOWS processing window, the program flow to handle the transfer of the program segment WM_PAINT message, and then call the BeginPaint get device context handle, and then call the customer area TextOut (0,0) output Department Save the key characters. So no matter what button you can show customers the upper-left corner District, Not only that, no matter how you zoom window (WINDOWS forcing its customers to re-draw the district), the characters will be displayed in the right place, it is necessary to draw all the important moves on to deal with the process WM_PAINT message to the paragraph.



Assembly Language Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.