Tutorials to .com

Tutorials to .com » Software » Asm » WIN32 compilation: 25. Bitmaps preliminary

WIN32 compilation: 25. Bitmaps preliminary

Print View , by: iSee ,Total views: 18 ,Word Count: 1945 ,Date: Wed, 6 May 2009 Time: 7:04 PM

25th Division: the initial bitmap


In this lesson, we will be in the process of learning how to use bitmaps. Be more precise, we need to learn how a window in the client area to display a bitmap.

Theory

Bitmap is stored in a computer in the picture. Bitmap file format of a considerable number (translator: If. BMP.JPG.GIF.PIC, etc.) but Windows only supports Windows Bitmap Graphics format, that is, BMP files. This lesson is also referred to BMP bitmap file. The use of digital map is the simplest definition of it in the resource file (. Rc) in. There are two ways to define. The first method is defined as an integer macro, as follows:
# define IDB_MYBITMAP 100
IDB_MYBITMAP BITMAP "c: \ project \ example.bmp"
We are the first line of the definition of an integer value of 100 macros. The second line of this integral we have to point to the definition of macro bitmap, so that the compiler will know where the path of the bitmap.
Another method is to effect a name to it, which is defined as string, as follows:
MyBitMap BITMAP "c: \ project \ example.bmp"
The effect of the two methods are the same. Choose a method, depending on the program you like to use macro or use an integer string to point to a bitmap.
Now that we have the definition of the bitmap file in the resources, the next step is to make it appear in the window client area.
  1. In the process, we use the API function to obtain the bitmap handle LoadBitmap. The following is a function of the End LoadBitmap type:
      LoadBitmap proto hInstance: HINSTANCE, lpBitmapName: LPSTR


    The function returns a bitmap handle. Function has two parameters, which is the process handle hInstance. lpBitmapName is the name of the pointer bitmaps (for the definition of the second approach). If you use the definition of the first method, you can fill in the bitmap to an integer value or macro (this value corresponds to the above example is 100, an integer is a hong IDB_MYBITMAP). The following is a simple example:


      The first method:

      .386
      . model flat, stdcall
      ................
      . const
      IDB_MYBITMAP equ 100
      ...............
      . data?
      hInstance dd?
      ..............
      . code
      .............
      invoke GetModuleHandle, NULL
      mov hInstance, eax
      ............
      invoke LoadBitmap, hInstance, IDB_MYBITMAP
      ...........

      The second method:

      .386
      . model flat, stdcall
      ................
      . data
      BitmapName db "MyBitMap", 0
      ...............
      . data?
      hInstance dd?
      ..............
      . code
      .............
      invoke GetModuleHandle, NULL
      mov hInstance, eax
      ............
      invoke LoadBitmap, hInstance, addr BitmapName
      ...........

  1. The text of a device (DC) handle. You can respond to WM_PAINT message BeginPaint obtained through the API function. If other messages can be received by API function GetDC.
  2. DC to create the memory image. The aim is to establish a "paper hidden", the bit map "painted" in the above gap is needed. After the completion of this work, we adopt a function of the "artist" to copy the bitmap in the real DC. This is quickly displayed on the screen image of the double buffering technique. (Translated: You can reduce image jitter) this "artist" to use API function CreateCompatibleDC established, following the end of its type:
      CreateCompatibleDC proto hdc: HDC


    If the function successfully implemented, will return to DC memory image that is "paper" handle.

  1. Now we have a "paper" can be a bit picture above. This can be done through API function SelectObject completed the first of which parameter is the "artist" of the handle, the second parameter is a bitmap handle End Function The following is a type:
      SelectObject proto hdc: HDC, hGdiObject: DWORD
  1. Bitmap has been painting now in the "paper" has gone. Next we want to copy the bitmap in the real DC. There are many API function be able to complete this work, such as BitBlt and StretchBlt. BitBlt function of a DC just simply copy the contents to another DC, and the function to automatically adjust StretchBlt are copying the contents of the source of DC had adapted to the purpose of the size of the output of the region the size of DC, so the former faster than the latter. Here we use only the function BitBlt, following the end of its type:
      BitBlt proto hdcDest: DWORD, nxDest: DWORD, nyDest: DWORD, nWidth: DWORD, nHeight: DWORD, hdcSrc: DWORD, nxSrc: DWORD, nySrc: DWORD, dwROP: DWORD
    The purpose of hdcDest handle DC.
    nxDest, nyDest purpose DC output of the upper-left corner coordinates of the region.
    nWidth, nHeight purpose DC output of the length and width of the region.
    DC source hdcSrc handle.
    nxSrc, nySrc source of DC in the region to copy the coordinates of the upper-left corner.
    Code dwROP screen operation (ROP). Copy the contents of the parameters used to determine the color and output color of the region by which the original approach to computing. Typically, only need to simply copy the contents of the output lost regional coverage.
  1. All completed on the use of API release bitmap DeleteObject function object, that is, the bit map "erase."
