Home | API | MFC | C++ | C | Previous | Next

Programming Windows API

Displaying Graphics

Drawing Pixels

A pixel is the smallest image element that can be represented on screen. To draw a point within the client area of your window use the API function SetPixel(). The prototype for this function is

COLORREF SetPixel(HDC hdc,int x,int y,COLORREF color);

Where
hdc - the device context.
X - The x-coordinate, in logical units, of the point to be set.
Y - The y-coordinate, in logical units, of the point to be set.
Color – is a COLORREF to paint the point. If the color specified cannot be created on the video display then windows will use the nearest pure non dithered color and then return that value from the function.

If the function is successful, the return value is the RGB colour. If the function fails, the return value is -1.

Drawing Lines

The LineTo() function draws a line within the client area from the current graphics drawing point. The prototype is

BOOL LineTo(HDC hdc, int x, int y);

where
hdc is a handle to a device context.
x - specifies the x-coordinate the line's ending point.
y - specifies the y-coordinate the line's ending point. 

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.  The new starting point will then become the endpoint of the previous line. 

MoveToEx

The initial starting position for graphics output will be the screen coordinate position 0,0 however this can be set by the application with a call to the API function MoveToEx(). The prototype for the movetoex() function is -

BOOL MoveToEx(HDC hdc,int x,int y,LPPOINT lppt);

where
hdc - handle to a device context.
x - specifies the x-coordinate of the new position, in logical units.
y - specifies the y-coordinate of the new position, in logical units.
Lppt - is pointer to a POINT structure that receives the previous current position. If this parameter is a NULL pointer, the previous position is not returned.

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

GetCurrentPosition

Retrieves the current logical graphics starting position.  The prototype of this function is-

BOOL GetCurrentPositionEx(HDC hdc,LPPOINT lppt);

Where
hdc is handle to the device context
lppt is pointer to a POINT structure that receives the logical coordinates of the current position. 

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

Drawing Rectangles

Rectangles are drawn with the currently selected pen and brush. To draw a rectangle, use the Rectangle() function. The prototype is

BOOL Rectangle(HDC hdc,int left,int top,int right,int bottom);

hdc – is handle to the device context.
Left – is the x-coordinate of the upper-left corner of the rectangle.
Top – is the y-coordinate of the upper-left corner of the rectangle.
Right - the x-coordinate of the lower-right corner of the rectangle.
Bottom - the y-coordinate of the lower-right corner of the rectangle.
Return value - If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

If also possible to display rectangles with rounded corners using the API function call RoundRect(). The prototype for this function is

BOOL RoundRect(HDC hdc, int left,int top,int right,int bottom,int width,int height);

where
hdc - A handle to the device context.
Left - The x-coordinate of the upper-left corner of the rectangle.
Top- The y-coordinate of the upper-left corner of the rectangle.
Right - The x-coordinate of the lower-right corner of the rectangle.
Bottom - The y-coordinate of the lower-right corner of the rectangle.
Width - The width, of the ellipse used to draw the rounded corners.
Height - The height of the ellipse used to draw the rounded corners.

If the function succeeds, the return value is nonzero.If the function fails, the return value is zero.

Drawing an Ellipse

To draw an elipse or circle using the current pen and filled by the current brush, use the Ellipse() function. The prototype is

BOOL Ellipse(HDC hdc,int left,int top,int right,int bottom);

where
hdc - A handle to the device context.
left – is the x-coordinate of the upper-left corner of the bounding rectangle.
Top – is the y-coordinate  of the upper-left corner of the bounding rectangle.
Right – is the x-coordinate of the lower-right corner of the bounding rectangle.
Bottom – is the y-coordinate of the lower-right corner of the bounding rectangle.

If the function succeeds, the return value is nonzero.  If the function fails, the return value is zero.

To draw a circle the bounding rectangle is square. For example, to draw a circle that has center (50,50) with a radius of 10, use the following function call Ellipse(hdc,10,10,50,50);

Drawing a Semi Circular Wedge

To draw a semi-circular shaped wedge using the current pen and filled by the current brush, use the API function Pie(). The prototype of this function is

BOOL Pie(HDC hdc,int left,int top,int right,int bottom,int xr1,int yr1, int xr2,int yr2);

where
hdc - A handle to the device context.
Left - The x-coordinate of the upper-left corner of the bounding rectangle.
Top - The y-coordinate of the upper-left corner of the bounding rectangle.
Right - The x-coordinate of the lower-right corner of the bounding rectangle.
Bottom - The y-coordinate of the lower-right corner of the bounding rectangle.
xr1 - The x-coordinate of the endpoint of the first radial.
yr1 -The y-coordinate of the endpoint of the first radial.
xr2 - The x-coordinate of the endpoint of the second radial.
yr2 - The y-coordinate of the endpoint of the second radial.

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

Drawing a Chord

To draw a Chord using the current pen and filled using the current brush, use the API function Chord(). The prototype of this function is

BOOL Chord( HDC hdc,int x1,int y1,int x2,int y2,int x3,int y3,int x4, int y4);

