Tuesday, October 26, 2010

Initial bitmap


In this lesson we will learn how to use bitmap in the program. More precisely, we have to learn is how the client area of a window to display the bitmap.
Theory
Bitmap is stored in a computer in the picture. A considerable number of bitmap file format (Translation: If. BMP.JPG.GIF.PIC, etc.), but Windows only supports Windows Bitmap Graphics Format, or BMP file. This course is also referred to bitmap BMP files. Use bitmap easiest way is to define it in the resource file (. Rc) in the. There are two methods defined. The first method is to macro it is defined as an integer, as follows:

# Define IDB_MYB99vMAP 100
IDB_MYB99vMAP B99vMAP "c: projectexample.bmp"
The first line we define an integer with a value of 100 macros. The second line we have to point this macro to be defined integer bitmap, so the compiler will know where the path of the bitmap.
Another method is to give it a name, that is, it is defined as a string, as follows:
MyBitMap B99vMAP "c: projectexample.bmp"
Effects of two methods is the same. Choose a method, depending on the program you like to use integer or string to point to Macro bitmap.
Now that we have the bitmap defined in the resource file, the next step is to make it appear in the window client area.
In the process, we use the API function to obtain the bitmap handle LoadBitmap. Here is the complete LoadBitmap function 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 a pointer to a bitmap name (for the second definition of method). If you use the first definition of method, you can fill point or integer values of a bitmap macro (this value corresponds to the example above is 100, an integer macro is IDB_MYB99vMAP). Here is a simple example:


The first method:
.386
. Model flat, stdcall
................
. Const
IDB_MYB99vMAP equ 100
...............
. Data?
hInstance dd?
..............
. Code
.............
invoke GetModuleHandle, NULL
mov hInstance, eax
............
invoke LoadBitmap, hInstance, IDB_MYB99vMAP
...........

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
...........

Text access to a device (DC) handle. You can respond to WM_PAINT message received through the API function BeginPaint. If the other message, you can use the API function GetDC access.
Create the memory DC image. The aim is to establish a "hidden drawing paper", the bit map "painted" on it, for buffer purposes. After the completion of this work, we adopted a function of "drawing paper" on the bitmap copy of the real DC. This is quickly displayed on the screen image of the double-buffering technique. (Translation: You can reduce image jitter) this "drawing paper," with the API function CreateCompatibleDC established, following the completion of its type:
CreateCompatibleDC proto hdc: HDC

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

Now we have the "drawing paper," could place a picture on it. SelectObject API function that can be completed, the first parameter is the "drawing paper," the handle, the second parameter is a bitmap handle, here is the function of the complete type:
SelectObject proto hdc: HDC, hGdiObject: DWORD
Now the bitmap has been drawn in the "drawing paper" has gone. Next we want to copy the bitmap in the real DC. There are many API functions can finish the job, such as BitBlt and StretchBlt. Function BitBlt the contents of only one DC to another DC simply copy, while the function is to automatically adjust StretchBlt copy the contents of the size of the source DC has the purpose to adapt the size of DC's output area, so the former than the latter faster. Here, we only use the function BitBlt, following the completion of its type:
BitBlt proto hdcDest: DWORD, nxDest: DWORD, nyDest: DWORD, nWidth: DWORD, nHeight: DWORD, hdcSrc: DWORD, nxSrc: DWORD, nySrc: DWORD, dwROP: DWORD

hdcDest purpose DC handle.
nxDest, nyDest purpose DC output area of the upper-left corner coordinates.
nWidth, nHeight purpose DC output area length and width.
hdcSrc handle of the source DC.
nxSrc, nySrc source DC in the upper left corner coordinates of area to be copied.
dwROP screen surface computing code (ROP). Copy the contents of the parameter used to determine the color and the color of the original output area by which computing approach. Usually, simply copy the contents of the output area with a overwrite.
All completed, the API function with the release of the bitmap DeleteObject object, that is, the bit map "erase."
You're done! Now come back over the procedure: First, you need to define the bitmap in the resource file. Then, you need to load a bitmap in the program resources, and get a bitmap handle. Then, you need to get a bitmap output area of DC, and the creation of the DC's memory image, and to a bitmap image into the memory DC. Finally the bitmap image copied from the memory DC to DC in the real.
Examples:
.386
. Model flat, stdcall
option casemap: none
include masm32includewindows.inc
include masm32includeuser32.inc
include masm32includekernel32.inc
include masm32includegdi32.inc
includelib masm32libuser32.lib
includelib masm32libkernel32.lib
includelib masm32libgdi32.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 definitions
;------------------------------------------------- --------------------
# Define IDB_MAIN 1
IDB_MAIN B99vMAP "tweety78.bmp"

Analysis:


# Define IDB_MAIN 1
IDB_MAIN B99vMAP "tweety78.bmp"
Define macro IDB_MAIN integer value of 1, then it points to a resource file in the same directory with the file named "tweety.bmp" bitmap.
. If uMsg == WM_CREATE
invoke LoadBitmap, hInstance, IDB_MAIN
mov hBitmap, eax

In dealing with WM_CREATE message, we pass the API function LoadBitmap loading bitmap resources, and through the function return value to obtain the bitmap handle.
Then, we can place the picture 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 example, we choose in response to WM_PAINT message to draw the bitmap. First we get through the API function l BeginPaint DC client area window handle. Then we created through the API function CreateCompatibleDC memory image of the DC, and through API function SelectObject to bitmap image into memory. Next, we get through the API function GetClientRect the size of the window client area. Finally, we function through the API BitBlt the bitmap image copied from the memory DC to the DC area in the real customers. Finished the work, we release the DC through the API function DeleteDC memory image, with the release of the client area of API functions EndPaint DC, drawing the end of the work.

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






Recommended links:



Simple four-strokes, cool music box my office you scouring millions of songs



Top Personal Finance



Bee Notes Ongoing Expert Special Offers



Seven Strategies media advertising competition



New Computer Education



STORM two Strokes buttoned brightness adjustment



Streaming media based on: What is Streaming Media



Children's Books List: Foreign Books Victory, Was Embarrassing Domestic Children's Books



"Crocodile Group" under the Chip, "national character"



How can we know that you AUTOCAD Screen shown on the graphs of 1:1



ASF Converter



Infomation Animation Tools



Transport Stream Converter



DVR-MS to MPG



Dynamic library is missing is it fixable



1 comment:

  1. Hey There. I found your blog using msn. This is a really well written article.
    I will be sure to bookmark it and return to read more of your useful info.
    Thanks for the post. I will certainly return.

    Visit my weblog - test line cccam

    ReplyDelete