--- q_a/samples/angle/angle.c 2018/08/09 18:29:39 1.1.1.2 +++ q_a/samples/angle/angle.c 2018/08/09 18:30:06 1.1.1.3 @@ -1,14 +1,13 @@ -/**************************************************************************\ -* angle.c -- sample program demonstrating the new AngleArc() API. -* -* written Jan 92 by Steve Firebaugh -* -* design: There is one main window with one dialog box stretched to fill -* the top of it. The parameters for the AngleArc painted into the main -* window are stored in the entry fields of this dialog box. The user -* may change these values and see the effect on the arc. Invalid values -* will prevent the arc from being drawn. -\**************************************************************************/ + +/******************************************************************************\ +* This is a part of the Microsoft Source Code Samples. +* Copyright (C) 1993 Microsoft Corporation. +* All rights reserved. +* This source code is only intended as a supplement to +* Microsoft Development Tools and/or WinHelp documentation. +* See these sources for detailed information regarding the +* Microsoft samples programs. +\******************************************************************************/ #include @@ -16,8 +15,8 @@ #include #include "angle.h" -HANDLE hInst; -HWND hwndMain, hwndDlg; +HANDLE hInst; +HWND hwndMain, hwndDlg; /**************************************************************************\ @@ -27,21 +26,23 @@ HWND hwndMain, hwndDlg; * input parameters: c.f. generic sample * \**************************************************************************/ -int APIENTRY WinMain(HANDLE hInstance, HANDLE hPrevInstance, - LPSTR lpCmdLine, int nCmdShow) +int APIENTRY +WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, + LPSTR lpCmdLine, int nCmdShow) { - MSG msg; - RECT rect; + MSG msg; + RECT rect; - UNREFERENCED_PARAMETER( lpCmdLine ); + UNREFERENCED_PARAMETER(lpCmdLine); - /* Check for previous instance. If none, then register class. */ - if (!hPrevInstance) { - WNDCLASS wc; + /* Check for previous instance. If none, then register class. */ + if (!hPrevInstance) + { + WNDCLASS wc; - wc.style = NULL; - wc.lpfnWndProc = (WNDPROC)MainWndProc; + wc.style = 0; + wc.lpfnWndProc = (WNDPROC) MainWndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; @@ -49,52 +50,55 @@ int APIENTRY WinMain(HANDLE hInstance, H wc.hIcon = LoadIcon(hInstance, "AngleIcon"); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = GetStockObject(LTGRAY_BRUSH); - wc.lpszMenuName = NULL; + wc.lpszMenuName = NULL; wc.lpszClassName = "angle"; - if (!RegisterClass(&wc)) return (FALSE); - } /* class registered o.k. */ - - - /* Create the main window. Return false if CreateWindow() fails */ + if (!RegisterClass(&wc)) + return (FALSE); + } /* class registered o.k. */ + /*  Create the main window. Return false if CreateWindow() fails */ hInst = hInstance; hwndMain = CreateWindow( - "angle", - "AngleArc", - WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, - CW_USEDEFAULT, - CW_USEDEFAULT, - CW_USEDEFAULT, - CW_USEDEFAULT, - NULL, - NULL, - hInstance, - NULL); + "angle", + "AngleArc", + WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, + CW_USEDEFAULT, + CW_USEDEFAULT, + CW_USEDEFAULT, + CW_USEDEFAULT, + NULL, + NULL, + hInstance, + NULL); - if (!hwndMain) return (FALSE); + if (!hwndMain) + return (FALSE); ShowWindow(hwndMain, nCmdShow); UpdateWindow(hwndMain); - /* create the top dialog as a child of the main window. */ - hwndDlg = CreateDialog (hInst, "AngleDlg", hwndMain, (DLGPROC) DlgProc); + /* create the top dialog as a child of the main window. */ + hwndDlg = CreateDialog(hInst, "AngleDlg", hwndMain, (DLGPROC) DlgProc); - /* Send main window a WM_SIZE message so that it will size the top - * dialog correctly. Also, force a repaint of the main window - * now that the dialog is there and we can draw the arc. - */ - GetClientRect (hwndMain, &rect); - SendMessage (hwndMain, WM_SIZE, 0, (rect.right - rect.left)); - ShowWindow (hwndDlg, SW_SHOW); - InvalidateRect (hwndMain, NULL, FALSE); - - /* Loop getting messages and dispatching them. */ - while (GetMessage(&msg,NULL, NULL, NULL)) { - if (!IsDialogMessage (hwndDlg, &msg)){ - TranslateMessage(&msg); - DispatchMessage(&msg); - } + /* + * Send main window a WM_SIZE message so that it will size the top dialog + * correctly. Also, force a repaint of the main window now that the dialog is + * there and we can draw the arc. + */ + GetClientRect(hwndMain, &rect); + SendMessage(hwndMain, WM_SIZE, 0, (rect.right - rect.left)); + ShowWindow(hwndDlg, SW_SHOW); + InvalidateRect(hwndMain, NULL, FALSE); + + /* Loop getting messages and dispatching them. */ + while (GetMessage(&msg, NULL, 0, 0)) + { + if (!IsDialogMessage(hwndDlg, &msg)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } } return (msg.wParam); @@ -112,151 +116,158 @@ int APIENTRY WinMain(HANDLE hInstance, H * hwndDlg - dialog with entry fields containing arc parameters. * \**************************************************************************/ -LONG APIENTRY MainWndProc(HWND hwnd, UINT message, UINT wParam, LONG lParam) +LONG APIENTRY +MainWndProc(HWND hwnd, UINT message, UINT wParam, LONG lParam) { -char buffer[MAXCHARS]; -static HANDLE hPenGrid, hPenArc; + char buffer[MAXCHARS]; + static HANDLE hPenGrid, hPenArc; - switch (message) { - - /**********************************************************************\ - * WM_CREATE - * - * Then create three pens for drawing with later. - \**********************************************************************/ - case WM_CREATE: - hPenGrid = CreatePen (PS_SOLID, 1, GRIDCOLOR); - hPenArc = CreatePen (PS_SOLID, 2, (COLORREF) 0x01000005); - break; - - - /**********************************************************************\ - * WM_DESTROY - * - * Complement of the WM_CREATE message. Delete the pens that were - * created and then call postquitmessage. - \**********************************************************************/ - case WM_DESTROY: - DeleteObject (hPenGrid); - DeleteObject (hPenArc); - - PostQuitMessage(0); - break; - - - /**********************************************************************\ - * WM_SIZE - * - * Stretch the top dialog to fill the width of the main window. - \**********************************************************************/ - case WM_SIZE: - SetWindowPos (hwndDlg, NULL, 0,0, LOWORD(lParam), DIALOGHEIGHT, NULL); - break; - - - - /**********************************************************************\ - * WM_PAINT - * - * First shift the viewport origin down so that 0,0 is the top left - * most visible point (out from underneath the top dialog). Second, - * draw the grid with wider lines on the axes. Finally, read the - * values out of the top dialog, do elementary validation, and then - * try to call AngleArc() with the values. If a value fails validation, - * then write a small error message, and don't draw the arc. - \**********************************************************************/ - case WM_PAINT: { - HDC hdc; - PAINTSTRUCT ps; - RECT rect; - int i; - int x,y,radius; - float start, sweep; - BOOL success; - - hdc = BeginPaint(hwnd, &ps); - - SetViewportOrgEx (hdc, 0, DIALOGHEIGHT, NULL); - - GetClientRect (hwndMain, &rect); - - SelectObject(hdc, hPenGrid); - /* Draw vertical lines. */ - for (i = 0; i<= rect.right; i+=TICKSPACE){ - MoveToEx (hdc, i, rect.top, NULL); - LineTo (hdc, i, rect.bottom); - } - MoveToEx (hdc, 1, 0, NULL); - LineTo (hdc, 1, rect.bottom); + switch (message) + { - /* Draw horizontal lines. */ - for (i = 0; i<= rect.bottom; i+=TICKSPACE){ - MoveToEx (hdc, rect.left,i, NULL); - LineTo (hdc, rect.right,i); - } - MoveToEx (hdc, 0, 1, NULL); - LineTo (hdc, rect.right,1); + /**********************************************************************\ + * WM_CREATE + * + * Then create three pens for drawing with later. + \**********************************************************************/ + case WM_CREATE: + hPenGrid = CreatePen(PS_SOLID, 1, GRIDCOLOR); + hPenArc = CreatePen(PS_SOLID, 2, (COLORREF) 0x01000005); + break; - /* new color pen for the actual arc. */ - SelectObject(hdc, hPenArc); + /**********************************************************************\ + * WM_DESTROY + * + * Complement of the WM_CREATE message. Delete the pens that were + * created and then call postquitmessage. + \**********************************************************************/ + case WM_DESTROY: + DeleteObject(hPenGrid); + DeleteObject(hPenArc); - /* Query the top dialog parameters, - * if a value is bad, report that and break out of conditional. - * if all values are good, then set the current point and call - * AngleArc(). - */ - if (IsWindow(hwndDlg)) { - x = GetDlgItemInt(hwndDlg, DID_X, &success, TRUE); - if (!success) { - TextOut (hdc, 10, rect.bottom-2*DIALOGHEIGHT, "Bad X", 5); - break; - } - y = GetDlgItemInt(hwndDlg, DID_Y, &success, TRUE); - if (!success) { - TextOut (hdc, 30, rect.bottom-2*DIALOGHEIGHT, "Bad Y", 5); + PostQuitMessage(0); break; - } - radius = GetDlgItemInt(hwndDlg, DID_RADIUS, &success, TRUE); - if (!success) { - TextOut (hdc, 50, rect.bottom-2*DIALOGHEIGHT, "Bad Radius", 10); - break; - } - /* Hard to validate these floating point numbers. Good chance - * that invalid values will just map to 0.0 - */ - if (!GetDlgItemText(hwndDlg, DID_START, buffer, MAXCHARS)) { - TextOut (hdc, 70, rect.bottom-2*DIALOGHEIGHT, "Bad Start", 9); + /**********************************************************************\ + * WM_SIZE + * + * Stretch the top dialog to fill the width of the main window. + \**********************************************************************/ + case WM_SIZE: + SetWindowPos(hwndDlg, NULL, 0, 0, LOWORD(lParam), DIALOGHEIGHT, 0); break; - } - start = (float)atof (buffer); - if (!GetDlgItemText(hwndDlg, DID_SWEEP, buffer, MAXCHARS)) { - TextOut (hdc, 90, rect.bottom-2*DIALOGHEIGHT, "Bad Sweep", 9); - break; - } - sweep = (float)atof (buffer); - MoveToEx (hdc, x, y, NULL); - /**********************************************************/ - /**********************************************************/ - AngleArc (hdc, x, y, (DWORD) radius, start, sweep); - /**********************************************************/ - /**********************************************************/ - } - - EndPaint (hwnd, &ps); + /**********************************************************************\ + * WM_PAINT + * + * First shift the viewport origin down so that 0,0 is the top left + * most visible point (out from underneath the top dialog). Second, + * draw the grid with wider lines on the axes. Finally, read the + * values out of the top dialog, do elementary validation, and then + * try to call AngleArc() with the values. If a value fails validation, + * then write a small error message, and don't draw the arc. + \**********************************************************************/ + case WM_PAINT: + { + HDC hdc; + PAINTSTRUCT ps; + RECT rect; + int i; + int x, y, radius; + float start, sweep; + BOOL success; + + hdc = BeginPaint(hwnd, &ps); + + SetViewportOrgEx(hdc, 0, DIALOGHEIGHT, NULL); + + GetClientRect(hwndMain, &rect); + + SelectObject(hdc, hPenGrid); + /* Draw vertical lines. */ + for (i = 0; i <= rect.right; i += TICKSPACE) + { + MoveToEx(hdc, i, rect.top, NULL); + LineTo(hdc, i, rect.bottom); + } + MoveToEx(hdc, 1, 0, NULL); + LineTo(hdc, 1, rect.bottom); + + /* Draw horizontal lines. */ + for (i = 0; i <= rect.bottom; i += TICKSPACE) + { + MoveToEx(hdc, rect.left, i, NULL); + LineTo(hdc, rect.right, i); + } + MoveToEx(hdc, 0, 1, NULL); + LineTo(hdc, rect.right, 1); + + /* new color pen for the actual arc. */ + SelectObject(hdc, hPenArc); + + + /* + * Query the top dialog parameters, if a value is bad, report that + * and break out of conditional. if all values are good, then set + * the current point and call AngleArc(). + */ + if (IsWindow(hwndDlg)) + { + x = GetDlgItemInt(hwndDlg, DID_X, &success, TRUE); + if (!success) + { + TextOut(hdc, 10, rect.bottom - 2 * DIALOGHEIGHT, "Bad X", 5); + break; + } + y = GetDlgItemInt(hwndDlg, DID_Y, &success, TRUE); + if (!success) + { + TextOut(hdc, 30, rect.bottom - 2 * DIALOGHEIGHT, "Bad Y", 5); + break; + } + radius = GetDlgItemInt(hwndDlg, DID_RADIUS, &success, TRUE); + if (!success) + { + TextOut(hdc, 50, rect.bottom - 2 * DIALOGHEIGHT, "Bad Radius", 10); + break; + } + + /* + * Hard to validate these floating point numbers. Good chance + * that invalid values will just map to 0.0 + */ + if (!GetDlgItemText(hwndDlg, DID_START, buffer, MAXCHARS)) + { + TextOut(hdc, 70, rect.bottom - 2 * DIALOGHEIGHT, "Bad Start", 9); + break; + } + start = (float) atof(buffer); + + if (!GetDlgItemText(hwndDlg, DID_SWEEP, buffer, MAXCHARS)) + { + TextOut(hdc, 90, rect.bottom - 2 * DIALOGHEIGHT, "Bad Sweep", 9); + break; + } + sweep = (float) atof(buffer); + + MoveToEx(hdc, x, y, NULL); + + /**********************************************************/ + /**********************************************************/ + AngleArc(hdc, x, y, (DWORD) radius, start, sweep); + /**********************************************************/ + /**********************************************************/ + } + EndPaint(hwnd, &ps); - } return FALSE; + } return FALSE; - - default: - return (DefWindowProc(hwnd, message, wParam, lParam)); - } - return (NULL); + } /* end switch */ + return (DefWindowProc(hwnd, message, wParam, lParam)); } @@ -272,38 +283,41 @@ static HANDLE hPenGrid, hPenArc; * hwndmain - the main window. also the parent of this dialog * \**************************************************************************/ -LONG APIENTRY DlgProc(HWND hwnd, UINT message, UINT wParam, LONG lParam) +LONG APIENTRY +DlgProc(HWND hwnd, UINT message, UINT wParam, LONG lParam) { - UNREFERENCED_PARAMETER(lParam); + UNREFERENCED_PARAMETER(lParam); - switch (message) { - /**********************************************************************\ - * WM_INITDIALOG - * - * Fill the entry fields with sensible original values. - \**********************************************************************/ - case WM_INITDIALOG: - SetDlgItemText(hwnd, DID_X , "100"); - SetDlgItemText(hwnd, DID_Y , "100"); - SetDlgItemText(hwnd, DID_RADIUS, "50"); - SetDlgItemText(hwnd, DID_START , "0.0"); - SetDlgItemText(hwnd, DID_SWEEP , "270.0"); - return TRUE; + switch (message) + { + /**********************************************************************\ + * WM_INITDIALOG + * + * Fill the entry fields with sensible original values. + \**********************************************************************/ + case WM_INITDIALOG: + SetDlgItemText(hwnd, DID_X, "100"); + SetDlgItemText(hwnd, DID_Y, "100"); + SetDlgItemText(hwnd, DID_RADIUS, "50"); + SetDlgItemText(hwnd, DID_START, "0.0"); + SetDlgItemText(hwnd, DID_SWEEP, "270.0"); + return TRUE; - /**********************************************************************\ - * WM_COMMAND, DID_DRAW - * - * Invalidate the main window so that we force a repaint. - \**********************************************************************/ - case WM_COMMAND: - if (LOWORD(wParam) == DID_DRAW) { - InvalidateRect (hwndMain, NULL, TRUE); - } - return FALSE; + /**********************************************************************\ + * WM_COMMAND, DID_DRAW + * + * Invalidate the main window so that we force a repaint. + \**********************************************************************/ + case WM_COMMAND: + if (LOWORD(wParam) == DID_DRAW) + { + InvalidateRect(hwndMain, NULL, TRUE); + } + return FALSE; - } /* end switch */ - return (NULL); + } /* end switch */ + return 0; }