You're done! Now again look at the whole process: First of all, you need the definition of the bitmap in the resource file. Then, in the procedure you need to load Bitmap resources and access to the bitmap handle. After that, you need access to the region bitmap output DC, as well as to create the memory image of the DC and the DC into a bitmap image in memory. Finally, the bitmap image to copy from memory DC to DC in the real.

Examples:

.386
. model flat, stdcall
option casemap: none
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

WinMain proto: DWORD,: DWORD,: DWORD,: DWORD
IDB_MAIN equ 1

. data
ClassName db "SimpleWin32ASMBitmapClass", 0
AppName db "Win32ASM Simple Bitmap Example", 0

. data?
hInstance HINSTANCE?
CommandLine LPSTR?
hBitmap dd?

. 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 hInstance
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 ps: PAINTSTRUCT
LOCAL hdc: HDC
LOCAL hMemDC: HDC
LOCAL rect: RECT
. if uMsg == WM_CREATE
invoke LoadBitmap, hInstance, IDB_MAIN
mov hBitmap, eax
. elseif uMsg == WM_PAINT
invoke BeginPaint, hWnd, addr ps
mov hdc, eax
invoke CreateCompatibleDC, hdc
mov hMemDC, eax
invoke SelectObject, hMemDC, hBitmap
invoke GetClientRect, hWnd, addr rect
invoke BitBlt, hdc, 0,0, rect.right, rect.bottom, hMemDC, 0,0, SRCCOPY
invoke DeleteDC, hMemDC
invoke EndPaint, hWnd, addr ps
. elseif uMsg == WM_DESTROY
invoke DeleteObject, hBitmap
invoke PostQuitMessage, NULL
. ELSE
invoke DefWindowProc, hWnd, uMsg, wParam, lParam
ret
. ENDIF
xor eax, eax
ret
WndProc endp
end start

;------------------------------------------------- --------------------
; Resource definition
;------------------------------------------------- --------------------
# define IDB_MAIN 1
IDB_MAIN BITMAP "tweety78.bmp"

Analysis:


# define IDB_MAIN 1
IDB_MAIN BITMAP "tweety78.bmp"
IDB_MAIN macro definition of an integer value of 1, and then put it point to a resource file and directory in the same document, entitled "tweety.bmp" bitmap.

. if uMsg == WM_CREATE
invoke LoadBitmap, hInstance, IDB_MAIN
mov hBitmap, eax

WM_CREATE message in dealing with our API function through LoadBitmap loading bitmap resources, and through the function return value to obtain the bitmap handle.
Then, we can put digital pictures in the window client area.

. elseif uMsg == WM_PAINT
invoke BeginPaint, hWnd, addr ps
mov hdc, eax
invoke CreateCompatibleDC, hdc
mov hMemDC, eax
invoke SelectObject, hMemDC, hBitmap
invoke GetClientRect, hWnd, addr rect
invoke BitBlt, hdc, 0,0, rect.right, rect.bottom, hMemDC, 0,0, SRCCOPY
invoke DeleteDC, hMemDC
invoke EndPaint, hWnd, addr ps

In this case, we choose in response to WM_PAINT message to draw the bitmap. First of all, we function through the API to obtain l BeginPaint window handle customer DC area. Then we create the API function CreateCompatibleDC image memory DC, and through the API function SelectObject bitmap image into the memory. Next, we have achieved through API function GetClientRect the size of the window client area. Finally, we adopted the API function BitBlt the bitmap image to copy from memory DC to real customers in the DC area. Shows that after the completion of the work, we DeleteDC through the API function to release memory image DC, and the release of API function EndPaint Guest Area DC, drawing the end of the work.

. elseif uMsg == WM_DESTROY
invoke DeleteObject, hBitmap
invoke PostQuitMessage, NULL
When we no longer need the bitmap, the DeleteObject function through the API to release it.


Assembly Language Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.