Page 1 of 1

MASM-GDI(Graphics Device Interface)

#1 Hooker  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 29
  • Joined: 17-August 12

Post icon  Posted 14 November 2012 - 11:17 PM

MASM-GDI(Graphics Device Interface)
In this tutorial we'll see about drawing and painting with brush, pen. First requirement for painting is getting the handle of the display device context. In case of painting the client area we use GetDC function which gives device context of window client area and in case of painting the whole window including title bar,scrollbar menubar we use GetWindowDC.

Pen
Pen is a tool used to draw used to draw lines and curves it can be created by using the CreatePen API function. In order to paint using a pen we have to get the handle of the device context. and we use SelectObject function to select the object to the device context after using the pen we have to reselect the old pen back to the device context.And release the device context using ReleaseDC.

Brush
A brush is a tool used to paint close shapes like ellipse,rectangle.There are many brushes that can be used according to our needs.
Solid Brush -->brush with a solid color.
Pattern Brush -->brush with a specified bitmap pattern.
Hatch brush -->brush with a hatch pattern.
CreateSolidBrush API is used to create a brush with a specified solid color. CreatePatternBrush API is used to create a brush with specified bitmap pattern. CreateHatchBrush API is used to create a brush with specified hatch pattern and color

Solid and Hatch brush

Example program:
;program explains use of Solid pen and hatch brush
include \masm32\include\masm32rt.inc

Dlgproc proto :DWORD,:DWORD,:DWORD,:DWORD

.data
AppName db "DIC tutorial",0
click db "Brush and pen",0
hatch db "hatched brush",0
Solid db "Solid Pen",0
brush db "Solid brush",0

.data?
hInstance dd ?
hwndEdit dd ?

.const
IDD_DLG1001                     Equ 1001

.code
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,1001,0,addr Dlgproc,0
invoke ExitProcess,0

Dlgproc proc hWnd:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD
local hdc:HDC
local rect:RECT
local hPen:DWORD
local hBrush:DWORD
local hOldpen:DWORD
local hOldbrush:DWORD
local hSolidbrush:DWORD
local hOldSolidbrush:DWORD
local ps:PAINTSTRUCT


.if uMsg==WM_CTLCOLORDLG
	mov eax,wParam
	invoke SetBkColor,eax,White
	invoke GetStockObject,WHITE_BRUSH
	ret
	
.elseif uMsg==WM_PAINT
	invoke BeginPaint,hWnd,addr ps
	mov hdc,eax
	invoke TextOut,hdc,190,0,addr click,sizeof click-1
	invoke TextOut,hdc,130,50,addr hatch,sizeof hatch-1
	invoke TextOut,hdc,220,170,addr Solid,sizeof Solid-1
	invoke TextOut,hdc,330,250,addr brush,sizeof brush-1

	invoke CreatePen,PS_SOLID,4,Black	;create our pen
	mov hPen,eax
	invoke CreateHatchBrush,HS_BDIAGONAL,Red	; our hatch brush
	mov hBrush,eax
	invoke CreateSolidBrush,Green	;our solid brush
	mov hSolidbrush,eax
	
	invoke SelectObject,hdc,hBrush
	mov hOldbrush,eax
	invoke Ellipse,hdc,11,11,111,111
	invoke SelectObject,hdc,hOldbrush

	invoke SelectObject,hdc,hPen
	mov eax,hOldpen
	invoke Ellipse,hdc,110,110,210,210
	invoke SelectObject,hdc,hOldpen
	
	invoke SelectObject,hdc,hSolidbrush
	mov hOldSolidbrush,eax
	invoke Ellipse,hdc,210,210,310,310
	invoke SelectObject,hdc,hOldSolidbrush
	
	invoke EndPaint,hWnd,addr ps
	
.elseif uMsg==WM_CLOSE
	invoke DeleteObject,hPen
	invoke DeleteObject,hBrush
	invoke DeleteObject,hSolidbrush	
	invoke EndDialog,hWnd,0
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
Ret
Dlgproc endp

end start


Analysis:
I had created a dialog box using a resource editor and declared some local variables in stack.
.if uMsg==WM_CTLCOLORDLG
	mov eax,wParam
	invoke SetBkColor,eax,White
	invoke GetStockObject,WHITE_BRUSH
	ret


WM_CTLCOLORDLG message is send to dialogbox before window draws the dialog box we can use this opportunity to set our background color white. Using GetStockbObject function retrieves handle to the predefined white brush.

