Category : C Source Code
Archive   : ECH.ZIP
Filename : ESCHER.C

 
Output of file : ESCHER.C contained in archive : ECH.ZIP
// ESCHER.CPP - Program to display Escher's triangle in
// a window using mapping modes.
//
// Help Desk Companion by Paul J. Perry
//

#define STRICT
#define ID_TIMER 100
#define NUMBOFCOLORS 5


#include
#include
#include "escher.h"


// Function Prototypes
LRESULT CALLBACK _export MainWndProc(HWND, UINT, WPARAM, LPARAM);
void WM_CommandHandler(HWND, int, HWND, UINT);


// Global Variables
int index;
int CurrentMapMode = MM_ISOTROPIC;

/*******************************************/
#pragma argsused
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdParam, int nCmdShow)
{
char ProgName[] = "Escher's Triangle";
HWND hWnd;
MSG msg;


if (!hPrevInstance)
{
WNDCLASS wndclass;

wndclass.lpszClassName = ProgName;
wndclass.lpfnWndProc = (WNDPROC) MainWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = "MAINMENU";
wndclass.style = CS_VREDRAW | CS_HREDRAW;

if (!RegisterClass(&wndclass))
exit(1);
}

hWnd = CreateWindow(ProgName, ProgName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}


/*******************************************/
#pragma argsused
void WM_CommandHandler(HWND hWnd, int id, HWND hWndCtl, UINT codeNotify)
{
HMENU hMenu;

hMenu = GetMenu(hWnd);

switch (id)
{
case IDM_ISO :
{
CurrentMapMode = MM_ISOTROPIC;
CheckMenuItem(hMenu, IDM_ISO, MF_CHECKED);
CheckMenuItem(hMenu, IDM_ANISO, MF_UNCHECKED);
InvalidateRect(hWnd, NULL, TRUE);

break;
}

case IDM_ANISO :
{
CurrentMapMode = MM_ANISOTROPIC;
CheckMenuItem(hMenu, IDM_ANISO, MF_CHECKED);
CheckMenuItem(hMenu, IDM_ISO, MF_UNCHECKED);
InvalidateRect(hWnd, NULL, TRUE);

break;
}

case IDM_EXIT :
{
SendMessage(hWnd, WM_CLOSE, 0, 0L);
break;
}

}
}


/*******************************************/
LRESULT CALLBACK _export MainWndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE :
{
if (!SetTimer(hWnd, ID_TIMER, 1000, NULL))
MessageBox(hWnd, "Too many timers set", " ",
MB_ICONEXCLAMATION | MB_OK);

return 0;
}

case WM_TIMER :
{
index = index%NUMBOFCOLORS;
InvalidateRect(hWnd, NULL, TRUE);

return 0;
}


case WM_PAINT :
{
int count[3] = {7,7,7};

POINT arr[] = { {10, 183}, // bottom polygon
{30, 218},
{230, 218},
{150, 80},
{130, 115},
{170, 183},
{10, 183},

{230, 218}, // right-hand polygon
{250, 183},
{150, 10},
{70, 148},
{110, 148},
{150, 80},
{230, 218},

{10, 183}, // left-hand polygon
{170, 183},
{150, 148},
{70, 148},
{150, 10},
{110, 10},
{10, 183} };

COLORREF colors[NUMBOFCOLORS] =
{ RGB(255, 0, 0), // red
RGB(0, 255, 0), // green
RGB(0, 0, 255), // blue
RGB(255, 0, 255), // purple
RGB(128, 128, 128) }; // gray

RECT rect;
HPEN hPen, hOldPen;
HBRUSH hBrush, hOldBrush;
HDC PaintDC;
PAINTSTRUCT ps;

PaintDC = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rect);

// Select a new pen in the display context
hPen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
hOldPen = SelectPen(PaintDC, hPen);

// Select a new brush into the display context
hBrush = CreateSolidBrush(colors[index++]);
hOldBrush = SelectBrush(PaintDC, hBrush);

// Set the new mapping mode
GetClientRect(hWnd, &rect);
SetMapMode(PaintDC, CurrentMapMode);
SetWindowExt(PaintDC, 260, 228);
SetViewportExt(PaintDC, rect.right, rect.bottom);

// draw figure and do fill
PolyPolygon (PaintDC, arr, count, 3);

// cleanup
SelectBrush(PaintDC, hOldBrush);
DeleteBrush(hBrush);

SelectPen(PaintDC, hOldPen);
DeletePen(hPen);


EndPaint(hWnd, &ps);
return 0;
}

case WM_COMMAND :
{
return HANDLE_WM_COMMAND(hWnd, wParam, lParam, WM_CommandHandler);
}


case WM_DESTROY :
{
KillTimer(hWnd, ID_TIMER);

PostQuitMessage(0);
return 0;
}
}

return DefWindowProc (hWnd, message, wParam, lParam);
}




  3 Responses to “Category : C Source Code
Archive   : ECH.ZIP
Filename : ESCHER.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/