Annotation of q_a/samples/angle/angle.c, revision 1.1.1.1

1.1       root        1: /**************************************************************************\
                      2: *  angle.c -- sample program demonstrating the new AngleArc() API.
                      3: *
                      4: *  written Jan 92 by Steve Firebaugh
                      5: *
                      6: *  design:  There is one main window with one dialog box stretched to fill
                      7: *   the top of it.  The parameters for the AngleArc painted into the main
                      8: *   window are stored in the entry fields of this dialog box.  The user
                      9: *   may change these values and see the effect on the arc.  Invalid values
                     10: *   will prevent the arc from being drawn.
                     11: \**************************************************************************/
                     12: 
                     13: 
                     14: #include <windows.h>
                     15: #include <math.h>
                     16: #include <string.h>
                     17: #include "angle.h"
                     18: 
                     19: HANDLE hInst;
                     20: HWND   hwndMain, hwndDlg;
                     21: 
                     22: 
                     23: /**************************************************************************\
                     24: *
                     25: *  function:  WinMain()
                     26: *
                     27: *  input parameters:  c.f. generic sample
                     28: *
                     29: \**************************************************************************/
                     30: int APIENTRY WinMain(HANDLE hInstance, HANDLE hPrevInstance,
                     31:                      LPSTR lpCmdLine, int nCmdShow)
                     32: {
                     33:     MSG   msg;
                     34:     RECT rect;
                     35: 
                     36:     UNREFERENCED_PARAMETER( lpCmdLine );
                     37: 
                     38: 
                     39:     /* Check for previous instance.  If none, then register class. */
                     40:     if (!hPrevInstance) {
                     41:         WNDCLASS  wc;
                     42: 
                     43:         wc.style = NULL;
                     44:         wc.lpfnWndProc = (WNDPROC)MainWndProc;
                     45: 
                     46:         wc.cbClsExtra = 0;
                     47:         wc.cbWndExtra = 0;
                     48:         wc.hInstance = hInstance;
                     49:         wc.hIcon = LoadIcon(hInstance, "AngleIcon");
                     50:         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
                     51:         wc.hbrBackground = GetStockObject(LTGRAY_BRUSH);
                     52:         wc.lpszMenuName =  NULL;
                     53:         wc.lpszClassName = "angle";
                     54: 
                     55:         if (!RegisterClass(&wc)) return (FALSE);
                     56:     }  /* class registered o.k. */
                     57: 
                     58: 
                     59:     /* Create the main window.  Return false if CreateWindow() fails */
                     60:     hInst = hInstance;
                     61: 
                     62:     hwndMain = CreateWindow(
                     63:         "angle",
                     64:         "AngleArc",
                     65:         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
                     66:         CW_USEDEFAULT,
                     67:         CW_USEDEFAULT,
                     68:         CW_USEDEFAULT,
                     69:         CW_USEDEFAULT,
                     70:         NULL,
                     71:         NULL,
                     72:         hInstance,
                     73:         NULL);
                     74: 
                     75:     if (!hwndMain) return (FALSE);
                     76:     ShowWindow(hwndMain, nCmdShow);
                     77:     UpdateWindow(hwndMain);
                     78: 
                     79: 
                     80:     /* create the top dialog as a child of the main window. */
                     81:     hwndDlg = CreateDialog (hInst, "AngleDlg", hwndMain, (WNDPROC) DlgProc);
                     82: 
                     83:     /* Send main window a WM_SIZE message so that it will size the top
                     84:      *  dialog correctly.  Also, force a repaint of the main window
                     85:      *  now that the dialog is there and we can draw the arc.
                     86:      */
                     87:     GetClientRect (hwndMain, &rect);
                     88:     SendMessage (hwndMain, WM_SIZE, 0, (rect.right - rect.left));
                     89:     ShowWindow (hwndDlg, SW_SHOW);
                     90:     InvalidateRect (hwndMain, NULL, FALSE);
                     91: 
                     92:     /* Loop getting messages and dispatching them. */
                     93:     while (GetMessage(&msg,NULL, NULL, NULL)) {
                     94:       if (!IsDialogMessage (hwndDlg, &msg)){
                     95:        TranslateMessage(&msg);
                     96:        DispatchMessage(&msg);
                     97:       }
                     98:     }
                     99: 
                    100:     return (msg.wParam);
                    101: }
                    102: 
                    103: 
                    104: 
                    105: /**************************************************************************\
                    106: *
                    107: *  function:  MainWndProc()
                    108: *
                    109: *  input parameters:  normal window procedure parameters.
                    110: *
                    111: *  global variables:
                    112: *   hwndDlg - dialog with entry fields containing arc parameters.
                    113: *
                    114: \**************************************************************************/
                    115: LONG APIENTRY MainWndProc(HWND hwnd, UINT message, UINT wParam, LONG lParam)
                    116: {
                    117: char buffer[MAXCHARS];
                    118: static HANDLE  hPenGrid, hPenArc;
                    119: 
                    120:   switch (message) {
                    121: 
                    122:     /**********************************************************************\
                    123:     *  WM_CREATE
                    124:     *
                    125:     *  Then create three pens for drawing with later.
                    126:     \**********************************************************************/
                    127:     case WM_CREATE:
                    128:         hPenGrid   = CreatePen (PS_SOLID, 1, GRIDCOLOR);
                    129:         hPenArc    = CreatePen (PS_SOLID, 2, (COLORREF) 0x01000005);
                    130:     break;
                    131: 
                    132: 
                    133:     /**********************************************************************\
                    134:     *  WM_DESTROY
                    135:     *
                    136:     * Complement of the WM_CREATE message.  Delete the pens that were
                    137:     *  created and then call postquitmessage.
                    138:     \**********************************************************************/
                    139:     case WM_DESTROY:
                    140:       DeleteObject (hPenGrid);
                    141:       DeleteObject (hPenArc);
                    142: 
                    143:       PostQuitMessage(0);
                    144:     break;
                    145: 
                    146: 
                    147:     /**********************************************************************\
                    148:     *  WM_SIZE
                    149:     *
                    150:     * Stretch the top dialog to fill the width of the main window.
                    151:     \**********************************************************************/
                    152:     case WM_SIZE:
                    153:         SetWindowPos (hwndDlg, NULL, 0,0, LOWORD(lParam), DIALOGHEIGHT, NULL);
                    154:     break;
                    155: 
                    156: 
                    157: 
                    158:     /**********************************************************************\
                    159:     *  WM_PAINT
                    160:     *
                    161:     * First shift the viewport origin down so that 0,0 is the top left
                    162:     *  most visible point (out from underneath the top dialog).  Second,
                    163:     *  draw the grid with wider lines on the axes.  Finally, read the
                    164:     *  values out of the top dialog, do elementary validation, and then
                    165:     *  try to call AngleArc() with the values.  If a value fails validation,
                    166:     *  then write a small error message, and don't draw the arc.
                    167:     \**********************************************************************/
                    168:     case WM_PAINT: {
                    169:         HDC hdc;
                    170:         PAINTSTRUCT ps;
                    171:         RECT rect;
                    172:         int i;
                    173:         int x,y,radius;
                    174:         float start, sweep;
                    175:         BOOL  success;
                    176: 
                    177:         hdc = BeginPaint(hwnd, &ps);
                    178: 
                    179:         SetViewportOrgEx (hdc, 0, DIALOGHEIGHT, NULL);
                    180: 
                    181:         GetClientRect (hwndMain, &rect);
                    182: 
                    183:         SelectObject(hdc, hPenGrid);
                    184:         /* Draw vertical lines.  */
                    185:         for (i = 0; i<= rect.right; i+=TICKSPACE){
                    186:           MoveToEx (hdc, i, rect.top, NULL);
                    187:           LineTo (hdc, i, rect.bottom);
                    188:         }
                    189:         MoveToEx (hdc, 1, 0, NULL);
                    190:         LineTo (hdc, 1, rect.bottom);
                    191: 
                    192:         /* Draw horizontal lines.  */
                    193:         for (i = 0; i<= rect.bottom; i+=TICKSPACE){
                    194:           MoveToEx (hdc, rect.left,i, NULL);
                    195:           LineTo (hdc, rect.right,i);
                    196:         }
                    197:         MoveToEx (hdc, 0, 1, NULL);
                    198:         LineTo (hdc, rect.right,1);
                    199: 
                    200:         /* new color pen for the actual arc. */
                    201:         SelectObject(hdc, hPenArc);
                    202: 
                    203: 
                    204:         /* Query the top dialog parameters,
                    205:          *  if a value is bad, report that and break out of conditional.
                    206:          *  if all values are good, then set the current point and call
                    207:          *  AngleArc().
                    208:          */
                    209:         if (IsWindow(hwndDlg)) {
                    210:           x = GetDlgItemInt(hwndDlg, DID_X, &success, TRUE);
                    211:           if (!success) {
                    212:             TextOut (hdc, 10, rect.bottom-2*DIALOGHEIGHT, "Bad X", 5);
                    213:             break;
                    214:           }
                    215:           y = GetDlgItemInt(hwndDlg, DID_Y, &success, TRUE);
                    216:           if (!success) {
                    217:             TextOut (hdc, 30, rect.bottom-2*DIALOGHEIGHT, "Bad Y", 5);
                    218:             break;
                    219:           }
                    220: 
                    221:           radius = GetDlgItemInt(hwndDlg, DID_RADIUS, &success, TRUE);
                    222:           if (!success) {
                    223:             TextOut (hdc, 50, rect.bottom-2*DIALOGHEIGHT, "Bad Radius", 10);
                    224:             break;
                    225:           }
                    226: 
                    227:           /* Hard to validate these floating point numbers. Good chance
                    228:            *  that invalid values will just map to 0.0
                    229:            */
                    230:           if (!GetDlgItemText(hwndDlg, DID_START, buffer, MAXCHARS)) {
                    231:             TextOut (hdc, 70, rect.bottom-2*DIALOGHEIGHT, "Bad Start", 9);
                    232:             break;
                    233:           }
                    234:           start = (float)atof (buffer);
                    235: 
                    236:           if (!GetDlgItemText(hwndDlg, DID_SWEEP, buffer, MAXCHARS)) {
                    237:             TextOut (hdc, 90, rect.bottom-2*DIALOGHEIGHT, "Bad Sweep", 9);
                    238:             break;
                    239:           }
                    240:           sweep = (float)atof (buffer);
                    241: 
                    242:           MoveToEx (hdc, x, y, NULL);
                    243: 
                    244:           /**********************************************************/
                    245:           /**********************************************************/
                    246:           AngleArc (hdc, x, y, (DWORD) radius, start, sweep);
                    247:           /**********************************************************/
                    248:           /**********************************************************/
                    249:         }
                    250: 
                    251:         EndPaint (hwnd, &ps);
                    252: 
                    253:     } return FALSE;
                    254: 
                    255: 
                    256:     default:
                    257:       return (DefWindowProc(hwnd, message, wParam, lParam));
                    258:     }
                    259:     return (NULL);
                    260: }
                    261: 
                    262: 
                    263: 
                    264: 
                    265: /**************************************************************************\
                    266: *
                    267: *  function:  DlgProc()
                    268: *
                    269: *  input parameters:  normal window procedure parameters.
                    270: *
                    271: *  global variables:
                    272: *   hwndmain - the main window.  also the parent of this dialog
                    273: *
                    274: \**************************************************************************/
                    275: LONG APIENTRY DlgProc(HWND hwnd, UINT message, UINT wParam, LONG lParam)
                    276: {
                    277:   UNREFERENCED_PARAMETER(lParam);
                    278: 
                    279:   switch (message) {
                    280:     /**********************************************************************\
                    281:     *  WM_INITDIALOG
                    282:     *
                    283:     * Fill the entry fields with sensible original values.
                    284:     \**********************************************************************/
                    285:     case WM_INITDIALOG:
                    286:       SetDlgItemText(hwnd, DID_X     , "100");
                    287:       SetDlgItemText(hwnd, DID_Y     , "100");
                    288:       SetDlgItemText(hwnd, DID_RADIUS, "50");
                    289:       SetDlgItemText(hwnd, DID_START , "0.0");
                    290:       SetDlgItemText(hwnd, DID_SWEEP , "270.0");
                    291:     return TRUE;
                    292: 
                    293: 
                    294: 
                    295:     /**********************************************************************\
                    296:     *  WM_COMMAND, DID_DRAW
                    297:     *
                    298:     * Invalidate the main window so that we force a repaint.
                    299:     \**********************************************************************/
                    300:     case WM_COMMAND:
                    301:       if (LOWORD(wParam) == DID_DRAW) {
                    302:         InvalidateRect (hwndMain, NULL, TRUE);
                    303:       }
                    304:     return FALSE;
                    305: 
                    306: 
                    307:     } /* end switch */
                    308:     return (NULL);
                    309: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.