.elseif uMsg==WM_PAINT
	invoke BeginPaint,hWnd,addr ps
	mov hdc,eax
	invoke TextOut,hdc,190,0,addr click,sizeof click-1
	invoke TextOut,hdc,130,50,addr hatch,sizeof hatch-1
	invoke TextOut,hdc,220,170,addr Solid,sizeof Solid-1
	invoke TextOut,hdc,330,250,addr brush,sizeof brush-1

	invoke CreatePen,PS_SOLID,4,Black	;create our pen
	mov hPen,eax
	invoke CreateHatchBrush,HS_BDIAGONAL,Red	; our hatch brush
	mov hBrush,eax
	invoke CreateSolidBrush,Green	;our solid brush
	mov hSolidbrush,eax
	
	invoke SelectObject,hdc,hBrush
	mov hOldbrush,eax
	invoke Ellipse,hdc,11,11,111,111
	invoke SelectObject,hdc,hOldbrush

	invoke SelectObject,hdc,hPen
	mov eax,hOldpen
	invoke Ellipse,hdc,110,110,210,210
	invoke SelectObject,hdc,hOldpen
	
	invoke SelectObject,hdc,hSolidbrush
	mov hOldSolidbrush,eax
	invoke Ellipse,hdc,210,210,310,310
	invoke SelectObject,hdc,hOldSolidbrush
	
	invoke EndPaint,hWnd,addr ps


When our dialogbox recieve WM_PAINT message .We use TextOut function in GDI to display our some text in client area. We want to exclude null terminator so we subtracted 1 form text length.We create our pen with CreatePen function, PS_SOLID makes pen solid. Then Brush is created with CreateHatchBrush function the following styles can be used.
HS_BDIAGONAL- 45-degree downward left-to-right hatch
HS_CROSS- Horizontal and vertical crosshatch
HS_DIAGCROSS- 45-degree crosshatch
HS_FDIAGONAL- 45-degree upward left-to-right hatch
HS_HORIZONTAL- Horizontal hatch
HS_VERTICAL - Vertical hatch
We create our solid brush by CreateSolidBrush function green color is set as brush color value.Its time to draw Ellipse in our window.We select our brush to our hdc which inturn returns the handle of the old brush. we use Ellipse function to draw our ellipse. Then we restore old brush back in hdc. Similarly we do the same with other brush and pen. Finally release our DC.
.elseif uMsg==WM_CLOSE
	invoke DeleteObject,hPen
	invoke DeleteObject,hBrush
	invoke DeleteObject,hSolidbrush	
	invoke EndDialog,hWnd,0


when our dialogbox receives WM_CLOSE message before closing our dialog box we have to delete what we have created. Using DeleteObject API we delete it.

[b]Pattern brush[/b]
[code]
;Program explains use of Pattern brush
Dlgproc proc hWnd:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD
local hdc:HDC
local ps:PAINTSTRUCT
local hBmp:DWORD
local hPattern:DWORD
local hOldpattern:DWORD

.if uMsg==WM_CTLCOLORDLG
	mov eax,wParam
	invoke SetBkColor,eax,White
	invoke GetStockObject,WHITE_BRUSH
	ret

.elseif uMsg==WM_PAINT
	invoke BeginPaint,hWnd,addr ps
	mov hdc,eax
	invoke TextOut,hdc,130,30,addr pattern,sizeof pattern-1
	
	invoke LoadBitmap,hInstance,IDB_PATTERN
	mov hBmp,eax

	invoke CreatePatternBrush,hBmp
	mov hPattern,eax	

	invoke SelectObject,hdc,hPattern
	mov hOldpattern,eax
	invoke Rectangle,hdc,11,11,111,111
	invoke Ellipse,hdc,110,110,210,210
	invoke DeleteObject,hBmp
	invoke SelectObject,hdc,hOldpattern	
	invoke EndPaint,hWnd,addr ps
		
.elseif uMsg==WM_CLOSE
	invoke DeleteObject,hBmp
	invoke DeleteObject,hPattern
	invoke EndDialog,hWnd,0
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
Ret
Dlgproc endp



Analysis:
As in the previous program we set our background white. When window sends WM_PAINT message display our text in window. Load our bitmap, using its handle create our pattern brush using CreatePatternBrush function. SelectObject function selects our pattern brush into device context then we draw rectangle and ellipse.Don't forget to restore old brush back into device context.
In the WM_CLOSE message we delete what we have created and close our dialog box.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Page 1 of 1