hdc - handle to the device context.
x1 - The x-coordinate of the upper-left corner of the bounding rectangle.
y1 - The y-coordinate of the upper-left corner of the bounding rectangle.
x2 - The x-coordinate of the lower-right corner of the bounding rectangle.
y2 - The y-coordinate of the lower-right corner of the bounding rectangle.
x3 - The x-coordinate of the endpoint of the radial defining the beginning of the chord.
y3 - The y-coordinate  of the endpoint of the radial defining the beginning of the chord.
x4 - The x-coordinate of the endpoint of the radial defining the end of the chord.
y4 - The y-coordinate of the endpoint of the radial defining the end of the chord.

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero

Drawing Polygons

The API function used to draw a polygon is Polygon(). The prototype of this function is

BOOL Polygon(HDC hdc,const POINT *apt,int cpt);

hdc - A handle to the device context.
apt - A pointer to an array of POINT structures that specify the vertices of the polygon
cpt - The number of vertices in the array. This value must be greater than or equal to 2.

If the function succeeds, the return value is nonzero.  If the function fails, the return value is zero.

Bézier Curves

The PolyBezier() function draws one or more Bézier curves. The prototype of this function is

BOOL PolyBezier(HDC hdc,const POINT *apt,DWORD cpt);

where
hdc - A handle to a device context.
apt - A pointer to an array of POINT structures that contain the endpoints and control points of the curve(s), in logical units.
cpt - The number of points in the lppt array. This value must be one more than three times the number of curves to be drawn.

If the function succeeds, the return value is nonzero.  If the function fails, the return value is zero.

Example

The following code creates a window to demonstrate various different graphic objects

//displaying graphics
#include <windows.h>
#define IDB_BITMAP1 1000
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HBITMAP hbmp = LoadBitmap(GetModuleHandle(0), MAKEINTRESOURCE(IDB_BITMAP1));
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PWSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wc = {0};
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpszClassName = TEXT("Shapes");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClass(&wc);
CreateWindow(wc.lpszClassName, TEXT("Shapes"),WS_OVERLAPPEDWINDOW | WS_VISIBLE,100, 100, 600, 300, NULL, NULL, hInstance, NULL);
while( GetMessage(&msg, NULL, 0, 0)) {
DispatchMessage(&msg);
}

return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
const POINT polygon[10] = { 30, 185, 95, 185, 105, 130, 65, 145, 30, 135 };
const POINT bezier[4] = {280, 180, 320, 180, 325, 130, 350, 130};
HBRUSH hbrush=NULL;
HPEN hpen;
switch(msg)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
MoveToEx(hdc,10,10,NULL);
SelectObject(hdc,hbrush);

//draws line around window
LineTo(hdc,570,10);
LineTo(hdc,570,240);
LineTo(hdc,10,240);
LineTo(hdc,10,10);

//draws ellipse with default pen and brush
Ellipse(hdc, 30, 30, 120, 90);
SelectObject(hdc, hbrush);
TextOut(hdc,50,95,TEXT("Ellipse"),7);

//creates hbrush with colour green and draws roundrect
hbrush=CreateSolidBrush(RGB(0,255,0));
SelectObject(hdc, hbrush);
RoundRect(hdc, 150, 30, 240, 90, 15, 20);
TextOut(hdc,130,95,TEXT("Rounded rectangle"),17);

//creates hbrush with yellow cross hatch and draws chord
hbrush=CreateHatchBrush(HS_CROSS ,RGB(255,255,0));
SelectObject(hdc, hbrush);
Chord(hdc, 270, 30, 360, 90, 270, 45, 360, 45);
TextOut(hdc,300,95,TEXT("Chord"),5);

//creates hbrush with red cross hatch and draws polygon
hbrush=CreateHatchBrush(HS_VERTICAL ,RGB(255,0,0));
SelectObject(hdc, hbrush);
Polygon(hdc, polygon, 5);
TextOut(hdc,50,195,TEXT("Polygon"),7);

//creates hbrush black and draws rectangle
hbrush=CreateSolidBrush(RGB(0,0,0));
SelectObject(hdc, hbrush);
Rectangle(hdc, 150, 130, 230, 180);
TextOut(hdc,160,195,TEXT("Rectangle"),9);

//draws Bezier curve
PolyBezier(hdc, bezier, 4);
TextOut(hdc,290,195,TEXT("Bezier"),6);

//creates hbrush white and draws pie
hbrush=CreateSolidBrush(RGB(255,255,255));
SelectObject(hdc, hbrush);
Pie(hdc, 400,30, 490,90, 270,45, 360,45);
TextOut(hdc,435,95,TEXT("Pie"),3);

//draws 3 lines with 3 different style in 3 different colours
MoveToEx(hdc,400,130,NULL);
hpen=CreatePen(PS_DASH,1,RGB(0, 0, 255));
SelectObject(hdc,hpen);
LineTo(hdc,490,130);
MoveToEx(hdc,400,155,NULL);
hpen=CreatePen(PS_DOT,1,RGB(255, 0, 255));
SelectObject(hdc,hpen);
LineTo(hdc,490,155);
MoveToEx(hdc,400,180,NULL);
hpen=CreatePen(PS_DASHDOTDOT,1,RGB(255, 0, 0));
SelectObject(hdc,hpen);
LineTo(hdc,490,180);
TextOut(hdc,430,195,TEXT("Lines"),5);
EndPaint(hwnd, &ps);
DeleteObject(hbrush);
DeleteObject(hpen);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}

Download


Home | API | MFC | C++ | C | Previous | Next
Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | ScrollBar Control | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions