Title: Learning to Draw Basic Graphics
1Learning to Draw Basic Graphics
2Graphics Essentials
- Graphics Coordinate System
- Windows coordinate system origin at upper-left
corner, x values increasing to the right and y
values increasing down - Drawing area client area
- Basics of Color
- RGB
- COLORREF green RGB(0, 255, 0)
- Intensities of each component range from 0 to 255
- Windows colors determined by the combination of
the intensities of the RGB
3GDI (Graphics Device Interface)
- Provide an interface between drawing operation
and graphics driver
4GDI Component Device Context
- Device Context
- A gate way to a physical graphics device
- All drawing is actually done to a device context
- Through DC, drawing can be done with GDI
- Two ways to obtain DC
- PAINTSTRUCT ps
- HDC hDC BeginPaint(hWindow, ps)
- // returns a handle to a device context
- GDI drawing operations go here
- EndPaint(hWindow, ps)
- // release the device context
- HDC hDC GetDC(hWindow)
- GDI drawing operations go here
- ReleaseDC(hWindow, hDC)
5Graphics components in GDI
- Pens
- Draw lines and curves
- Brushes
- Paint the interior of polygons, ellipses, and
paths - Bitmaps
- A graphical image stored as an array of pixels
- Can be 1 bit per pixel to 32 bits per pixel
- 8 bits per pixel 256 color bitmaps, 24 bits per
pixel 224 color bitmaps - Palettes
- A set of colors used by the GDI when rendering a
bitmap
6Painting Windows
- In WndProc( ) function
- case WM_PAINT
- hDC hDC
- PAINTSTRUCT ps
- hDC BeginPaint(hWindow, ps)
- GamePaint(hDC) // paint the game
- EndPaint(hWindow, ps)
Void GamePaint(HDC hDC) GDI drawing
operations go here MoveToEx(hDC, 0, 0,
NULL) LineTo(hDC, 50, 50)
7Painting Windows Cont.
- Painting Text 1 approach at specified point
- Void GamePaint(HDC hDC)
-
- GDI drawing operations go here
- TextOut(hDC, 10, 10, TEXT(Hello World), 16)
-
Painting Text 2 approach within a
rectangle Void GamePaint(HDC hDC) GDI
drawing operations go here RECT
rect GetClientRect(hWindow, rect) DrawText(hD
C, TEXT(Hello World), -1, rect, DT_SINGLELINE
DT_CENTER DT_VCENTER)
8Painting Windows Cont.
- Draw line
- Void GamePaint(HDC hDC)
-
- MoveToEx(hDC, 10, 40, NULL)
- LineTo(hDC, 44, 10)
- LineTo(hDC, 78, 40)
-
Draw Rectangle Void GamePaint(HDC
hDC) Rectangle(hDC, 16, 36, 72,
70) Rectangle(hDC, 34, 50, 54, 70)
Fill Rectangle int FillRect(HDC hDC, CONST RECT
lprc, HBRUSH hbr)
9Painting Windows Cont.
- Draw Ellipse
- BOOL Ellipse(HDC hDC, int xLeft, int yTopm int
xRight, int yBottom)
- Draw Polygon
- Void GamePaint(HDC hDC)
-
- POINT points3
- points0 5, 10
- points1 25, 30
- points2 15, 20
- Polygon(hDC, points, 3)
-
10Working with Pens and Brushes
- Creating Pens to control line
- HPEN CreatePen(int iPenStyle, int iWidth,
COLORREF crColor) - Ex
- HPEN hBluePen CreatePen(PS_SOLID, 1, RGB(0, 0,
255))
- Selecting Pens
- HPEN hPen SelectObject(hDC, hBluePen)
- // do some drawing here
- SelectObject(hDC, hPen)
- DeleteObject(hBluePen)
- Creating Brushes to fill colors
- HBRUSH hPurpleBrush CreateSolidBrush(RGB(255,
0, 255))