73.2.8 Example: Drawing the Front-View of a House

This section contains example code that demonstrates the tasks described in this section:

void TransformAndDraw(int iTransform, HWND hWnd)

{

HDC hDC;

XFORM xForm;

RECT rect;

/* Retrieve a DC handle for the application's window. */

hDC = GetDC(hWnd);

/* */

/* Set the mapping mode to LOENGLISH. This will move the */

/* client-area origin from the upper-left corner of the */

/* window to the lower left corner (this also re-orients */

/* the y-axis so that drawing operations occur in a true */

/* Cartesian space). And, it will guarantee portability-- */

/* the object drawn will retain it's dimensions on any */

/* display running Windows. */

/* */

SetMapMode(hDC, MM_LOENGLISH);

/* */

/* Set the appropriate world-transform (based on the user's */

/* menu selection). */

/* */

switch (iTransform) {

case SCALE: /* Scale to 1/2 original size. */

xForm.eM11 = (FLOAT)0.5;

xForm.eM12 = (FLOAT)0.0;

xForm.eM21 = (FLOAT)0.0;

xForm.eM22 = (FLOAT)0.5;

xForm.eDx = (FLOAT)0.0;

xForm.eDy = (FLOAT)0.0;

SetWorldTransform(hDC, &xForm);

break;

case TRANSLATE: /* Translate right by 3/4 inch. */

xForm.eM11 = (FLOAT) 1.0;

xForm.eM12 = (FLOAT) 0.0;

xForm.eM21 = (FLOAT) 0.0;

xForm.eM22 = (FLOAT) 1.0;

xForm.eDx = (FLOAT)75.0;

xForm.eDy = (FLOAT) 0.0;

SetWorldTransform(hDC, &xForm);

break;

case ROTATE: /* Rotate 30 degrees counter clockwise. */

xForm.eM11 = (FLOAT) 0.8660;

xForm.eM12 = (FLOAT) 0.5000;

xForm.eM21 = (FLOAT)-0.5000;

xForm.eM22 = (FLOAT) 0.8660;

xForm.eDx = (FLOAT) 0.0;

xForm.eDy = (FLOAT) 0.0;

SetWorldTransform(hDC, &xForm);

break;

case SHEAR: /* Shear along the x-axis with a */

/* proportionality constant of 1.0. */

xForm.eM11 = (FLOAT) 1.0;

xForm.eM12 = (FLOAT) 1.0;

xForm.eM21 = (FLOAT) 0.0;

xForm.eM22 = (FLOAT) 1.0;

xForm.eDx = (FLOAT) 0.0;

xForm.eDy = (FLOAT) 0.0;

SetWorldTransform(hDC, &xForm);

break;

case REFLECT: /* Reflect about a horizontal axis. */

xForm.eM11 = (FLOAT) 1.0;

xForm.eM12 = (FLOAT) 0.0;

xForm.eM21 = (FLOAT) 0.0;

xForm.eM22 = (FLOAT)-1.0;

xForm.eDx = (FLOAT) 0.0;

xForm.eDy = (FLOAT) 0.0;

SetWorldTransform(hDC, &xForm);

break;

case NORMAL: /* Set the unity transform. */

xForm.eM11 = (FLOAT)1.0;

xForm.eM12 = (FLOAT)0.0;

xForm.eM21 = (FLOAT)0.0;

xForm.eM22 = (FLOAT)1.0;

xForm.eDx = (FLOAT)0.0;

xForm.eDy = (FLOAT)0.0;

SetWorldTransform(hDC, &xForm);

break;

}

/* Find the midpoint of the client area */

GetClientRect(hWnd, (LPRECT)&rect);

DPtoLP(hDC, (LPPOINT)&rect, 2);

/* */

/* Since the app uses the Rectangle() function to draw the wall, */

/* windows, and doorway, and since each rectangle should be */

/* empty, the hollow brush is selected into the DC. (If this */

/* brush weren't selected, it would be impossible to distinguish */

/* the windows and door from the wall.) */

/* */

SelectObject(hDC, GetStockObject(HOLLOW_BRUSH));

/* Draw the front wall. */

Rectangle(hDC, (rect.right/2 - 100), (rect.bottom/2 + 75),

(rect.right/2 + 100), (rect.bottom/2 - 75));

/* Draw the left window. */

Rectangle(hDC, (rect.right/2 - 75), (rect.bottom/2 + 63),

(rect.right/2 - 25), (rect.bottom/2 + 25));

/* Draw the right window. */

Rectangle(hDC, (rect.right/2 + 25), (rect.bottom/2 + 63),

(rect.right/2 + 75), (rect.bottom/2 + 25));

/* Draw the door. */

Rectangle(hDC, (rect.right/2 - 12), (rect.bottom/2 - 25),

(rect.right/2 + 12), (rect.bottom/2 - 75));

/* Draw the roof-line. */

MoveToEx(hDC, (rect.right/2 - 100), (rect.bottom/2 + 75), (LPPOINT)NULL);

LineTo(hDC, (rect.right/2), (rect.bottom/2 + 125));

LineTo(hDC, (rect.right/2 + 100), (rect.bottom/2 + 75));

ReleaseDC(hWnd, hDC);

}